1 17 18 19 20 package org.apache.fop.render.ps; 21 22 import java.awt.geom.AffineTransform ; 24 import java.io.IOException ; 25 26 import org.w3c.dom.Document ; 28 import org.w3c.dom.svg.SVGDocument; 29 import org.w3c.dom.svg.SVGSVGElement; 30 31 import org.apache.avalon.framework.configuration.Configuration; 33 import org.apache.batik.bridge.GVTBuilder; 34 import org.apache.batik.bridge.BridgeContext; 35 import org.apache.batik.bridge.ViewBox; 36 import org.apache.batik.dom.svg.SVGDOMImplementation; 37 import org.apache.batik.gvt.GraphicsNode; 38 39 import org.apache.fop.fonts.FontInfo; 41 import org.apache.fop.render.Renderer; 42 import org.apache.fop.render.XMLHandler; 43 import org.apache.fop.render.RendererContext; 44 import org.apache.fop.svg.SVGUserAgent; 45 import org.apache.xmlgraphics.java2d.ps.PSGraphics2D; 46 import org.apache.xmlgraphics.ps.PSGenerator; 47 48 import org.apache.commons.logging.Log; 50 import org.apache.commons.logging.LogFactory; 51 52 60 public class PSSVGHandler implements XMLHandler, PSRendererContextConstants { 61 62 63 private static Log log = LogFactory.getLog(PSSVGHandler.class); 64 65 68 public PSSVGHandler() { 69 } 70 71 72 public void handleXML(RendererContext context, 73 Document doc, String ns) throws Exception { 74 PSInfo psi = getPSInfo(context); 75 76 if (SVGDOMImplementation.SVG_NAMESPACE_URI.equals(ns)) { 77 renderSVGDocument(context, doc, psi); 78 } 79 } 80 81 87 public static PSInfo getPSInfo(RendererContext context) { 88 PSInfo psi = new PSInfo(); 89 psi.psGenerator = (PSGenerator)context.getProperty(PS_GENERATOR); 90 psi.fontInfo = (org.apache.fop.fonts.FontInfo) context.getProperty(PS_FONT_INFO); 91 psi.width = ((Integer )context.getProperty(WIDTH)).intValue(); 92 psi.height = ((Integer )context.getProperty(HEIGHT)).intValue(); 93 psi.currentXPosition = ((Integer )context.getProperty(XPOS)).intValue(); 94 psi.currentYPosition = ((Integer )context.getProperty(YPOS)).intValue(); 95 psi.cfg = (Configuration)context.getProperty(HANDLER_CONFIGURATION); 96 return psi; 97 } 98 99 102 public static class PSInfo { 103 104 105 private PSGenerator psGenerator; 106 107 private org.apache.fop.fonts.FontInfo fontInfo; 108 109 private int width; 110 111 private int height; 112 113 private int currentXPosition; 114 115 private int currentYPosition; 116 117 private Configuration cfg; 118 119 123 public PSGenerator getPSGenerator() { 124 return psGenerator; 125 } 126 127 131 public void setPsGenerator(PSGenerator psGenerator) { 132 this.psGenerator = psGenerator; 133 } 134 135 139 public FontInfo getFontInfo() { 140 return fontInfo; 141 } 142 143 147 public void setFontInfo(FontInfo fontInfo) { 148 this.fontInfo = fontInfo; 149 } 150 151 155 public int getCurrentXPosition() { 156 return currentXPosition; 157 } 158 159 163 public void setCurrentXPosition(int currentXPosition) { 164 this.currentXPosition = currentXPosition; 165 } 166 167 171 public int getCurrentYPosition() { 172 return currentYPosition; 173 } 174 175 179 public void setCurrentYPosition(int currentYPosition) { 180 this.currentYPosition = currentYPosition; 181 } 182 183 187 public int getWidth() { 188 return width; 189 } 190 191 195 public void setWidth(int width) { 196 this.width = width; 197 } 198 199 203 public int getHeight() { 204 return height; 205 } 206 207 211 public void setHeight(int height) { 212 this.height = height; 213 } 214 215 219 public Configuration getHandlerConfiguration() { 220 return this.cfg; 221 } 222 223 227 public void setHandlerConfiguration(Configuration cfg) { 228 this.cfg = cfg; 229 } 230 231 } 232 233 239 protected void renderSVGDocument(RendererContext context, 240 Document doc, PSInfo psInfo) { 241 int xOffset = psInfo.currentXPosition; 242 int yOffset = psInfo.currentYPosition; 243 PSGenerator gen = psInfo.psGenerator; 244 245 boolean strokeText = false; 247 Configuration cfg = psInfo.getHandlerConfiguration(); 248 if (cfg != null) { 249 strokeText = cfg.getChild("stroke-text", true).getValueAsBoolean(strokeText); 250 } 251 252 SVGUserAgent ua 253 = new SVGUserAgent( 254 context.getUserAgent().getSourcePixelUnitToMillimeter(), 255 new AffineTransform ()); 256 257 PSGraphics2D graphics = new PSGraphics2D(strokeText, gen); 258 graphics.setGraphicContext(new org.apache.xmlgraphics.java2d.GraphicContext()); 259 260 GVTBuilder builder = new GVTBuilder(); 261 NativeTextHandler nativeTextHandler = null; 262 BridgeContext ctx = new BridgeContext(ua); 263 if (!strokeText) { 264 nativeTextHandler = new NativeTextHandler(graphics, psInfo.getFontInfo()); 265 graphics.setCustomTextHandler(nativeTextHandler); 266 PSTextPainter textPainter = new PSTextPainter(nativeTextHandler); 267 ctx.setTextPainter(textPainter); 268 PSTextElementBridge tBridge = new PSTextElementBridge(textPainter); 269 ctx.putBridge(tBridge); 270 } 271 272 GraphicsNode root; 273 try { 274 root = builder.build(ctx, doc); 275 } catch (Exception e) { 276 log.error("SVG graphic could not be built: " 277 + e.getMessage(), e); 278 return; 279 } 280 float w = (float)ctx.getDocumentSize().getWidth() * 1000f; 282 float h = (float)ctx.getDocumentSize().getHeight() * 1000f; 283 284 float sx = psInfo.getWidth() / (float)w; 285 float sy = psInfo.getHeight() / (float)h; 286 287 ctx = null; 288 builder = null; 289 290 try { 291 gen.commentln("%FOPBeginSVG"); 292 gen.saveGraphicsState(); 293 298 gen.writeln("newpath"); 299 gen.defineRect(xOffset / 1000f, yOffset / 1000f, 300 psInfo.getWidth() / 1000f, psInfo.getHeight() / 1000f); 301 gen.writeln("clip"); 302 303 gen.concatMatrix(sx, 0, 0, sy, xOffset / 1000f, yOffset / 1000f); 307 308 SVGSVGElement svg = ((SVGDocument)doc).getRootElement(); 309 AffineTransform at = ViewBox.getPreserveAspectRatioTransform(svg, 310 psInfo.getWidth() / 1000f, psInfo.getHeight() / 1000f); 311 317 318 AffineTransform transform = new AffineTransform (); 319 transform.translate(xOffset, yOffset); 321 gen.getCurrentState().concatMatrix(transform); 322 try { 323 root.paint(graphics); 324 } catch (Exception e) { 325 log.error("SVG graphic could not be rendered: " 326 + e.getMessage(), e); 327 } 328 329 gen.restoreGraphicsState(); 330 gen.commentln("%FOPEndSVG"); 331 } catch (IOException ioe) { 332 log.error("SVG graphic could not be rendered: " 333 + ioe.getMessage(), ioe); 334 } 335 } 336 337 338 public boolean supportsRenderer(Renderer renderer) { 339 return (renderer instanceof PSRenderer); 340 } 341 342 343 public String getNamespace() { 344 return SVGDOMImplementation.SVG_NAMESPACE_URI; 345 } 346 347 } 348 349 | Popular Tags |