aboutsummaryrefslogtreecommitdiffstats
path: root/examples/mathml/src/org/apache
diff options
context:
space:
mode:
authorJeremias Maerki <jeremias@apache.org>2003-01-29 16:25:25 +0000
committerJeremias Maerki <jeremias@apache.org>2003-01-29 16:25:25 +0000
commit3bb877507c6bbeb32ca72d0aa2196f2203e6de09 (patch)
treefe9a69b9d2421583735732462c3fe27ca8153677 /examples/mathml/src/org/apache
parent5b2e97ba5b0fc57ba7e5edf1acdba8da31065eef (diff)
downloadxmlgraphics-fop-3bb877507c6bbeb32ca72d0aa2196f2203e6de09.tar.gz
xmlgraphics-fop-3bb877507c6bbeb32ca72d0aa2196f2203e6de09.zip
Move over from contrib/mathml
git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@195908 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'examples/mathml/src/org/apache')
-rw-r--r--examples/mathml/src/org/apache/fop/mathml/MathMLElement.java124
-rw-r--r--examples/mathml/src/org/apache/fop/mathml/MathMLElementMapping.java87
-rw-r--r--examples/mathml/src/org/apache/fop/mathml/MathMLObj.java26
3 files changed, 237 insertions, 0 deletions
diff --git a/examples/mathml/src/org/apache/fop/mathml/MathMLElement.java b/examples/mathml/src/org/apache/fop/mathml/MathMLElement.java
new file mode 100644
index 000000000..066b7b54c
--- /dev/null
+++ b/examples/mathml/src/org/apache/fop/mathml/MathMLElement.java
@@ -0,0 +1,124 @@
+/* $Id$
+ * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
+ * For details on use and redistribution please refer to the
+ * LICENSE file included with these sources.
+ */
+
+package org.apache.fop.mathml;
+
+import java.awt.Color;
+import java.awt.Dimension;
+import java.awt.geom.Point2D;
+
+import org.apache.fop.fo.FONode;
+import org.apache.fop.apps.FOPException;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.*;
+import org.xml.sax.Attributes;
+
+import org.apache.batik.svggen.SVGGraphics2D;
+import org.apache.batik.dom.svg.SVGDOMImplementation;
+
+import net.sourceforge.jeuclid.MathBase;
+import net.sourceforge.jeuclid.DOMMathBuilder;
+
+public class MathMLElement extends MathMLObj {
+ Document svgDoc = null;
+ float width;
+ float height;
+ boolean converted = false;
+
+ public MathMLElement(FONode parent) {
+ super(parent);
+ }
+
+ public void handleAttrs(Attributes attlist) throws FOPException {
+ super.handleAttrs(attlist);
+ createBasicDocument();
+ }
+
+ public void convertToSVG() {
+ try {
+ if (!converted) {
+ converted = true;
+ String fontname = "Helvetica";
+ int fontstyle = 0;
+ int inlinefontstyle = 0;
+ int displayfontsize = 12;
+ int inlinefontsize = 12;
+
+ MathBase base = new MathBase(
+ (new DOMMathBuilder(doc)).getMathRootElement(),
+ fontname, fontstyle, inlinefontsize,
+ displayfontsize);
+
+ base.setDebug(false);
+
+ svgDoc = createSVG(base);
+
+ width = base.getWidth();
+ height = base.getHeight();
+
+ doc = svgDoc;
+ }
+ } catch (Throwable t) {
+ userAgent.getLogger().error("Could not convert MathML to SVG", t);
+ width = 0;
+ height = 0;
+ }
+
+ }
+
+ public static Document createSVG(MathBase base) {
+
+ DOMImplementation impl =
+ SVGDOMImplementation.getDOMImplementation();
+ String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
+ Document svgdocument = impl.createDocument(svgNS, "svg", null);
+
+ SVGGraphics2D g = new SVGGraphics2D(svgdocument);
+
+ g.setSVGCanvasSize(
+ new Dimension(base.getWidth(), base.getHeight()));
+
+ //g.setColor(Color.white);
+ //g.fillRect(0, 0, base.getWidth(), base.getHeight());
+ g.setColor(Color.black);
+
+ base.paint(g);
+
+ //if (antialiasing)
+ //element.setAttribute("text-rendering", "optimizeLegibility");
+ //else
+ //element.setAttribute("text-rendering", "geometricPrecision");
+
+ // this should be done in a better way
+ Element root = g.getRoot();
+ svgdocument = impl.createDocument(svgNS, "svg", null);
+ Node node = svgdocument.importNode(root, true);
+ ((org.apache.batik.dom.svg.SVGOMDocument) svgdocument).
+ getRootElement().appendChild(node);
+
+ return svgdocument;
+
+ }
+
+ public Document getDocument() {
+ convertToSVG();
+ return doc;
+ }
+
+ public String getDocumentNamespace() {
+ if (svgDoc == null) {
+ return MathMLElementMapping.URI;
+ }
+ return "http://www.w3.org/2000/svg";
+ }
+
+ public Point2D getDimension(Point2D view) {
+ convertToSVG();
+ return new Point2D.Float(width, height);
+ }
+}
+
diff --git a/examples/mathml/src/org/apache/fop/mathml/MathMLElementMapping.java b/examples/mathml/src/org/apache/fop/mathml/MathMLElementMapping.java
new file mode 100644
index 000000000..f1f2b0fd3
--- /dev/null
+++ b/examples/mathml/src/org/apache/fop/mathml/MathMLElementMapping.java
@@ -0,0 +1,87 @@
+/* $Id$
+ * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
+ * For details on use and redistribution please refer to the
+ * LICENSE file included with these sources.
+ */
+
+package org.apache.fop.mathml;
+
+import org.apache.fop.fo.FOTreeBuilder;
+import org.apache.fop.fo.FONode;
+import org.apache.fop.fo.ElementMapping;
+import org.apache.fop.image.analyser.XMLReader;
+import org.apache.fop.image.FopImage;
+import org.w3c.dom.Document;
+
+import java.util.HashMap;
+
+import net.sourceforge.jeuclid.MathBase;
+import net.sourceforge.jeuclid.DOMMathBuilder;
+
+public class MathMLElementMapping implements ElementMapping {
+
+ public static final String URI = "http://www.w3.org/1998/Math/MathML";
+
+ private static HashMap foObjs = null;
+
+ private static synchronized void setupMathML() {
+ if (foObjs == null) {
+ foObjs = new HashMap();
+ foObjs.put("math", new ME());
+ foObjs.put(DEFAULT, new MathMLMaker());
+
+ XMLReader.setConverter(URI, new MathMLConverter());
+ }
+ }
+
+ public void addToBuilder(FOTreeBuilder builder) {
+ setupMathML();
+ builder.addMapping(URI, foObjs);
+ }
+
+ static class MathMLMaker extends ElementMapping.Maker {
+ public FONode make(FONode parent) {
+ return new MathMLObj(parent);
+ }
+ }
+
+ static class ME extends ElementMapping.Maker {
+ public FONode make(FONode parent) {
+ return new MathMLElement(parent);
+ }
+ }
+
+ static class MathMLConverter implements XMLReader.Converter {
+ public FopImage.ImageInfo convert(Document doc) {
+ try {
+ FopImage.ImageInfo info = new FopImage.ImageInfo();
+ String fontname = "Helvetica";
+ int fontstyle = 0;
+ int inlinefontstyle = 0;
+ int inlinefontsize = 12;
+ int displayfontsize = 12;
+
+ MathBase base = new MathBase(
+ (new DOMMathBuilder(doc)).getMathRootElement(),
+ fontname, fontstyle, inlinefontsize,
+ displayfontsize);
+
+ base.setDebug(false);
+
+ info.data = MathMLElement.createSVG(base);
+
+ info.width = base.getWidth();
+ info.height = base.getHeight();
+
+ info.mimeType = "image/svg+xml";
+ info.str = "http://www.w3.org/2000/svg";
+
+ return info;
+ } catch (Throwable t) {
+ }
+ return null;
+
+ }
+ }
+
+}
diff --git a/examples/mathml/src/org/apache/fop/mathml/MathMLObj.java b/examples/mathml/src/org/apache/fop/mathml/MathMLObj.java
new file mode 100644
index 000000000..f7f8d82ca
--- /dev/null
+++ b/examples/mathml/src/org/apache/fop/mathml/MathMLObj.java
@@ -0,0 +1,26 @@
+/* $Id$
+ * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
+ * For details on use and redistribution please refer to the
+ * LICENSE file included with these sources.
+ */
+
+package org.apache.fop.mathml;
+
+// FOP
+import org.apache.fop.fo.FONode;
+import org.apache.fop.fo.XMLObj;
+
+/**
+ * Catch all MathML object as default element.
+ */
+public class MathMLObj extends XMLObj {
+
+ public MathMLObj(FONode parent) {
+ super(parent);
+ }
+
+ public String getNameSpace() {
+ return MathMLElementMapping.URI;
+ }
+}
+