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.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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.batik.bridge.BridgeContext;
  25. import org.apache.batik.bridge.UnitProcessor;
  26. import org.apache.batik.bridge.UserAgent;
  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.TranscodingHints;
  31. import org.apache.batik.transcoder.image.ImageTranscoder;
  32. import org.apache.batik.transcoder.keys.FloatKey;
  33. import org.apache.fop.Version;
  34. import org.apache.fop.fonts.FontInfo;
  35. import org.apache.fop.fonts.FontSetup;
  36. import org.w3c.dom.Document;
  37. import org.w3c.dom.svg.SVGLength;
  38. /**
  39. * This class enables to transcode an input to a pdf document.
  40. *
  41. * <p>Two transcoding hints (<tt>KEY_WIDTH</tt> and
  42. * <tt>KEY_HEIGHT</tt>) can be used to respectively specify the image
  43. * width and the image height. If only one of these keys is specified,
  44. * the transcoder preserves the aspect ratio of the original image.
  45. *
  46. * <p>The <tt>KEY_BACKGROUND_COLOR</tt> defines the background color
  47. * to use for opaque image formats, or the background color that may
  48. * be used for image formats that support alpha channel.
  49. *
  50. * <p>The <tt>KEY_AOI</tt> represents the area of interest to paint
  51. * in device space.
  52. *
  53. * <p>Three additional transcoding hints that act on the SVG
  54. * processor can be specified:
  55. *
  56. * <p><tt>KEY_LANGUAGE</tt> to set the default language to use (may be
  57. * used by a &lt;switch> SVG element for example),
  58. * <tt>KEY_USER_STYLESHEET_URI</tt> to fix the URI of a user
  59. * stylesheet, and <tt>KEY_PIXEL_TO_MM</tt> to specify the pixel to
  60. * millimeter conversion factor.
  61. *
  62. * @author <a href="mailto:keiron@aftexsw.com">Keiron Liddle</a>
  63. * @version $Id$
  64. */
  65. public class PDFTranscoder extends AbstractFOPTranscoder
  66. implements Configurable {
  67. /**
  68. * The key is used to specify the resolution for on-the-fly images generated
  69. * due to complex effects like gradients and filters.
  70. */
  71. public static final TranscodingHints.Key KEY_DEVICE_RESOLUTION = new FloatKey();
  72. private Configuration cfg = null;
  73. /** Graphics2D instance that is used to paint to */
  74. protected PDFDocumentGraphics2D graphics = null;
  75. /**
  76. * Constructs a new <tt>ImageTranscoder</tt>.
  77. */
  78. public PDFTranscoder() {
  79. super();
  80. this.handler = new FOPErrorHandler();
  81. }
  82. /**
  83. * @see org.apache.fop.svg.AbstractFOPTranscoder#createUserAgent()
  84. */
  85. protected UserAgent createUserAgent() {
  86. return new AbstractFOPTranscoder.FOPTranscoderUserAgent() {
  87. // The PDF stuff wants everything at 72dpi
  88. public float getPixelUnitToMillimeter() {
  89. return super.getPixelUnitToMillimeter();
  90. //return 25.4f / 72; //72dpi = 0.352778f;
  91. }
  92. };
  93. }
  94. /**
  95. * @see org.apache.avalon.framework.configuration.Configurable#configure(Configuration)
  96. */
  97. public void configure(Configuration cfg) throws ConfigurationException {
  98. this.cfg = cfg;
  99. }
  100. /**
  101. * Transcodes the specified Document as an image in the specified output.
  102. *
  103. * @param document the document to transcode
  104. * @param uri the uri of the document or null if any
  105. * @param output the ouput where to transcode
  106. * @exception TranscoderException if an error occured while transcoding
  107. */
  108. protected void transcode(Document document, String uri,
  109. TranscoderOutput output)
  110. throws TranscoderException {
  111. graphics = new PDFDocumentGraphics2D();
  112. graphics.getPDFDocument().getInfo().setProducer("Apache FOP Version "
  113. + Version.getVersion()
  114. + ": PDF Transcoder for Batik");
  115. try {
  116. if (this.cfg != null) {
  117. PDFDocumentGraphics2DConfigurator configurator
  118. = new PDFDocumentGraphics2DConfigurator();
  119. configurator.configure(graphics, this.cfg);
  120. }
  121. } catch (Exception e) {
  122. throw new TranscoderException(
  123. "Error while setting up PDFDocumentGraphics2D", e);
  124. }
  125. super.transcode(document, uri, output);
  126. if (getLogger().isTraceEnabled()) {
  127. getLogger().trace("document size: " + width + " x " + height);
  128. }
  129. // prepare the image to be painted
  130. UnitProcessor.Context uctx = UnitProcessor.createContext(ctx,
  131. document.getDocumentElement());
  132. float widthInPt = UnitProcessor.userSpaceToSVG(width, SVGLength.SVG_LENGTHTYPE_PT,
  133. UnitProcessor.HORIZONTAL_LENGTH, uctx);
  134. int w = (int)(widthInPt + 0.5);
  135. float heightInPt = UnitProcessor.userSpaceToSVG(height, SVGLength.SVG_LENGTHTYPE_PT,
  136. UnitProcessor.HORIZONTAL_LENGTH, uctx);
  137. int h = (int)(heightInPt + 0.5);
  138. if (getLogger().isTraceEnabled()) {
  139. getLogger().trace("document size: " + w + "pt x " + h + "pt");
  140. }
  141. // prepare the image to be painted
  142. //int w = (int)(width + 0.5);
  143. //int h = (int)(height + 0.5);
  144. try {
  145. if (hints.containsKey(KEY_DEVICE_RESOLUTION)) {
  146. graphics.setDeviceDPI(((Float)hints.get(KEY_DEVICE_RESOLUTION)).floatValue());
  147. }
  148. graphics.setupDocument(output.getOutputStream(), 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. /** @see org.apache.batik.transcoder.SVGAbstractTranscoder#createBridgeContext() */
  168. protected BridgeContext createBridgeContext() {
  169. BridgeContext ctx = new PDFBridgeContext(userAgent, graphics.getFontInfo());
  170. return ctx;
  171. }
  172. }