Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

AbstractPSTranscoder.java 6.2KB

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