1 17 18 19 20 package org.apache.fop.mathml; 21 22 import java.awt.Color ; 23 import java.awt.Dimension ; 24 import java.awt.geom.Point2D ; 25 26 import org.apache.fop.apps.FOPException; 27 import org.apache.fop.fo.FONode; 28 import org.apache.fop.fo.PropertyList; 29 import org.w3c.dom.DOMImplementation ; 30 import org.w3c.dom.Document ; 31 import org.w3c.dom.Element ; 32 import org.w3c.dom.Node ; 33 34 import org.xml.sax.Attributes ; 35 import org.xml.sax.Locator ; 36 37 import org.apache.batik.svggen.SVGGraphics2D; 38 import org.apache.batik.dom.svg.SVGDOMImplementation; 39 40 import net.sourceforge.jeuclid.MathBase; 41 import net.sourceforge.jeuclid.DOMMathBuilder; 42 43 46 public class MathMLElement extends MathMLObj { 47 48 private Document svgDoc = null; 49 private float width; 50 private float height; 51 private boolean converted = false; 52 53 56 public MathMLElement(FONode parent) { 57 super(parent); 58 } 59 60 63 public void processNode(String elementName, 64 Locator locator, 65 Attributes attlist, 66 PropertyList propertyList) throws FOPException { 67 super.processNode(elementName, locator, attlist, propertyList); 68 createBasicDocument(); 69 } 70 71 74 public void convertToSVG() { 75 try { 76 if (!converted) { 77 converted = true; 78 String fontname = "Helvetica"; 79 int fontstyle = 0; 80 int displayfontsize = 12; 82 int inlinefontsize = 12; 83 84 MathBase base = new MathBase( 85 (new DOMMathBuilder(doc)).getMathRootElement(), 86 fontname, fontstyle, inlinefontsize, 87 displayfontsize); 88 89 base.setDebug(false); 90 91 svgDoc = createSVG(base); 92 93 width = base.getWidth(); 94 height = base.getHeight(); 95 96 doc = svgDoc; 97 } 98 } catch (Throwable t) { 99 getLogger().error("Could not convert MathML to SVG", t); 100 width = 0; 101 height = 0; 102 } 103 104 } 105 106 111 public static Document createSVG(MathBase base) { 112 113 DOMImplementation impl = SVGDOMImplementation.getDOMImplementation(); 114 String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI; 115 Document svgdocument = impl.createDocument(svgNS, "svg", null); 116 117 SVGGraphics2D g = new SVGGraphics2D(svgdocument); 118 119 g.setSVGCanvasSize( 120 new Dimension (base.getWidth(), base.getHeight())); 121 122 g.setColor(Color.black); 125 126 base.paint(g); 127 128 133 Element root = g.getRoot(); 135 svgdocument = impl.createDocument(svgNS, "svg", null); 136 Node node = svgdocument.importNode(root, true); 137 ((org.apache.batik.dom.svg.SVGOMDocument) svgdocument). 138 getRootElement().appendChild(node); 139 140 return svgdocument; 141 142 } 143 144 145 public Document getDOMDocument() { 146 convertToSVG(); 147 return doc; 148 } 149 150 153 public String getNamespaceURI() { 154 if (svgDoc == null) { 155 return MathMLElementMapping.NAMESPACE; 156 } 157 return "http://www.w3.org/2000/svg"; 158 } 159 160 163 public Point2D getDimension(Point2D view) { 164 convertToSVG(); 165 return new Point2D.Float (width, height); 166 } 167 } 168 169 | Popular Tags |