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

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