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.

PDFTranscoder.java 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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.svg;
  19. import java.awt.Color;
  20. import java.io.BufferedOutputStream;
  21. import java.io.IOException;
  22. import java.io.OutputStream;
  23. import org.w3c.dom.Document;
  24. import org.w3c.dom.svg.SVGLength;
  25. import org.apache.batik.bridge.BridgeContext;
  26. import org.apache.batik.bridge.UnitProcessor;
  27. import org.apache.batik.ext.awt.RenderingHintsKeyExt;
  28. import org.apache.batik.transcoder.TranscoderException;
  29. import org.apache.batik.transcoder.TranscoderOutput;
  30. import org.apache.batik.transcoder.image.ImageTranscoder;
  31. import org.apache.fop.Version;
  32. import org.apache.fop.configuration.Configuration;
  33. import org.apache.fop.fonts.FontInfo;
  34. import org.apache.fop.svg.font.FOPFontFamilyResolverImpl;
  35. /**
  36. * <p>This class enables to transcode an input to a PDF document.</p>
  37. *
  38. * <p>Two transcoding hints (<code>KEY_WIDTH</code> and
  39. * <code>KEY_HEIGHT</code>) can be used to respectively specify the image
  40. * width and the image height. If only one of these keys is specified,
  41. * the transcoder preserves the aspect ratio of the original image.
  42. *
  43. * <p>The <code>KEY_BACKGROUND_COLOR</code> defines the background color
  44. * to use for opaque image formats, or the background color that may
  45. * be used for image formats that support alpha channel.
  46. *
  47. * <p>The <code>KEY_AOI</code> represents the area of interest to paint
  48. * in device space.
  49. *
  50. * <p>Three additional transcoding hints that act on the SVG
  51. * processor can be specified:
  52. *
  53. * <p><code>KEY_LANGUAGE</code> to set the default language to use (may be
  54. * used by a &lt;switch&gt; SVG element for example),
  55. * <code>KEY_USER_STYLESHEET_URI</code> to fix the URI of a user
  56. * stylesheet, and <code>KEY_PIXEL_TO_MM</code> to specify the pixel to
  57. * millimeter conversion factor.
  58. *
  59. * <p><code>KEY_AUTO_FONTS</code> to disable the auto-detection of fonts installed in the system.
  60. * The PDF Transcoder cannot use AWT's font subsystem and that's why the fonts have to be
  61. * configured differently. By default, font auto-detection is enabled to match the behaviour
  62. * of the other transcoders, but this may be associated with a price in the form of a small
  63. * performance penalty. If font auto-detection is not desired, it can be disable using this key.
  64. *
  65. * <p>This work was authored by Keiron Liddle (keiron@aftexsw.com).</p>
  66. */
  67. public class PDFTranscoder extends AbstractFOPTranscoder {
  68. /** Graphics2D instance that is used to paint to */
  69. protected PDFDocumentGraphics2D graphics;
  70. /**
  71. * Constructs a new {@link PDFTranscoder}.
  72. */
  73. public PDFTranscoder() {
  74. super();
  75. this.handler = new FOPErrorHandler();
  76. }
  77. /**
  78. * {@inheritDoc}
  79. */
  80. protected FOPTranscoderUserAgent createUserAgent() {
  81. return new AbstractFOPTranscoder.FOPTranscoderUserAgent() {
  82. // The PDF stuff wants everything at 72dpi
  83. public float getPixelUnitToMillimeter() {
  84. return super.getPixelUnitToMillimeter();
  85. //return 25.4f / 72; //72dpi = 0.352778f;
  86. }
  87. };
  88. }
  89. /**
  90. * Transcodes the specified Document as an image in the specified output.
  91. *
  92. * @param document the document to transcode
  93. * @param uri the uri of the document or null if any
  94. * @param output the ouput where to transcode
  95. * @exception TranscoderException if an error occured while transcoding
  96. */
  97. protected void transcode(Document document, String uri,
  98. TranscoderOutput output)
  99. throws TranscoderException {
  100. graphics = new PDFDocumentGraphics2D(isTextStroked());
  101. graphics.getPDFDocument().getInfo().setProducer("Apache FOP Version "
  102. + Version.getVersion()
  103. + ": PDF Transcoder for Batik");
  104. if (hints.containsKey(KEY_DEVICE_RESOLUTION)) {
  105. graphics.setDeviceDPI(getDeviceResolution());
  106. }
  107. setupImageInfrastructure(uri);
  108. try {
  109. Configuration effCfg = getEffectiveConfiguration();
  110. if (effCfg != null) {
  111. PDFDocumentGraphics2DConfigurator configurator
  112. = new PDFDocumentGraphics2DConfigurator();
  113. boolean useComplexScriptFeatures = false; //TODO - FIX ME
  114. configurator.configure(graphics, effCfg, useComplexScriptFeatures);
  115. } else {
  116. graphics.setupDefaultFontInfo();
  117. }
  118. ((FOPTranscoderUserAgent) userAgent).setFontFamilyResolver(
  119. new FOPFontFamilyResolverImpl(graphics.getFontInfo()));
  120. } catch (Exception e) {
  121. throw new TranscoderException(
  122. "Error while setting up PDFDocumentGraphics2D", e);
  123. }
  124. super.transcode(document, uri, output);
  125. if (getLogger().isTraceEnabled()) {
  126. getLogger().trace("document size: " + width + " x " + height);
  127. }
  128. // prepare the image to be painted
  129. UnitProcessor.Context uctx = UnitProcessor.createContext(ctx,
  130. document.getDocumentElement());
  131. float widthInPt = UnitProcessor.userSpaceToSVG(width, SVGLength.SVG_LENGTHTYPE_PT,
  132. UnitProcessor.HORIZONTAL_LENGTH, uctx);
  133. int w = (int)(widthInPt + 0.5);
  134. float heightInPt = UnitProcessor.userSpaceToSVG(height, SVGLength.SVG_LENGTHTYPE_PT,
  135. UnitProcessor.HORIZONTAL_LENGTH, uctx);
  136. int h = (int)(heightInPt + 0.5);
  137. if (getLogger().isTraceEnabled()) {
  138. getLogger().trace("document size: " + w + "pt x " + h + "pt");
  139. }
  140. // prepare the image to be painted
  141. //int w = (int)(width + 0.5);
  142. //int h = (int)(height + 0.5);
  143. try {
  144. OutputStream out = output.getOutputStream();
  145. if (!(out instanceof BufferedOutputStream)) {
  146. out = new BufferedOutputStream(out);
  147. }
  148. graphics.setupDocument(out, w, h);
  149. graphics.setSVGDimension(width, height);
  150. if (hints.containsKey(ImageTranscoder.KEY_BACKGROUND_COLOR)) {
  151. graphics.setBackgroundColor(
  152. (Color)hints.get(ImageTranscoder.KEY_BACKGROUND_COLOR));
  153. }
  154. graphics.setGraphicContext(
  155. new org.apache.xmlgraphics.java2d.GraphicContext());
  156. graphics.preparePainting();
  157. graphics.transform(curTxf);
  158. graphics.setRenderingHint(
  159. RenderingHintsKeyExt.KEY_TRANSCODING,
  160. RenderingHintsKeyExt.VALUE_TRANSCODING_VECTOR);
  161. this.root.paint(graphics);
  162. graphics.finish();
  163. } catch (IOException ex) {
  164. throw new TranscoderException(ex);
  165. }
  166. }
  167. /** {@inheritDoc} */
  168. protected BridgeContext createBridgeContext() {
  169. //For compatibility with Batik 1.6
  170. return createBridgeContext("1.x");
  171. }
  172. /** {@inheritDoc} */
  173. public BridgeContext createBridgeContext(String version) {
  174. FontInfo fontInfo = graphics.getFontInfo();
  175. if (isTextStroked()) {
  176. fontInfo = null;
  177. }
  178. BridgeContext ctx = new PDFBridgeContext(userAgent, fontInfo,
  179. getImageManager(), getImageSessionContext());
  180. return ctx;
  181. }
  182. }