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.

AbstractPageObject.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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.render.afp.modca;
  19. import java.awt.Color;
  20. import java.io.UnsupportedEncodingException;
  21. import java.util.ArrayList;
  22. import java.util.List;
  23. import org.apache.fop.render.afp.fonts.AFPFont;
  24. import org.apache.fop.render.afp.tools.StringUtils;
  25. /**
  26. * Pages contain the data objects that comprise a presentation document. Each
  27. * page has a set of data objects associated with it. Each page within a
  28. * document is independent from any other page, and each must establish its own
  29. * environment parameters.
  30. *
  31. * The page is the level in the document component hierarchy that is used for
  32. * printing or displaying a document's content. The data objects contained in
  33. * the page envelope in the data stream are presented when the page is
  34. * presented. Each data object has layout information associated with it that
  35. * directs the placement and orientation of the data on the page. In addition,
  36. * each page contains layout information that specifies the measurement units,
  37. * page width, and page depth.
  38. *
  39. * A page is initiated by a begin page structured field and terminated by an end
  40. * page structured field. Structured fields that define objects and active
  41. * environment groups or that specify attributes of the page may be encountered
  42. * in page state.
  43. *
  44. */
  45. public abstract class AbstractPageObject extends AbstractNamedAFPObject {
  46. /**
  47. * The active environment group for the page
  48. */
  49. protected ActiveEnvironmentGroup _activeEnvironmentGroup = null;
  50. /**
  51. * The presentation text object, we only have one per page
  52. */
  53. private PresentationTextObject _presentationTextObject = null;
  54. /**
  55. * The list of objects within the page
  56. */
  57. protected List _objects = new ArrayList();
  58. /**
  59. * The list of tag logical elements
  60. */
  61. protected ArrayList _tagLogicalElements = new ArrayList();
  62. /**
  63. * The list of the include page segments
  64. */
  65. protected ArrayList _segments = new ArrayList();
  66. /**
  67. * The page width
  68. */
  69. private int _width;
  70. /**
  71. * The page height
  72. */
  73. private int _height;
  74. /**
  75. * The page rotation
  76. */
  77. private int _rotation = 0;
  78. /**
  79. * The page state
  80. */
  81. private boolean _complete = false;
  82. /**
  83. * Construct a new page object for the specified name argument, the page
  84. * name should be an 8 character identifier.
  85. *
  86. * @param name
  87. * the name of the page.
  88. * @param width
  89. * the width of the page.
  90. * @param height
  91. * the height of the page.
  92. * @param rotation
  93. * the rotation of the page.
  94. */
  95. public AbstractPageObject(String name, int width, int height, int rotation) {
  96. super(name);
  97. _name = name;
  98. _rotation = rotation;
  99. _width = width;
  100. _height = height;
  101. /**
  102. * Every page object must have an ActiveEnvironmentGroup
  103. */
  104. _activeEnvironmentGroup = new ActiveEnvironmentGroup(_width, _height);
  105. if (_rotation != 0) {
  106. switch (_rotation) {
  107. case 90:
  108. _activeEnvironmentGroup.setPosition(_width, 0, _rotation);
  109. break;
  110. case 180:
  111. _activeEnvironmentGroup.setPosition(_width, _height, _rotation);
  112. break;
  113. case 270:
  114. _activeEnvironmentGroup.setPosition(0, _height, _rotation);
  115. break;
  116. }
  117. }
  118. /**
  119. * We have a presentation text object per page
  120. */
  121. _presentationTextObject = new PresentationTextObject();
  122. _objects.add(_presentationTextObject);
  123. }
  124. /**
  125. * Helper method to create a map coded font object on the current page, this
  126. * method delegates the construction of the map coded font object to the
  127. * active environment group on the page.
  128. *
  129. * @param fontReference
  130. * the font number used as the resource identifier
  131. * @param font
  132. * the font
  133. * @param size
  134. * the point size of the font
  135. */
  136. public void createFont(byte fontReference, AFPFont font, int size) {
  137. _activeEnvironmentGroup.createFont(fontReference, font, size, 0);
  138. }
  139. /**
  140. * Helper method to create a line on the current page, this method delegates
  141. * to the presentation text object in order to construct the line.
  142. *
  143. * @param x1
  144. * the first x coordinate of the line
  145. * @param y1
  146. * the first y coordinate of the line
  147. * @param x2
  148. * the second x coordinate of the line
  149. * @param y2
  150. * the second y coordinate of the line
  151. * @param thickness
  152. * the thickness of the line
  153. * @param rotation
  154. * the rotation of the line
  155. * @param col
  156. * The text color.
  157. */
  158. public void createLine(int x1, int y1, int x2, int y2, int thickness, int rotation, Color col) {
  159. if (_presentationTextObject == null) {
  160. _presentationTextObject = new PresentationTextObject();
  161. _objects.add(_presentationTextObject);
  162. }
  163. _presentationTextObject.createLineData(x1, y1, x2, y2, thickness, rotation, col);
  164. }
  165. /**
  166. * Helper method to create text on the current page, this method delegates
  167. * to the presentation text object in order to construct the text.
  168. *
  169. * @param fontNumber
  170. * the font number used as the resource identifier
  171. * @param x
  172. * the x coordinate of the text data
  173. * @param y
  174. * the y coordinate of the text data
  175. * @param rotation
  176. * the rotation of the text data
  177. * @param col
  178. * the text color
  179. * @param vsci
  180. * The variable space character increment.
  181. * @param ica
  182. * The inter character adjustment.
  183. * @param data
  184. * the text data to create
  185. */
  186. public void createText(int fontNumber, int x, int y, int rotation, Color col, int vsci, int ica, byte[] data) {
  187. if (_presentationTextObject == null) {
  188. _presentationTextObject = new PresentationTextObject();
  189. _objects.add(_presentationTextObject);
  190. }
  191. _presentationTextObject.createTextData(fontNumber, x, y, rotation, col, vsci, ica, data);
  192. }
  193. /**
  194. * Helper method to mark the end of the page. This should end the control
  195. * sequence on the current presenation text object.
  196. */
  197. public void endPage() {
  198. if (_presentationTextObject != null) {
  199. _presentationTextObject.endControlSequence();
  200. }
  201. _complete = true;
  202. }
  203. /**
  204. * This method will create shading on the page using the specified
  205. * coordinates (the shading contrast is controlled via the red, green blue
  206. * parameters, by converting this to grey scale).
  207. *
  208. * @param x
  209. * the x coordinate of the shading
  210. * @param y
  211. * the y coordinate of the shading
  212. * @param w
  213. * the width of the shaded area
  214. * @param h
  215. * the height of the shaded area
  216. * @param red
  217. * the red value
  218. * @param green
  219. * the green value
  220. * @param blue
  221. * the blue value
  222. */
  223. public void createShading(int x, int y, int w, int h, int red, int green,
  224. int blue) {
  225. int xCoord = 0;
  226. int yCoord = 0;
  227. int width = 0;
  228. int height = 0;
  229. switch (_rotation) {
  230. case 90:
  231. xCoord = _width - y - h;
  232. yCoord = x;
  233. width = h;
  234. height = w;
  235. break;
  236. case 180:
  237. xCoord = _width - x - w;
  238. yCoord = _height - y - h;
  239. width = w;
  240. height = h;
  241. break;
  242. case 270:
  243. xCoord = y;
  244. yCoord = _height - x - w;
  245. width = h;
  246. height = w;
  247. break;
  248. default:
  249. xCoord = x;
  250. yCoord = y;
  251. width = w;
  252. height = h;
  253. break;
  254. }
  255. // Convert the color to grey scale
  256. float shade = (float) ((red * 0.3) + (green * 0.59) + (blue * 0.11));
  257. int greyscale = Math.round((shade / 255) * 16);
  258. String imageName = "IMG"
  259. + StringUtils.lpad(String.valueOf(_objects.size() + 1),
  260. '0', 5);
  261. IMImageObject io = new IMImageObject(imageName);
  262. ImageOutputControl ioc = new ImageOutputControl(0, 0);
  263. ImageInputDescriptor iid = new ImageInputDescriptor();
  264. ImageCellPosition icp = new ImageCellPosition(xCoord, yCoord);
  265. icp.setXFillSize(width);
  266. icp.setYFillSize(height);
  267. icp.setXSize(64);
  268. icp.setYSize(8);
  269. //defing this as a resource
  270. ImageRasterData ird = new ImageRasterData(ImageRasterPattern
  271. .getRasterData(greyscale));
  272. io.setImageOutputControl(ioc);
  273. io.setImageInputDescriptor(iid);
  274. io.setImageCellPosition(icp);
  275. io.setImageRasterData(ird);
  276. _objects.add(io);
  277. }
  278. /**
  279. * Helper method to create an image on the current page and to return
  280. * the object.
  281. */
  282. public ImageObject getImageObject() {
  283. if (_presentationTextObject != null) {
  284. _presentationTextObject.endControlSequence();
  285. }
  286. _presentationTextObject = null;
  287. String imageName = "IMG"
  288. + StringUtils.lpad(String.valueOf(_objects.size() + 1),
  289. '0', 5);
  290. ImageObject io = new ImageObject(imageName);
  291. _objects.add(io);
  292. return io;
  293. }
  294. /**
  295. * Creates a TagLogicalElement on the page.
  296. *
  297. * @param name
  298. * the name of the tag
  299. * @param value
  300. * the value of the tag
  301. */
  302. public void createTagLogicalElement(String name, String value) {
  303. TagLogicalElement tle = new TagLogicalElement(name, value);
  304. _tagLogicalElements.add(tle);
  305. }
  306. /**
  307. * Creates a NoOperation on the page.
  308. *
  309. * @param content the byte data
  310. */
  311. public void createNoOperation(String content) {
  312. NoOperation noOp = new NoOperation(content);
  313. _objects.add(noOp);
  314. }
  315. /**
  316. * Creates an IncludePageSegment on the current page.
  317. *
  318. * @param name
  319. * the name of the page segment
  320. * @param xCoor
  321. * the x cooridinate of the page segment.
  322. * @param yCoor
  323. * the y cooridinate of the page segment.
  324. */
  325. public void createIncludePageSegment(String name, int xCoor, int yCoor) {
  326. IncludePageSegment ips = new IncludePageSegment(name, xCoor, yCoor);
  327. _segments.add(ips);
  328. }
  329. /**
  330. * Returns the ActiveEnvironmentGroup associated with this page.
  331. *
  332. * @return the ActiveEnvironmentGroup object
  333. */
  334. public ActiveEnvironmentGroup getActiveEnvironmentGroup() {
  335. return _activeEnvironmentGroup;
  336. }
  337. /**
  338. * Returns an indication if the page is complete
  339. */
  340. public boolean isComplete() {
  341. return _complete;
  342. }
  343. /**
  344. * Returns the height of the page
  345. */
  346. public int getHeight() {
  347. return _height;
  348. }
  349. /**
  350. * Returns the width of the page
  351. */
  352. public int getWidth() {
  353. return _width;
  354. }
  355. /**
  356. * Returns the rotation of the page
  357. */
  358. public int getRotation() {
  359. return _rotation;
  360. }
  361. }