1 17 18 19 20 package org.apache.fop.render.ps; 21 22 23 import java.awt.Color ; 24 25 import java.io.IOException ; 26 27 import org.apache.avalon.framework.configuration.Configuration; 28 import org.apache.batik.bridge.BridgeContext; 29 import org.apache.batik.bridge.UnitProcessor; 30 import org.apache.batik.transcoder.TranscoderException; 31 import org.apache.batik.transcoder.TranscoderOutput; 32 33 import org.apache.batik.transcoder.image.ImageTranscoder; 34 35 import org.apache.fop.fonts.FontInfo; 36 import org.apache.fop.fonts.FontSetup; 37 import org.apache.fop.svg.AbstractFOPTranscoder; 38 import org.apache.xmlgraphics.java2d.ps.AbstractPSDocumentGraphics2D; 39 import org.apache.xmlgraphics.java2d.ps.TextHandler; 40 41 import org.w3c.dom.Document ; 42 import org.w3c.dom.svg.SVGLength; 43 44 71 public abstract class AbstractPSTranscoder extends AbstractFOPTranscoder { 72 73 private Configuration cfg = null; 74 protected AbstractPSDocumentGraphics2D graphics = null; 75 76 79 public AbstractPSTranscoder() { 80 super(); 81 } 82 83 protected abstract AbstractPSDocumentGraphics2D createDocumentGraphics2D(); 84 85 93 protected void transcode(Document document, String uri, 94 TranscoderOutput output) 95 throws TranscoderException { 96 97 graphics = createDocumentGraphics2D(); 98 if (!isTextStroked()) { 99 FontInfo fontInfo = new FontInfo(); 100 FontSetup.setup(fontInfo, null, null); 102 graphics.setCustomTextHandler(new NativeTextHandler(graphics, fontInfo)); 103 } 104 105 super.transcode(document, uri, output); 106 107 getLogger().trace("document size: " + width + " x " + height); 108 109 UnitProcessor.Context uctx = UnitProcessor.createContext(ctx, 111 document.getDocumentElement()); 112 float widthInPt = UnitProcessor.userSpaceToSVG(width, SVGLength.SVG_LENGTHTYPE_PT, 113 UnitProcessor.HORIZONTAL_LENGTH, uctx); 114 int w = (int)(widthInPt + 0.5); 115 float heightInPt = UnitProcessor.userSpaceToSVG(height, SVGLength.SVG_LENGTHTYPE_PT, 116 UnitProcessor.HORIZONTAL_LENGTH, uctx); 117 int h = (int)(heightInPt + 0.5); 118 getLogger().trace("document size: " + w + "pt x " + h + "pt"); 119 120 try { 121 graphics.setupDocument(output.getOutputStream(), w, h); 122 graphics.setViewportDimension(width, height); 123 124 if (hints.containsKey(ImageTranscoder.KEY_BACKGROUND_COLOR)) { 125 graphics.setBackgroundColor 126 ((Color )hints.get(ImageTranscoder.KEY_BACKGROUND_COLOR)); 127 } 128 graphics.setGraphicContext 129 (new org.apache.xmlgraphics.java2d.GraphicContext()); 130 graphics.setTransform(curTxf); 131 132 this.root.paint(graphics); 133 134 graphics.finish(); 135 } catch (IOException ex) { 136 throw new TranscoderException(ex); 137 } 138 } 139 140 141 protected boolean isTextStroked() { 142 boolean stroke = false; 143 if (hints.containsKey(KEY_STROKE_TEXT)) { 144 stroke = ((Boolean )hints.get(KEY_STROKE_TEXT)).booleanValue(); 145 } 146 return stroke; 147 } 148 149 150 protected BridgeContext createBridgeContext() { 151 152 BridgeContext ctx = new BridgeContext(userAgent); 153 if (!isTextStroked()) { 154 TextHandler handler = graphics.getCustomTextHandler(); 155 if (handler instanceof NativeTextHandler) { 156 NativeTextHandler nativeTextHandler = (NativeTextHandler)handler; 157 PSTextPainter textPainter = new PSTextPainter(nativeTextHandler); 158 ctx.setTextPainter(textPainter); 159 ctx.putBridge(new PSTextElementBridge(textPainter)); 160 } 161 } 162 163 return ctx; 165 } 166 167 168 } 169 | Popular Tags |