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.

AbstractPSTranscoder.java 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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.ps;
  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.avalon.framework.configuration.Configuration;
  26. import org.apache.batik.bridge.BridgeContext;
  27. import org.apache.batik.bridge.UnitProcessor;
  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.xmlgraphics.java2d.ps.AbstractPSDocumentGraphics2D;
  32. import org.apache.xmlgraphics.java2d.TextHandler;
  33. import org.apache.fop.fonts.FontInfo;
  34. import org.apache.fop.fonts.FontSetup;
  35. import org.apache.fop.svg.AbstractFOPTranscoder;
  36. /**
  37. * This class enables to transcode an input to a PostScript document.
  38. *
  39. * <p>Two transcoding hints (<tt>KEY_WIDTH</tt> and
  40. * <tt>KEY_HEIGHT</tt>) can be used to respectively specify the image
  41. * width and the image height. If only one of these keys is specified,
  42. * the transcoder preserves the aspect ratio of the original image.
  43. *
  44. * <p>The <tt>KEY_BACKGROUND_COLOR</tt> defines the background color
  45. * to use for opaque image formats, or the background color that may
  46. * be used for image formats that support alpha channel.
  47. *
  48. * <p>The <tt>KEY_AOI</tt> represents the area of interest to paint
  49. * in device space.
  50. *
  51. * <p>Three additional transcoding hints that act on the SVG
  52. * processor can be specified:
  53. *
  54. * <p><tt>KEY_LANGUAGE</tt> to set the default language to use (may be
  55. * used by a &lt;switch> SVG element for example),
  56. * <tt>KEY_USER_STYLESHEET_URI</tt> to fix the URI of a user
  57. * stylesheet, and <tt>KEY_PIXEL_TO_MM</tt> to specify the pixel to
  58. * millimeter conversion factor.
  59. *
  60. * @author <a href="mailto:keiron@aftexsw.com">Keiron Liddle</a>
  61. * @version $Id$
  62. */
  63. public abstract class AbstractPSTranscoder extends AbstractFOPTranscoder {
  64. private Configuration cfg = null;
  65. protected AbstractPSDocumentGraphics2D graphics = null;
  66. /**
  67. * Constructs a new <tt>AbstractPSTranscoder</tt>.
  68. */
  69. public AbstractPSTranscoder() {
  70. super();
  71. }
  72. protected abstract AbstractPSDocumentGraphics2D createDocumentGraphics2D();
  73. /**
  74. * Transcodes the specified Document as an image in the specified output.
  75. *
  76. * @param document the document to transcode
  77. * @param uri the uri of the document or null if any
  78. * @param output the ouput where to transcode
  79. * @exception TranscoderException if an error occured while transcoding
  80. */
  81. protected void transcode(Document document, String uri,
  82. TranscoderOutput output)
  83. throws TranscoderException {
  84. graphics = createDocumentGraphics2D();
  85. if (!isTextStroked()) {
  86. FontInfo fontInfo = new FontInfo();
  87. //TODO Do custom font configuration here somewhere/somehow
  88. FontSetup.setup(fontInfo);
  89. graphics.setCustomTextHandler(new NativeTextHandler(graphics, fontInfo));
  90. }
  91. super.transcode(document, uri, output);
  92. getLogger().trace("document size: " + width + " x " + height);
  93. // prepare the image to be painted
  94. UnitProcessor.Context uctx = UnitProcessor.createContext(ctx,
  95. document.getDocumentElement());
  96. float widthInPt = UnitProcessor.userSpaceToSVG(width, SVGLength.SVG_LENGTHTYPE_PT,
  97. UnitProcessor.HORIZONTAL_LENGTH, uctx);
  98. int w = (int)(widthInPt + 0.5);
  99. float heightInPt = UnitProcessor.userSpaceToSVG(height, SVGLength.SVG_LENGTHTYPE_PT,
  100. UnitProcessor.HORIZONTAL_LENGTH, uctx);
  101. int h = (int)(heightInPt + 0.5);
  102. getLogger().trace("document size: " + w + "pt x " + h + "pt");
  103. try {
  104. OutputStream out = output.getOutputStream();
  105. if (!(out instanceof BufferedOutputStream)) {
  106. out = new BufferedOutputStream(out);
  107. }
  108. graphics.setupDocument(out, w, h);
  109. graphics.setViewportDimension(width, height);
  110. if (hints.containsKey(ImageTranscoder.KEY_BACKGROUND_COLOR)) {
  111. graphics.setBackgroundColor
  112. ((Color)hints.get(ImageTranscoder.KEY_BACKGROUND_COLOR));
  113. }
  114. graphics.setGraphicContext
  115. (new org.apache.xmlgraphics.java2d.GraphicContext());
  116. graphics.setTransform(curTxf);
  117. this.root.paint(graphics);
  118. graphics.finish();
  119. } catch (IOException ex) {
  120. throw new TranscoderException(ex);
  121. }
  122. }
  123. /** {@inheritDoc} */
  124. protected BridgeContext createBridgeContext() {
  125. BridgeContext ctx = new BridgeContext(userAgent);
  126. if (!isTextStroked()) {
  127. TextHandler handler = graphics.getCustomTextHandler();
  128. if (handler instanceof NativeTextHandler) {
  129. NativeTextHandler nativeTextHandler = (NativeTextHandler)handler;
  130. PSTextPainter textPainter = new PSTextPainter(nativeTextHandler);
  131. ctx.setTextPainter(textPainter);
  132. ctx.putBridge(new PSTextElementBridge(textPainter));
  133. }
  134. }
  135. //ctx.putBridge(new PSImageElementBridge());
  136. return ctx;
  137. }
  138. }