1 17 18 19 20 package org.apache.fop.fo.extensions.svg; 21 22 import org.apache.fop.apps.FOPException; 24 import org.apache.fop.fo.FONode; 25 import org.apache.fop.fo.PropertyList; 26 import org.apache.fop.util.ContentHandlerFactory; 27 import org.apache.fop.util.DOMBuilderContentHandlerFactory; 28 29 import org.apache.batik.dom.svg.SVGOMDocument; 30 import org.apache.batik.dom.svg.SVGOMElement; 31 import org.apache.batik.dom.svg.SVGContext; 32 import org.apache.batik.dom.util.XMLSupport; 33 import org.w3c.dom.Element ; 34 import org.w3c.dom.svg.SVGDocument; 35 import org.xml.sax.Attributes ; 36 import org.xml.sax.Locator ; 37 import org.apache.batik.bridge.UnitProcessor; 38 import org.apache.batik.util.SVGConstants; 39 40 import org.w3c.dom.DOMImplementation ; 41 42 import org.apache.batik.dom.svg.SVGDOMImplementation; 43 44 import java.net.URL ; 45 import java.awt.geom.AffineTransform ; 46 import java.awt.geom.Point2D ; 47 import java.awt.geom.Rectangle2D ; 48 49 53 public class SVGElement extends SVGObj { 54 55 60 public SVGElement(FONode parent) { 61 super(parent); 62 } 63 64 67 public ContentHandlerFactory getContentHandlerFactory() { 68 return new DOMBuilderContentHandlerFactory(getNamespaceURI(), 69 SVGDOMImplementation.getDOMImplementation()); 70 } 71 72 75 public void processNode(String elementName, Locator locator, 76 Attributes attlist, PropertyList propertyList) throws FOPException { 77 super.processNode(elementName, locator, attlist, propertyList); 78 init(); 79 } 80 81 86 public Point2D getDimension(final Point2D view) { 87 88 Element svgRoot = element; 90 91 92 93 try { 94 URL baseURL = new URL (getUserAgent().getBaseURL() == null 95 ? new java.io.File ("").toURL().toExternalForm() 96 : getUserAgent().getBaseURL()); 97 if (baseURL != null) { 98 SVGOMDocument svgdoc = (SVGOMDocument)doc; 99 svgdoc.setURLObject(baseURL); 100 } 103 } catch (Exception e) { 104 getLogger().error("Could not set base URL for svg", e); 105 } 106 107 Element e = ((SVGDocument)doc).getRootElement(); 108 final float ptmm = getUserAgent().getSourcePixelUnitToMillimeter(); 109 SVGContext dc = new SVGContext() { 111 public float getPixelToMM() { 112 return ptmm; 113 } 114 public float getPixelUnitToMillimeter() { 115 return ptmm; 116 } 117 118 public Rectangle2D getBBox() { 119 return new Rectangle2D.Double (0, 0, view.getX(), view.getY()); 120 } 121 122 125 public AffineTransform getScreenTransform() { 126 throw new UnsupportedOperationException ("NYI"); 127 } 128 129 133 public void setScreenTransform(AffineTransform at) { 134 throw new UnsupportedOperationException ("NYI"); 135 } 136 137 public AffineTransform getCTM() { 138 return new AffineTransform (); 139 } 140 141 public AffineTransform getGlobalTransform() { 142 return new AffineTransform (); 143 } 144 145 public float getViewportWidth() { 146 return (float)view.getX(); 147 } 148 149 public float getViewportHeight() { 150 return (float)view.getY(); 151 } 152 153 public float getFontSize() { 154 return 12; 155 } 156 157 public void deselectAll() { 158 } 159 }; 160 ((SVGOMElement)e).setSVGContext(dc); 161 162 e.setAttributeNS(XMLSupport.XMLNS_NAMESPACE_URI, "xmlns", 164 SVGDOMImplementation.SVG_NAMESPACE_URI); 165 int fontSize = 12; 167 Point2D p2d = getSize(fontSize, svgRoot, getUserAgent().getSourcePixelUnitToMillimeter()); 168 ((SVGOMElement)e).setSVGContext(null); 169 170 return p2d; 171 } 172 173 private void init() { 174 DOMImplementation impl = SVGDOMImplementation.getDOMImplementation(); 175 String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI; 176 doc = impl.createDocument(svgNS, "svg", null); 177 178 element = doc.getDocumentElement(); 179 180 buildTopLevel(doc, element); 181 } 182 183 190 public static Point2D getSize(int size, Element svgRoot, float ptmm) { 191 String str; 192 UnitProcessor.Context ctx; 193 ctx = new PDFUnitContext(size, svgRoot, ptmm); 194 str = svgRoot.getAttributeNS(null, SVGConstants.SVG_WIDTH_ATTRIBUTE); 195 if (str.length() == 0) { 196 str = "100%"; 197 } 198 float width = UnitProcessor.svgHorizontalLengthToUserSpace 199 (str, SVGConstants.SVG_WIDTH_ATTRIBUTE, ctx); 200 201 str = svgRoot.getAttributeNS(null, SVGConstants.SVG_HEIGHT_ATTRIBUTE); 202 if (str.length() == 0) { 203 str = "100%"; 204 } 205 float height = UnitProcessor.svgVerticalLengthToUserSpace 206 (str, SVGConstants.SVG_HEIGHT_ATTRIBUTE, ctx); 207 return new Point2D.Float (width, height); 208 } 209 210 216 public static class PDFUnitContext implements UnitProcessor.Context { 217 218 219 private Element e; 220 private int fontSize; 221 private float pixeltoMM; 222 223 229 public PDFUnitContext(int size, Element e, float ptmm) { 230 this.e = e; 231 this.fontSize = size; 232 this.pixeltoMM = ptmm; 233 } 234 235 239 public Element getElement() { 240 return e; 241 } 242 243 249 public UnitProcessor.Context getParentElementContext() { 250 return null; 251 } 252 253 257 public float getPixelToMM() { 258 return pixeltoMM; 259 } 260 261 265 public float getPixelUnitToMillimeter() { 266 return pixeltoMM; 267 } 268 269 273 public float getFontSize() { 274 return fontSize; 275 } 276 277 281 public float getXHeight() { 282 return 0.5f; 283 } 284 285 289 public float getViewportWidth() { 290 return 100; 291 } 292 293 297 public float getViewportHeight() { 298 return 100; 299 } 300 } 301 302 } 303 304 | Popular Tags |