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.

HemfGraphics.java 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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.hemf.draw;
  16. import static org.apache.poi.hwmf.record.HwmfBrushStyle.BS_NULL;
  17. import static org.apache.poi.hwmf.record.HwmfBrushStyle.BS_SOLID;
  18. import java.awt.Color;
  19. import java.awt.Graphics2D;
  20. import java.awt.geom.Path2D;
  21. import java.awt.geom.Point2D;
  22. import java.awt.geom.Rectangle2D;
  23. import java.util.function.Consumer;
  24. import org.apache.poi.hemf.record.emf.HemfRecord;
  25. import org.apache.poi.hwmf.draw.HwmfDrawProperties;
  26. import org.apache.poi.hwmf.draw.HwmfGraphics;
  27. import org.apache.poi.hwmf.record.HwmfColorRef;
  28. import org.apache.poi.hwmf.record.HwmfObjectTableEntry;
  29. import org.apache.poi.hwmf.record.HwmfPenStyle;
  30. import org.apache.poi.util.Internal;
  31. public class HemfGraphics extends HwmfGraphics {
  32. private static final HwmfColorRef WHITE = new HwmfColorRef(Color.WHITE);
  33. private static final HwmfColorRef LTGRAY = new HwmfColorRef(new Color(0x00C0C0C0));
  34. private static final HwmfColorRef GRAY = new HwmfColorRef(new Color(0x00808080));
  35. private static final HwmfColorRef DKGRAY = new HwmfColorRef(new Color(0x00404040));
  36. private static final HwmfColorRef BLACK = new HwmfColorRef(Color.BLACK);
  37. public HemfGraphics(Graphics2D graphicsCtx, Rectangle2D bbox) {
  38. super(graphicsCtx,bbox);
  39. // add dummy entry for object ind ex 0, as emf is 1-based
  40. objectIndexes.set(0);
  41. }
  42. @Override
  43. public HemfDrawProperties getProperties() {
  44. return (HemfDrawProperties)super.getProperties();
  45. }
  46. @Override
  47. protected HemfDrawProperties newProperties(HwmfDrawProperties oldProps) {
  48. return (oldProps == null)
  49. ? new HemfDrawProperties()
  50. : new HemfDrawProperties((HemfDrawProperties)oldProps);
  51. }
  52. public void draw(HemfRecord r) {
  53. r.draw(this);
  54. }
  55. @Internal
  56. public void draw(Consumer<Path2D> pathConsumer, FillDrawStyle fillDraw) {
  57. final HemfDrawProperties prop = getProperties();
  58. final boolean useBracket = prop.getUsePathBracket();
  59. final Path2D path;
  60. if (useBracket) {
  61. path = prop.getPath();
  62. } else {
  63. path = new Path2D.Double();
  64. path.setWindingRule(prop.getWindingRule());
  65. }
  66. // add dummy move-to at start, to handle invalid emfs not containing that move-to
  67. if (path.getCurrentPoint() == null) {
  68. Point2D pnt = prop.getLocation();
  69. path.moveTo(pnt.getX(), pnt.getY());
  70. }
  71. try {
  72. pathConsumer.accept(path);
  73. } catch (Exception e) {
  74. // workaround if a path has been started and no MoveTo command
  75. // has been specified before the first lineTo/splineTo
  76. final Point2D loc = prop.getLocation();
  77. path.moveTo(loc.getX(), loc.getY());
  78. pathConsumer.accept(path);
  79. }
  80. Point2D curPnt = path.getCurrentPoint();
  81. if (curPnt == null) {
  82. return;
  83. }
  84. prop.setLocation(curPnt);
  85. if (!useBracket) {
  86. switch (fillDraw) {
  87. case FILL:
  88. super.fill(path);
  89. break;
  90. case DRAW:
  91. super.draw(path);
  92. break;
  93. case FILL_DRAW:
  94. super.fill(path);
  95. super.draw(path);
  96. break;
  97. }
  98. }
  99. }
  100. /**
  101. * Adds or sets an record of type {@link HwmfObjectTableEntry} to the object table.
  102. * If the {@code index} is less than 1, the method acts the same as
  103. * {@link HwmfGraphics#addObjectTableEntry(HwmfObjectTableEntry)}, otherwise the
  104. * index is used to access the object table.
  105. * As the table is filled successively, the index must be between 1 and size+1
  106. *
  107. * @param entry the record to be stored
  108. * @param index the index to be overwritten, regardless if its content was unset before
  109. *
  110. * @see HwmfGraphics#addObjectTableEntry(HwmfObjectTableEntry)
  111. */
  112. public void addObjectTableEntry(HwmfObjectTableEntry entry, int index) {
  113. if (index < 1) {
  114. throw new IndexOutOfBoundsException("Object table entry index in EMF must be > 0 - invalid index: "+index);
  115. }
  116. objectIndexes.set(index);
  117. objectTable.put(index, entry);
  118. }
  119. @Override
  120. public void applyObjectTableEntry(int index) {
  121. if ((index & 0x80000000) != 0) {
  122. selectStockObject(index);
  123. } else {
  124. super.applyObjectTableEntry(index);
  125. }
  126. }
  127. private void selectStockObject(int objectIndex) {
  128. final HemfDrawProperties prop = getProperties();
  129. switch (objectIndex) {
  130. case 0x80000000:
  131. // WHITE_BRUSH - A white, solid-color brush
  132. // BrushStyle: BS_SOLID
  133. // Color: 0x00FFFFFF
  134. prop.setBrushColor(WHITE);
  135. prop.setBrushStyle(BS_SOLID);
  136. break;
  137. case 0x80000001:
  138. // LTGRAY_BRUSH - A light gray, solid-color brush
  139. // BrushStyle: BS_SOLID
  140. // Color: 0x00C0C0C0
  141. prop.setBrushColor(LTGRAY);
  142. prop.setBrushStyle(BS_SOLID);
  143. break;
  144. case 0x80000002:
  145. // GRAY_BRUSH - A gray, solid-color brush
  146. // BrushStyle: BS_SOLID
  147. // Color: 0x00808080
  148. prop.setBrushColor(GRAY);
  149. prop.setBrushStyle(BS_SOLID);
  150. break;
  151. case 0x80000003:
  152. // DKGRAY_BRUSH - A dark gray, solid color brush
  153. // BrushStyle: BS_SOLID
  154. // Color: 0x00404040
  155. prop.setBrushColor(DKGRAY);
  156. prop.setBrushStyle(BS_SOLID);
  157. break;
  158. case 0x80000004:
  159. // BLACK_BRUSH - A black, solid color brush
  160. // BrushStyle: BS_SOLID
  161. // Color: 0x00000000
  162. prop.setBrushColor(BLACK);
  163. prop.setBrushStyle(BS_SOLID);
  164. break;
  165. case 0x80000005:
  166. // NULL_BRUSH - A null brush
  167. // BrushStyle: BS_NULL
  168. prop.setBrushStyle(BS_NULL);
  169. break;
  170. case 0x80000006:
  171. // WHITE_PEN - A white, solid-color pen
  172. // PenStyle: PS_COSMETIC + PS_SOLID
  173. // ColorRef: 0x00FFFFFF
  174. prop.setPenStyle(HwmfPenStyle.valueOf(0));
  175. prop.setPenWidth(1);
  176. prop.setPenColor(WHITE);
  177. break;
  178. case 0x80000007:
  179. // BLACK_PEN - A black, solid-color pen
  180. // PenStyle: PS_COSMETIC + PS_SOLID
  181. // ColorRef: 0x00000000
  182. prop.setPenStyle(HwmfPenStyle.valueOf(0));
  183. prop.setPenWidth(1);
  184. prop.setPenColor(BLACK);
  185. break;
  186. case 0x80000008:
  187. // NULL_PEN - A null pen
  188. // PenStyle: PS_NULL
  189. prop.setPenStyle(HwmfPenStyle.valueOf(HwmfPenStyle.HwmfLineDash.NULL.wmfFlag));
  190. break;
  191. case 0x8000000A:
  192. // OEM_FIXED_FONT - A fixed-width, OEM character set
  193. // Charset: OEM_CHARSET
  194. // PitchAndFamily: FF_DONTCARE + FIXED_PITCH
  195. break;
  196. case 0x8000000B:
  197. // ANSI_FIXED_FONT - A fixed-width font
  198. // Charset: ANSI_CHARSET
  199. // PitchAndFamily: FF_DONTCARE + FIXED_PITCH
  200. break;
  201. case 0x8000000C:
  202. // ANSI_VAR_FONT - A variable-width font
  203. // Charset: ANSI_CHARSET
  204. // PitchAndFamily: FF_DONTCARE + VARIABLE_PITCH
  205. break;
  206. case 0x8000000D:
  207. // SYSTEM_FONT - A font that is guaranteed to be available in the operating system
  208. break;
  209. case 0x8000000E:
  210. // DEVICE_DEFAULT_FONT
  211. // The default font that is provided by the graphics device driver for the current output device
  212. break;
  213. case 0x8000000F:
  214. // DEFAULT_PALETTE
  215. // The default palette that is defined for the current output device.
  216. break;
  217. case 0x80000010:
  218. // SYSTEM_FIXED_FONT
  219. // A fixed-width font that is guaranteed to be available in the operating system.
  220. break;
  221. case 0x80000011:
  222. // DEFAULT_GUI_FONT
  223. // The default font that is used for user interface objects such as menus and dialog boxes.
  224. break;
  225. case 0x80000012:
  226. // DC_BRUSH
  227. // The solid-color brush that is currently selected in the playback device context.
  228. break;
  229. case 0x80000013:
  230. // DC_PEN
  231. // The solid-color pen that is currently selected in the playback device context.
  232. break;
  233. }
  234. }
  235. }