plantuml/src/net/sourceforge/plantuml/math/ScientificEquationSafe.java

167 lines
5.4 KiB
Java
Raw Normal View History

2016-12-01 20:29:25 +00:00
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
2023-02-22 18:43:48 +00:00
* (C) Copyright 2009-2024, Arnaud Roques
2016-12-01 20:29:25 +00:00
*
2023-02-22 18:43:48 +00:00
* Project Info: https://plantuml.com
2022-08-17 17:34:24 +00:00
*
2017-03-15 19:13:31 +00:00
* If you like this project or if you find it useful, you can support us at:
2022-08-17 17:34:24 +00:00
*
2023-02-22 18:43:48 +00:00
* https://plantuml.com/patreon (only 1$ per month!)
* https://plantuml.com/paypal
2022-08-17 17:34:24 +00:00
*
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
2022-08-17 17:34:24 +00:00
*
2016-12-01 20:29:25 +00:00
*
*/
2016-11-18 21:12:09 +00:00
package net.sourceforge.plantuml.math;
2023-02-22 18:43:48 +00:00
import static net.atmp.ImageBuilder.plainImageBuilder;
2021-06-27 16:50:40 +00:00
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.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Arrays;
2023-02-22 18:43:48 +00:00
import net.atmp.ImageBuilder;
import net.atmp.PixelImage;
2016-11-18 21:12:09 +00:00
import net.sourceforge.plantuml.FileFormat;
import net.sourceforge.plantuml.FileFormatOption;
import net.sourceforge.plantuml.api.ImageDataSimple;
import net.sourceforge.plantuml.core.ImageData;
2023-02-22 18:43:48 +00:00
import net.sourceforge.plantuml.klimt.AffineTransformType;
2023-02-02 17:59:43 +00:00
import net.sourceforge.plantuml.klimt.MutableImage;
2023-02-22 18:43:48 +00:00
import net.sourceforge.plantuml.klimt.drawing.eps.EpsGraphics;
import net.sourceforge.plantuml.klimt.shape.GraphicStrings;
2023-02-26 18:51:17 +00:00
import net.sourceforge.plantuml.klimt.shape.TextBlock;
2023-02-22 18:43:48 +00:00
import net.sourceforge.plantuml.klimt.shape.UImageSvg;
2022-08-17 17:34:24 +00:00
import net.sourceforge.plantuml.log.Logme;
import net.sourceforge.plantuml.security.SImageIO;
2022-12-17 11:01:10 +00:00
import net.sourceforge.plantuml.utils.Log;
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) {
2022-08-17 17:34:24 +00:00
Logme.error(e);
2016-12-14 21:01:03 +00:00
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) {
2022-08-17 17:34:24 +00:00
Logme.error(e);
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;
2020-12-19 21:21:54 +00:00
public UImageSvg getSvg(double scale, Color foregroundColor, Color backgroundColor) {
if (equation != null)
2016-11-18 21:12:09 +00:00
try {
final UImageSvg svg = equation.getSvg(scale, foregroundColor, backgroundColor);
dimSvg = new ImageDataSimple(equation.getDimension());
return svg;
} catch (Exception e) {
printTrace(e);
2016-11-18 21:12:09 +00:00
}
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
dimSvg = plainImageBuilder(getRollback(), new FileFormatOption(FileFormat.SVG)).write(baos);
} catch (IOException e1) {
return null;
2016-11-18 21:12:09 +00:00
}
return new UImageSvg(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) {
if (equation != null)
2016-11-18 21:12:09 +00:00
try {
return equation.getImage(foregroundColor, backgroundColor);
} catch (Exception e) {
printTrace(e);
2016-11-18 21:12:09 +00:00
}
try {
2023-02-22 18:43:48 +00:00
final byte[] bytes = ImageBuilder.plainPngBuilder(getRollback()).writeByteArray();
return new PixelImage(SImageIO.read(bytes), AffineTransformType.TYPE_BILINEAR);
} catch (IOException e1) {
return null;
2016-11-18 21:12:09 +00:00
}
}
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
}
2022-08-17 17:34:24 +00:00
Logme.error(e);
2016-12-01 20:29:25 +00:00
}
2023-02-26 18:51:17 +00:00
private TextBlock getRollback() {
return GraphicStrings.createBlackOnWhiteMonospaced(Arrays.asList(formula));
2016-11-18 21:12:09 +00:00
}
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();
SImageIO.write(image, "png", os);
2016-11-18 21:12:09 +00:00
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
}