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

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