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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. AFPDataObjectInfo info = createDataObjectInfo();
  70. assert (info instanceof AFPGraphicsObjectInfo);
  71. AFPGraphicsObjectInfo graphicsObjectInfo = (AFPGraphicsObjectInfo) info;
  72. AFPResourceInfo resourceInfo = graphicsObjectInfo.getResourceInfo();
  73. setDefaultToInlineResourceLevel(graphicsObjectInfo);
  74. // Create a new AFPGraphics2D
  75. AFPPaintingState paintingState = afpContext.getPaintingState();
  76. final boolean textAsShapes = paintingState.isStrokeGOCAText();
  77. AFPGraphics2D g2d = new AFPGraphics2D(
  78. textAsShapes,
  79. afpContext.getPaintingState(),
  80. afpContext.getResourceManager(),
  81. resourceInfo,
  82. (textAsShapes ? null : afpContext.getFontInfo()));
  83. g2d.setGraphicContext(new org.apache.xmlgraphics.java2d.GraphicContext());
  84. paintingState.setImageUri(image.getInfo().getOriginalURI());
  85. // Create an AFPBridgeContext
  86. BridgeContext bridgeContext = AFPSVGHandler.createBridgeContext(userAgent, g2d);
  87. // Cloning SVG DOM as Batik attaches non-thread-safe facilities (like the CSS engine)
  88. // to it.
  89. Document clonedDoc = BatikUtil.cloneSVGDocument(imageSVG.getDocument());
  90. // Build the SVG DOM and provide the painter with it
  91. GraphicsNode root;
  92. try {
  93. GVTBuilder builder = new GVTBuilder();
  94. root = builder.build(bridgeContext, clonedDoc);
  95. } catch (Exception e) {
  96. SVGEventProducer eventProducer = SVGEventProducer.Provider.get(
  97. context.getUserAgent().getEventBroadcaster());
  98. eventProducer.svgNotBuilt(this, e, image.getInfo().getOriginalURI());
  99. return;
  100. }
  101. // Image positioning
  102. AFPObjectAreaInfo objectAreaInfo = AFPImageHandler.createObjectAreaInfo(paintingState, pos);
  103. graphicsObjectInfo.setObjectAreaInfo(objectAreaInfo);
  104. paintingState.save(); // save
  105. AffineTransform placement = new AffineTransform();
  106. placement.translate(pos.x, pos.y);
  107. paintingState.concatenate(placement);
  108. //Set up painter and target
  109. graphicsObjectInfo.setGraphics2D(g2d);
  110. // Create Graphics2DImagePainter
  111. Dimension imageSize = image.getSize().getDimensionMpt();
  112. Graphics2DImagePainter painter = new Graphics2DImagePainterImpl(
  113. root, bridgeContext, imageSize);
  114. graphicsObjectInfo.setPainter(painter);
  115. // Create the GOCA GraphicsObject in the DataStream
  116. AFPResourceManager resourceManager = afpContext.getResourceManager();
  117. resourceManager.createObject(graphicsObjectInfo);
  118. paintingState.restore(); // resume
  119. }
  120. private void setDefaultToInlineResourceLevel(AFPGraphicsObjectInfo graphicsObjectInfo) {
  121. AFPResourceInfo resourceInfo = graphicsObjectInfo.getResourceInfo();
  122. //level not explicitly set/changed so default to inline for GOCA graphic objects
  123. // (due to a bug in the IBM AFP Workbench Viewer (2.04.01.07), hard copy works just fine)
  124. if (!resourceInfo.levelChanged()) {
  125. resourceInfo.setLevel(new AFPResourceLevel(ResourceType.INLINE));
  126. }
  127. }
  128. /** {@inheritDoc} */
  129. public int getPriority() {
  130. return 400;
  131. }
  132. /** {@inheritDoc} */
  133. public Class getSupportedImageClass() {
  134. return ImageXMLDOM.class;
  135. }
  136. /** {@inheritDoc} */
  137. public ImageFlavor[] getSupportedImageFlavors() {
  138. return FLAVORS;
  139. }
  140. /** {@inheritDoc} */
  141. public boolean isCompatible(RenderingContext targetContext, Image image) {
  142. boolean supported = (image == null || (image instanceof ImageXMLDOM
  143. && image.getFlavor().isCompatible(BatikImageFlavors.SVG_DOM)))
  144. && targetContext instanceof AFPRenderingContext;
  145. if (supported) {
  146. AFPRenderingContext afpContext = (AFPRenderingContext)targetContext;
  147. if (!afpContext.getPaintingState().isGOCAEnabled()) {
  148. return false;
  149. }
  150. String mode = (String)targetContext.getHint(ImageHandlerUtil.CONVERSION_MODE);
  151. if (ImageHandlerUtil.isConversionModeBitmap(mode)) {
  152. //Disabling this image handler automatically causes a bitmap to be generated
  153. return false;
  154. }
  155. }
  156. return supported;
  157. }
  158. }