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.

PDFSVGHandler.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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.geom.AffineTransform;
  21. import java.io.IOException;
  22. import java.io.OutputStream;
  23. import java.util.Map;
  24. import org.apache.avalon.framework.configuration.Configuration;
  25. import org.apache.batik.bridge.BridgeContext;
  26. import org.apache.batik.bridge.GVTBuilder;
  27. import org.apache.batik.dom.svg.SVGDOMImplementation;
  28. import org.apache.batik.gvt.GraphicsNode;
  29. import org.apache.batik.util.SVGConstants;
  30. import org.apache.commons.logging.Log;
  31. import org.apache.commons.logging.LogFactory;
  32. import org.apache.fop.apps.FOUserAgent;
  33. import org.apache.fop.fonts.FontInfo;
  34. import org.apache.fop.pdf.PDFDocument;
  35. import org.apache.fop.pdf.PDFPage;
  36. import org.apache.fop.pdf.PDFPaintingState;
  37. import org.apache.fop.pdf.PDFResourceContext;
  38. import org.apache.fop.pdf.PDFStream;
  39. import org.apache.fop.render.AbstractGenericSVGHandler;
  40. import org.apache.fop.render.Renderer;
  41. import org.apache.fop.render.RendererContext;
  42. import org.apache.fop.render.RendererContextConstants;
  43. import org.apache.fop.svg.PDFAElementBridge;
  44. import org.apache.fop.svg.PDFBridgeContext;
  45. import org.apache.fop.svg.PDFGraphics2D;
  46. import org.apache.fop.svg.SVGEventProducer;
  47. import org.apache.fop.svg.SVGUserAgent;
  48. import org.w3c.dom.Document;
  49. /**
  50. * PDF XML handler for SVG (uses Apache Batik).
  51. * This handler handles XML for foreign objects when rendering to PDF.
  52. * It renders SVG to the PDF document using the PDFGraphics2D.
  53. * The properties from the PDF renderer are subject to change.
  54. */
  55. public class PDFSVGHandler extends AbstractGenericSVGHandler
  56. implements PDFRendererContextConstants {
  57. /** logging instance */
  58. private static Log log = LogFactory.getLog(PDFSVGHandler.class);
  59. /**
  60. * Get the pdf information from the render context.
  61. *
  62. * @param context the renderer context
  63. * @return the pdf information retrieved from the context
  64. */
  65. public static PDFInfo getPDFInfo(RendererContext context) {
  66. PDFInfo pdfi = new PDFInfo();
  67. pdfi.pdfDoc = (PDFDocument)context.getProperty(PDF_DOCUMENT);
  68. pdfi.outputStream = (OutputStream)context.getProperty(OUTPUT_STREAM);
  69. pdfi.pdfPaintingState = (PDFPaintingState)context.getProperty(PDF_PAINTING_STATE);
  70. pdfi.pdfPage = (PDFPage)context.getProperty(PDF_PAGE);
  71. pdfi.pdfContext = (PDFResourceContext)context.getProperty(PDF_CONTEXT);
  72. pdfi.currentStream = (PDFStream)context.getProperty(PDF_STREAM);
  73. pdfi.width = ((Integer)context.getProperty(WIDTH)).intValue();
  74. pdfi.height = ((Integer)context.getProperty(HEIGHT)).intValue();
  75. pdfi.fi = (FontInfo)context.getProperty(PDF_FONT_INFO);
  76. pdfi.currentFontName = (String)context.getProperty(PDF_FONT_NAME);
  77. pdfi.currentFontSize = ((Integer)context.getProperty(PDF_FONT_SIZE)).intValue();
  78. pdfi.currentXPosition = ((Integer)context.getProperty(XPOS)).intValue();
  79. pdfi.currentYPosition = ((Integer)context.getProperty(YPOS)).intValue();
  80. pdfi.cfg = (Configuration)context.getProperty(HANDLER_CONFIGURATION);
  81. Map foreign = (Map)context.getProperty(RendererContextConstants.FOREIGN_ATTRIBUTES);
  82. if (foreign != null
  83. && BITMAP.equalsIgnoreCase((String)foreign.get(CONVERSION_MODE))) {
  84. pdfi.paintAsBitmap = true;
  85. }
  86. return pdfi;
  87. }
  88. /**
  89. * PDF information structure for drawing the XML document.
  90. */
  91. public static class PDFInfo {
  92. /** see PDF_DOCUMENT */
  93. public PDFDocument pdfDoc;
  94. /** see OUTPUT_STREAM */
  95. public OutputStream outputStream;
  96. /** see PDF_STATE */
  97. public PDFPaintingState pdfPaintingState;
  98. /** see PDF_PAGE */
  99. public PDFPage pdfPage;
  100. /** see PDF_CONTEXT */
  101. public PDFResourceContext pdfContext;
  102. /** see PDF_STREAM */
  103. public PDFStream currentStream;
  104. /** see PDF_WIDTH */
  105. public int width;
  106. /** see PDF_HEIGHT */
  107. public int height;
  108. /** see PDF_FONT_INFO */
  109. public FontInfo fi;
  110. /** see PDF_FONT_NAME */
  111. public String currentFontName;
  112. /** see PDF_FONT_SIZE */
  113. public int currentFontSize;
  114. /** see PDF_XPOS */
  115. public int currentXPosition;
  116. /** see PDF_YPOS */
  117. public int currentYPosition;
  118. /** see PDF_HANDLER_CONFIGURATION */
  119. public Configuration cfg;
  120. /** true if SVG should be rendered as a bitmap instead of natively */
  121. public boolean paintAsBitmap;
  122. }
  123. /**
  124. * {@inheritDoc}
  125. */
  126. protected void renderSVGDocument(RendererContext context,
  127. Document doc) {
  128. PDFRenderer renderer = (PDFRenderer)context.getRenderer();
  129. PDFInfo pdfInfo = getPDFInfo(context);
  130. if (pdfInfo.paintAsBitmap) {
  131. try {
  132. super.renderSVGDocument(context, doc);
  133. } catch (IOException ioe) {
  134. SVGEventProducer eventProducer = SVGEventProducer.Provider.get(
  135. context.getUserAgent().getEventBroadcaster());
  136. eventProducer.svgRenderingError(this, ioe, getDocumentURI(doc));
  137. }
  138. return;
  139. }
  140. int xOffset = pdfInfo.currentXPosition;
  141. int yOffset = pdfInfo.currentYPosition;
  142. FOUserAgent userAgent = context.getUserAgent();
  143. final float deviceResolution = userAgent.getTargetResolution();
  144. if (log.isDebugEnabled()) {
  145. log.debug("Generating SVG at " + deviceResolution + "dpi.");
  146. }
  147. final float uaResolution = userAgent.getSourceResolution();
  148. SVGUserAgent ua = new SVGUserAgent(userAgent, new AffineTransform());
  149. //Scale for higher resolution on-the-fly images from Batik
  150. double s = uaResolution / deviceResolution;
  151. AffineTransform resolutionScaling = new AffineTransform();
  152. resolutionScaling.scale(s, s);
  153. GVTBuilder builder = new GVTBuilder();
  154. //Controls whether text painted by Batik is generated using text or path operations
  155. boolean strokeText = false;
  156. Configuration cfg = pdfInfo.cfg;
  157. if (cfg != null) {
  158. strokeText = cfg.getChild("stroke-text", true).getValueAsBoolean(strokeText);
  159. }
  160. BridgeContext ctx = new PDFBridgeContext(ua,
  161. (strokeText ? null : pdfInfo.fi),
  162. userAgent.getFactory().getImageManager(),
  163. userAgent.getImageSessionContext(),
  164. new AffineTransform());
  165. GraphicsNode root;
  166. try {
  167. root = builder.build(ctx, doc);
  168. builder = null;
  169. } catch (Exception e) {
  170. SVGEventProducer eventProducer = SVGEventProducer.Provider.get(
  171. context.getUserAgent().getEventBroadcaster());
  172. eventProducer.svgNotBuilt(this, e, getDocumentURI(doc));
  173. return;
  174. }
  175. // get the 'width' and 'height' attributes of the SVG document
  176. float w = (float)ctx.getDocumentSize().getWidth() * 1000f;
  177. float h = (float)ctx.getDocumentSize().getHeight() * 1000f;
  178. float sx = pdfInfo.width / w;
  179. float sy = pdfInfo.height / h;
  180. //Scaling and translation for the bounding box of the image
  181. AffineTransform scaling = new AffineTransform(
  182. sx, 0, 0, sy, xOffset / 1000f, yOffset / 1000f);
  183. //Transformation matrix that establishes the local coordinate system for the SVG graphic
  184. //in relation to the current coordinate system
  185. AffineTransform imageTransform = new AffineTransform();
  186. imageTransform.concatenate(scaling);
  187. imageTransform.concatenate(resolutionScaling);
  188. /*
  189. * Clip to the svg area.
  190. * Note: To have the svg overlay (under) a text area then use
  191. * an fo:block-container
  192. */
  193. pdfInfo.currentStream.add("%SVG setup\n");
  194. renderer.saveGraphicsState();
  195. renderer.setColor(Color.black, false, null);
  196. renderer.setColor(Color.black, true, null);
  197. if (!scaling.isIdentity()) {
  198. pdfInfo.currentStream.add("%viewbox\n");
  199. pdfInfo.currentStream.add(CTMHelper.toPDFString(scaling, false) + " cm\n");
  200. }
  201. //SVGSVGElement svg = ((SVGDocument)doc).getRootElement();
  202. if (pdfInfo.pdfContext == null) {
  203. pdfInfo.pdfContext = pdfInfo.pdfPage;
  204. }
  205. PDFGraphics2D graphics = new PDFGraphics2D(true, pdfInfo.fi,
  206. pdfInfo.pdfDoc,
  207. pdfInfo.pdfContext, pdfInfo.pdfPage.referencePDF(),
  208. pdfInfo.currentFontName, pdfInfo.currentFontSize);
  209. graphics.setGraphicContext(new org.apache.xmlgraphics.java2d.GraphicContext());
  210. if (!resolutionScaling.isIdentity()) {
  211. pdfInfo.currentStream.add("%resolution scaling for " + uaResolution
  212. + " -> " + deviceResolution + "\n");
  213. pdfInfo.currentStream.add(
  214. CTMHelper.toPDFString(resolutionScaling, false) + " cm\n");
  215. graphics.scale(1 / s, 1 / s);
  216. }
  217. pdfInfo.currentStream.add("%SVG start\n");
  218. //Save state and update coordinate system for the SVG image
  219. pdfInfo.pdfPaintingState.push();
  220. pdfInfo.pdfPaintingState.concatenate(imageTransform);
  221. //Now that we have the complete transformation matrix for the image, we can update the
  222. //transformation matrix for the AElementBridge.
  223. PDFAElementBridge aBridge = (PDFAElementBridge)ctx.getBridge(
  224. SVGDOMImplementation.SVG_NAMESPACE_URI, SVGConstants.SVG_A_TAG);
  225. aBridge.getCurrentTransform().setTransform(pdfInfo.pdfPaintingState.getTransform());
  226. graphics.setPaintingState(pdfInfo.pdfPaintingState);
  227. graphics.setOutputStream(pdfInfo.outputStream);
  228. try {
  229. root.paint(graphics);
  230. pdfInfo.currentStream.add(graphics.getString());
  231. } catch (Exception e) {
  232. SVGEventProducer eventProducer = SVGEventProducer.Provider.get(
  233. context.getUserAgent().getEventBroadcaster());
  234. eventProducer.svgRenderingError(this, e, getDocumentURI(doc));
  235. }
  236. pdfInfo.pdfPaintingState.pop();
  237. renderer.restoreGraphicsState();
  238. pdfInfo.currentStream.add("%SVG end\n");
  239. }
  240. /** {@inheritDoc} */
  241. public boolean supportsRenderer(Renderer renderer) {
  242. return (renderer instanceof PDFRenderer);
  243. }
  244. }