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.

GraphicsObject.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.afp.modca;
  19. import java.awt.Color;
  20. import java.io.IOException;
  21. import java.io.OutputStream;
  22. import java.util.Iterator;
  23. import java.util.List;
  24. import org.apache.xmlgraphics.java2d.color.ColorConverter;
  25. import org.apache.xmlgraphics.java2d.color.ColorUtil;
  26. import org.apache.fop.afp.AFPDataObjectInfo;
  27. import org.apache.fop.afp.AFPObjectAreaInfo;
  28. import org.apache.fop.afp.Completable;
  29. import org.apache.fop.afp.Factory;
  30. import org.apache.fop.afp.StructuredData;
  31. import org.apache.fop.afp.fonts.CharacterSet;
  32. import org.apache.fop.afp.goca.GraphicsAreaBegin;
  33. import org.apache.fop.afp.goca.GraphicsAreaEnd;
  34. import org.apache.fop.afp.goca.GraphicsBox;
  35. import org.apache.fop.afp.goca.GraphicsChainedSegment;
  36. import org.apache.fop.afp.goca.GraphicsCharacterString;
  37. import org.apache.fop.afp.goca.GraphicsData;
  38. import org.apache.fop.afp.goca.GraphicsEndProlog;
  39. import org.apache.fop.afp.goca.GraphicsFillet;
  40. import org.apache.fop.afp.goca.GraphicsFullArc;
  41. import org.apache.fop.afp.goca.GraphicsImage;
  42. import org.apache.fop.afp.goca.GraphicsLine;
  43. import org.apache.fop.afp.goca.GraphicsSetArcParameters;
  44. import org.apache.fop.afp.goca.GraphicsSetCharacterSet;
  45. import org.apache.fop.afp.goca.GraphicsSetCurrentPosition;
  46. import org.apache.fop.afp.goca.GraphicsSetFractionalLineWidth;
  47. import org.apache.fop.afp.goca.GraphicsSetLineType;
  48. import org.apache.fop.afp.goca.GraphicsSetLineWidth;
  49. import org.apache.fop.afp.goca.GraphicsSetPatternSymbol;
  50. import org.apache.fop.afp.goca.GraphicsSetProcessColor;
  51. /**
  52. * Top-level GOCA graphics object.
  53. *
  54. * Acts as container and factory of all other graphic objects
  55. */
  56. public class GraphicsObject extends AbstractDataObject {
  57. /** the graphics data */
  58. private GraphicsData currentData;
  59. /** list of objects contained within this container */
  60. protected List<GraphicsData> objects
  61. = new java.util.ArrayList<GraphicsData>();
  62. /** the graphics state */
  63. private final GraphicsState graphicsState = new GraphicsState();
  64. /** color converter */
  65. private ColorConverter colorConverter;
  66. /**
  67. * Default constructor
  68. *
  69. * @param factory the object factory
  70. * @param name the name of graphics object
  71. */
  72. public GraphicsObject(Factory factory, String name) {
  73. super(factory, name);
  74. }
  75. /** {@inheritDoc} */
  76. @Override
  77. public void setViewport(AFPDataObjectInfo dataObjectInfo) {
  78. super.setViewport(dataObjectInfo);
  79. AFPObjectAreaInfo objectAreaInfo = dataObjectInfo.getObjectAreaInfo();
  80. int width = objectAreaInfo.getWidth();
  81. int height = objectAreaInfo.getHeight();
  82. int widthRes = objectAreaInfo.getWidthRes();
  83. int heightRes = objectAreaInfo.getHeightRes();
  84. final int leftEdge = 0;
  85. final int topEdge = 0;
  86. GraphicsDataDescriptor graphicsDataDescriptor = factory.createGraphicsDataDescriptor(
  87. leftEdge, width, topEdge, height, widthRes, heightRes);
  88. getObjectEnvironmentGroup().setDataDescriptor(graphicsDataDescriptor);
  89. }
  90. /** @param object the structured data */
  91. public void addObject(StructuredData object) {
  92. if (currentData == null) {
  93. newData();
  94. } else if (currentData.getDataLength() + object.getDataLength()
  95. >= GraphicsData.MAX_DATA_LEN) {
  96. // graphics data full so transfer current incomplete segment to new data
  97. GraphicsChainedSegment currentSegment
  98. = (GraphicsChainedSegment)currentData.removeCurrentSegment();
  99. currentSegment.setName(newData().createSegmentName());
  100. currentData.addSegment(currentSegment);
  101. }
  102. currentData.addObject(object);
  103. }
  104. /**
  105. * Gets the current graphics data, creating a new one if necessary
  106. *
  107. * @return the current graphics data
  108. */
  109. private GraphicsData getData() {
  110. if (this.currentData == null) {
  111. return newData();
  112. }
  113. return this.currentData;
  114. }
  115. /**
  116. * Creates a new graphics data
  117. *
  118. * @return a newly created graphics data
  119. */
  120. private GraphicsData newData() {
  121. if (currentData != null) {
  122. currentData.setComplete(true);
  123. }
  124. this.currentData = factory.createGraphicsData();
  125. objects.add(currentData);
  126. return currentData;
  127. }
  128. /**
  129. * Sets the current color
  130. *
  131. * @param color the active color to use
  132. */
  133. public void setColor(Color color) {
  134. if (!ColorUtil.isSameColor(color, graphicsState.color)) {
  135. addObject(new GraphicsSetProcessColor(colorConverter.convert(color)));
  136. graphicsState.color = color;
  137. }
  138. }
  139. /**
  140. * Sets the color converter
  141. *
  142. * @param colorConverter ColorConverter to filter the color
  143. * when creating a GraphicsSetProcessColor.
  144. */
  145. public void setColorConverter(ColorConverter colorConverter) {
  146. this.colorConverter = colorConverter;
  147. }
  148. /**
  149. * Sets the current position
  150. *
  151. * @param coords the x and y coordinates of the current position
  152. */
  153. public void setCurrentPosition(int[] coords) {
  154. addObject(new GraphicsSetCurrentPosition(coords));
  155. }
  156. /**
  157. * Sets the line width
  158. *
  159. * @param lineWidth the line width multiplier
  160. */
  161. public void setLineWidth(int lineWidth) {
  162. if ((float) lineWidth != graphicsState.lineWidth) {
  163. addObject(new GraphicsSetLineWidth(lineWidth));
  164. graphicsState.lineWidth = (float) lineWidth;
  165. }
  166. }
  167. /**
  168. * Sets the line width
  169. *
  170. * @param lineWidth the line width multiplier
  171. */
  172. public void setLineWidth(float lineWidth) {
  173. float epsilon = Float.intBitsToFloat(0x00800000); // Float.MIN_NORMAL (JDK1.6)
  174. if (Math.abs(graphicsState.lineWidth - lineWidth) > epsilon) {
  175. addObject(new GraphicsSetFractionalLineWidth(lineWidth));
  176. graphicsState.lineWidth = lineWidth;
  177. }
  178. }
  179. /**
  180. * Sets the line type
  181. *
  182. * @param lineType the line type
  183. */
  184. public void setLineType(byte lineType) {
  185. if (lineType != graphicsState.lineType) {
  186. addObject(new GraphicsSetLineType(lineType));
  187. graphicsState.lineType = lineType;
  188. }
  189. }
  190. /**
  191. * Sets whether the following shape is to be filled.
  192. *
  193. * @param fill true if the following shape is to be filled
  194. */
  195. public void setFill(boolean fill) {
  196. setPatternSymbol(fill
  197. ? GraphicsSetPatternSymbol.SOLID_FILL
  198. : GraphicsSetPatternSymbol.NO_FILL);
  199. }
  200. /**
  201. * Sets the fill pattern of the next shape.
  202. *
  203. * @param patternSymbol the fill pattern of the next shape
  204. */
  205. public void setPatternSymbol(byte patternSymbol) {
  206. if (patternSymbol != graphicsState.patternSymbol) {
  207. addObject(new GraphicsSetPatternSymbol(patternSymbol));
  208. graphicsState.patternSymbol = patternSymbol;
  209. }
  210. }
  211. /**
  212. * Sets the character set to use
  213. *
  214. * @param characterSet the character set (font) reference
  215. */
  216. public void setCharacterSet(int characterSet) {
  217. if (characterSet != graphicsState.characterSet) {
  218. graphicsState.characterSet = characterSet;
  219. }
  220. addObject(new GraphicsSetCharacterSet(characterSet));
  221. }
  222. /**
  223. * Adds a line at the given x/y coordinates
  224. *
  225. * @param coords the x/y coordinates (can be a series)
  226. */
  227. public void addLine(int[] coords) {
  228. addLine(coords, false);
  229. }
  230. /**
  231. * Adds a line at the given x/y coordinates
  232. *
  233. * @param coords the x/y coordinates (can be a series)
  234. * @param relative relative true for a line at current position (relative to)
  235. */
  236. public void addLine(int[] coords, boolean relative) {
  237. addObject(new GraphicsLine(coords, relative));
  238. }
  239. /**
  240. * Adds a box at the given coordinates
  241. *
  242. * @param coords the x/y coordinates
  243. */
  244. public void addBox(int[] coords) {
  245. addObject(new GraphicsBox(coords));
  246. }
  247. /**
  248. * Adds a fillet (curve) at the given coordinates
  249. *
  250. * @param coords the x/y coordinates
  251. */
  252. public void addFillet(int[] coords) {
  253. addFillet(coords, false);
  254. }
  255. /**
  256. * Adds a fillet (curve) at the given coordinates
  257. *
  258. * @param coords the x/y coordinates
  259. * @param relative relative true for a fillet (curve) at current position (relative to)
  260. */
  261. public void addFillet(int[] coords, boolean relative) {
  262. addObject(new GraphicsFillet(coords, relative));
  263. }
  264. /**
  265. * Sets the arc parameters
  266. *
  267. * @param xmaj the maximum value of the x coordinate
  268. * @param ymin the minimum value of the y coordinate
  269. * @param xmin the minimum value of the x coordinate
  270. * @param ymaj the maximum value of the y coordinate
  271. */
  272. public void setArcParams(int xmaj, int ymin, int xmin, int ymaj) {
  273. addObject(new GraphicsSetArcParameters(xmaj, ymin, xmin, ymaj));
  274. }
  275. /**
  276. * Adds a full arc
  277. *
  278. * @param x the x coordinate
  279. * @param y the y coordinate
  280. * @param mh the integer portion of the multiplier
  281. * @param mhr the fractional portion of the multiplier
  282. */
  283. public void addFullArc(int x, int y, int mh, int mhr) {
  284. addObject(new GraphicsFullArc(x, y, mh, mhr));
  285. }
  286. /**
  287. * Adds an image
  288. *
  289. * @param x the x coordinate
  290. * @param y the y coordinate
  291. * @param width the image width
  292. * @param height the image height
  293. * @param imgData the image data
  294. */
  295. public void addImage(int x, int y, int width, int height, byte[] imgData) {
  296. addObject(new GraphicsImage(x, y, width, height, imgData));
  297. }
  298. /**
  299. * Adds a string
  300. *
  301. * @param str the string
  302. * @param x the x coordinate
  303. * @param y the y coordinate
  304. * @param charSet the character set associated with the string
  305. */
  306. public void addString(String str, int x, int y, CharacterSet charSet) {
  307. addObject(new GraphicsCharacterString(str, x, y, charSet));
  308. }
  309. /**
  310. * Begins a graphics area (start of fill)
  311. */
  312. public void beginArea() {
  313. addObject(new GraphicsAreaBegin());
  314. }
  315. /**
  316. * Ends a graphics area (end of fill)
  317. */
  318. public void endArea() {
  319. addObject(new GraphicsAreaEnd());
  320. }
  321. /**
  322. * Ends the prolog.
  323. */
  324. public void endProlog() {
  325. addObject(new GraphicsEndProlog());
  326. }
  327. /** {@inheritDoc} */
  328. @Override
  329. public String toString() {
  330. return "GraphicsObject: " + getName();
  331. }
  332. /**
  333. * Creates a new graphics segment
  334. */
  335. public void newSegment() {
  336. getData().newSegment();
  337. graphicsState.lineWidth = 0; //Looks like a new segment invalidates the graphics state
  338. }
  339. /** {@inheritDoc} */
  340. @Override
  341. public void setComplete(boolean complete) {
  342. Iterator<GraphicsData> it = objects.iterator();
  343. while (it.hasNext()) {
  344. Completable completedObject = it.next();
  345. completedObject.setComplete(true);
  346. }
  347. super.setComplete(complete);
  348. }
  349. /** {@inheritDoc} */
  350. @Override
  351. protected void writeStart(OutputStream os) throws IOException {
  352. super.writeStart(os);
  353. byte[] data = new byte[17];
  354. copySF(data, Type.BEGIN, Category.GRAPHICS);
  355. os.write(data);
  356. }
  357. /** {@inheritDoc} */
  358. @Override
  359. protected void writeContent(OutputStream os) throws IOException {
  360. super.writeContent(os);
  361. writeObjects(objects, os);
  362. }
  363. /** {@inheritDoc} */
  364. @Override
  365. protected void writeEnd(OutputStream os) throws IOException {
  366. byte[] data = new byte[17];
  367. copySF(data, Type.END, Category.GRAPHICS);
  368. os.write(data);
  369. }
  370. /** the internal graphics state */
  371. private static final class GraphicsState {
  372. private GraphicsState() {
  373. }
  374. /** the current color */
  375. private Color color;
  376. /** the current line type */
  377. private byte lineType;
  378. /** the current line width */
  379. private float lineWidth;
  380. /** the current fill pattern */
  381. private byte patternSymbol;
  382. /** the current character set */
  383. private int characterSet;
  384. }
  385. }