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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * Copyright 1999-2005 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* $Id$ */
  17. package org.apache.fop.svg;
  18. import java.awt.Color;
  19. import java.io.IOException;
  20. import org.apache.avalon.framework.configuration.Configurable;
  21. import org.apache.avalon.framework.configuration.Configuration;
  22. import org.apache.avalon.framework.configuration.ConfigurationException;
  23. import org.apache.avalon.framework.container.ContainerUtil;
  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.w3c.dom.Document;
  34. import org.w3c.dom.svg.SVGLength;
  35. /**
  36. * This class enables to transcode an input to a pdf document.
  37. *
  38. * <p>Two transcoding hints (<tt>KEY_WIDTH</tt> and
  39. * <tt>KEY_HEIGHT</tt>) 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 <tt>KEY_BACKGROUND_COLOR</tt> 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 <tt>KEY_AOI</tt> 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><tt>KEY_LANGUAGE</tt> to set the default language to use (may be
  54. * used by a &lt;switch> SVG element for example),
  55. * <tt>KEY_USER_STYLESHEET_URI</tt> to fix the URI of a user
  56. * stylesheet, and <tt>KEY_PIXEL_TO_MM</tt> to specify the pixel to
  57. * millimeter conversion factor.
  58. *
  59. * @author <a href="mailto:keiron@aftexsw.com">Keiron Liddle</a>
  60. * @version $Id$
  61. */
  62. public class PDFTranscoder extends AbstractFOPTranscoder
  63. implements Configurable {
  64. /**
  65. * The key is used to specify the resolution for on-the-fly images generated
  66. * due to complex effects like gradients and filters.
  67. */
  68. public static final TranscodingHints.Key KEY_DEVICE_RESOLUTION = new FloatKey();
  69. private Configuration cfg = null;
  70. /** Graphics2D instance that is used to paint to */
  71. protected PDFDocumentGraphics2D graphics = null;
  72. /**
  73. * Constructs a new <tt>ImageTranscoder</tt>.
  74. */
  75. public PDFTranscoder() {
  76. super();
  77. this.handler = new FOPErrorHandler();
  78. }
  79. /**
  80. * @see org.apache.fop.svg.AbstractFOPTranscoder#createUserAgent()
  81. */
  82. protected UserAgent createUserAgent() {
  83. return new AbstractFOPTranscoder.FOPTranscoderUserAgent() {
  84. // The PDF stuff wants everything at 72dpi
  85. public float getPixelUnitToMillimeter() {
  86. return super.getPixelUnitToMillimeter();
  87. //return 25.4f / 72; //72dpi = 0.352778f;
  88. }
  89. };
  90. }
  91. /**
  92. * @see org.apache.avalon.framework.configuration.Configurable#configure(Configuration)
  93. */
  94. public void configure(Configuration cfg) throws ConfigurationException {
  95. this.cfg = cfg;
  96. }
  97. /**
  98. * Transcodes the specified Document as an image in the specified output.
  99. *
  100. * @param document the document to transcode
  101. * @param uri the uri of the document or null if any
  102. * @param output the ouput where to transcode
  103. * @exception TranscoderException if an error occured while transcoding
  104. */
  105. protected void transcode(Document document, String uri,
  106. TranscoderOutput output)
  107. throws TranscoderException {
  108. graphics = new PDFDocumentGraphics2D();
  109. try {
  110. if (this.cfg != null) {
  111. ContainerUtil.configure(graphics, this.cfg);
  112. }
  113. ContainerUtil.initialize(graphics);
  114. } catch (Exception e) {
  115. throw new TranscoderException(
  116. "Error while setting up PDFDocumentGraphics2D", e);
  117. }
  118. super.transcode(document, uri, output);
  119. getLogger().trace("document size: " + width + " x " + height);
  120. // prepare the image to be painted
  121. UnitProcessor.Context uctx = UnitProcessor.createContext(ctx,
  122. document.getDocumentElement());
  123. float widthInPt = UnitProcessor.userSpaceToSVG(width, SVGLength.SVG_LENGTHTYPE_PT,
  124. UnitProcessor.HORIZONTAL_LENGTH, uctx);
  125. int w = (int)(widthInPt + 0.5);
  126. float heightInPt = UnitProcessor.userSpaceToSVG(height, SVGLength.SVG_LENGTHTYPE_PT,
  127. UnitProcessor.HORIZONTAL_LENGTH, uctx);
  128. int h = (int)(heightInPt + 0.5);
  129. getLogger().trace("document size: " + w + "pt x " + h + "pt");
  130. // prepare the image to be painted
  131. //int w = (int)(width + 0.5);
  132. //int h = (int)(height + 0.5);
  133. try {
  134. if (hints.containsKey(KEY_DEVICE_RESOLUTION)) {
  135. graphics.setDeviceDPI(((Float)hints.get(KEY_DEVICE_RESOLUTION)).floatValue());
  136. }
  137. graphics.setupDocument(output.getOutputStream(), w, h);
  138. graphics.setSVGDimension(width, height);
  139. if (hints.containsKey(ImageTranscoder.KEY_BACKGROUND_COLOR)) {
  140. graphics.setBackgroundColor
  141. ((Color)hints.get(ImageTranscoder.KEY_BACKGROUND_COLOR));
  142. }
  143. graphics.setGraphicContext
  144. (new org.apache.xmlgraphics.java2d.GraphicContext());
  145. graphics.preparePainting();
  146. graphics.transform(curTxf);
  147. graphics.setRenderingHint
  148. (RenderingHintsKeyExt.KEY_TRANSCODING,
  149. RenderingHintsKeyExt.VALUE_TRANSCODING_VECTOR);
  150. this.root.paint(graphics);
  151. graphics.finish();
  152. } catch (IOException ex) {
  153. throw new TranscoderException(ex);
  154. }
  155. }
  156. /** @see org.apache.batik.transcoder.SVGAbstractTranscoder#createBridgeContext() */
  157. protected BridgeContext createBridgeContext() {
  158. BridgeContext ctx = new PDFBridgeContext(userAgent, graphics.getFontInfo());
  159. return ctx;
  160. }
  161. }