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.

AFPDataObjectFactory.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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;
  19. import java.awt.geom.Rectangle2D;
  20. import org.apache.fop.afp.ioca.ImageContent;
  21. import org.apache.fop.afp.modca.AbstractDataObject;
  22. import org.apache.fop.afp.modca.AbstractNamedAFPObject;
  23. import org.apache.fop.afp.modca.Document;
  24. import org.apache.fop.afp.modca.GraphicsObject;
  25. import org.apache.fop.afp.modca.ImageObject;
  26. import org.apache.fop.afp.modca.IncludeObject;
  27. import org.apache.fop.afp.modca.ObjectContainer;
  28. import org.apache.fop.afp.modca.Overlay;
  29. import org.apache.fop.afp.modca.PageSegment;
  30. import org.apache.fop.afp.modca.Registry;
  31. import org.apache.fop.afp.modca.ResourceObject;
  32. import org.apache.fop.afp.modca.triplets.MappingOptionTriplet;
  33. import org.apache.fop.afp.modca.triplets.ObjectClassificationTriplet;
  34. import org.apache.xmlgraphics.image.codec.tiff.TIFFImage;
  35. import org.apache.xmlgraphics.java2d.Graphics2DImagePainter;
  36. /**
  37. * Factory for high level data objects (Image/Graphics etc)
  38. */
  39. public class AFPDataObjectFactory {
  40. private final Factory factory;
  41. /**
  42. * Main constructor
  43. *
  44. * @param factory an object factory
  45. */
  46. public AFPDataObjectFactory(Factory factory) {
  47. this.factory = factory;
  48. }
  49. /**
  50. * Creates and configures an ObjectContainer.
  51. *
  52. * @param dataObjectInfo the object container info
  53. * @return a newly created Object Container
  54. */
  55. public ObjectContainer createObjectContainer(AFPDataObjectInfo dataObjectInfo) {
  56. ObjectContainer objectContainer = factory.createObjectContainer();
  57. // set data object viewport (i.e. position, rotation, dimension, resolution)
  58. objectContainer.setViewport(dataObjectInfo);
  59. // set object classification
  60. Registry.ObjectType objectType = dataObjectInfo.getObjectType();
  61. AFPResourceInfo resourceInfo = dataObjectInfo.getResourceInfo();
  62. AFPResourceLevel resourceLevel = resourceInfo.getLevel();
  63. final boolean dataInContainer = true;
  64. final boolean containerHasOEG = resourceLevel.isInline();
  65. final boolean dataInOCD = true;
  66. objectContainer.setObjectClassification(
  67. ObjectClassificationTriplet.CLASS_TIME_INVARIANT_PAGINATED_PRESENTATION_OBJECT,
  68. objectType, dataInContainer, containerHasOEG, dataInOCD);
  69. objectContainer.setData(dataObjectInfo.getData());
  70. return objectContainer;
  71. }
  72. /**
  73. * Creates and configures an IOCA Image Object.
  74. *
  75. * @param imageObjectInfo the image object info
  76. * @return a newly created IOCA Image Object
  77. */
  78. public ImageObject createImage(AFPImageObjectInfo imageObjectInfo) {
  79. // IOCA bitmap image
  80. ImageObject imageObj = factory.createImageObject();
  81. // set data object viewport (i.e. position, rotation, dimension, resolution)
  82. imageObj.setViewport(imageObjectInfo);
  83. if (imageObjectInfo.hasCompression()) {
  84. int compression = imageObjectInfo.getCompression();
  85. switch (compression) {
  86. case TIFFImage.COMP_FAX_G3_1D:
  87. imageObj.setEncoding(ImageContent.COMPID_G3_MH);
  88. break;
  89. case TIFFImage.COMP_FAX_G3_2D:
  90. imageObj.setEncoding(ImageContent.COMPID_G3_MR);
  91. break;
  92. case TIFFImage.COMP_FAX_G4_2D:
  93. imageObj.setEncoding(ImageContent.COMPID_G3_MMR);
  94. break;
  95. default:
  96. throw new IllegalStateException(
  97. "Invalid compression scheme: " + compression);
  98. }
  99. }
  100. if (imageObjectInfo.isColor()) {
  101. imageObj.setIDESize((byte) 24);
  102. } else {
  103. imageObj.setIDESize((byte) imageObjectInfo.getBitsPerPixel());
  104. }
  105. imageObj.setData(imageObjectInfo.getData());
  106. return imageObj;
  107. }
  108. /**
  109. * Creates and returns a new graphics object.
  110. *
  111. * @param graphicsObjectInfo the graphics object info
  112. * @return a new graphics object
  113. */
  114. public GraphicsObject createGraphic(AFPGraphicsObjectInfo graphicsObjectInfo) {
  115. // set newly created graphics object in g2d
  116. GraphicsObject graphicsObj = factory.createGraphicsObject();
  117. // set data object viewport (i.e. position, rotation, dimension, resolution)
  118. graphicsObj.setViewport(graphicsObjectInfo);
  119. AFPGraphics2D g2d = graphicsObjectInfo.getGraphics2D();
  120. g2d.setGraphicsObject(graphicsObj);
  121. // paint to graphics object
  122. Graphics2DImagePainter painter = graphicsObjectInfo.getPainter();
  123. Rectangle2D area = graphicsObjectInfo.getArea();
  124. g2d.scale(1, -1);
  125. g2d.translate(0, -area.getHeight());
  126. painter.paint(g2d, area);
  127. graphicsObj.setComplete(true);
  128. // return painted graphics object
  129. return graphicsObj;
  130. }
  131. /**
  132. * Creates and returns a new include object.
  133. *
  134. * @param includeName the include name
  135. * @param dataObjectInfo a data object info
  136. *
  137. * @return a new include object
  138. */
  139. public IncludeObject createInclude(String includeName, AFPDataObjectInfo dataObjectInfo) {
  140. IncludeObject includeObj = factory.createInclude(includeName);
  141. if (dataObjectInfo instanceof AFPImageObjectInfo) {
  142. // IOCA image object
  143. includeObj.setObjectType(IncludeObject.TYPE_IMAGE);
  144. } else if (dataObjectInfo instanceof AFPGraphicsObjectInfo) {
  145. // graphics object
  146. includeObj.setObjectType(IncludeObject.TYPE_GRAPHIC);
  147. } else {
  148. // object container
  149. includeObj.setObjectType(IncludeObject.TYPE_OTHER);
  150. // set mandatory object classification (type other)
  151. Registry.ObjectType objectType = dataObjectInfo.getObjectType();
  152. if (objectType != null) {
  153. // set object classification
  154. final boolean dataInContainer = true;
  155. final boolean containerHasOEG = false; // environment parameters set in include
  156. final boolean dataInOCD = true;
  157. includeObj.setObjectClassification(
  158. // object scope not defined
  159. ObjectClassificationTriplet.CLASS_TIME_VARIANT_PRESENTATION_OBJECT,
  160. objectType, dataInContainer, containerHasOEG, dataInOCD);
  161. } else {
  162. throw new IllegalStateException(
  163. "Failed to set Object Classification Triplet on Object Container.");
  164. }
  165. }
  166. AFPObjectAreaInfo objectAreaInfo = dataObjectInfo.getObjectAreaInfo();
  167. int xOffset = objectAreaInfo.getX();
  168. int yOffset = objectAreaInfo.getY();
  169. includeObj.setObjectAreaOffset(xOffset, yOffset);
  170. int width = objectAreaInfo.getWidth();
  171. int height = objectAreaInfo.getHeight();
  172. includeObj.setObjectAreaSize(width, height);
  173. int rotation = objectAreaInfo.getRotation();
  174. includeObj.setObjectAreaOrientation(rotation);
  175. int widthRes = objectAreaInfo.getWidthRes();
  176. int heightRes = objectAreaInfo.getHeightRes();
  177. includeObj.setMeasurementUnits(widthRes, heightRes);
  178. includeObj.setMappingOption(MappingOptionTriplet.SCALE_TO_FIT);
  179. return includeObj;
  180. }
  181. /**
  182. * Creates a resource object wrapper for named includable data objects
  183. *
  184. * @param namedObj an named object
  185. * @param resourceInfo resource information
  186. * @param objectType the object type
  187. * @return a new resource object wrapper
  188. */
  189. public ResourceObject createResource(AbstractNamedAFPObject namedObj,
  190. AFPResourceInfo resourceInfo, Registry.ObjectType objectType) {
  191. ResourceObject resourceObj = null;
  192. String resourceName = resourceInfo.getName();
  193. if (resourceName != null) {
  194. resourceObj = factory.createResource(resourceName);
  195. } else {
  196. resourceObj = factory.createResource();
  197. }
  198. if (namedObj instanceof Document) {
  199. resourceObj.setType(ResourceObject.TYPE_DOCUMENT);
  200. } else if (namedObj instanceof PageSegment) {
  201. resourceObj.setType(ResourceObject.TYPE_PAGE_SEGMENT);
  202. } else if (namedObj instanceof Overlay) {
  203. resourceObj.setType(ResourceObject.TYPE_OVERLAY_OBJECT);
  204. } else if (namedObj instanceof AbstractDataObject) {
  205. AbstractDataObject dataObj = (AbstractDataObject)namedObj;
  206. if (namedObj instanceof ObjectContainer) {
  207. resourceObj.setType(ResourceObject.TYPE_OBJECT_CONTAINER);
  208. // set object classification
  209. final boolean dataInContainer = true;
  210. final boolean containerHasOEG = false; // must be included
  211. final boolean dataInOCD = true;
  212. // mandatory triplet for object container
  213. resourceObj.setObjectClassification(
  214. ObjectClassificationTriplet.CLASS_TIME_INVARIANT_PAGINATED_PRESENTATION_OBJECT,
  215. objectType, dataInContainer, containerHasOEG, dataInOCD);
  216. } else if (namedObj instanceof ImageObject) {
  217. // ioca image type
  218. resourceObj.setType(ResourceObject.TYPE_IMAGE);
  219. } else if (namedObj instanceof GraphicsObject) {
  220. resourceObj.setType(ResourceObject.TYPE_GRAPHIC);
  221. } else {
  222. throw new UnsupportedOperationException(
  223. "Unsupported resource object for data object type " + dataObj);
  224. }
  225. } else {
  226. throw new UnsupportedOperationException(
  227. "Unsupported resource object type " + namedObj);
  228. }
  229. // set the resource information/classification on the data object
  230. resourceObj.setDataObject(namedObj);
  231. return resourceObj;
  232. }
  233. }