You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

HwmfPenStyle.java 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.hwmf.record;
  16. import java.awt.BasicStroke;
  17. import org.apache.poi.util.BitField;
  18. import org.apache.poi.util.BitFieldFactory;
  19. /**
  20. * The 16-bit PenStyle Enumeration is used to specify different types of pens
  21. * that can be used in graphics operations.
  22. *
  23. * Various styles can be combined by using a logical OR statement, one from
  24. * each subsection of Style, EndCap, Join, and Type (Cosmetic).
  25. *
  26. * The defaults in case the other values of the subsection aren't set are
  27. * solid, round end caps, round joins and cosmetic type.
  28. */
  29. public class HwmfPenStyle implements Cloneable {
  30. public enum HwmfLineCap {
  31. /** Rounded ends */
  32. ROUND(0, BasicStroke.CAP_ROUND),
  33. /** Square protrudes by half line width */
  34. SQUARE(1, BasicStroke.CAP_SQUARE),
  35. /** Line ends at end point*/
  36. FLAT(2, BasicStroke.CAP_BUTT);
  37. public final int wmfFlag;
  38. public final int awtFlag;
  39. HwmfLineCap(int wmfFlag, int awtFlag) {
  40. this.wmfFlag = wmfFlag;
  41. this.awtFlag = awtFlag;
  42. }
  43. static HwmfLineCap valueOf(int wmfFlag) {
  44. for (HwmfLineCap hs : values()) {
  45. if (hs.wmfFlag == wmfFlag) return hs;
  46. }
  47. return null;
  48. }
  49. }
  50. public enum HwmfLineJoin {
  51. /**Line joins are round. */
  52. ROUND(0, BasicStroke.JOIN_ROUND),
  53. /** Line joins are beveled. */
  54. BEVEL(1, BasicStroke.JOIN_BEVEL),
  55. /**
  56. * Line joins are mitered when they are within the current limit set by the
  57. * SETMITERLIMIT META_ESCAPE record. A join is beveled when it would exceed the limit
  58. */
  59. MITER(2, BasicStroke.JOIN_MITER);
  60. public final int wmfFlag;
  61. public final int awtFlag;
  62. HwmfLineJoin(int wmfFlag, int awtFlag) {
  63. this.wmfFlag = wmfFlag;
  64. this.awtFlag = awtFlag;
  65. }
  66. static HwmfLineJoin valueOf(int wmfFlag) {
  67. for (HwmfLineJoin hs : values()) {
  68. if (hs.wmfFlag == wmfFlag) return hs;
  69. }
  70. return null;
  71. }
  72. }
  73. public enum HwmfLineDash {
  74. /**
  75. * The pen is solid.
  76. */
  77. SOLID(0x0000, null),
  78. /**
  79. * The pen is dashed. (-----)
  80. */
  81. DASH(0x0001, 10, 8),
  82. /**
  83. * The pen is dotted. (.....)
  84. */
  85. DOT(0x0002, 2, 4),
  86. /**
  87. * The pen has alternating dashes and dots. (_._._._)
  88. */
  89. DASHDOT(0x0003, 10, 8, 2, 8),
  90. /**
  91. * The pen has dashes and double dots. (_.._.._)
  92. */
  93. DASHDOTDOT(0x0004, 10, 4, 2, 4, 2, 4),
  94. /**
  95. * The pen is invisible.
  96. */
  97. NULL(0x0005, null),
  98. /**
  99. * The pen is solid. When this pen is used in any drawing record that takes a
  100. * bounding rectangle, the dimensions of the figure are shrunk so that it fits
  101. * entirely in the bounding rectangle, taking into account the width of the pen.
  102. */
  103. INSIDEFRAME(0x0006, null),
  104. /**
  105. * The pen uses a styling array supplied by the user.
  106. * (this is currently not supported and drawn as solid ... no idea where the user
  107. * styling is supposed to come from ...)
  108. */
  109. USERSTYLE(0x0007, null);
  110. public final int wmfFlag;
  111. public final float[] dashes;
  112. HwmfLineDash(int wmfFlag, float... dashes) {
  113. this.wmfFlag = wmfFlag;
  114. this.dashes = dashes;
  115. }
  116. static HwmfLineDash valueOf(int wmfFlag) {
  117. for (HwmfLineDash hs : values()) {
  118. if (hs.wmfFlag == wmfFlag) return hs;
  119. }
  120. return null;
  121. }
  122. }
  123. private static final BitField SUBSECTION_DASH = BitFieldFactory.getInstance(0x00007);
  124. private static final BitField SUBSECTION_ALTERNATE = BitFieldFactory.getInstance(0x00008);
  125. private static final BitField SUBSECTION_ENDCAP = BitFieldFactory.getInstance(0x00300);
  126. private static final BitField SUBSECTION_JOIN = BitFieldFactory.getInstance(0x03000);
  127. private static final BitField SUBSECTION_GEOMETRIC = BitFieldFactory.getInstance(0x10000);
  128. protected int flag;
  129. public static HwmfPenStyle valueOf(int flag) {
  130. HwmfPenStyle ps = new HwmfPenStyle();
  131. ps.flag = flag;
  132. return ps;
  133. }
  134. public HwmfLineCap getLineCap() {
  135. return HwmfLineCap.valueOf(SUBSECTION_ENDCAP.getValue(flag));
  136. }
  137. public HwmfLineJoin getLineJoin() {
  138. return HwmfLineJoin.valueOf(SUBSECTION_JOIN.getValue(flag));
  139. }
  140. public HwmfLineDash getLineDash() {
  141. return HwmfLineDash.valueOf(SUBSECTION_DASH.getValue(flag));
  142. }
  143. /**
  144. * Convienence method which should be used instead of accessing {@link HwmfLineDash#dashes}
  145. * directly, so an subclass can provide user-style dashes
  146. *
  147. * @return the dash pattern
  148. */
  149. public float[] getLineDashes() {
  150. return getLineDash().dashes;
  151. }
  152. /**
  153. * The pen sets every other pixel (this style is applicable only for cosmetic pens).
  154. */
  155. public boolean isAlternateDash() {
  156. return SUBSECTION_ALTERNATE.isSet(flag);
  157. }
  158. /**
  159. * A pen type that specifies a line with a width that is measured in logical units
  160. * and a style that can contain any of the attributes of a brush.
  161. */
  162. public boolean isGeometric() {
  163. return SUBSECTION_GEOMETRIC.isSet(flag);
  164. }
  165. /**
  166. * Creates a new object of the same class and with the
  167. * same contents as this object.
  168. * @return a clone of this instance.
  169. * @exception OutOfMemoryError if there is not enough memory.
  170. * @see java.lang.Cloneable
  171. */
  172. @Override
  173. public HwmfPenStyle clone() {
  174. try {
  175. return (HwmfPenStyle)super.clone();
  176. } catch (CloneNotSupportedException e) {
  177. // this shouldn't happen, since we are Cloneable
  178. throw new InternalError();
  179. }
  180. }
  181. @Override
  182. public String toString() {
  183. return
  184. "{ lineCap: '"+getLineCap()+"'"+
  185. ", lineDash: '"+getLineDash()+"'"+
  186. ", lineJoin: '"+getLineJoin()+"'"+
  187. (isAlternateDash()?", alternateDash: true ":"")+
  188. (isGeometric()?", geometric: true ":"")+
  189. "}";
  190. }
  191. }