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 9.2KB

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