From 3bb877507c6bbeb32ca72d0aa2196f2203e6de09 Mon Sep 17 00:00:00 2001 From: Jeremias Maerki Date: Wed, 29 Jan 2003 16:25:25 +0000 Subject: Move over from contrib/mathml git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@195908 13f79535-47bb-0310-9956-ffa450edef68 --- examples/mathml/.cvsignore | 1 + examples/mathml/README | 10 ++ examples/mathml/build.bat | 35 +++++ examples/mathml/build.sh | 30 ++++ examples/mathml/build.xml | 139 +++++++++++++++++ examples/mathml/docs/example.mml | 63 ++++++++ examples/mathml/docs/mathml.fo | 172 +++++++++++++++++++++ examples/mathml/lib/readme.txt | 1 + .../services/org.apache.fop.fo.ElementMapping | 2 + .../src/org/apache/fop/mathml/MathMLElement.java | 124 +++++++++++++++ .../apache/fop/mathml/MathMLElementMapping.java | 87 +++++++++++ .../src/org/apache/fop/mathml/MathMLObj.java | 26 ++++ 12 files changed, 690 insertions(+) create mode 100644 examples/mathml/.cvsignore create mode 100644 examples/mathml/README create mode 100755 examples/mathml/build.bat create mode 100644 examples/mathml/build.sh create mode 100644 examples/mathml/build.xml create mode 100644 examples/mathml/docs/example.mml create mode 100644 examples/mathml/docs/mathml.fo create mode 100644 examples/mathml/lib/readme.txt create mode 100644 examples/mathml/resources/META-INF/services/org.apache.fop.fo.ElementMapping create mode 100644 examples/mathml/src/org/apache/fop/mathml/MathMLElement.java create mode 100644 examples/mathml/src/org/apache/fop/mathml/MathMLElementMapping.java create mode 100644 examples/mathml/src/org/apache/fop/mathml/MathMLObj.java diff --git a/examples/mathml/.cvsignore b/examples/mathml/.cvsignore new file mode 100644 index 000000000..378eac25d --- /dev/null +++ b/examples/mathml/.cvsignore @@ -0,0 +1 @@ +build diff --git a/examples/mathml/README b/examples/mathml/README new file mode 100644 index 000000000..72860b124 --- /dev/null +++ b/examples/mathml/README @@ -0,0 +1,10 @@ +FOP MathML Readme + +This directory contains an extension for MathML that can be used with FOP. + +This extension works with JEuclid which is available from: +http://sourceforge.net/projects/jeuclid/ + +This simply uses JEuclid to convert the MathML CML into SVG. +The SVG is then handled as normal. + diff --git a/examples/mathml/build.bat b/examples/mathml/build.bat new file mode 100755 index 000000000..96aa30a7f --- /dev/null +++ b/examples/mathml/build.bat @@ -0,0 +1,35 @@ +@echo off + +echo MathML Build System +echo --------------------- + +if "%JAVA_HOME%" == "" goto error + +set LIBDIR=..\..\lib +set LOCALCLASSPATH=%JAVA_HOME%\lib\tools.jar;%JAVA_HOME%\lib\classes.zip +set LOCALCLASSPATH=%LOCALCLASSPATH%;%LIBDIR%\ant.jar +set LOCALCLASSPATH=%LOCALCLASSPATH%;%LIBDIR%\xml-apis.jar +set LOCALCLASSPATH=%LOCALCLASSPATH%;%LIBDIR%\xercesImpl-2.2.1.jar +set LOCALCLASSPATH=%LOCALCLASSPATH%;%LIBDIR%\xalan-2.4.1.jar + +set ANT_HOME=%LIBDIR% + +echo Building with classpath %LOCALCLASSPATH% + +echo Starting Ant... + +%JAVA_HOME%\bin\java.exe -Dant.home=%ANT_HOME% -classpath "%LOCALCLASSPATH%" org.apache.tools.ant.Main %1 %2 %3 %4 %5 + +goto end + +:error + +echo ERROR: JAVA_HOME not found in your environment. +echo Please, set the JAVA_HOME variable in your environment to match the +echo location of the Java Virtual Machine you want to use. + +:end + +rem set LOCALCLASSPATH= + +pause \ No newline at end of file diff --git a/examples/mathml/build.sh b/examples/mathml/build.sh new file mode 100644 index 000000000..986d3c1e3 --- /dev/null +++ b/examples/mathml/build.sh @@ -0,0 +1,30 @@ +#!/bin/sh +echo +echo "MathML Build System" +echo "---------------------" +echo + +if [ "$JAVA_HOME" = "" ] ; then + echo "ERROR: JAVA_HOME not found in your environment." + echo + echo "Please, set the JAVA_HOME variable in your environment to match the" + echo "location of the Java Virtual Machine you want to use." + exit 1 +fi +LIBDIR=../../lib +LOCALCLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/classes.zip +LOCALCLASSPATH=$LOCALCLASSPATH:$LIBDIR/ant.jar +LOCALCLASSPATH=$LOCALCLASSPATH:$LIBDIR/xml-apis.jar +LOCALCLASSPATH=$LOCALCLASSPATH:$LIBDIR/xercesImpl-2.2.1.jar +LOCALCLASSPATH=$LOCALCLASSPATH:$LIBDIR/xalan-2.4.1.jar +ANT_HOME=$LIBDIR + +echo +CP=$LOCALCLASSPATH +echo Building with classpath $CP + + +echo Starting Ant... +echo + +$JAVA_HOME/bin/java -Dant.home=$ANT_HOME -classpath "$CP" org.apache.tools.ant.Main $* diff --git a/examples/mathml/build.xml b/examples/mathml/build.xml new file mode 100644 index 000000000..78e84b514 --- /dev/null +++ b/examples/mathml/build.xml @@ -0,0 +1,139 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/examples/mathml/docs/example.mml b/examples/mathml/docs/example.mml new file mode 100644 index 000000000..0f04968c2 --- /dev/null +++ b/examples/mathml/docs/example.mml @@ -0,0 +1,63 @@ + + + + { + + + + + + + + a + , + ... + , + a + + + + + k +   + a + 's + + + + , + + + + + + b + , + ... + , + b + + + + + +   + b + 's + + + + + + + + k + + + +  elements + + + + } + + diff --git a/examples/mathml/docs/mathml.fo b/examples/mathml/docs/mathml.fo new file mode 100644 index 000000000..a5ff53f26 --- /dev/null +++ b/examples/mathml/docs/mathml.fo @@ -0,0 +1,172 @@ + + + + + + + + + + + + + Plan Extension + + + Page + + + + + + The MathML Extension + + + +The mathml extension is an addition to FOP that can be used to draw mathml +expressions. + + + + Examples + + + + + + + + I + + ( + x + ) + + = + F + ( + x + ) + + + C + + + + + + + + + + + + ( + + + + + ( + + + + a + + + b + + + + + c + + + d + + + + ) + + + + + ( + + + + e + + + f + + + + + g + + + h + + + + ) + + + + + + 0 + + + + ( + + + + i + + + j + + + + + k + + + l + + + + ) + + + + + ) + + + + + + + + +An external mathml document. + + + + + + + + + diff --git a/examples/mathml/lib/readme.txt b/examples/mathml/lib/readme.txt new file mode 100644 index 000000000..9166db6f7 --- /dev/null +++ b/examples/mathml/lib/readme.txt @@ -0,0 +1 @@ +Put jeuclid.jar in this directory! diff --git a/examples/mathml/resources/META-INF/services/org.apache.fop.fo.ElementMapping b/examples/mathml/resources/META-INF/services/org.apache.fop.fo.ElementMapping new file mode 100644 index 000000000..406a8319a --- /dev/null +++ b/examples/mathml/resources/META-INF/services/org.apache.fop.fo.ElementMapping @@ -0,0 +1,2 @@ +org.apache.fop.mathml.MathMLElementMapping + 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; + } +} + -- cgit v1.2.3