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

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