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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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.io.IOException;
  20. import java.io.OutputStream;
  21. import java.io.UnsupportedEncodingException;
  22. import java.util.List;
  23. import org.apache.fop.afp.AFPLineDataInfo;
  24. import org.apache.fop.afp.AFPTextDataInfo;
  25. import org.apache.fop.afp.Completable;
  26. import org.apache.fop.afp.Factory;
  27. import org.apache.fop.afp.fonts.AFPFont;
  28. /**
  29. * Pages contain the data objects that comprise a presentation document. Each
  30. * page has a set of data objects associated with it. Each page within a
  31. * document is independent from any other page, and each must establish its own
  32. * environment parameters.
  33. * <p>
  34. * The page is the level in the document component hierarchy that is used for
  35. * printing or displaying a document's content. The data objects contained in
  36. * the page envelope in the data stream are presented when the page is
  37. * presented. Each data object has layout information associated with it that
  38. * directs the placement and orientation of the data on the page. In addition,
  39. * each page contains layout information that specifies the measurement units,
  40. * page width, and page depth.
  41. * <p>
  42. * A page is initiated by a begin page structured field and terminated by an end
  43. * page structured field. Structured fields that define objects and active
  44. * environment groups or that specify attributes of the page may be encountered
  45. * in page state.
  46. */
  47. public abstract class AbstractPageObject extends AbstractNamedAFPObject implements Completable {
  48. /** The active environment group for the page */
  49. protected ActiveEnvironmentGroup activeEnvironmentGroup = null;
  50. /** The current presentation text object */
  51. private PresentationTextObject currentPresentationTextObject = null;
  52. /** The list of tag logical elements */
  53. protected List/*<TagLogicalElement>*/ tagLogicalElements = null;
  54. /** The list of objects within this resource container */
  55. protected List/*<AbstractStructuredAFPObject>*/ objects = new java.util.ArrayList();
  56. /** The page width */
  57. private int width;
  58. /** The page height */
  59. private int height;
  60. /** The page rotation */
  61. protected int rotation = 0;
  62. /** The page state */
  63. protected boolean complete = false;
  64. /** The width resolution */
  65. private int widthRes;
  66. /** The height resolution */
  67. private int heightRes;
  68. /** the object factory */
  69. protected final Factory factory;
  70. /**
  71. * Default constructor
  72. *
  73. * @param factory the object factory
  74. */
  75. public AbstractPageObject(Factory factory) {
  76. this.factory = factory;
  77. }
  78. /**
  79. * Main constructor
  80. *
  81. * @param factory the object factory
  82. * @param name the name of this page object
  83. */
  84. public AbstractPageObject(Factory factory, String name) {
  85. super(name);
  86. this.factory = factory;
  87. }
  88. /**
  89. * Construct a new page object for the specified name argument, the page
  90. * name should be an 8 character identifier.
  91. *
  92. * @param factory
  93. * the object factory.
  94. * @param name
  95. * the name of the page.
  96. * @param width
  97. * the width of the page.
  98. * @param height
  99. * the height of the page.
  100. * @param rotation
  101. * the rotation of the page.
  102. * @param widthRes
  103. * the width resolution of the page.
  104. * @param heightRes
  105. * the height resolution of the page.
  106. */
  107. public AbstractPageObject(Factory factory,
  108. String name, int width, int height, int rotation,
  109. int widthRes, int heightRes) {
  110. super(name);
  111. this.factory = factory;
  112. this.width = width;
  113. this.height = height;
  114. this.rotation = rotation;
  115. this.widthRes = widthRes;
  116. this.heightRes = heightRes;
  117. }
  118. /**
  119. * Helper method to create a map coded font object on the current page, this
  120. * method delegates the construction of the map coded font object to the
  121. * active environment group on the page.
  122. *
  123. * @param fontReference
  124. * the font number used as the resource identifier
  125. * @param font
  126. * the font
  127. * @param size
  128. * the point size of the font
  129. */
  130. public void createFont(int fontReference, AFPFont font, int size) {
  131. getActiveEnvironmentGroup().createFont(fontReference, font, size, 0);
  132. }
  133. /**
  134. * Helper method to create a line on the current page, this method delegates
  135. * to the presentation text object in order to construct the line.
  136. *
  137. * @param lineDataInfo the line data information.
  138. */
  139. public void createLine(AFPLineDataInfo lineDataInfo) {
  140. getPresentationTextObject().createLineData(lineDataInfo);
  141. }
  142. /**
  143. * Helper method to create text on the current page, this method delegates
  144. * to the presentation text object in order to construct the text.
  145. *
  146. * @param textDataInfo
  147. * the afp text data
  148. * @throws UnsupportedEncodingException thrown if character encoding is not supported
  149. */
  150. public void createText(AFPTextDataInfo textDataInfo) throws UnsupportedEncodingException {
  151. getPresentationTextObject().createTextData(textDataInfo);
  152. }
  153. /**
  154. * Helper method to mark the end of the page. This should end the control
  155. * sequence on the current presentation text object.
  156. */
  157. public void endPage() {
  158. if (currentPresentationTextObject != null) {
  159. currentPresentationTextObject.endControlSequence();
  160. }
  161. setComplete(true);
  162. }
  163. /**
  164. * Ends the presentation text object
  165. */
  166. protected void endPresentationObject() {
  167. if (currentPresentationTextObject != null) {
  168. currentPresentationTextObject.endControlSequence();
  169. currentPresentationTextObject = null;
  170. }
  171. }
  172. /**
  173. * Helper method to create a presentation text object
  174. * on the current page and to return the object.
  175. *
  176. * @return the presentation text object
  177. */
  178. public PresentationTextObject getPresentationTextObject() {
  179. if (currentPresentationTextObject == null) {
  180. PresentationTextObject presentationTextObject
  181. = factory.createPresentationTextObject();
  182. addObject(presentationTextObject);
  183. this.currentPresentationTextObject = presentationTextObject;
  184. }
  185. return currentPresentationTextObject;
  186. }
  187. /**
  188. * Creates a TagLogicalElement on the page.
  189. *
  190. * @param name
  191. * the name of the tag
  192. * @param value
  193. * the value of the tag
  194. * @param tleID
  195. * unique ID within AFP stream
  196. */
  197. public void createTagLogicalElement(String name, String value, int tleID) {
  198. TagLogicalElement tle = new TagLogicalElement(name, value, tleID);
  199. if (tagLogicalElements == null) {
  200. tagLogicalElements = new java.util.ArrayList/*<TagLogicalElement>*/();
  201. }
  202. tagLogicalElements.add(tle);
  203. }
  204. /**
  205. * Creates a NoOperation on the page.
  206. *
  207. * @param content the byte data
  208. */
  209. public void createNoOperation(String content) {
  210. addObject(new NoOperation(content));
  211. }
  212. /**
  213. * Creates an IncludePageSegment on the current page.
  214. *
  215. * @param name
  216. * the name of the page segment
  217. * @param x
  218. * the x coordinate of the page segment.
  219. * @param y
  220. * the y coordinate of the page segment.
  221. */
  222. public void createIncludePageSegment(String name, int x, int y, boolean hard) {
  223. IncludePageSegment ips = factory.createIncludePageSegment(name, x, y);
  224. addObject(ips);
  225. if (hard) {
  226. //For performance reasons, page segments can be turned into hard page segments
  227. //using the Map Page Segment (MPS) structured field.
  228. getActiveEnvironmentGroup().addMapPageSegment(name);
  229. }
  230. }
  231. /**
  232. * Returns the ActiveEnvironmentGroup associated with this page.
  233. *
  234. * @return the ActiveEnvironmentGroup object
  235. */
  236. public ActiveEnvironmentGroup getActiveEnvironmentGroup() {
  237. if (activeEnvironmentGroup == null) {
  238. // every page object must have an ActiveEnvironmentGroup
  239. this.activeEnvironmentGroup
  240. = factory.createActiveEnvironmentGroup(width, height, widthRes, heightRes);
  241. if (rotation != 0) {
  242. switch (rotation) {
  243. case 90:
  244. activeEnvironmentGroup.setObjectAreaPosition(width, 0, rotation);
  245. break;
  246. case 180:
  247. activeEnvironmentGroup.setObjectAreaPosition(width, height, rotation);
  248. break;
  249. case 270:
  250. activeEnvironmentGroup.setObjectAreaPosition(0, height, rotation);
  251. break;
  252. default:
  253. }
  254. }
  255. }
  256. return activeEnvironmentGroup;
  257. }
  258. /**
  259. * Returns the height of the page
  260. *
  261. * @return the height of the page
  262. */
  263. public int getHeight() {
  264. return height;
  265. }
  266. /**
  267. * Returns the width of the page
  268. *
  269. * @return the width of the page
  270. */
  271. public int getWidth() {
  272. return width;
  273. }
  274. /**
  275. * Returns the rotation of the page
  276. *
  277. * @return the rotation of the page
  278. */
  279. public int getRotation() {
  280. return rotation;
  281. }
  282. /** {@inheritDoc} */
  283. protected void writeContent(OutputStream os) throws IOException {
  284. super.writeContent(os);
  285. writeObjects(this.objects, os);
  286. }
  287. /**
  288. * Adds an AFP object reference to this page
  289. *
  290. * @param obj an AFP object
  291. */
  292. public void addObject(Object obj) {
  293. objects.add(obj);
  294. }
  295. /** {@inheritDoc} */
  296. public void setComplete(boolean complete) {
  297. this.complete = complete;
  298. }
  299. /** {@inheritDoc} */
  300. public boolean isComplete() {
  301. return this.complete;
  302. }
  303. }