2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2010 - Calixte DENIZET
5 * This file must be used under the terms of the CeCILL.
6 * This source file is licensed as described in the file COPYING, which
7 * you should have received as part of this distribution. The terms
8 * are also available at
9 * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
13 package org.scilab.modules.helptools.image;
15 import java.awt.Graphics2D;
16 import java.awt.Color;
17 import java.awt.image.BufferedImage;
19 import java.io.IOException;
22 import javax.swing.ImageIcon;
24 import javax.xml.parsers.ParserConfigurationException;
26 import org.w3c.dom.Node;
27 import org.w3c.dom.Document;
28 import org.xml.sax.SAXException;
30 import net.sourceforge.jeuclid.MathMLParserSupport;
31 import net.sourceforge.jeuclid.MutableLayoutContext;
32 import net.sourceforge.jeuclid.layout.JEuclidView;
33 import net.sourceforge.jeuclid.context.LayoutContextImpl;
34 import net.sourceforge.jeuclid.context.Parameter;
37 * A MathML to PNG converter
38 * @author Calixte DENIZET
40 public class MathMLImageConverter implements ExternalImageConverter {
42 private static final Graphics2D TEMPGRAPHIC = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB).createGraphics();
43 private static MathMLImageConverter instance;
45 private MathMLImageConverter() { }
50 public String getMimeType() {
51 return "image/mathml";
55 * Since it is a singleton class...
58 public static ExternalImageConverter getInstance() {
59 if (instance == null) {
60 instance = new MathMLImageConverter();
68 public String convertToImage(String currentFile, String mathml, Map<String, String> attributes, File imageFile, String imageName) {
71 doc = MathMLParserSupport.parseString(mathml);
72 } catch (final SAXException e) {
74 } catch (final ParserConfigurationException e) {
76 } catch (final IOException e) {
80 String fontsize = attributes.get("fontsize");
82 if (fontsize != null) {
83 fs = Integer.parseInt(fontsize);
86 Image img = convertMathML(doc, fs);
87 if (img != null && ImageConverter.convertIconToPNG(img.icon, imageFile)) {
88 return ImageConverter.generateCode(img, imageName, attributes);
97 public String convertToImage(File mathml, Map<String, String> attributes, File imageFile, String imageName) {
100 doc = MathMLParserSupport.parseFile(mathml);
101 } catch (final SAXException e) {
103 } catch (final IOException e) {
107 String fontsize = attributes.get("fontsize");
109 if (fontsize != null) {
110 fs = Integer.parseInt(fontsize);
113 Image img = convertMathML(doc, fs);
114 if (img != null && ImageConverter.convertIconToPNG(img.icon, imageFile)) {
115 return ImageConverter.generateCode(img, imageName, attributes);
122 * @param mathml the document to convert
123 * @param fontSize the font size
124 * @return an image informations
126 private static Image convertMathML(Document mathml, int fontSize) {
127 LayoutContextImpl parameters = new LayoutContextImpl(LayoutContextImpl.getDefaultLayoutContext());
128 parameters.setParameter(Parameter.MATHSIZE, fontSize);
129 JEuclidView jev = new JEuclidView((Node) mathml, parameters, TEMPGRAPHIC);
131 int width = (int) Math.ceil(jev.getWidth());
132 int ascent = (int) Math.ceil(jev.getAscentHeight());
133 int height = (int) Math.ceil(jev.getDescentHeight()) + ascent;
135 if (width <= 0 || height <= 0) {
139 BufferedImage bimg = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
140 Graphics2D g2d = bimg.createGraphics();
141 g2d.setColor(new Color(255, 255, 255, 0));
142 g2d.fillRect(0, 0, width, height);
144 jev.draw(g2d, 0, ascent);
147 return new Image(new ImageIcon(bimg), width, height, ascent, height - ascent);