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

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