1 17 18 19 20 package org.apache.fop.render.java2d; 21 22 import java.awt.geom.AffineTransform ; 23 24 import org.w3c.dom.Document ; 25 26 import org.apache.fop.render.Renderer; 27 import org.apache.fop.render.XMLHandler; 28 import org.apache.fop.render.RendererContext; 29 import org.apache.fop.svg.SVGUserAgent; 30 31 import org.apache.commons.logging.Log; 33 import org.apache.commons.logging.LogFactory; 34 35 import org.apache.batik.bridge.GVTBuilder; 36 import org.apache.batik.bridge.BridgeContext; 37 import org.apache.batik.dom.svg.SVGDOMImplementation; 38 import org.apache.batik.gvt.GraphicsNode; 39 40 45 public class Java2DSVGHandler implements XMLHandler, Java2DRendererContextConstants { 46 47 48 private static Log log = LogFactory.getLog(Java2DSVGHandler.class); 49 50 53 public Java2DSVGHandler() { 54 } 56 57 58 public void handleXML(RendererContext context, 59 Document doc, String ns) throws Exception { 60 Java2DInfo pdfi = getJava2DInfo(context); 61 62 if (SVGDOMImplementation.SVG_NAMESPACE_URI.equals(ns)) { 63 renderSVGDocument(context, doc, pdfi); 64 } 65 } 66 67 73 public static Java2DInfo getJava2DInfo(RendererContext context) { 74 Java2DInfo pdfi = new Java2DInfo(); 75 pdfi.state = (Java2DGraphicsState)context.getProperty(JAVA2D_STATE); 76 pdfi.width = ((Integer )context.getProperty(WIDTH)).intValue(); 77 pdfi.height = ((Integer )context.getProperty(HEIGHT)).intValue(); 78 pdfi.currentXPosition = ((Integer )context.getProperty(XPOS)).intValue(); 79 pdfi.currentYPosition = ((Integer )context.getProperty(YPOS)).intValue(); 80 return pdfi; 81 } 82 83 86 public static class Java2DInfo { 87 88 public Java2DGraphicsState state; 89 90 public int width; 91 92 public int height; 93 94 public int currentXPosition; 95 96 public int currentYPosition; 97 98 99 public String toString() { 100 return "Java2DInfo {" 101 + "state = " + state + ", " 102 + "width = " + width + ", " 103 + "height = " + height + ", " 104 + "currentXPosition = " + currentXPosition + ", " 105 + "currentYPosition = " + currentYPosition + "}"; 106 } 107 } 108 109 115 protected void renderSVGDocument(RendererContext context, 116 Document doc, 117 Java2DInfo info) { 118 119 log.debug("renderSVGDocument(" + context + ", " + doc + ", " + info + ")"); 120 121 int x = info.currentXPosition; 122 int y = info.currentYPosition; 123 124 float ptom = context.getUserAgent().getSourcePixelUnitToMillimeter(); 125 SVGUserAgent ua = new SVGUserAgent(ptom, new AffineTransform ()); 126 127 GVTBuilder builder = new GVTBuilder(); 128 BridgeContext ctx = new BridgeContext(ua); 129 130 GraphicsNode root; 131 try { 132 root = builder.build(ctx, doc); 133 } catch (Exception e) { 134 log.error("SVG graphic could not be built: " + e.getMessage(), e); 135 return; 136 } 137 138 float iw = (float) ctx.getDocumentSize().getWidth() * 1000f; 141 float ih = (float) ctx.getDocumentSize().getHeight() * 1000f; 142 143 float w = (float) info.width; 144 float h = (float) info.height; 145 146 AffineTransform origTransform = info.state.getGraph().getTransform(); 147 148 info.state.getGraph().translate(x / 1000f, y / 1000f); 150 151 AffineTransform at = AffineTransform.getScaleInstance(w / iw, h / ih); 154 if (!at.isIdentity()) { 155 info.state.getGraph().transform(at); 156 } 157 158 try { 159 root.paint(info.state.getGraph()); 160 } catch (Exception e) { 161 log.error("Error while painting SVG", e); 162 } 163 164 info.state.getGraph().setTransform(origTransform); 165 } 166 167 168 public boolean supportsRenderer(Renderer renderer) { 169 return (renderer instanceof Java2DRenderer); 170 } 171 172 173 174 public String getNamespace() { 175 return SVGDOMImplementation.SVG_NAMESPACE_URI; 176 } 177 } 178 | Popular Tags |