KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > mathml > MathMLElement


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 /* $Id: MathMLElement.java 426576 2006-07-28 15:44:37Z jeremias $ */
19  
20 package org.apache.fop.mathml;
21
22 import java.awt.Color JavaDoc;
23 import java.awt.Dimension JavaDoc;
24 import java.awt.geom.Point2D JavaDoc;
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 JavaDoc;
30 import org.w3c.dom.Document JavaDoc;
31 import org.w3c.dom.Element JavaDoc;
32 import org.w3c.dom.Node JavaDoc;
33
34 import org.xml.sax.Attributes JavaDoc;
35 import org.xml.sax.Locator JavaDoc;
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 /**
44  * Defines the top-level element for MathML.
45  */

46 public class MathMLElement extends MathMLObj {
47
48     private Document JavaDoc svgDoc = null;
49     private float width;
50     private float height;
51     private boolean converted = false;
52
53     /**
54      * @see org.apache.fop.fo.FONode#FONode(FONode)
55      */

56     public MathMLElement(FONode parent) {
57         super(parent);
58     }
59
60     /**
61      * @see org.apache.fop.fo.FONode#processNode
62      */

63     public void processNode(String JavaDoc elementName,
64                             Locator JavaDoc locator,
65                             Attributes JavaDoc attlist,
66                             PropertyList propertyList) throws FOPException {
67         super.processNode(elementName, locator, attlist, propertyList);
68         createBasicDocument();
69     }
70
71     /**
72      * Converts the MathML to SVG.
73      */

74     public void convertToSVG() {
75         try {
76             if (!converted) {
77                 converted = true;
78                 String JavaDoc fontname = "Helvetica";
79                 int fontstyle = 0;
80                 //int inlinefontstyle = 0;
81
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 JavaDoc t) {
99             getLogger().error("Could not convert MathML to SVG", t);
100             width = 0;
101             height = 0;
102         }
103
104     }
105
106     /**
107      * Create the SVG from MathML.
108      * @param base the root element
109      * @return the DOM document containing SVG
110      */

111     public static Document JavaDoc createSVG(MathBase base) {
112
113         DOMImplementation JavaDoc impl = SVGDOMImplementation.getDOMImplementation();
114         String JavaDoc svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
115         Document JavaDoc svgdocument = impl.createDocument(svgNS, "svg", null);
116
117         SVGGraphics2D g = new SVGGraphics2D(svgdocument);
118
119         g.setSVGCanvasSize(
120           new Dimension JavaDoc(base.getWidth(), base.getHeight()));
121
122         //g.setColor(Color.white);
123
//g.fillRect(0, 0, base.getWidth(), base.getHeight());
124
g.setColor(Color.black);
125
126         base.paint(g);
127
128         //if (antialiasing)
129
//element.setAttribute("text-rendering", "optimizeLegibility");
130
//else
131
//element.setAttribute("text-rendering", "geometricPrecision");
132

133         // this should be done in a better way
134
Element JavaDoc 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     /** @see org.apache.fop.fo.XMLObj#getDOMDocument() */
145     public Document JavaDoc getDOMDocument() {
146         convertToSVG();
147         return doc;
148     }
149
150     /**
151      * @see org.apache.fop.fo.FONode#getNamespaceURI()
152      */

153     public String JavaDoc getNamespaceURI() {
154         if (svgDoc == null) {
155             return MathMLElementMapping.NAMESPACE;
156         }
157         return "http://www.w3.org/2000/svg";
158     }
159
160     /**
161      * @see org.apache.fop.fo.XMLObj#getDimension(Point2D)
162      */

163     public Point2D JavaDoc getDimension(Point2D JavaDoc view) {
164         convertToSVG();
165         return new Point2D.Float JavaDoc(width, height);
166     }
167 }
168
169
Popular Tags