Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

AbstractPSTranscoder.java 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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.batik.bridge.BridgeContext;
  26. import org.apache.batik.bridge.UnitProcessor;
  27. import org.apache.batik.transcoder.TranscoderException;
  28. import org.apache.batik.transcoder.TranscoderOutput;
  29. import org.apache.batik.transcoder.image.ImageTranscoder;
  30. import org.apache.xmlgraphics.java2d.ps.AbstractPSDocumentGraphics2D;
  31. import org.apache.fop.apps.FOPException;
  32. import org.apache.fop.fonts.FontInfo;
  33. import org.apache.fop.svg.AbstractFOPTranscoder;
  34. import org.apache.fop.svg.PDFDocumentGraphics2DConfigurator;
  35. import org.apache.fop.svg.font.FOPFontFamilyResolverImpl;
  36. /**
  37. * <p>This class enables to transcode an input to a PostScript document.</p>
  38. *
  39. * <p>Two transcoding hints (<code>KEY_WIDTH</code> and
  40. * <code>KEY_HEIGHT</code>) 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 <code>KEY_BACKGROUND_COLOR</code> 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 <code>KEY_AOI</code> 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><code>KEY_LANGUAGE</code> to set the default language to use (may be
  55. * used by a &lt;switch&gt; SVG element for example),
  56. * <code>KEY_USER_STYLESHEET_URI</code> to fix the URI of a user
  57. * stylesheet, and <code>KEY_PIXEL_TO_MM</code> to specify the pixel to
  58. * millimeter conversion factor.
  59. *
  60. * <p>This work was authored by Keiron Liddle (keiron@aftexsw.com).</p>
  61. */
  62. public abstract class AbstractPSTranscoder extends AbstractFOPTranscoder {
  63. /** the root Graphics2D instance for generating PostScript */
  64. protected AbstractPSDocumentGraphics2D graphics;
  65. private FontInfo fontInfo;
  66. /**
  67. * Constructs a new {@link AbstractPSTranscoder}.
  68. */
  69. public AbstractPSTranscoder() {
  70. super();
  71. }
  72. /**
  73. * Creates the root Graphics2D instance for generating PostScript.
  74. * @return the root Graphics2D
  75. */
  76. protected abstract AbstractPSDocumentGraphics2D createDocumentGraphics2D();
  77. /** {@inheritDoc} */
  78. protected boolean getAutoFontsDefault() {
  79. //Currently set to false because auto-fonts requires a lot of memory in the PostScript
  80. //case: All fonts (even the unsupported TTF fonts) need to be loaded and TrueType loading
  81. //is currently very memory-intensive. At default JVM memory settings, this would result
  82. //in OutOfMemoryErrors otherwise.
  83. return false;
  84. }
  85. /**
  86. * Transcodes the specified Document as an image in the specified output.
  87. *
  88. * @param document the document to transcode
  89. * @param uri the uri of the document or null if any
  90. * @param output the ouput where to transcode
  91. * @exception TranscoderException if an error occured while transcoding
  92. */
  93. protected void transcode(Document document, String uri,
  94. TranscoderOutput output)
  95. throws TranscoderException {
  96. graphics = createDocumentGraphics2D();
  97. if (!isTextStroked()) {
  98. try {
  99. boolean useComplexScriptFeatures = false; //TODO - FIX ME
  100. this.fontInfo = PDFDocumentGraphics2DConfigurator.createFontInfo(
  101. getEffectiveConfiguration(), useComplexScriptFeatures);
  102. graphics.setCustomTextHandler(new NativeTextHandler(graphics, fontInfo));
  103. ((FOPTranscoderUserAgent) userAgent).setFontFamilyResolver(
  104. new FOPFontFamilyResolverImpl(fontInfo));
  105. } catch (FOPException fe) {
  106. throw new TranscoderException(fe);
  107. }
  108. }
  109. super.transcode(document, uri, output);
  110. getLogger().trace("document size: " + width + " x " + height);
  111. // prepare the image to be painted
  112. UnitProcessor.Context uctx = UnitProcessor.createContext(ctx,
  113. document.getDocumentElement());
  114. float widthInPt = UnitProcessor.userSpaceToSVG(width, SVGLength.SVG_LENGTHTYPE_PT,
  115. UnitProcessor.HORIZONTAL_LENGTH, uctx);
  116. int w = (int)(widthInPt + 0.5);
  117. float heightInPt = UnitProcessor.userSpaceToSVG(height, SVGLength.SVG_LENGTHTYPE_PT,
  118. UnitProcessor.HORIZONTAL_LENGTH, uctx);
  119. int h = (int)(heightInPt + 0.5);
  120. getLogger().trace("document size: " + w + "pt x " + h + "pt");
  121. try {
  122. OutputStream out = output.getOutputStream();
  123. if (!(out instanceof BufferedOutputStream)) {
  124. out = new BufferedOutputStream(out);
  125. }
  126. graphics.setupDocument(out, w, h);
  127. graphics.setViewportDimension(width, height);
  128. if (hints.containsKey(ImageTranscoder.KEY_BACKGROUND_COLOR)) {
  129. graphics.setBackgroundColor(
  130. (Color)hints.get(ImageTranscoder.KEY_BACKGROUND_COLOR));
  131. }
  132. graphics.setGraphicContext(
  133. new org.apache.xmlgraphics.java2d.GraphicContext());
  134. graphics.setTransform(curTxf);
  135. this.root.paint(graphics);
  136. graphics.finish();
  137. } catch (IOException ex) {
  138. throw new TranscoderException(ex);
  139. }
  140. }
  141. /** {@inheritDoc} */
  142. protected BridgeContext createBridgeContext() {
  143. //For compatibility with Batik 1.6
  144. return createBridgeContext("1.x");
  145. }
  146. /** {@inheritDoc} */
  147. public BridgeContext createBridgeContext(String version) {
  148. BridgeContext ctx = new PSBridgeContext(userAgent, (isTextStroked() ? null : fontInfo),
  149. getImageManager(), getImageSessionContext());
  150. return ctx;
  151. }
  152. }