1
0
mirror of https://github.com/octoleo/plantuml.git synced 2024-05-29 06:30:48 +00:00
plantuml/src/net/sourceforge/plantuml/math/ScientificEquationSafe.java

175 lines
6.1 KiB
Java
Raw Normal View History

2016-12-01 20:29:25 +00:00
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
2019-01-16 18:34:41 +00:00
* (C) Copyright 2009-2020, Arnaud Roques
2016-12-01 20:29:25 +00:00
*
* 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.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Arrays;
import net.sourceforge.plantuml.FileFormat;
import net.sourceforge.plantuml.FileFormatOption;
import net.sourceforge.plantuml.Log;
2017-04-05 17:37:42 +00:00
import net.sourceforge.plantuml.SvgString;
2016-11-18 21:12:09 +00:00
import net.sourceforge.plantuml.api.ImageDataSimple;
import net.sourceforge.plantuml.core.ImageData;
2019-03-29 22:14:07 +00:00
import net.sourceforge.plantuml.eps.EpsGraphics;
2016-11-18 21:12:09 +00:00
import net.sourceforge.plantuml.graphic.GraphicStrings;
import net.sourceforge.plantuml.graphic.TextBlock;
2020-05-30 15:20:23 +00:00
import net.sourceforge.plantuml.security.ImageIO;
2020-12-06 21:43:09 +00:00
import net.sourceforge.plantuml.style.ClockwiseTopRightBottomLeft;
2020-06-07 10:03:18 +00:00
import net.sourceforge.plantuml.ugraphic.AffineTransformType;
2020-12-01 21:39:27 +00:00
import net.sourceforge.plantuml.ugraphic.ImageBuilder;
2020-12-06 21:43:09 +00:00
import net.sourceforge.plantuml.ugraphic.ImageParameter;
2020-06-07 10:03:18 +00:00
import net.sourceforge.plantuml.ugraphic.MutableImage;
import net.sourceforge.plantuml.ugraphic.PixelImage;
2020-03-18 10:50:02 +00:00
import net.sourceforge.plantuml.ugraphic.color.ColorMapperIdentity;
2016-11-18 21:12:09 +00:00
2016-12-14 21:01:03 +00:00
public class ScientificEquationSafe {
2016-11-18 21:12:09 +00:00
2016-12-14 21:01:03 +00:00
private final ScientificEquation equation;
private final String formula;
2016-11-18 21:12:09 +00:00
2016-12-14 21:01:03 +00:00
private ScientificEquationSafe(String formula, ScientificEquation equation) {
this.formula = formula;
this.equation = equation;
}
public static ScientificEquationSafe fromAsciiMath(String formula) {
try {
return new ScientificEquationSafe(formula, new AsciiMath(formula));
} catch (Exception e) {
e.printStackTrace();
Log.info("Error parsing " + formula);
return new ScientificEquationSafe(formula, null);
}
}
public static ScientificEquationSafe fromLatex(String formula) {
2016-11-18 21:12:09 +00:00
try {
2016-12-14 21:01:03 +00:00
return new ScientificEquationSafe(formula, new LatexBuilder(formula));
2016-11-18 21:12:09 +00:00
} catch (Exception e) {
2016-12-01 20:29:25 +00:00
e.printStackTrace();
2016-12-14 21:01:03 +00:00
Log.info("Error parsing " + formula);
return new ScientificEquationSafe(formula, null);
2016-11-18 21:12:09 +00:00
}
}
private ImageData dimSvg;
2017-04-05 17:37:42 +00:00
public SvgString getSvg(double scale, Color foregroundColor, Color backgroundColor) {
2016-11-18 21:12:09 +00:00
try {
2017-04-05 17:37:42 +00:00
final SvgString svg = equation.getSvg(scale, foregroundColor, backgroundColor);
2016-12-14 21:01:03 +00:00
dimSvg = new ImageDataSimple(equation.getDimension());
2016-11-18 21:12:09 +00:00
return svg;
} catch (Exception e) {
2016-12-01 20:29:25 +00:00
printTrace(e);
2016-11-18 21:12:09 +00:00
final ImageBuilder imageBuilder = getRollback();
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
2017-04-19 18:30:16 +00:00
dimSvg = imageBuilder.writeImageTOBEMOVED(new FileFormatOption(FileFormat.SVG), 42, baos);
2016-11-18 21:12:09 +00:00
} catch (IOException e1) {
return null;
}
2017-04-05 17:37:42 +00:00
return new SvgString(new String(baos.toByteArray()), scale);
2016-11-18 21:12:09 +00:00
}
}
2020-06-07 10:03:18 +00:00
public MutableImage getImage(Color foregroundColor, Color backgroundColor) {
2016-11-18 21:12:09 +00:00
try {
2020-06-07 10:03:18 +00:00
return equation.getImage(foregroundColor, backgroundColor);
2016-11-18 21:12:09 +00:00
} catch (Exception e) {
2016-12-01 20:29:25 +00:00
printTrace(e);
2016-11-18 21:12:09 +00:00
final ImageBuilder imageBuilder = getRollback();
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
2017-04-19 18:30:16 +00:00
imageBuilder.writeImageTOBEMOVED(new FileFormatOption(FileFormat.PNG), 42, baos);
2020-06-07 10:03:18 +00:00
return new PixelImage(ImageIO.read(new ByteArrayInputStream(baos.toByteArray())),
AffineTransformType.TYPE_BILINEAR);
2016-11-18 21:12:09 +00:00
} catch (IOException e1) {
return null;
}
}
}
2016-12-01 20:29:25 +00:00
private void printTrace(Exception e) {
2016-12-14 21:01:03 +00:00
System.err.println("formula=" + formula);
2020-03-18 10:50:02 +00:00
if (equation != null) {
2016-12-14 21:01:03 +00:00
System.err.println("Latex=" + equation.getSource());
2016-12-01 20:29:25 +00:00
}
e.printStackTrace();
}
2016-11-18 21:12:09 +00:00
private ImageBuilder getRollback() {
2016-12-14 21:01:03 +00:00
final TextBlock block = GraphicStrings.createBlackOnWhiteMonospaced(Arrays.asList(formula));
2020-12-06 21:43:09 +00:00
final ImageParameter imageParameter = new ImageParameter(new ColorMapperIdentity(), false, null, 1.0, null,
null, ClockwiseTopRightBottomLeft.none(), null);
final ImageBuilder imageBuilder = ImageBuilder.build(imageParameter);
2016-11-18 21:12:09 +00:00
imageBuilder.setUDrawable(block);
return imageBuilder;
}
2017-04-05 17:37:42 +00:00
public ImageData export(OutputStream os, FileFormatOption fileFormat, float scale, Color foregroundColor,
Color backgroundColor) throws IOException {
2016-11-18 21:12:09 +00:00
if (fileFormat.getFileFormat() == FileFormat.PNG) {
2020-06-07 10:03:18 +00:00
final BufferedImage image = getImage(foregroundColor, backgroundColor).withScale(scale).getImage();
2016-11-18 21:12:09 +00:00
ImageIO.write(image, "png", os);
return new ImageDataSimple(image.getWidth(), image.getHeight());
}
if (fileFormat.getFileFormat() == FileFormat.SVG) {
2018-07-27 21:56:46 +00:00
os.write(getSvg(1, foregroundColor, backgroundColor).getSvg(true).getBytes());
2016-11-18 21:12:09 +00:00
return dimSvg;
}
2019-03-29 22:14:07 +00:00
if (fileFormat.getFileFormat() == FileFormat.EPS) {
2020-06-07 10:03:18 +00:00
final BufferedImage image = getImage(foregroundColor, backgroundColor).withScale(scale).getImage();
2019-03-29 22:14:07 +00:00
final EpsGraphics out = new EpsGraphics();
out.drawImage(image, 0, 0);
out.close();
os.write(out.getEPSCode().getBytes());
return new ImageDataSimple(image.getWidth(), image.getHeight());
}
throw new UnsupportedOperationException();
2016-11-18 21:12:09 +00:00
}
2018-09-23 12:15:14 +00:00
public final String getFormula() {
return formula;
}
2016-11-18 21:12:09 +00:00
}