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

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