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

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