1 17 18 19 20 package org.apache.fop.render; 21 22 import java.awt.Dimension ; 24 import java.awt.Graphics2D ; 25 import java.awt.geom.AffineTransform ; 26 import java.awt.geom.Rectangle2D ; 27 import java.io.IOException ; 28 29 import org.w3c.dom.Document ; 31 32 import org.apache.batik.bridge.GVTBuilder; 34 import org.apache.batik.bridge.BridgeContext; 35 import org.apache.batik.dom.svg.SVGDOMImplementation; 36 import org.apache.batik.gvt.GraphicsNode; 37 38 import org.apache.fop.render.Graphics2DAdapter; 40 import org.apache.fop.render.Graphics2DImagePainter; 41 import org.apache.fop.render.RendererContextConstants; 42 import org.apache.fop.render.XMLHandler; 43 import org.apache.fop.render.RendererContext; 44 import org.apache.fop.render.RendererContext.RendererContextWrapper; 45 import org.apache.fop.svg.SVGUserAgent; 46 47 import org.apache.commons.logging.Log; 49 import org.apache.commons.logging.LogFactory; 50 51 57 public abstract class AbstractGenericSVGHandler implements XMLHandler, RendererContextConstants { 58 59 60 private static Log log = LogFactory.getLog(AbstractGenericSVGHandler.class); 61 62 63 public void handleXML(RendererContext context, 64 Document doc, String ns) throws Exception { 65 66 if (SVGDOMImplementation.SVG_NAMESPACE_URI.equals(ns)) { 67 renderSVGDocument(context, doc); 68 } 69 } 70 71 77 protected void renderSVGDocument(final RendererContext context, 78 final Document doc) throws IOException { 79 final RendererContextWrapper wrappedContext = RendererContext.wrapRendererContext(context); 80 int x = wrappedContext.getCurrentXPosition(); 81 int y = wrappedContext.getCurrentYPosition(); 82 83 SVGUserAgent ua = new SVGUserAgent( 85 context.getUserAgent().getSourcePixelUnitToMillimeter(), 86 new AffineTransform ()); 87 GVTBuilder builder = new GVTBuilder(); 88 final BridgeContext ctx = new BridgeContext(ua); 89 90 final GraphicsNode root; 92 try { 93 root = builder.build(ctx, doc); 94 } catch (Exception e) { 95 log.error("SVG graphic could not be built: " + e.getMessage(), e); 96 return; 97 } 98 99 Graphics2DImagePainter painter = new Graphics2DImagePainter() { 101 102 public void paint(Graphics2D g2d, Rectangle2D area) { 103 float iw = (float) ctx.getDocumentSize().getWidth(); 106 float ih = (float) ctx.getDocumentSize().getHeight(); 107 float w = (float) area.getWidth(); 108 float h = (float) area.getHeight(); 109 g2d.scale(w / iw, h / ih); 110 111 root.paint(g2d); 112 } 113 114 public Dimension getImageSize() { 115 return new Dimension (wrappedContext.getWidth(), wrappedContext.getHeight()); 116 } 117 118 }; 119 120 Graphics2DAdapter adapter = context.getRenderer().getGraphics2DAdapter(); 122 adapter.paintImage(painter, context, 123 x, y, wrappedContext.getWidth(), wrappedContext.getHeight()); 124 } 125 126 127 public String getNamespace() { 128 return SVGDOMImplementation.SVG_NAMESPACE_URI; 129 } 130 131 } 132 133 | Popular Tags |