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.

PDFImageHandlerSVG.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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.pdf;
  19. import java.awt.Color;
  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.commons.logging.Log;
  25. import org.apache.commons.logging.LogFactory;
  26. import org.apache.batik.anim.dom.SVGDOMImplementation;
  27. import org.apache.batik.bridge.BridgeContext;
  28. import org.apache.batik.bridge.GVTBuilder;
  29. import org.apache.batik.gvt.GraphicsNode;
  30. import org.apache.batik.util.SVGConstants;
  31. import org.apache.xmlgraphics.image.loader.Image;
  32. import org.apache.xmlgraphics.image.loader.ImageFlavor;
  33. import org.apache.xmlgraphics.image.loader.impl.ImageXMLDOM;
  34. import org.apache.xmlgraphics.util.UnitConv;
  35. import org.apache.fop.apps.FOUserAgent;
  36. import org.apache.fop.events.EventBroadcaster;
  37. import org.apache.fop.image.loader.batik.BatikImageFlavors;
  38. import org.apache.fop.image.loader.batik.BatikUtil;
  39. import org.apache.fop.pdf.TransparencyDisallowedException;
  40. import org.apache.fop.render.ImageHandler;
  41. import org.apache.fop.render.ImageHandlerUtil;
  42. import org.apache.fop.render.RenderingContext;
  43. import org.apache.fop.render.pdf.PDFLogicalStructureHandler.MarkedContentInfo;
  44. import org.apache.fop.render.ps.PSImageHandlerSVG;
  45. import org.apache.fop.svg.PDFAElementBridge;
  46. import org.apache.fop.svg.PDFBridgeContext;
  47. import org.apache.fop.svg.PDFGraphics2D;
  48. import org.apache.fop.svg.SVGEventProducer;
  49. import org.apache.fop.svg.SVGUserAgent;
  50. import org.apache.fop.svg.font.FOPFontFamilyResolverImpl;
  51. /**
  52. * Image Handler implementation which handles SVG images.
  53. */
  54. public class PDFImageHandlerSVG implements ImageHandler {
  55. /** logging instance */
  56. private static Log log = LogFactory.getLog(PDFImageHandlerSVG.class);
  57. /** {@inheritDoc} */
  58. public void handleImage(RenderingContext context,
  59. Image image, Rectangle pos)
  60. throws IOException {
  61. PDFRenderingContext pdfContext = (PDFRenderingContext)context;
  62. PDFContentGenerator generator = pdfContext.getGenerator();
  63. ImageXMLDOM imageSVG = (ImageXMLDOM)image;
  64. FOUserAgent userAgent = context.getUserAgent();
  65. final float deviceResolution = userAgent.getTargetResolution();
  66. if (log.isDebugEnabled()) {
  67. log.debug("Generating SVG at " + deviceResolution + "dpi.");
  68. }
  69. final float uaResolution = userAgent.getSourceResolution();
  70. SVGUserAgent ua = new SVGUserAgent(userAgent, new FOPFontFamilyResolverImpl(pdfContext.getFontInfo()),
  71. new AffineTransform());
  72. GVTBuilder builder = new GVTBuilder();
  73. //Controls whether text painted by Batik is generated using text or path operations
  74. boolean strokeText = PSImageHandlerSVG.shouldStrokeText(imageSVG.getDocument().getChildNodes());
  75. //TODO connect with configuration elsewhere.
  76. BridgeContext ctx = new PDFBridgeContext(ua,
  77. (strokeText ? null : pdfContext.getFontInfo()),
  78. userAgent.getImageManager(),
  79. userAgent.getImageSessionContext(),
  80. new AffineTransform());
  81. //Cloning SVG DOM as Batik attaches non-thread-safe facilities (like the CSS engine)
  82. //to it.
  83. Document clonedDoc = BatikUtil.cloneSVGDocument(imageSVG.getDocument());
  84. GraphicsNode root;
  85. try {
  86. root = builder.build(ctx, clonedDoc);
  87. } catch (Exception e) {
  88. SVGEventProducer eventProducer = SVGEventProducer.Provider.get(
  89. context.getUserAgent().getEventBroadcaster());
  90. eventProducer.svgNotBuilt(this, e, image.getInfo().getOriginalURI());
  91. return;
  92. }
  93. // get the 'width' and 'height' attributes of the SVG document
  94. float w = image.getSize().getWidthMpt();
  95. float h = image.getSize().getHeightMpt();
  96. float sx = pos.width / w;
  97. float sy = pos.height / h;
  98. //Scaling and translation for the bounding box of the image
  99. AffineTransform scaling = new AffineTransform(
  100. sx, 0, 0, sy, pos.x / 1000f, pos.y / 1000f);
  101. double sourceScale = UnitConv.IN2PT / uaResolution;
  102. scaling.scale(sourceScale, sourceScale);
  103. //Scale for higher resolution on-the-fly images from Batik
  104. AffineTransform resolutionScaling = new AffineTransform();
  105. double targetScale = uaResolution / deviceResolution;
  106. resolutionScaling.scale(targetScale, targetScale);
  107. resolutionScaling.scale(1.0 / sx, 1.0 / sy);
  108. //Transformation matrix that establishes the local coordinate system for the SVG graphic
  109. //in relation to the current coordinate system
  110. AffineTransform imageTransform = new AffineTransform();
  111. imageTransform.concatenate(scaling);
  112. imageTransform.concatenate(resolutionScaling);
  113. if (log.isTraceEnabled()) {
  114. log.trace("nat size: " + w + "/" + h);
  115. log.trace("req size: " + pos.width + "/" + pos.height);
  116. log.trace("source res: " + uaResolution + ", targetRes: " + deviceResolution
  117. + " --> target scaling: " + targetScale);
  118. log.trace(image.getSize());
  119. log.trace("sx: " + sx + ", sy: " + sy);
  120. log.trace("scaling: " + scaling);
  121. log.trace("resolution scaling: " + resolutionScaling);
  122. log.trace("image transform: " + resolutionScaling);
  123. }
  124. /*
  125. * Clip to the svg area.
  126. * Note: To have the svg overlay (under) a text area then use
  127. * an fo:block-container
  128. */
  129. if (log.isTraceEnabled()) {
  130. generator.comment("SVG setup");
  131. }
  132. generator.saveGraphicsState();
  133. if (context.getUserAgent().isAccessibilityEnabled()) {
  134. MarkedContentInfo mci = pdfContext.getMarkedContentInfo();
  135. generator.beginMarkedContentSequence(mci.tag, mci.mcid);
  136. }
  137. generator.updateColor(Color.black, false, null);
  138. generator.updateColor(Color.black, true, null);
  139. if (!scaling.isIdentity()) {
  140. if (log.isTraceEnabled()) {
  141. generator.comment("viewbox");
  142. }
  143. generator.add(CTMHelper.toPDFString(scaling, false) + " cm\n");
  144. }
  145. //SVGSVGElement svg = ((SVGDocument)doc).getRootElement();
  146. PDFGraphics2D graphics = new PDFGraphics2D(true, pdfContext.getFontInfo(),
  147. generator.getDocument(),
  148. generator.getResourceContext(), pdfContext.getPage().makeReference(),
  149. "", 0, new TransparencyIgnoredEventListener(pdfContext, imageSVG));
  150. graphics.setGraphicContext(new org.apache.xmlgraphics.java2d.GraphicContext());
  151. if (!resolutionScaling.isIdentity()) {
  152. if (log.isTraceEnabled()) {
  153. generator.comment("resolution scaling for " + uaResolution
  154. + " -> " + deviceResolution);
  155. }
  156. generator.add(
  157. CTMHelper.toPDFString(resolutionScaling, false) + " cm\n");
  158. graphics.scale(
  159. 1.0 / resolutionScaling.getScaleX(),
  160. 1.0 / resolutionScaling.getScaleY());
  161. }
  162. if (log.isTraceEnabled()) {
  163. generator.comment("SVG start");
  164. }
  165. //Save state and update coordinate system for the SVG image
  166. generator.getState().save();
  167. generator.getState().concatenate(imageTransform);
  168. //Now that we have the complete transformation matrix for the image, we can update the
  169. //transformation matrix for the AElementBridge.
  170. PDFAElementBridge aBridge = (PDFAElementBridge)ctx.getBridge(
  171. SVGDOMImplementation.SVG_NAMESPACE_URI, SVGConstants.SVG_A_TAG);
  172. aBridge.getCurrentTransform().setTransform(generator.getState().getTransform());
  173. graphics.setPaintingState(generator.getState());
  174. graphics.setOutputStream(generator.getOutputStream());
  175. try {
  176. root.paint(graphics);
  177. ctx.dispose();
  178. generator.add(graphics.getString());
  179. } catch (TransparencyDisallowedException e) {
  180. SVGEventProducer eventProducer = SVGEventProducer.Provider.get(
  181. context.getUserAgent().getEventBroadcaster());
  182. eventProducer.bitmapWithTransparency(this, e.getProfile(), image.getInfo().getOriginalURI());
  183. } catch (Exception e) {
  184. SVGEventProducer eventProducer = SVGEventProducer.Provider.get(
  185. context.getUserAgent().getEventBroadcaster());
  186. eventProducer.svgRenderingError(this, e, image.getInfo().getOriginalURI());
  187. }
  188. generator.getState().restore();
  189. if (context.getUserAgent().isAccessibilityEnabled()) {
  190. generator.restoreGraphicsStateAccess();
  191. } else {
  192. generator.restoreGraphicsState();
  193. }
  194. if (log.isTraceEnabled()) {
  195. generator.comment("SVG end");
  196. }
  197. }
  198. private static class TransparencyIgnoredEventListener
  199. implements PDFGraphics2D.TransparencyIgnoredEventListener {
  200. private final RenderingContext context;
  201. private final Image image;
  202. public TransparencyIgnoredEventListener(RenderingContext context, Image image) {
  203. this.context = context;
  204. this.image = image;
  205. }
  206. private boolean warningIssued;
  207. public void transparencyIgnored(Object pdfProfile) {
  208. if (!warningIssued) {
  209. EventBroadcaster broadcaster = context.getUserAgent().getEventBroadcaster();
  210. SVGEventProducer producer = SVGEventProducer.Provider.get(broadcaster);
  211. producer.transparencyIgnored(this, pdfProfile, image.getInfo().getOriginalURI());
  212. warningIssued = true;
  213. }
  214. }
  215. }
  216. /** {@inheritDoc} */
  217. public int getPriority() {
  218. return 400;
  219. }
  220. /** {@inheritDoc} */
  221. public Class getSupportedImageClass() {
  222. return ImageXMLDOM.class;
  223. }
  224. /** {@inheritDoc} */
  225. public ImageFlavor[] getSupportedImageFlavors() {
  226. return new ImageFlavor[] {
  227. BatikImageFlavors.SVG_DOM
  228. };
  229. }
  230. /** {@inheritDoc} */
  231. public boolean isCompatible(RenderingContext targetContext, Image image) {
  232. boolean supported = (image == null
  233. || (image instanceof ImageXMLDOM
  234. && image.getFlavor().isCompatible(BatikImageFlavors.SVG_DOM)))
  235. && targetContext instanceof PDFRenderingContext;
  236. if (supported) {
  237. String mode = (String)targetContext.getHint(ImageHandlerUtil.CONVERSION_MODE);
  238. if (ImageHandlerUtil.isConversionModeBitmap(mode)) {
  239. //Disabling this image handler automatically causes a bitmap to be generated
  240. return false;
  241. }
  242. }
  243. return supported;
  244. }
  245. }