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.3KB

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