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 12KB

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