2016-12-01 20:29:25 +00:00
|
|
|
/* ========================================================================
|
|
|
|
* PlantUML : a free UML diagram generator
|
|
|
|
* ========================================================================
|
|
|
|
*
|
|
|
|
* (C) Copyright 2009-2017, Arnaud Roques
|
|
|
|
*
|
|
|
|
* Project Info: http://plantuml.com
|
|
|
|
*
|
|
|
|
* This file is part of PlantUML.
|
|
|
|
*
|
|
|
|
* PlantUML is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* PlantUML distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
|
|
|
|
* License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
|
|
|
|
* USA.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Original Author: Arnaud Roques
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*/
|
2016-11-18 21:12:09 +00:00
|
|
|
package net.sourceforge.plantuml.math;
|
|
|
|
|
2016-12-01 20:29:25 +00:00
|
|
|
import java.awt.Color;
|
2016-11-18 21:12:09 +00:00
|
|
|
import java.awt.geom.Dimension2D;
|
|
|
|
import java.awt.image.BufferedImage;
|
|
|
|
import java.io.BufferedReader;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
import java.lang.reflect.InvocationTargetException;
|
|
|
|
|
|
|
|
import javax.script.Invocable;
|
|
|
|
import javax.script.ScriptEngine;
|
|
|
|
import javax.script.ScriptEngineManager;
|
|
|
|
import javax.script.ScriptException;
|
|
|
|
|
2016-12-14 21:01:03 +00:00
|
|
|
public class AsciiMath implements ScientificEquation {
|
2016-11-18 21:12:09 +00:00
|
|
|
|
|
|
|
private static final String ASCIIMATH_PARSER_JS_LOCATION = "/net/sourceforge/plantuml/math/";
|
|
|
|
|
|
|
|
private static String JAVASCRIPT_CODE;
|
|
|
|
|
2016-12-14 21:01:03 +00:00
|
|
|
private final LatexBuilder builder;
|
2016-12-01 20:29:25 +00:00
|
|
|
private final String tex;
|
|
|
|
|
2016-11-18 21:12:09 +00:00
|
|
|
static {
|
|
|
|
try {
|
|
|
|
final BufferedReader br = new BufferedReader(new InputStreamReader(
|
2016-12-01 20:29:25 +00:00
|
|
|
AsciiMath.class.getResourceAsStream(ASCIIMATH_PARSER_JS_LOCATION + "ASCIIMathTeXImg.js"), "UTF-8"));
|
2016-11-18 21:12:09 +00:00
|
|
|
final StringBuilder sb = new StringBuilder();
|
|
|
|
String s = null;
|
|
|
|
while ((s = br.readLine()) != null) {
|
|
|
|
sb.append(s);
|
|
|
|
sb.append("\n");
|
|
|
|
}
|
|
|
|
br.close();
|
|
|
|
JAVASCRIPT_CODE = sb.toString();
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-12-01 20:29:25 +00:00
|
|
|
public AsciiMath(String form) throws ScriptException, NoSuchMethodException {
|
2016-11-18 21:12:09 +00:00
|
|
|
final ScriptEngine engine = new ScriptEngineManager().getEngineByName("JavaScript");
|
|
|
|
engine.eval(JAVASCRIPT_CODE);
|
|
|
|
final Invocable inv = (Invocable) engine;
|
2016-12-14 21:01:03 +00:00
|
|
|
this.tex = (String) inv.invokeFunction("plantuml", form);
|
|
|
|
this.builder = new LatexBuilder(tex);
|
2016-11-18 21:12:09 +00:00
|
|
|
}
|
|
|
|
|
2016-12-01 20:29:25 +00:00
|
|
|
public Dimension2D getDimension() {
|
2016-12-14 21:01:03 +00:00
|
|
|
return builder.getDimension();
|
2016-11-18 21:12:09 +00:00
|
|
|
}
|
|
|
|
|
2016-12-01 20:29:25 +00:00
|
|
|
public String getSvg(Color foregroundColor, Color backgroundColor) throws ClassNotFoundException,
|
|
|
|
IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException,
|
|
|
|
SecurityException, InstantiationException, IOException {
|
2016-12-14 21:01:03 +00:00
|
|
|
return builder.getSvg(foregroundColor, backgroundColor);
|
2016-11-18 21:12:09 +00:00
|
|
|
}
|
|
|
|
|
2016-12-14 21:01:03 +00:00
|
|
|
public BufferedImage getImage(double scale, Color foregroundColor, Color backgroundColor)
|
|
|
|
throws ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException,
|
|
|
|
IllegalAccessException, IllegalArgumentException, InvocationTargetException {
|
|
|
|
return builder.getImage(scale, foregroundColor, backgroundColor);
|
2016-11-18 21:12:09 +00:00
|
|
|
}
|
|
|
|
|
2016-12-14 21:01:03 +00:00
|
|
|
public String getSource() {
|
2016-12-01 20:29:25 +00:00
|
|
|
return tex;
|
2016-11-18 21:12:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|