Permalink
Browse files

Added cx:mathml-to-svg step (wrapper for the JEuclid converter)

  • Loading branch information...
ndw committed Feb 10, 2015
1 parent 0190722 commit 9e3c05c1b406fd6471868ac0c47aa397589e9857
View
@@ -256,6 +256,7 @@
<!-- Optional features -->
<exclude name="com/xmlcalabash/extensions/MetadataExtractor.java"/>
<exclude name="com/xmlcalabash/extensions/DiTAA.java"/>
<exclude name="com/xmlcalabash/extensions/MathMLtoSVG.java"/>
<exclude name="com/xmlcalabash/extensions/PlantUML.java"/>
<exclude name="com/xmlcalabash/extensions/DeltaXML.java"/>
<exclude name="com/xmlcalabash/extensions/SendMail.java"/>
@@ -314,7 +315,7 @@
depends="compile-MetadataException,compile-DiTAA,compile-PlantUML,
compile-XEP,compile-FOP,compile-AH,compile-SendMail,
compile-DeltaXML,compile-XMLUnit,compile-XCC,
compile-RDF"/>
compile-RDF,compile-MathMLtoSVG"/>
<condition property="XMLUnit.present">
<and>
@@ -384,6 +385,21 @@
</javac>
</target>
<available classname="net.sourceforge.jeuclid.converter.Converter"
property="MathMLtoSVG.present">
<classpath refid="build.classpath"/>
</available>
<target name="compile-MathMLtoSVG" depends="init" if="MathMLtoSVG.present">
<javac destdir="${build.dir}"
classpathref="build.classpath"
source="${j.ver}" target="${j.ver}" includeantruntime="false">
<src path="src"/>
<include name="com/xmlcalabash/extensions/MathMLtoSVG.java"/>
</javac>
</target>
<available classname="net.sourceforge.plantuml.SourceStringReader"
property="PlantUML.present">
<classpath refid="build.classpath"/>
@@ -79,9 +79,6 @@
<implementation type="p:replace"
class-name="com.xmlcalabash.library.Replace"/>
<implementation type="p:serialize"
class-name="com.xmlcalabash.library.Serialize"/>
<implementation type="p:set-attributes"
class-name="com.xmlcalabash.library.SetAttributes"/>
@@ -163,6 +160,9 @@
<implementation type="cx:java-properties"
class-name="com.xmlcalabash.extensions.JavaProperties"/>
<implementation type="cx:mathml-to-svg"
class-name="com.xmlcalabash.extensions.MathMLtoSVG"/>
<implementation type="cx:message"
class-name="com.xmlcalabash.extensions.Message"/>
@@ -62,6 +62,12 @@
<p:option name="href" cx:type="xsd:anyURI"/>
</p:declare-step>
<p:declare-step type="cx:mathml-to-svg">
<p:input port="source" sequence="false"/>
<p:input port="parameters" kind="parameter"/>
<p:output port="result" sequence="false"/>
</p:declare-step>
<p:declare-step type="cx:message">
<p:input port="source" sequence="true"/>
<p:output port="result" sequence="true"/>
@@ -0,0 +1,140 @@
package com.xmlcalabash.extensions;
import com.xmlcalabash.core.XProcException;
import com.xmlcalabash.core.XProcRuntime;
import com.xmlcalabash.io.ReadablePipe;
import com.xmlcalabash.io.WritablePipe;
import com.xmlcalabash.library.DefaultStep;
import com.xmlcalabash.model.RuntimeValue;
import com.xmlcalabash.runtime.XAtomicStep;
import net.sf.saxon.s9api.DocumentBuilder;
import net.sf.saxon.s9api.QName;
import net.sf.saxon.s9api.SaxonApiException;
import net.sf.saxon.s9api.XdmNode;
import net.sourceforge.jeuclid.MathMLParserSupport;
import net.sourceforge.jeuclid.MutableLayoutContext;
import net.sourceforge.jeuclid.context.LayoutContextImpl;
import net.sourceforge.jeuclid.context.Parameter;
import net.sourceforge.jeuclid.converter.Converter;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Source;
import javax.xml.transform.sax.SAXSource;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.util.Properties;
/**
* Created by IntelliJ IDEA.
* User: ndw
* Date: Oct 8, 2008
* Time: 7:44:07 AM
* To change this template use File | Settings | File Templates.
*/
public class MathMLtoSVG extends DefaultStep {
private ReadablePipe source = null;
private WritablePipe result = null;
private Properties options = new Properties();
private MutableLayoutContext params = null;
/**
* Creates a new instance of MathMLtoSVG
*/
public MathMLtoSVG(XProcRuntime runtime, XAtomicStep step) {
super(runtime,step);
}
public void setInput(String port, ReadablePipe pipe) {
source = pipe;
}
public void setOutput(String port, WritablePipe pipe) {
result = pipe;
}
public void setParameter(QName name, RuntimeValue value) {
if (!"".equals(name.getNamespaceURI())) {
throw new XProcException(step.getNode(), "The cx:mathml-to-svg parameters are in no namespace: " + name + " (" + name.getNamespaceURI() + ")");
}
options.setProperty(name.getLocalName(), value.getString());
}
public void reset() {
source.resetReader();
result.resetWriter();
}
public void run() throws SaxonApiException {
super.run();
XdmNode doc = source.read();
if (doc == null || source.moreDocuments()) {
throw XProcException.dynamicError(6, "Reading source on " + getStep().getName());
}
Converter conv = Converter.getInstance();
params = new LayoutContextImpl(LayoutContextImpl.getDefaultLayoutContext());
ByteArrayOutputStream bos = new ByteArrayOutputStream();
setJParameter(Parameter.ANTIALIAS, "antialias");
setJParameter(Parameter.ANTIALIAS_MINSIZE, "antialias-minsize");
setJParameter(Parameter.DEBUG, "debug");
setJParameter(Parameter.DISPLAY, "display");
setJParameter(Parameter.FONTS_DOUBLESTRUCK, "fonts-doublestruck");
setJParameter(Parameter.FONTS_FRAKTUR, "fonts-fraktur");
setJParameter(Parameter.FONTS_MONOSPACED, "fonts-monospaced");
setJParameter(Parameter.FONTS_SANSSERIF, "fonts-sansserif");
setJParameter(Parameter.FONTS_SCRIPT, "fonts-script");
setJParameter(Parameter.FONTS_SERIF, "fonts-serif");
setJParameter(Parameter.MATHBACKGROUND, "mathbackground");
setJParameter(Parameter.MATHCOLOR, "mathcolor");
setJParameter(Parameter.MATHSIZE, "mathsize");
setJParameter(Parameter.MFRAC_KEEP_SCRIPTLEVEL, "mfrac-keep-scriptlevel");
setJParameter(Parameter.SCRIPTLEVEL, "scriptlevel");
setJParameter(Parameter.SCRIPTMINSIZE, "scriptminsize");
setJParameter(Parameter.SCRIPTSIZEMULTIPLIER, "scriptsizemultiplier");
try {
// This is a bit of a hack. Trying to use the DOM wrapper from NodeInfo didn't work.
String mathML = doc.toString();
Document jdoc = MathMLParserSupport.parseString(mathML);
conv.convert(jdoc, bos, Converter.TYPE_SVG, params);
} catch (SAXException e) {
throw new XProcException(e);
} catch (ParserConfigurationException e) {
throw new XProcException(e);
} catch (IOException e) {
throw new XProcException(e);
}
try {
result.write(parseSVG(bos.toString("utf-8")));
} catch (UnsupportedEncodingException e) {
// this can't happen
}
}
private void setJParameter(Parameter p, String key) {
String s = options.getProperty(key);
if (s != null) {
params.setParameter(p, p.fromString(s));
}
}
private XdmNode parseSVG(String svg) throws SaxonApiException {
DocumentBuilder builder = runtime.getProcessor().newDocumentBuilder();
builder.setLineNumbering(true);
builder.setDTDValidation(false);
builder.setBaseURI(step.getNode().getBaseURI());
StringReader reader = new StringReader(svg);
Source src = new SAXSource(new InputSource(reader));
return builder.build(src);
}
}

0 comments on commit 9e3c05c

Please sign in to comment.