Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

PDFSVGHandler.java 11KB

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