1 51 package org.apache.fop.svg; 52 53 import org.apache.fop.fo.*; 55 import org.apache.fop.layout.Area; 56 import org.apache.fop.layout.FontState; 57 import org.apache.fop.apps.FOPException; 58 import org.apache.fop.layout.inline.*; 59 import org.apache.fop.configuration.Configuration; 60 61 import org.apache.batik.dom.svg.*; 62 import org.apache.batik.dom.util.XMLSupport; 63 import org.w3c.dom.*; 64 import org.w3c.dom.svg.*; 65 import org.apache.batik.bridge.*; 66 67 import org.w3c.dom.DOMImplementation ; 68 import org.apache.batik.dom.svg.SVGDOMImplementation; 69 70 import java.net.URL ; 71 import java.awt.geom.AffineTransform ; 72 import java.awt.geom.Rectangle2D ; 73 74 77 public class SVGElement extends SVGObj { 78 79 82 public static class Maker extends FObj.Maker { 83 84 92 public FObj make(FObj parent, PropertyList propertyList, 93 String systemId, int line, int column) 94 throws FOPException { 95 return new SVGElement(parent, propertyList, 96 systemId, line, column); 97 } 98 } 99 100 105 public static FObj.Maker maker() { 106 return new SVGElement.Maker(); 107 } 108 109 FontState fs; 110 111 117 public SVGElement(FObj parent, PropertyList propertyList, 118 String systemId, int line, int column) { 119 super(parent, propertyList, "svg", systemId, line, column); 120 init(); 121 } 122 123 130 public int layout(final Area area) throws FOPException { 131 132 if (!(area instanceof ForeignObjectArea)) { 133 throw new FOPException("SVG not in fo:instream-foreign-object"); 135 } 136 137 if (this.marker == START) { 138 this.fs = area.getFontState(); 139 140 this.marker = 0; 141 } 142 143 final Element svgRoot = element; 144 145 146 final ForeignObjectArea foa = (ForeignObjectArea)area; 147 SVGContext dc = new SVGContext() { 148 public float getPixelToMM() { 149 return 0.35277777777777777778f; 151 } 152 153 public float getPixelUnitToMillimeter() { 154 return 0.35277777777777777778f; 156 } 157 158 public Rectangle2D getBBox() { 159 return new Rectangle2D.Double (0, 0, foa.getContentWidth(), foa.getContentHeight()); 160 } 161 162 165 public AffineTransform getScreenTransform() { 166 throw new UnsupportedOperationException ("NYI"); 167 } 168 169 173 public void setScreenTransform(AffineTransform at) { 174 throw new UnsupportedOperationException ("NYI"); 175 } 176 177 public AffineTransform getCTM() { 178 return new AffineTransform (); 179 } 180 181 public AffineTransform getGlobalTransform() { 182 return new AffineTransform (); 183 } 184 185 public float getViewportWidth() { 186 return (float)foa.getContentWidth(); 187 } 188 189 public float getViewportHeight() { 190 return (float)foa.getContentHeight(); 191 } 192 193 public float getFontSize(){ 194 return fs.getFontSize() / 1000f; 195 } 196 }; 197 ((SVGOMElement)svgRoot).setSVGContext(dc); 198 199 try { 200 URL baseURL = Configuration.getBaseURL(); 201 if (baseURL != null) { 202 ((SVGOMDocument)doc).setURLObject(baseURL); 203 } 204 } catch (Exception e) { 205 log.error("Could not set base URL for svg", e); 206 } 207 208 Element e = ((SVGDocument)doc).getRootElement(); 209 210 e.setAttributeNS(XMLSupport.XMLNS_NAMESPACE_URI, "xmlns", SVGDOMImplementation.SVG_NAMESPACE_URI); 212 214 String s; 215 SVGUserAgent userAgent = new SVGUserAgent(new AffineTransform ()); 216 userAgent.setLogger(log); 217 BridgeContext ctx = new BridgeContext(userAgent); 218 UnitProcessor.Context uctx = UnitProcessor.createContext(ctx, e); 219 220 s = e.getAttributeNS(null, SVGOMDocument.SVG_WIDTH_ATTRIBUTE); 222 if (s.length() == 0) { 223 s = SVGOMDocument.SVG_SVG_WIDTH_DEFAULT_VALUE; 224 } 225 float width = UnitProcessor.svgHorizontalLengthToUserSpace(s, SVGOMDocument.SVG_WIDTH_ATTRIBUTE, uctx); 226 227 s = e.getAttributeNS(null, SVGOMDocument.SVG_HEIGHT_ATTRIBUTE); 229 if (s.length() == 0) { 230 s = SVGOMDocument.SVG_SVG_HEIGHT_DEFAULT_VALUE; 231 } 232 float height = UnitProcessor.svgVerticalLengthToUserSpace(s, SVGOMDocument.SVG_HEIGHT_ATTRIBUTE, uctx); 233 234 SVGArea svg = new SVGArea(fs, width, height); 235 svg.setSVGDocument(doc); 236 svg.start(); 237 238 239 svg.end(); 240 241 242 foa.setObject(svg); 243 foa.setIntrinsicWidth(svg.getWidth()); 244 foa.setIntrinsicHeight(svg.getHeight()); 245 246 ((SVGOMElement)svgRoot).setSVGContext(null); 247 248 249 return Status.OK; 250 } 251 252 private void init() { 253 DOMImplementation impl = SVGDOMImplementation.getDOMImplementation(); 254 String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI; 255 doc = impl.createDocument(svgNS, "svg", null); 256 257 element = doc.getDocumentElement(); 258 259 buildTopLevel(doc, element); 260 } 261 262 } 263 | Popular Tags |