1
0
mirror of https://github.com/octoleo/plantuml.git synced 2024-05-31 23:50:49 +00:00
plantuml/src/net/sourceforge/plantuml/creole/atom/AtomMath.java

110 lines
3.5 KiB
Java
Raw Normal View History

2016-12-01 20:29:25 +00:00
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
2022-03-07 19:33:46 +00:00
* (C) Copyright 2009-2023, 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
*
*
*/
2020-03-18 10:50:02 +00:00
package net.sourceforge.plantuml.creole.atom;
2016-12-01 20:29:25 +00:00
import java.awt.Color;
import java.awt.image.BufferedImage;
2022-09-12 20:08:34 +00:00
import net.sourceforge.plantuml.awt.geom.XDimension2D;
2016-12-01 20:29:25 +00:00
import net.sourceforge.plantuml.graphic.StringBounder;
2016-12-14 21:01:03 +00:00
import net.sourceforge.plantuml.math.ScientificEquationSafe;
2016-12-01 20:29:25 +00:00
import net.sourceforge.plantuml.ugraphic.UGraphic;
import net.sourceforge.plantuml.ugraphic.UImage;
import net.sourceforge.plantuml.ugraphic.UImageSvg;
2020-03-18 10:50:02 +00:00
import net.sourceforge.plantuml.ugraphic.color.ColorMapper;
import net.sourceforge.plantuml.ugraphic.color.HColor;
import net.sourceforge.plantuml.ugraphic.color.HColorSimple;
2016-12-01 20:29:25 +00:00
2019-03-29 22:14:07 +00:00
public class AtomMath extends AbstractAtom implements Atom {
// ::remove file when WASM
2016-12-01 20:29:25 +00:00
2016-12-14 21:01:03 +00:00
private final ScientificEquationSafe math;
2020-03-18 10:50:02 +00:00
private final HColor foreground;
private final HColor background;
2016-12-01 20:29:25 +00:00
2022-09-20 20:35:41 +00:00
public AtomMath(ScientificEquationSafe math, HColor foreground, HColor background) {
2016-12-01 20:29:25 +00:00
this.math = math;
this.foreground = foreground;
2016-12-14 21:01:03 +00:00
this.background = background;
2016-12-01 20:29:25 +00:00
}
2022-09-12 20:08:34 +00:00
private XDimension2D calculateDimensionSlow(StringBounder stringBounder) {
2020-06-07 10:03:18 +00:00
final BufferedImage image = math.getImage(Color.BLACK, Color.WHITE).withScale(1).getImage();
2022-09-12 20:08:34 +00:00
return new XDimension2D(image.getWidth(), image.getHeight());
2016-12-01 20:29:25 +00:00
}
2022-09-12 20:08:34 +00:00
private XDimension2D dim;
2016-12-01 20:29:25 +00:00
2022-09-12 20:08:34 +00:00
public XDimension2D calculateDimension(StringBounder stringBounder) {
2016-12-01 20:29:25 +00:00
if (dim == null) {
dim = calculateDimensionSlow(stringBounder);
}
return dim;
}
public double getStartingAltitude(StringBounder stringBounder) {
return 0;
}
public void drawU(UGraphic ug) {
2022-09-20 20:35:41 +00:00
final ColorMapper colorMapper = ug.getColorMapper();
2016-12-01 20:29:25 +00:00
final boolean isSvg = ug.matchesProperty("SVG");
2017-03-21 21:37:59 +00:00
final Color back;
2022-09-20 20:35:41 +00:00
if (background == null)
2017-03-21 21:37:59 +00:00
back = null;
2022-09-20 20:35:41 +00:00
else
back = getColor(colorMapper, background, Color.WHITE);
final Color fore = getColor(colorMapper, foreground, Color.BLACK);
2018-10-21 19:44:14 +00:00
// final double dpiFactor = ug.dpiFactor();
2016-12-01 20:29:25 +00:00
if (isSvg) {
2020-12-19 21:21:54 +00:00
final UImageSvg svg = math.getSvg(1, fore, back);
ug.draw(svg);
2016-12-01 20:29:25 +00:00
} else {
2020-06-07 10:03:18 +00:00
final UImage image = new UImage(math.getImage(fore, back)).withFormula(math.getFormula());
2018-09-23 12:15:14 +00:00
ug.draw(image);
2016-12-01 20:29:25 +00:00
}
}
2022-09-20 20:35:41 +00:00
private Color getColor(ColorMapper colorMapper, HColor color, Color defaultValue) {
2022-08-23 16:39:27 +00:00
if (color instanceof HColorSimple)
2022-09-15 17:24:26 +00:00
return color.toColor(colorMapper);
2022-08-23 16:39:27 +00:00
2016-12-01 20:29:25 +00:00
return defaultValue;
}
}