Browse Source

Removed MathML example as per discussion on fop-dev mailing list

git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@736295 13f79535-47bb-0310-9956-ffa450edef68
tags/fop-1_0
Maximilian Berger 15 years ago
parent
commit
cc2a5e8e3c

+ 5
- 16
examples/mathml/README View File

@@ -1,21 +1,10 @@
FOP MathML Readme

This directory contains an extension for MathML that can be used with FOP.
The MathML extension is now maintained by the JEuclid project:

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.
http://jeuclid.sourceforge.net/jeuclid-fop/

Building the MathML extension:
To build the MathML extension, please download JEuclid using the link above.
Put the jeuclid.jar into the lib directory and run the Ant build script in
this directory.
Further information:

Usage:
When you've built the MathML extension, add jeuclid.jar and
build/mathml-fop.jar to the classpath used when running FOP. You can then
simply use MathML directly in fo:instream-foreign-object elements or
reference a MathML file in fo:external-graphic elements as shown in
the demo file: docs/mathml.fo
http://jeuclid.sourceforge.net/
http://sourceforge.net/projects/jeuclid/

+ 0
- 155
examples/mathml/build.xml View File

@@ -1,155 +0,0 @@
<?xml version="1.0"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!-- $Id$ -->
<!-- ===========================================================================

============================================================================ -->

<project default="package" basedir=".">

<path id="libs-build-classpath">
<fileset dir="../../lib">
<include name="*.jar"/>
</fileset>
<fileset dir="../../build">
<include name="fop.jar"/>
</fileset>
<fileset dir="lib">
<include name="*.jar"/>
</fileset>
</path>

<!-- =================================================================== -->
<!-- Initialization target -->
<!-- =================================================================== -->
<target name="init">
<tstamp/>
<property name="Name" value="MathML-FOP"/>
<property name="name" value="mathml-fop"/>
<property name="version" value="0.1-CVS"/>
<filter token="version" value="${version}"/>
<property name="year" value="2002-2004"/>

<echo message="------------------- ${Name} ${version} [${year}] ----------------"/>

<property name="build.compiler" value="classic"/>
<property name="debug" value="on"/>
<property name="optimize" value="off"/>
<property name="deprecation" value="on"/>

<property name="src.dir" value="./src"/>
<property name="lib.dir" value="./lib"/>
<property name="packages" value="org.apache.fop.*"/>

<property name="build.dir" value="./build"/>
<property name="build.src" value="./build/src"/>
<property name="build.dest" value="./build/classes"/>

<property name="resource.dir" value="resources"/>
<property name="mathml.dir" value="org/apache/fop/mathml"/>

<available property="jeuclid.present" classname="net.sourceforge.jeuclid.MathBase" classpathref="libs-build-classpath"/>
</target>
<target name="jeuclid-check" depends="init" unless="jeuclid.present">
<echo message="============================================================================="/>
<echo message="jeuclid.jar is missing in the lib directory."/>
<echo message="You can download it at: http://sourceforge.net/projects/jeuclid/"/>
<echo message="============================================================================="/>
<fail message="Dependecy check failed."/>
</target>

<!-- =================================================================== -->
<!-- Help on usage -->
<!-- =================================================================== -->
<target name="usage">
<echo message="Use the -projecthelp option instead"/>
</target>

<!-- =================================================================== -->
<!-- Prepares the build directory -->
<!-- =================================================================== -->
<target name="prepare" depends="init, jeuclid-check">
<!-- create directories -->
<echo message="Preparing the build directories"/>
<mkdir dir="${build.dir}"/>
<mkdir dir="${build.src}"/>
<mkdir dir="${build.src}/${mathml.dir}"/>
</target>

<!-- =================================================================== -->
<!-- Prepares the source code -->
<!-- =================================================================== -->
<target name="prepare-src" depends="prepare">
<!-- copy src files -->
<copy todir="${build.src}">
<fileset dir="${src.dir}"
excludes="**/Makefile*, **/package.html"/>
</copy>
<copy todir="${build.dest}/">
<fileset dir="${resource.dir}"/>
</copy>
</target>

<!-- =================================================================== -->
<!-- Compiles the source directory -->
<!-- =================================================================== -->
<target name="compile" depends="prepare-src" description="Compile the Java sources">
<echo message="Compiling the sources "/>
<!-- create directories -->
<mkdir dir="${build.dest}"/>

<javac srcdir="${build.src}"
destdir="${build.dest}"
debug="${debug}"
deprecation="${deprecation}"
optimize="${optimize}"
excludes="">
<classpath refid="libs-build-classpath"/>
</javac>
</target>

<!-- =================================================================== -->
<!-- Creates the class package -->
<!-- =================================================================== -->
<target name="package" depends="compile" description="Create jar file">
<echo message="Creating the jar file ${build.dir}/${name}.jar"/>
<tstamp>
<format property="ts" pattern="yyyyMMdd-HHmmss-z"/>
</tstamp>

<jar jarfile="${build.dir}/${name}.jar"
basedir="${build.dest}"
includes="**">
<manifest>
<attribute name="Implementation-Title" value="${Name}"/>
<attribute name="Implementation-Version" value="${version}"/>
<attribute name="Implementation-Vendor" value="Apache Software Foundation (http://xmlgraphics.apache.org/fop/)"/>
<attribute name="Build-Id" value="${ts} (${user.name} [${os.name} ${os.version} ${os.arch}])"/>
</manifest>
</jar>
</target>

<!-- =================================================================== -->
<!-- Clean targets -->
<!-- =================================================================== -->
<target name="clean" depends="init" description="Cleans the build directory">
<delete dir="${build.dir}"/>
</target>

</project>


+ 0
- 63
examples/mathml/docs/example.mml View File

@@ -1,63 +0,0 @@
<?xml version="1.0" encoding="ISO-8859-1"?>
<math xmlns="http://www.w3.org/1998/Math/MathML" mode="display">
<mrow>
<mo stretchy="false">{</mo>
<munder>
<mrow>
<mover>
<mrow>
<mpadded width="0"/>
<!--mpadded width="0">(</mpadded-->
<!-- \mathstrut -ugly hack a la TeX -->
<mi>a</mi>
<mo>,</mo>
<mo>...</mo>
<mo>,</mo>
<mi>a</mi>
</mrow>
<mover>
<mo>&#x0FE37;</mo>
<mrow>
<mi>k</mi>
<mtext>&#x000A0;</mtext>
<mi>a</mi>
<mtext>'s</mtext>
</mrow>
</mover>
</mover>
<mo>,</mo>
<mover>
<mrow>
<mpadded/>
<!--mpadded width="0">(</mpadded-->
<!-- \mathstrut -ugly hack a la TeX -->
<mi>b</mi>
<mo>,</mo>
<mo>...</mo>
<mo>,</mo>
<mi>b</mi>
</mrow>
<mover>
<mo>&#x0FE37;</mo>
<mrow>
<mi>&#x02113;</mi>
<mtext>&#x000A0;</mtext>
<mi>b</mi>
<mtext>'s</mtext>
</mrow>
</mover>
</mover>
</mrow>
<munder>
<mo>&#x0FE38;</mo>
<mrow>
<mi>k</mi>
<mo>+</mo>
<mi>&#x02113;</mi>
<mtext>&#x000A0;elements</mtext>
</mrow>
</munder>
</munder>
<mo stretchy="false">}</mo>
</mrow>
</math>

+ 0
- 172
examples/mathml/docs/mathml.fo View File

@@ -1,172 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
<fo:layout-master-set>
<fo:simple-page-master
margin-right="1.5cm"
margin-left="1.5cm"
margin-bottom="2cm"
margin-top="1cm"
page-width="21cm"
page-height="29.7cm"
master-name="first">
<fo:region-body margin-top="1cm"/>
<fo:region-before extent="1cm"/>
<fo:region-after extent="1.5cm"/>
</fo:simple-page-master>
</fo:layout-master-set>

<fo:page-sequence master-reference="first">
<fo:static-content flow-name="xsl-region-before">
<fo:block line-height="14pt" font-size="10pt"
text-align="end">MathML Extension</fo:block>
</fo:static-content>
<fo:static-content flow-name="xsl-region-after">
<fo:block line-height="14pt" font-size="10pt"
text-align="end">Page <fo:page-number/></fo:block>
</fo:static-content>

<fo:flow flow-name="xsl-region-body">

<fo:block space-before.optimum="3pt" space-after.optimum="15pt">
The MathML Extension
</fo:block>

<fo:block space-before.optimum="3pt" space-after.optimum="15pt">
The mathml extension is an addition to FOP that can be used to draw mathml
expressions.
</fo:block>

<fo:block space-before.optimum="3pt" space-after.optimum="15pt">
Examples
</fo:block>

<fo:block space-before.optimum="3pt" space-after.optimum="15pt">
<fo:block space-after.optimum="5pt">
<fo:instream-foreign-object>
<math xmlns="http://www.w3.org/1998/Math/MathML" mode="inline">
<mrow>
<mi>I</mi>
<mrow>
<mo>(</mo>
<mi>x</mi>
<mo>)</mo>
</mrow>
<mo>=</mo>
<mi>F</mi>
<mo stretchy="false">(</mo>
<mi>x</mi>
<mo stretchy="false">)</mo>
<mo>+</mo>
<mi>C</mi>
</mrow>
</math>
</fo:instream-foreign-object>
</fo:block>
</fo:block>

<fo:block space-before.optimum="3pt" space-after.optimum="15pt">
<fo:block space-after.optimum="5pt">
<fo:instream-foreign-object>
<math xmlns="http://www.w3.org/1998/Math/MathML" mode="inline">
<mrow>
<mo>(</mo>
<mtable>
<mtr>
<mtd columnalign="center">
<mrow>
<mo>(</mo>
<mtable>
<mtr>
<mtd columnalign="center">
<mi>a</mi>
</mtd>
<mtd columnalign="center">
<mi>b</mi>
</mtd>
</mtr>
<mtr>
<mtd columnalign="center">
<mi>c</mi>
</mtd>
<mtd columnalign="center">
<mi>d</mi>
</mtd>
</mtr>
</mtable>
<mo>)</mo>
</mrow>
</mtd>
<mtd columnalign="center">
<mrow>
<mo>(</mo>
<mtable>
<mtr>
<mtd columnalign="center">
<mi>e</mi>
</mtd>
<mtd columnalign="center">
<mi>f</mi>
</mtd>
</mtr>
<mtr>
<mtd columnalign="center">
<mi>g</mi>
</mtd>
<mtd columnalign="center">
<mi>h</mi>
</mtd>
</mtr>
</mtable>
<mo>)</mo>
</mrow>
</mtd>
</mtr>
<mtr>
<mtd columnalign="center">
<mn>0</mn>
</mtd>
<mtd columnalign="center">
<mrow>
<mo>(</mo>
<mtable>
<mtr>
<mtd columnalign="center">
<mi>i</mi>
</mtd>
<mtd columnalign="center">
<mi>j</mi>
</mtd>
</mtr>
<mtr>
<mtd columnalign="center">
<mi>k</mi>
</mtd>
<mtd columnalign="center">
<mi>l</mi>
</mtd>
</mtr>
</mtable>
<mo>)</mo>
</mrow>
</mtd>
</mtr>
</mtable>
<mo>)</mo>
</mrow>
</math>

</fo:instream-foreign-object>
</fo:block>
</fo:block>

<fo:block>
An external mathml document.
</fo:block>

<fo:block>
<fo:external-graphic src="example.mml"/>
</fo:block>
</fo:flow>
</fo:page-sequence>
</fo:root>


+ 0
- 1
examples/mathml/lib/readme.txt View File

@@ -1 +0,0 @@
Put jeuclid.jar in this directory!

+ 0
- 2
examples/mathml/resources/META-INF/services/org.apache.fop.fo.ElementMapping View File

@@ -1,2 +0,0 @@
org.apache.fop.mathml.MathMLElementMapping


+ 0
- 1
examples/mathml/resources/META-INF/services/org.apache.xmlgraphics.image.loader.spi.ImageLoaderFactory View File

@@ -1 +0,0 @@
org.apache.fop.mathml.ImageLoaderFactoryMathML

+ 0
- 1
examples/mathml/resources/META-INF/services/org.apache.xmlgraphics.image.loader.spi.ImagePreloader View File

@@ -1 +0,0 @@
org.apache.fop.mathml.PreloaderMathML

+ 0
- 66
examples/mathml/src/org/apache/fop/mathml/ImageLoaderFactoryMathML.java View File

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

/* $Id$ */

package org.apache.fop.mathml;

import org.apache.xmlgraphics.image.loader.ImageFlavor;
import org.apache.xmlgraphics.image.loader.impl.AbstractImageLoaderFactory;
import org.apache.xmlgraphics.image.loader.spi.ImageLoader;

/**
* Factory class for the ImageLoader for MathML.
*/
public class ImageLoaderFactoryMathML extends AbstractImageLoaderFactory {

/** MathML MIME type */
public static final String MIME_MATHML = "text/mathml";

private static final ImageFlavor[] FLAVORS = new ImageFlavor[] {
ImageFlavor.GRAPHICS2D};

private static final String[] MIMES = new String[] {
MIME_MATHML};

/** {@inheritDoc} */
public String[] getSupportedMIMETypes() {
return MIMES;
}

/** {@inheritDoc} */
public ImageFlavor[] getSupportedFlavors(String mime) {
return FLAVORS;
}

/** {@inheritDoc} */
public ImageLoader newImageLoader(ImageFlavor targetFlavor) {
return new ImageLoaderMathML(targetFlavor);
}

/** {@inheritDoc} */
public int getUsagePenalty(String mime, ImageFlavor flavor) {
return 0;
}

/** {@inheritDoc} */
public boolean isAvailable() {
//TODO Refine!
return true;
}

}

+ 0
- 71
examples/mathml/src/org/apache/fop/mathml/ImageLoaderMathML.java View File

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

/* $Id$ */

package org.apache.fop.mathml;

import java.io.IOException;
import java.util.Map;

import org.apache.xmlgraphics.image.loader.Image;
import org.apache.xmlgraphics.image.loader.ImageException;
import org.apache.xmlgraphics.image.loader.ImageFlavor;
import org.apache.xmlgraphics.image.loader.ImageInfo;
import org.apache.xmlgraphics.image.loader.ImageSessionContext;
import org.apache.xmlgraphics.image.loader.impl.AbstractImageLoader;
import org.apache.xmlgraphics.image.loader.impl.ImageGraphics2D;

/**
* ImageLoader for MathML (using JEuclid).
*/
public class ImageLoaderMathML extends AbstractImageLoader {

private ImageFlavor targetFlavor;

/**
* Main constructor.
* @param targetFlavor the target flavor
*/
public ImageLoaderMathML(ImageFlavor targetFlavor) {
if (!(ImageFlavor.GRAPHICS2D.equals(targetFlavor))) {
throw new IllegalArgumentException("Unsupported target ImageFlavor: " + targetFlavor);
}
this.targetFlavor = targetFlavor;
}

/** {@inheritDoc} */
public ImageFlavor getTargetFlavor() {
return this.targetFlavor;
}

/** {@inheritDoc} */
public Image loadImage(ImageInfo info, Map hints, ImageSessionContext session)
throws ImageException, IOException {
if (!ImageLoaderFactoryMathML.MIME_MATHML.equals(info.getMimeType())) {
throw new IllegalArgumentException("ImageInfo must be from an MathML image");
}
Image img = info.getOriginalImage();
if (!(img instanceof ImageGraphics2D)) {
throw new IllegalArgumentException(
"ImageInfo was expected to contain the MathML document as Graphics2D image");
}
ImageGraphics2D g2dImage = (ImageGraphics2D)img;
return g2dImage;
}

}

+ 0
- 163
examples/mathml/src/org/apache/fop/mathml/MathMLElement.java View File

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

/* $Id$ */

package org.apache.fop.mathml;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.geom.Point2D;

import net.sourceforge.jeuclid.DOMMathBuilder;
import net.sourceforge.jeuclid.MathBase;

import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

import org.xml.sax.Attributes;
import org.xml.sax.Locator;

import org.apache.batik.dom.svg.SVGDOMImplementation;
import org.apache.batik.svggen.SVGGraphics2D;

import org.apache.fop.apps.FOPException;
import org.apache.fop.fo.FONode;
import org.apache.fop.fo.PropertyList;

/**
* Defines the top-level element for MathML.
*/
public class MathMLElement extends MathMLObj {

private Document svgDoc = null;
private float width;
private float height;
private boolean converted = false;

/**
* @see org.apache.fop.fo.FONode#FONode(FONode)
*/
public MathMLElement(FONode parent) {
super(parent);
}

/** {@inheritDoc} */
public void processNode(String elementName,
Locator locator,
Attributes attlist,
PropertyList propertyList) throws FOPException {
super.processNode(elementName, locator, attlist, propertyList);
createBasicDocument();
}

/**
* Converts the MathML to SVG.
*/
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) {
getLogger().error("Could not convert MathML to SVG", t);
width = 0;
height = 0;
}

}

/**
* Create the SVG from MathML.
* @param base the root element
* @return the DOM document containing SVG
*/
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;

}

/** {@inheritDoc} */
public Document getDOMDocument() {
convertToSVG();
return doc;
}

/** {@inheritDoc} */
public String getNamespaceURI() {
if (svgDoc == null) {
return MathMLElementMapping.NAMESPACE;
}
return "http://www.w3.org/2000/svg";
}

/** {@inheritDoc} */
public Point2D getDimension(Point2D view) {
convertToSVG();
return new Point2D.Float(width, height);
}
}


+ 0
- 68
examples/mathml/src/org/apache/fop/mathml/MathMLElementMapping.java View File

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

/* $Id$ */

package org.apache.fop.mathml;

import java.util.HashMap;

import org.w3c.dom.DOMImplementation;

import org.apache.fop.fo.ElementMapping;
import org.apache.fop.fo.FONode;

/**
* This class provides the element mapping for FOP.
*/
public class MathMLElementMapping extends ElementMapping {

/** MathML Namespace */
public static final String NAMESPACE = "http://www.w3.org/1998/Math/MathML";

/** Main constructor. */
public MathMLElementMapping() {
this.namespaceURI = NAMESPACE;
}

/** {@inheritDoc} */
public DOMImplementation getDOMImplementation() {
return getDefaultDOMImplementation();
}

/** {@inheritDoc} */
protected void initialize() {
if (foObjs == null) {
foObjs = new HashMap();
foObjs.put("math", new ME());
foObjs.put(DEFAULT, new MathMLMaker());
}
}

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);
}
}

}

+ 0
- 48
examples/mathml/src/org/apache/fop/mathml/MathMLObj.java View File

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

/* $Id$ */

package org.apache.fop.mathml;

// FOP
import org.apache.fop.fo.FONode;
import org.apache.fop.fo.XMLObj;

/**
* Catch all MathML objects as default element.
*/
public class MathMLObj extends XMLObj {

/**
* @see org.apache.fop.fo.FONode#FONode(FONode)
*/
public MathMLObj(FONode parent) {
super(parent);
}

/** {@inheritDoc} */
public String getNamespaceURI() {
return MathMLElementMapping.NAMESPACE;
}

/** {@inheritDoc} */
public String getNormalNamespacePrefix() {
return "mathml";
}
}


+ 0
- 189
examples/mathml/src/org/apache/fop/mathml/PreloaderMathML.java View File

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

/* $Id$ */

package org.apache.fop.mathml;

import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.io.IOException;
import java.io.InputStream;

import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.stream.StreamSource;

import net.sourceforge.jeuclid.MathBase;
import net.sourceforge.jeuclid.SAXMathBuilder;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.apache.xmlgraphics.image.loader.Image;
import org.apache.xmlgraphics.image.loader.ImageContext;
import org.apache.xmlgraphics.image.loader.ImageInfo;
import org.apache.xmlgraphics.image.loader.ImageSize;
import org.apache.xmlgraphics.image.loader.impl.AbstractImagePreloader;
import org.apache.xmlgraphics.image.loader.impl.ImageGraphics2D;
import org.apache.xmlgraphics.image.loader.util.ImageUtil;
import org.apache.xmlgraphics.java2d.Graphics2DImagePainter;

import org.apache.fop.util.UnclosableInputStream;

/**
* Image preloader for MathML images.
*/
public class PreloaderMathML extends AbstractImagePreloader {

/** Logger instance */
private static Log log = LogFactory.getLog(PreloaderMathML.class);

private boolean jeuclidAvailable = true;

/** {@inheritDoc} */
public ImageInfo preloadImage(String uri, Source src, ImageContext context)
throws IOException {
if (!ImageUtil.hasInputStream(src)) {
//TODO Remove this and support DOMSource and possibly SAXSource
return null;
}
ImageInfo info = null;
if (jeuclidAvailable) {
try {
Loader loader = new Loader();
info = loader.getImage(uri, src, context);
} catch (NoClassDefFoundError e) {
jeuclidAvailable = false;
log.warn("JEuclid not in class path", e);
return null;
}
}
if (info != null) {
ImageUtil.closeQuietly(src); //Image is fully read
}
return info;
}

/**
* This method is put in another class so that the class loader does not
* attempt to load JEuclid related classes when constructing the MathMLPreloader
* class.
*/
class Loader {

private ImageInfo getImage(String uri, Source src, ImageContext context) {

InputStream in = new UnclosableInputStream(ImageUtil.needInputStream(src));
try {
int length = in.available();
in.mark(length + 1);

TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
Source source = new StreamSource(in);
SAXMathBuilder mathBuilder = new SAXMathBuilder();
SAXResult res = new SAXResult(mathBuilder);
transformer.transform(source, res);

String fontname = "Helvetica";
int fontstyle = 0;
int displayfontsize = 12;
int inlinefontsize = 12;

if (mathBuilder.getMathRootElement() == null) {
//not a MathML document
try {
in.reset();
} catch (IOException ioe) {
log.error("Error while resetting ImageInputStream", ioe);
}
return null;
}
final MathBase base = new MathBase(
mathBuilder.getMathRootElement(),
fontname, fontstyle, inlinefontsize,
displayfontsize);

ImageInfo info = new ImageInfo(uri, "text/mathml");
final ImageSize size = new ImageSize();
size.setSizeInMillipoints(
Math.round(base.getWidth() * 1000),
Math.round(base.getHeight() * 1000));
//Set the resolution to that of the FOUserAgent
size.setResolution(context.getSourceResolution());
size.calcPixelsFromSize();
info.setSize(size);

Graphics2DImagePainter painter = new Graphics2DImagePainter() {

public Dimension getImageSize() {
return size.getDimensionMpt();
}

public void paint(Graphics2D g2d, Rectangle2D area) {
base.paint(g2d);
}

};

//The whole image had to be loaded for this, so keep it
Image image = new ImageGraphics2D(info, painter);
info.getCustomObjects().put(ImageInfo.ORIGINAL_IMAGE, image);

return info;
} catch (NoClassDefFoundError ncdfe) {
try {
in.reset();
} catch (IOException ioe) {
// we're more interested in the original exception
}
jeuclidAvailable = false;
log.warn("JEuclid not in class path", ncdfe);
return null;
} catch (IOException e) {
// If the MathML is invalid then it throws an IOException
// so there is no way of knowing if it is an svg document

log.debug("Error while trying to load stream as an MathML file: "
+ e.getMessage());
// assuming any exception means this document is not svg
// or could not be loaded for some reason
try {
in.reset();
} catch (IOException ioe) {
// we're more interested in the original exception
}
return null;
} catch (TransformerException e) {
try {
in.reset();
} catch (IOException ioe) {
// we're more interested in the original exception
}
log.debug("Error while trying to parsing a MathML file: "
+ e.getMessage());
return null;
}
}
}

}

Loading…
Cancel
Save