| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
package net.sourceforge.jeuclid.converter; |
| 20 | |
|
| 21 | |
import java.awt.Dimension; |
| 22 | |
import java.io.IOException; |
| 23 | |
import java.io.OutputStream; |
| 24 | |
|
| 25 | |
import javax.xml.transform.Transformer; |
| 26 | |
import javax.xml.transform.TransformerException; |
| 27 | |
import javax.xml.transform.TransformerFactory; |
| 28 | |
import javax.xml.transform.dom.DOMSource; |
| 29 | |
import javax.xml.transform.stream.StreamResult; |
| 30 | |
|
| 31 | |
import net.sourceforge.jeuclid.LayoutContext; |
| 32 | |
import net.sourceforge.jeuclid.layout.JEuclidView; |
| 33 | |
|
| 34 | |
import org.apache.batik.svggen.SVGGeneratorContext; |
| 35 | |
import org.apache.batik.svggen.SVGGraphics2D; |
| 36 | |
import org.apache.commons.logging.Log; |
| 37 | |
import org.apache.commons.logging.LogFactory; |
| 38 | |
import org.w3c.dom.DOMImplementation; |
| 39 | |
import org.w3c.dom.Document; |
| 40 | |
import org.w3c.dom.Node; |
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
public class BatikConverter implements ConverterPlugin { |
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | 209 | private static final Log LOGGER = LogFactory.getLog(BatikConverter.class); |
| 53 | |
|
| 54 | |
private final DOMImplementation domImplementation; |
| 55 | |
|
| 56 | 209 | BatikConverter(final DOMImplementation domImpl) { |
| 57 | 209 | this.domImplementation = domImpl; |
| 58 | 209 | } |
| 59 | |
|
| 60 | |
|
| 61 | |
public Dimension convert(final Node doc, final LayoutContext context, |
| 62 | |
final OutputStream outStream) throws IOException { |
| 63 | 209 | final DocumentWithDimension svgDocDim = this.convert(doc, context); |
| 64 | 209 | if (svgDocDim != null) { |
| 65 | |
try { |
| 66 | 209 | final Transformer transformer = TransformerFactory |
| 67 | |
.newInstance().newTransformer(); |
| 68 | 209 | final DOMSource source = new DOMSource(svgDocDim |
| 69 | |
.getDocument()); |
| 70 | 209 | final StreamResult result = new StreamResult(outStream); |
| 71 | 209 | transformer.transform(source, result); |
| 72 | 0 | } catch (final TransformerException e) { |
| 73 | 0 | BatikConverter.LOGGER.warn(e); |
| 74 | 209 | } |
| 75 | 209 | return svgDocDim.getDimension(); |
| 76 | |
} |
| 77 | 0 | return null; |
| 78 | |
} |
| 79 | |
|
| 80 | |
|
| 81 | |
public DocumentWithDimension convert(final Node doc, |
| 82 | |
final LayoutContext context) { |
| 83 | |
|
| 84 | 418 | Document document = null; |
| 85 | |
final String svgNS = "http://www.w3.org/2000/svg"; |
| 86 | 418 | document = this.domImplementation.createDocument(svgNS, "svg", null); |
| 87 | 418 | if (document != null) { |
| 88 | 418 | final SVGGeneratorContext svgContext = SVGGeneratorContext |
| 89 | |
.createDefault(document); |
| 90 | 418 | svgContext.setComment("Converted from MathML using JEuclid"); |
| 91 | 418 | final SVGGraphics2D svgGenerator = new SVGGraphics2D(svgContext, |
| 92 | |
true); |
| 93 | 418 | final JEuclidView view = new JEuclidView(doc, context, |
| 94 | |
svgGenerator); |
| 95 | 418 | final int ascent = (int) Math.ceil(view.getAscentHeight()); |
| 96 | 418 | final int descent = (int) Math.ceil(view.getDescentHeight()); |
| 97 | 418 | final int height = ascent + descent; |
| 98 | 418 | final int width = (int) Math.ceil(view.getWidth()); |
| 99 | 418 | final Dimension size = new Dimension(width, height); |
| 100 | 418 | svgGenerator.setSVGCanvasSize(size); |
| 101 | 418 | view.draw(svgGenerator, 0, ascent); |
| 102 | 418 | document.replaceChild(svgGenerator.getRoot(), document |
| 103 | |
.getFirstChild()); |
| 104 | 418 | return new DocumentWithDimension(document, new Dimension(width, |
| 105 | |
height), descent); |
| 106 | |
} |
| 107 | 0 | return null; |
| 108 | |
} |
| 109 | |
} |