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.

AFPImageHandlerSVG.java 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 java.awt.Dimension;
  20. import java.awt.Rectangle;
  21. import java.awt.geom.AffineTransform;
  22. import java.io.IOException;
  23. import org.w3c.dom.Document;
  24. import org.apache.batik.bridge.BridgeContext;
  25. import org.apache.batik.bridge.GVTBuilder;
  26. import org.apache.batik.gvt.GraphicsNode;
  27. import org.apache.xmlgraphics.image.loader.Image;
  28. import org.apache.xmlgraphics.image.loader.ImageFlavor;
  29. import org.apache.xmlgraphics.image.loader.impl.ImageXMLDOM;
  30. import org.apache.xmlgraphics.java2d.Graphics2DImagePainter;
  31. import org.apache.fop.afp.AFPDataObjectInfo;
  32. import org.apache.fop.afp.AFPGraphics2D;
  33. import org.apache.fop.afp.AFPGraphicsObjectInfo;
  34. import org.apache.fop.afp.AFPObjectAreaInfo;
  35. import org.apache.fop.afp.AFPPaintingState;
  36. import org.apache.fop.afp.AFPResourceInfo;
  37. import org.apache.fop.afp.AFPResourceLevel;
  38. import org.apache.fop.afp.AFPResourceManager;
  39. import org.apache.fop.apps.FOUserAgent;
  40. import org.apache.fop.image.loader.batik.BatikImageFlavors;
  41. import org.apache.fop.image.loader.batik.BatikUtil;
  42. import org.apache.fop.image.loader.batik.Graphics2DImagePainterImpl;
  43. import org.apache.fop.render.ImageHandler;
  44. import org.apache.fop.render.ImageHandlerUtil;
  45. import org.apache.fop.render.RenderingContext;
  46. import org.apache.fop.svg.SVGEventProducer;
  47. /**
  48. * Image handler implementation which handles SVG images for AFP output.
  49. * <p>
  50. * Note: This class is not intended to be used as an {@link AFPImageHandler} but only as an
  51. * {@link ImageHandler}. It subclasses {@link AFPImageHandler} only to get access to common
  52. * methods.
  53. */
  54. public class AFPImageHandlerSVG implements ImageHandler {
  55. private static final ImageFlavor[] FLAVORS = new ImageFlavor[] {
  56. BatikImageFlavors.SVG_DOM
  57. };
  58. /** {@inheritDoc} */
  59. protected AFPDataObjectInfo createDataObjectInfo() {
  60. return new AFPGraphicsObjectInfo();
  61. }
  62. /** {@inheritDoc} */
  63. public void handleImage(RenderingContext context, Image image, Rectangle pos)
  64. throws IOException {
  65. AFPRenderingContext afpContext = (AFPRenderingContext)context;
  66. ImageXMLDOM imageSVG = (ImageXMLDOM)image;
  67. FOUserAgent userAgent = afpContext.getUserAgent();
  68. AFPGraphicsObjectInfo graphicsObjectInfo = (AFPGraphicsObjectInfo)createDataObjectInfo();
  69. AFPResourceInfo resourceInfo = graphicsObjectInfo.getResourceInfo();
  70. setDefaultToInlineResourceLevel(graphicsObjectInfo);
  71. // Create a new AFPGraphics2D
  72. final boolean textAsShapes = false; //afpInfo.strokeText(); //TODO make configurable
  73. AFPGraphics2D g2d = new AFPGraphics2D(
  74. textAsShapes,
  75. afpContext.getPaintingState(),
  76. afpContext.getResourceManager(),
  77. resourceInfo,
  78. afpContext.getFontInfo());
  79. g2d.setGraphicContext(new org.apache.xmlgraphics.java2d.GraphicContext());
  80. AFPPaintingState paintingState = g2d.getPaintingState();
  81. paintingState.setImageUri(image.getInfo().getOriginalURI());
  82. // Create an AFPBridgeContext
  83. BridgeContext bridgeContext = AFPSVGHandler.createBridgeContext(userAgent, g2d);
  84. // Cloning SVG DOM as Batik attaches non-thread-safe facilities (like the CSS engine)
  85. // to it.
  86. Document clonedDoc = BatikUtil.cloneSVGDocument(imageSVG.getDocument());
  87. // Build the SVG DOM and provide the painter with it
  88. GraphicsNode root;
  89. try {
  90. GVTBuilder builder = new GVTBuilder();
  91. root = builder.build(bridgeContext, clonedDoc);
  92. } catch (Exception e) {
  93. SVGEventProducer eventProducer = SVGEventProducer.Provider.get(
  94. context.getUserAgent().getEventBroadcaster());
  95. eventProducer.svgNotBuilt(this, e, image.getInfo().getOriginalURI());
  96. return;
  97. }
  98. // Image positioning
  99. AFPObjectAreaInfo objectAreaInfo = AFPImageHandler.createObjectAreaInfo(paintingState, pos);
  100. graphicsObjectInfo.setObjectAreaInfo(objectAreaInfo);
  101. paintingState.save(); // save
  102. AffineTransform placement = new AffineTransform();
  103. placement.translate(pos.x, pos.y);
  104. paintingState.concatenate(placement);
  105. //Set up painter and target
  106. graphicsObjectInfo.setGraphics2D(g2d);
  107. // Create Graphics2DImagePainter
  108. Dimension imageSize = image.getSize().getDimensionMpt();
  109. Graphics2DImagePainter painter = new Graphics2DImagePainterImpl(
  110. root, bridgeContext, imageSize);
  111. graphicsObjectInfo.setPainter(painter);
  112. // Create the GOCA GraphicsObject in the DataStream
  113. AFPResourceManager resourceManager = afpContext.getResourceManager();
  114. resourceManager.createObject(graphicsObjectInfo);
  115. paintingState.restore(); // resume
  116. }
  117. private void setDefaultToInlineResourceLevel(AFPGraphicsObjectInfo graphicsObjectInfo) {
  118. AFPResourceInfo resourceInfo = graphicsObjectInfo.getResourceInfo();
  119. //level not explicitly set/changed so default to inline for GOCA graphic objects
  120. // (due to a bug in the IBM AFP Workbench Viewer (2.04.01.07), hard copy works just fine)
  121. if (!resourceInfo.levelChanged()) {
  122. resourceInfo.setLevel(new AFPResourceLevel(AFPResourceLevel.INLINE));
  123. }
  124. }
  125. /** {@inheritDoc} */
  126. public int getPriority() {
  127. return 400;
  128. }
  129. /** {@inheritDoc} */
  130. public Class getSupportedImageClass() {
  131. return ImageXMLDOM.class;
  132. }
  133. /** {@inheritDoc} */
  134. public ImageFlavor[] getSupportedImageFlavors() {
  135. return FLAVORS;
  136. }
  137. /** {@inheritDoc} */
  138. public boolean isCompatible(RenderingContext targetContext, Image image) {
  139. boolean supported = (image == null || (image instanceof ImageXMLDOM
  140. && image.getFlavor().isCompatible(BatikImageFlavors.SVG_DOM)))
  141. && targetContext instanceof AFPRenderingContext;
  142. if (supported) {
  143. String mode = (String)targetContext.getHint(ImageHandlerUtil.CONVERSION_MODE);
  144. if (ImageHandlerUtil.isConversionModeBitmap(mode)) {
  145. //Disabling this image handler automatically causes a bitmap to be generated
  146. return false;
  147. }
  148. }
  149. return supported;
  150. }
  151. }