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.

ActiveEnvironmentGroup.java 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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.util.List;
  22. import org.apache.fop.afp.Factory;
  23. import org.apache.fop.afp.fonts.AFPFont;
  24. /**
  25. * An Active Environment Group (AEG) is associated with each page,
  26. * and is contained in the page's begin-end envelope in the data stream.
  27. * The active environment group contains layout and formatting information
  28. * that defines the measurement units and size of the page, and may contain
  29. * resource information.
  30. *
  31. * Any objects that are required for page presentation and that are to be
  32. * treated as resource objects must be mapped with a map structured field
  33. * in the AEG. The scope of an active environment group is the scope of its
  34. * containing page or overlay.
  35. *
  36. */
  37. public final class ActiveEnvironmentGroup extends AbstractEnvironmentGroup {
  38. /** The collection of MapCodedFont objects */
  39. private final List/*<MapCodedFonts>*/ mapCodedFonts
  40. = new java.util.ArrayList/*<MapCodedFonts>*/();
  41. /** the collection of MapPageSegments objects */
  42. private List mapPageSegments;
  43. /** the Object Area Descriptor for the active environment group */
  44. private ObjectAreaDescriptor objectAreaDescriptor;
  45. /** the Object Area Position for the active environment group */
  46. private ObjectAreaPosition objectAreaPosition;
  47. /** the PresentationTextDescriptor for the active environment group */
  48. private PresentationTextDescriptor presentationTextDataDescriptor;
  49. /** the PageDescriptor for the active environment group */
  50. private PageDescriptor pageDescriptor;
  51. /** the resource manager */
  52. private final Factory factory;
  53. /**
  54. * Constructor for the ActiveEnvironmentGroup, this takes a
  55. * name parameter which must be 8 characters long.
  56. *
  57. * @param factory the object factory
  58. * @param name the active environment group name
  59. * @param width the page width
  60. * @param height the page height
  61. * @param widthRes the page width resolution
  62. * @param heightRes the page height resolution
  63. */
  64. public ActiveEnvironmentGroup(Factory factory,
  65. String name, int width, int height, int widthRes, int heightRes) {
  66. super(name);
  67. this.factory = factory;
  68. // Create PageDescriptor
  69. this.pageDescriptor
  70. = factory.createPageDescriptor(width, height, widthRes, heightRes);
  71. // Create ObjectAreaDescriptor
  72. this.objectAreaDescriptor
  73. = factory.createObjectAreaDescriptor(width, height, widthRes, heightRes);
  74. // Create PresentationTextDataDescriptor
  75. this.presentationTextDataDescriptor
  76. = factory.createPresentationTextDataDescriptor(width, height,
  77. widthRes, heightRes);
  78. }
  79. /**
  80. * Set the position of the object area
  81. *
  82. * @param x the x offset
  83. * @param y the y offset
  84. * @param rotation the rotation
  85. */
  86. public void setObjectAreaPosition(int x, int y, int rotation) {
  87. this.objectAreaPosition = factory.createObjectAreaPosition(x, y, rotation);
  88. }
  89. /**
  90. * Accessor method to obtain the PageDescriptor object of the
  91. * active environment group.
  92. *
  93. * @return the page descriptor object
  94. */
  95. public PageDescriptor getPageDescriptor() {
  96. return pageDescriptor;
  97. }
  98. /**
  99. * Accessor method to obtain the PresentationTextDataDescriptor object of
  100. * the active environment group.
  101. *
  102. * @return the presentation text descriptor
  103. */
  104. public PresentationTextDescriptor getPresentationTextDataDescriptor() {
  105. return presentationTextDataDescriptor;
  106. }
  107. /** {@inheritDoc} */
  108. public void writeContent(OutputStream os) throws IOException {
  109. super.writeTriplets(os);
  110. writeObjects(mapCodedFonts, os);
  111. writeObjects(mapDataResources, os);
  112. writeObjects(mapPageOverlays, os);
  113. writeObjects(mapPageSegments, os);
  114. if (pageDescriptor != null) {
  115. pageDescriptor.writeToStream(os);
  116. }
  117. if (objectAreaDescriptor != null && objectAreaPosition != null) {
  118. objectAreaDescriptor.writeToStream(os);
  119. objectAreaPosition.writeToStream(os);
  120. }
  121. if (presentationTextDataDescriptor != null) {
  122. presentationTextDataDescriptor.writeToStream(os);
  123. }
  124. }
  125. /** {@inheritDoc} */
  126. protected void writeStart(OutputStream os) throws IOException {
  127. byte[] data = new byte[17];
  128. copySF(data, Type.BEGIN, Category.ACTIVE_ENVIRONMENT_GROUP);
  129. os.write(data);
  130. }
  131. /** {@inheritDoc} */
  132. protected void writeEnd(OutputStream os) throws IOException {
  133. byte[] data = new byte[17];
  134. copySF(data, Type.END, Category.ACTIVE_ENVIRONMENT_GROUP);
  135. os.write(data);
  136. }
  137. /**
  138. * Method to create a map coded font object
  139. *
  140. * @param fontRef the font number used as the resource identifier
  141. * @param font the font
  142. * @param size the point size of the font
  143. * @param orientation the orientation of the font (e.g. 0, 90, 180, 270)
  144. */
  145. public void createFont(int fontRef, AFPFont font, int size, int orientation) {
  146. MapCodedFont mapCodedFont = getCurrentMapCodedFont();
  147. if (mapCodedFont == null) {
  148. mapCodedFont = factory.createMapCodedFont();
  149. mapCodedFonts.add(mapCodedFont);
  150. }
  151. try {
  152. mapCodedFont.addFont(fontRef, font, size, orientation);
  153. } catch (MaximumSizeExceededException msee) {
  154. mapCodedFont = factory.createMapCodedFont();
  155. mapCodedFonts.add(mapCodedFont);
  156. try {
  157. mapCodedFont.addFont(fontRef, font, size, orientation);
  158. } catch (MaximumSizeExceededException ex) {
  159. // Should never happen (but log just in case)
  160. LOG.error("createFont():: resulted in a MaximumSizeExceededException");
  161. }
  162. }
  163. }
  164. /**
  165. * Getter method for the most recent MapCodedFont added to the
  166. * Active Environment Group (returns null if no MapCodedFonts exist)
  167. *
  168. * @return the most recent Map Coded Font.
  169. */
  170. private MapCodedFont getCurrentMapCodedFont() {
  171. int size = mapCodedFonts.size();
  172. if (size > 0) {
  173. return (MapCodedFont)mapCodedFonts.get(size - 1);
  174. } else {
  175. return null;
  176. }
  177. }
  178. /**
  179. * Add map page segment.
  180. * @param name of segment to add
  181. */
  182. public void addMapPageSegment(String name) {
  183. try {
  184. needMapPageSegment().addPageSegment(name);
  185. } catch (MaximumSizeExceededException e) {
  186. //Should not happen, handled internally
  187. throw new IllegalStateException("Internal error: " + e.getMessage());
  188. }
  189. }
  190. private MapPageSegment getCurrentMapPageSegment() {
  191. return (MapPageSegment)getLastElement(this.mapPageSegments);
  192. }
  193. private MapPageSegment needMapPageSegment() {
  194. if (this.mapPageSegments == null) {
  195. this.mapPageSegments = new java.util.ArrayList();
  196. }
  197. MapPageSegment seg = getCurrentMapPageSegment();
  198. if (seg == null || seg.isFull()) {
  199. seg = new MapPageSegment();
  200. this.mapPageSegments.add(seg);
  201. }
  202. return seg;
  203. }
  204. }