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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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.IOException;
  19. import java.io.OutputStream;
  20. import java.io.UnsupportedEncodingException;
  21. import java.util.ArrayList;
  22. import org.apache.fop.render.afp.fonts.AFPFont;
  23. /**
  24. * An Active Environment Group (AEG) is associated with each page,
  25. * and is contained in the page's begin-end envelope in the data stream.
  26. * The active environment group contains layout and formatting information
  27. * that defines the measurement units and size of the page, and may contain
  28. * resource information.
  29. *
  30. * Any objects that are required for page presentation and that are to be
  31. * treated as resource objects must be mapped with a map structured field
  32. * in the AEG. The scope of an active environment group is the scope of its
  33. * containing page or overlay.
  34. *
  35. */
  36. public final class ActiveEnvironmentGroup extends AbstractNamedAFPObject {
  37. /**
  38. * Default name for the active environment group
  39. */
  40. private static final String DEFAULT_NAME = "AEG00001";
  41. /**
  42. * The collection of MapCodedFont objects
  43. */
  44. private ArrayList _mapCodedFonts = new ArrayList();
  45. /**
  46. * The Object Area Descriptor for the active environment group
  47. */
  48. private ObjectAreaDescriptor _objectAreaDescriptor = null;
  49. /**
  50. * The Object Area Position for the active environment group
  51. */
  52. private ObjectAreaPosition _objectAreaPosition = null;
  53. /**
  54. * The PresentationTextDescriptor for the active environment group
  55. */
  56. private PresentationTextDescriptor _presentationTextDataDescriptor = null;
  57. /**
  58. * The PageDescriptor for the active environment group
  59. */
  60. private PageDescriptor _pageDescriptor = null;
  61. /**
  62. * The collection of MapPageOverlay objects
  63. */
  64. private ArrayList _mapPageOverlays = new ArrayList();
  65. /**
  66. * Default constructor for the ActiveEnvironmentGroup.
  67. * @param width the page width
  68. * @param height the page height
  69. */
  70. public ActiveEnvironmentGroup(int width, int height) {
  71. this(DEFAULT_NAME, width, height);
  72. }
  73. /**
  74. * Constructor for the ActiveEnvironmentGroup, this takes a
  75. * name parameter which must be 8 characters long.
  76. * @param name the active environment group name
  77. * @param width the page width
  78. * @param height the page height
  79. */
  80. public ActiveEnvironmentGroup(String name, int width, int height) {
  81. super(name);
  82. // Create PageDescriptor
  83. _pageDescriptor = new PageDescriptor(width, height);
  84. // Create ObjectAreaDescriptor
  85. _objectAreaDescriptor = new ObjectAreaDescriptor(width, height);
  86. // Create PresentationTextDataDescriptor
  87. _presentationTextDataDescriptor =
  88. new PresentationTextDescriptor(width, height);
  89. }
  90. /**
  91. * Set the position of the object area
  92. * @param x the x offset
  93. * @param y the y offset
  94. * @param rotation the rotation
  95. */
  96. public void setPosition(int x, int y, int rotation) {
  97. // Create ObjectAreaPosition
  98. _objectAreaPosition = new ObjectAreaPosition(x, y, rotation);
  99. }
  100. /**
  101. * Accessor method to obtain the PageDescriptor object of the
  102. * active environment group.
  103. * @return the page descriptor object
  104. */
  105. public PageDescriptor getPageDescriptor() {
  106. return _pageDescriptor;
  107. }
  108. /**
  109. * Accessor method to obtain the PresentationTextDataDescriptor object of
  110. * the active environment group.
  111. * @return the presentation text descriptor
  112. */
  113. public PresentationTextDescriptor getPresentationTextDataDescriptor() {
  114. return _presentationTextDataDescriptor;
  115. }
  116. /**
  117. * Accessor method to write the AFP datastream for the active environment group.
  118. * @param os The stream to write to
  119. * @throws java.io.IOException
  120. */
  121. public void writeDataStream(OutputStream os)
  122. throws IOException {
  123. writeStart(os);
  124. writeObjectList(_mapCodedFonts, os);
  125. writeObjectList(_mapPageOverlays, os);
  126. _pageDescriptor.writeDataStream(os);
  127. if (_objectAreaDescriptor != null && _objectAreaPosition != null) {
  128. _objectAreaDescriptor.writeDataStream(os);
  129. _objectAreaPosition.writeDataStream(os);
  130. }
  131. _presentationTextDataDescriptor.writeDataStream(os);
  132. writeEnd(os);
  133. }
  134. /**
  135. * Helper method to write the start of the active environment group.
  136. * @param os The stream to write to
  137. */
  138. private void writeStart(OutputStream os)
  139. throws IOException {
  140. byte[] data = new byte[17];
  141. data[0] = 0x5A; // Structured field identifier
  142. data[1] = 0x00; // Length byte 1
  143. data[2] = 0x10; // Length byte 2
  144. data[3] = (byte) 0xD3; // Structured field id byte 1
  145. data[4] = (byte) 0xA8; // Structured field id byte 2
  146. data[5] = (byte) 0xC9; // Structured field id byte 3
  147. data[6] = 0x00; // Flags
  148. data[7] = 0x00; // Reserved
  149. data[8] = 0x00; // Reserved
  150. for (int i = 0; i < _nameBytes.length; i++) {
  151. data[9 + i] = _nameBytes[i];
  152. }
  153. os.write(data);
  154. }
  155. /**
  156. * Helper method to write the end of the active environment group.
  157. * @param os The stream to write to
  158. */
  159. private void writeEnd(OutputStream os)
  160. throws IOException {
  161. byte[] data = new byte[17];
  162. data[0] = 0x5A; // Structured field identifier
  163. data[1] = 0x00; // Length byte 1
  164. data[2] = 0x10; // Length byte 2
  165. data[3] = (byte) 0xD3; // Structured field id byte 1
  166. data[4] = (byte) 0xA9; // Structured field id byte 2
  167. data[5] = (byte) 0xC9; // Structured field id byte 3
  168. data[6] = 0x00; // Flags
  169. data[7] = 0x00; // Reserved
  170. data[8] = 0x00; // Reserved
  171. for (int i = 0; i < _nameBytes.length; i++) {
  172. data[9 + i] = _nameBytes[i];
  173. }
  174. os.write(data);
  175. }
  176. /**
  177. * Method to create a map coded font object
  178. * @param fontReference the font number used as the resource identifier
  179. * @param font the font
  180. * @param size the point size of the font
  181. * @param orientation the orientation of the font (e.g. 0, 90, 180, 270)
  182. */
  183. public void createFont(
  184. byte fontReference,
  185. AFPFont font,
  186. int size,
  187. int orientation) {
  188. MapCodedFont mcf = getCurrentMapCodedFont();
  189. if (mcf == null) {
  190. mcf = new MapCodedFont();
  191. _mapCodedFonts.add(mcf);
  192. }
  193. try {
  194. mcf.addFont(
  195. fontReference,
  196. font,
  197. size,
  198. orientation);
  199. } catch (MaximumSizeExceededException msee) {
  200. mcf = new MapCodedFont();
  201. _mapCodedFonts.add(mcf);
  202. try {
  203. mcf.addFont(
  204. fontReference,
  205. font,
  206. size,
  207. orientation);
  208. } catch (MaximumSizeExceededException ex) {
  209. // Should never happen (but log just in case)
  210. log.error("createFont():: resulted in a MaximumSizeExceededException");
  211. }
  212. }
  213. }
  214. /**
  215. * Actually creates the MPO object.
  216. * Also creates the supporting object (an IPO)
  217. * @param name the name of the overlay to be used
  218. */
  219. public void createOverlay(String name) {
  220. MapPageOverlay mpo = getCurrentMapPageOverlay();
  221. if (mpo == null) {
  222. mpo = new MapPageOverlay();
  223. _mapPageOverlays.add(mpo);
  224. }
  225. try {
  226. mpo.addOverlay(name);
  227. } catch (MaximumSizeExceededException msee) {
  228. mpo = new MapPageOverlay();
  229. _mapPageOverlays.add(mpo);
  230. try {
  231. mpo.addOverlay(name);
  232. } catch (MaximumSizeExceededException ex) {
  233. // Should never happen (but log just in case)
  234. log.error("createOverlay():: resulted in a MaximumSizeExceededException");
  235. }
  236. }
  237. }
  238. /**
  239. * Getter method for the most recent MapCodedFont added to the
  240. * Active Environment Group (returns null if no MapCodedFonts exist)
  241. * @return the most recent Map Coded Font.
  242. */
  243. private MapCodedFont getCurrentMapCodedFont() {
  244. int size = _mapCodedFonts.size();
  245. if (size > 0) {
  246. return (MapCodedFont) _mapCodedFonts.get(_mapCodedFonts.size() - 1);
  247. } else {
  248. return null;
  249. }
  250. }
  251. /**
  252. * Getter method for the most recent MapPageOverlay added to the
  253. * Active Environment Group (returns null if no MapPageOverlay exist)
  254. * @return the most recent Map Coded Font
  255. */
  256. private MapPageOverlay getCurrentMapPageOverlay() {
  257. int size = _mapPageOverlays.size();
  258. if (size > 0) {
  259. return (MapPageOverlay) _mapPageOverlays.get(
  260. _mapPageOverlays.size() - 1);
  261. } else {
  262. return null;
  263. }
  264. }
  265. }