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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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.AFPDataObjectInfo;
  23. import org.apache.fop.afp.Factory;
  24. import org.apache.fop.afp.fonts.AFPFont;
  25. import org.apache.fop.afp.modca.triplets.AbstractTriplet;
  26. import org.apache.fop.afp.modca.triplets.EncodingTriplet;
  27. import org.apache.fop.afp.modca.triplets.FullyQualifiedNameTriplet;
  28. import org.apache.fop.afp.modca.triplets.ObjectClassificationTriplet;
  29. import org.apache.fop.afp.util.BinaryUtils;
  30. import org.apache.fop.apps.MimeConstants;
  31. import org.apache.fop.fonts.FontType;
  32. import org.apache.fop.render.afp.AFPFontConfig;
  33. /**
  34. * An Active Environment Group (AEG) is associated with each page,
  35. * and is contained in the page's begin-end envelope in the data stream.
  36. * The active environment group contains layout and formatting information
  37. * that defines the measurement units and size of the page, and may contain
  38. * resource information.
  39. *
  40. * Any objects that are required for page presentation and that are to be
  41. * treated as resource objects must be mapped with a map structured field
  42. * in the AEG. The scope of an active environment group is the scope of its
  43. * containing page or overlay.
  44. *
  45. */
  46. public final class ActiveEnvironmentGroup extends AbstractEnvironmentGroup {
  47. /** The collection of MapCodedFont objects */
  48. private final List/*<MapCodedFonts>*/ mapCodedFonts
  49. = new java.util.ArrayList/*<MapCodedFonts>*/();
  50. /** the collection of MapPageSegments objects */
  51. private List mapPageSegments;
  52. /** the Object Area Descriptor for the active environment group */
  53. private ObjectAreaDescriptor objectAreaDescriptor;
  54. /** the Object Area Position for the active environment group */
  55. private ObjectAreaPosition objectAreaPosition;
  56. /** the PresentationTextDescriptor for the active environment group */
  57. private PresentationTextDescriptor presentationTextDataDescriptor;
  58. /** the PageDescriptor for the active environment group */
  59. private PageDescriptor pageDescriptor;
  60. /** the resource manager */
  61. private final Factory factory;
  62. private MapDataResource mdr;
  63. /**
  64. * Constructor for the ActiveEnvironmentGroup, this takes a
  65. * name parameter which must be 8 characters long.
  66. *
  67. * @param factory the object factory
  68. * @param name the active environment group name
  69. * @param width the page width
  70. * @param height the page height
  71. * @param widthRes the page width resolution
  72. * @param heightRes the page height resolution
  73. */
  74. public ActiveEnvironmentGroup(Factory factory,
  75. String name, int width, int height, int widthRes, int heightRes) {
  76. super(name);
  77. this.factory = factory;
  78. // Create PageDescriptor
  79. this.pageDescriptor
  80. = factory.createPageDescriptor(width, height, widthRes, heightRes);
  81. // Create ObjectAreaDescriptor
  82. this.objectAreaDescriptor
  83. = factory.createObjectAreaDescriptor(width, height, widthRes, heightRes);
  84. // Create PresentationTextDataDescriptor
  85. this.presentationTextDataDescriptor
  86. = factory.createPresentationTextDataDescriptor(width, height,
  87. widthRes, heightRes);
  88. }
  89. /**
  90. * Set the position of the object area
  91. *
  92. * @param x the x offset
  93. * @param y the y offset
  94. * @param rotation the rotation
  95. */
  96. public void setObjectAreaPosition(int x, int y, int rotation) {
  97. this.objectAreaPosition = factory.createObjectAreaPosition(x, y, rotation);
  98. }
  99. /**
  100. * Accessor method to obtain the PageDescriptor object of the
  101. * active environment group.
  102. *
  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. *
  112. * @return the presentation text descriptor
  113. */
  114. public PresentationTextDescriptor getPresentationTextDataDescriptor() {
  115. return presentationTextDataDescriptor;
  116. }
  117. /** {@inheritDoc} */
  118. public void writeContent(OutputStream os) throws IOException {
  119. super.writeTriplets(os);
  120. writeObjects(mapCodedFonts, os);
  121. writeObjects(mapDataResources, os);
  122. writeObjects(mapPageOverlays, os);
  123. writeObjects(mapPageSegments, os);
  124. if (pageDescriptor != null) {
  125. pageDescriptor.writeToStream(os);
  126. }
  127. if (objectAreaDescriptor != null && objectAreaPosition != null) {
  128. objectAreaDescriptor.writeToStream(os);
  129. objectAreaPosition.writeToStream(os);
  130. }
  131. if (presentationTextDataDescriptor != null) {
  132. presentationTextDataDescriptor.writeToStream(os);
  133. }
  134. }
  135. /** {@inheritDoc} */
  136. protected void writeStart(OutputStream os) throws IOException {
  137. byte[] data = new byte[17];
  138. copySF(data, Type.BEGIN, Category.ACTIVE_ENVIRONMENT_GROUP);
  139. os.write(data);
  140. }
  141. /** {@inheritDoc} */
  142. protected void writeEnd(OutputStream os) throws IOException {
  143. byte[] data = new byte[17];
  144. copySF(data, Type.END, Category.ACTIVE_ENVIRONMENT_GROUP);
  145. os.write(data);
  146. }
  147. /**
  148. * Method to create a map coded font object
  149. *
  150. * @param fontRef the font number used as the resource identifier
  151. * @param font the font
  152. * @param size the point size of the font
  153. * @param orientation the orientation of the font (e.g. 0, 90, 180, 270)
  154. */
  155. public void createFont(int fontRef, AFPFont font, int size, int orientation) {
  156. if (font.getFontType() == FontType.TRUETYPE) {
  157. if (mdr == null) {
  158. mdr = factory.createMapDataResource();
  159. mapCodedFonts.add(mdr);
  160. }
  161. mdr.addTriplet(new EncodingTriplet(1200));
  162. String name = font.getFontName();
  163. if (((AFPFontConfig.AFPTrueTypeFont)font).getTTC() != null) {
  164. name = ((AFPFontConfig.AFPTrueTypeFont)font).getTTC();
  165. }
  166. mdr.setFullyQualifiedName(FullyQualifiedNameTriplet.TYPE_DATA_OBJECT_EXTERNAL_RESOURCE_REF,
  167. FullyQualifiedNameTriplet.FORMAT_CHARSTR, name, true);
  168. mdr.addTriplet(new FontFullyQualifiedNameTriplet((byte) fontRef));
  169. setupTruetypeMDR(mdr, false);
  170. mdr.addTriplet(new DataObjectFontTriplet(size / 1000));
  171. mdr.finishElement();
  172. } else {
  173. MapCodedFont mapCodedFont = getCurrentMapCodedFont();
  174. if (mapCodedFont == null) {
  175. mapCodedFont = factory.createMapCodedFont();
  176. mapCodedFonts.add(mapCodedFont);
  177. }
  178. try {
  179. mapCodedFont.addFont(fontRef, font, size, orientation);
  180. } catch (MaximumSizeExceededException msee) {
  181. mapCodedFont = factory.createMapCodedFont();
  182. mapCodedFonts.add(mapCodedFont);
  183. try {
  184. mapCodedFont.addFont(fontRef, font, size, orientation);
  185. } catch (MaximumSizeExceededException ex) {
  186. // Should never happen (but log just in case)
  187. LOG.error("createFont():: resulted in a MaximumSizeExceededException");
  188. }
  189. }
  190. }
  191. }
  192. public static void setupTruetypeMDR(AbstractTripletStructuredObject mdr, boolean res) {
  193. AFPDataObjectInfo dataInfo = new AFPDataObjectInfo();
  194. dataInfo.setMimeType(MimeConstants.MIME_AFP_TRUETYPE);
  195. mdr.setObjectClassification(ObjectClassificationTriplet.CLASS_DATA_OBJECT_FONT,
  196. dataInfo.getObjectType(), res, false, res);
  197. }
  198. public static class FontFullyQualifiedNameTriplet extends AbstractTriplet {
  199. private byte fqName;
  200. public FontFullyQualifiedNameTriplet(byte fqName) {
  201. super(FULLY_QUALIFIED_NAME);
  202. this.fqName = fqName;
  203. }
  204. public int getDataLength() {
  205. return 5;
  206. }
  207. public void writeToStream(OutputStream os) throws IOException {
  208. byte[] data = getData();
  209. data[2] = FullyQualifiedNameTriplet.TYPE_DATA_OBJECT_INTERNAL_RESOURCE_REF;
  210. data[3] = FullyQualifiedNameTriplet.FORMAT_CHARSTR;
  211. data[4] = fqName;
  212. os.write(data);
  213. }
  214. }
  215. static class DataObjectFontTriplet extends AbstractTriplet {
  216. private int pointSize;
  217. public DataObjectFontTriplet(int size) {
  218. super(DATA_OBJECT_FONT_DESCRIPTOR);
  219. pointSize = size;
  220. }
  221. public int getDataLength() {
  222. return 16;
  223. }
  224. public void writeToStream(OutputStream os) throws IOException {
  225. byte[] data = getData();
  226. data[3] = 0x20;
  227. byte[] pointSizeBytes = BinaryUtils.convert(pointSize * 20, 2);
  228. data[4] = pointSizeBytes[0]; //vfs
  229. data[5] = pointSizeBytes[1];
  230. // data[6] = pointSizeBytes[0]; //hsf
  231. // data[7] = pointSizeBytes[1];
  232. //charrot
  233. data[11] = 0x03; //encenv
  234. data[13] = 0x01; //encid
  235. os.write(data);
  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. *
  242. * @return the most recent Map Coded Font.
  243. */
  244. private MapCodedFont getCurrentMapCodedFont() {
  245. int size = mapCodedFonts.size();
  246. if (size > 0) {
  247. return (MapCodedFont)mapCodedFonts.get(size - 1);
  248. } else {
  249. return null;
  250. }
  251. }
  252. /**
  253. * Add map page segment.
  254. * @param name of segment to add
  255. */
  256. public void addMapPageSegment(String name) {
  257. try {
  258. needMapPageSegment().addPageSegment(name);
  259. } catch (MaximumSizeExceededException e) {
  260. //Should not happen, handled internally
  261. throw new IllegalStateException("Internal error: " + e.getMessage());
  262. }
  263. }
  264. private MapPageSegment getCurrentMapPageSegment() {
  265. return (MapPageSegment)getLastElement(this.mapPageSegments);
  266. }
  267. private MapPageSegment needMapPageSegment() {
  268. if (this.mapPageSegments == null) {
  269. this.mapPageSegments = new java.util.ArrayList();
  270. }
  271. MapPageSegment seg = getCurrentMapPageSegment();
  272. if (seg == null || seg.isFull()) {
  273. seg = new MapPageSegment();
  274. this.mapPageSegments.add(seg);
  275. }
  276. return seg;
  277. }
  278. }