1
0
mirror of https://github.com/octoleo/plantuml.git synced 2024-06-16 23:22:24 +00:00
plantuml/src/net/sourceforge/plantuml/math/AsciiMath.java

114 lines
3.7 KiB
Java
Raw Normal View History

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
*
2017-03-15 19:13:31 +00:00
* If you like this project or if you find it useful, you can support us at:
*
* http://plantuml.com/patreon (only 1$ per month!)
* http://plantuml.com/paypal
*
2016-12-01 20:29:25 +00:00
* 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;
2017-06-05 11:27:21 +00:00
import net.sourceforge.plantuml.BackSlash;
2017-04-05 17:37:42 +00:00
import net.sourceforge.plantuml.SvgString;
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);
2017-06-05 11:27:21 +00:00
sb.append(BackSlash.NEWLINE);
2016-11-18 21:12:09 +00:00
}
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;
2018-06-12 20:50:45 +00:00
this.tex = patchColor((String) inv.invokeFunction("plantuml", form));
2016-12-14 21:01:03 +00:00
this.builder = new LatexBuilder(tex);
2016-11-18 21:12:09 +00:00
}
2018-06-12 20:50:45 +00:00
private String patchColor(String latex) {
return latex.replace("\\color{", "\\textcolor{");
}
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
}
2017-04-05 17:37:42 +00:00
public SvgString getSvg(double scale, Color foregroundColor, Color backgroundColor) throws ClassNotFoundException,
2016-12-01 20:29:25 +00:00
IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException,
SecurityException, InstantiationException, IOException {
2017-04-05 17:37:42 +00:00
return builder.getSvg(scale, 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
}
}