1
0
mirror of https://github.com/octoleo/plantuml.git synced 2024-06-03 09:00:48 +00:00
plantuml/src/net/sourceforge/plantuml/ugraphic/LimitFinder.java

234 lines
7.8 KiB
Java
Raw Normal View History

2013-12-10 19:36:50 +00:00
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
2022-03-07 19:33:46 +00:00
* (C) Copyright 2009-2023, Arnaud Roques
2013-12-10 19:36:50 +00:00
*
2016-03-06 16:47:34 +00:00
* Project Info: http://plantuml.com
2013-12-10 19:36:50 +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:
*
* http://plantuml.com/patreon (only 1$ per month!)
* http://plantuml.com/paypal
*
2013-12-10 19:36:50 +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
*
*
*/
package net.sourceforge.plantuml.ugraphic;
import static net.sourceforge.plantuml.utils.ObjectUtils.instanceOfAny;
2023-02-02 17:59:43 +00:00
import net.atmp.SpecialText;
2020-04-05 15:13:04 +00:00
import net.sourceforge.plantuml.activitydiagram3.ftile.CenteredText;
2022-09-12 20:08:34 +00:00
import net.sourceforge.plantuml.awt.geom.XDimension2D;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.graphic.TextBlock;
2023-02-02 17:59:43 +00:00
import net.sourceforge.plantuml.klimt.DotPath;
import net.sourceforge.plantuml.klimt.UAntiAliasing;
import net.sourceforge.plantuml.klimt.UBackground;
import net.sourceforge.plantuml.klimt.UChange;
import net.sourceforge.plantuml.klimt.UClip;
import net.sourceforge.plantuml.klimt.UComment;
import net.sourceforge.plantuml.klimt.UHidden;
import net.sourceforge.plantuml.klimt.UImage;
import net.sourceforge.plantuml.klimt.ULine;
import net.sourceforge.plantuml.klimt.UPath;
import net.sourceforge.plantuml.klimt.UPixel;
import net.sourceforge.plantuml.klimt.UPolygon;
import net.sourceforge.plantuml.klimt.URectangle;
import net.sourceforge.plantuml.klimt.UShape;
import net.sourceforge.plantuml.klimt.UStroke;
import net.sourceforge.plantuml.klimt.UText;
import net.sourceforge.plantuml.klimt.UTranslate;
import net.sourceforge.plantuml.klimt.color.ColorMapper;
import net.sourceforge.plantuml.klimt.color.HColor;
import net.sourceforge.plantuml.klimt.font.StringBounder;
import net.sourceforge.plantuml.klimt.geom.MinMax;
import net.sourceforge.plantuml.klimt.geom.MinMaxMutable;
2013-12-10 19:36:50 +00:00
public class LimitFinder extends UGraphicNo {
2015-04-07 18:18:37 +00:00
@Override
2013-12-10 19:36:50 +00:00
public UGraphic apply(UChange change) {
2022-03-19 12:48:23 +00:00
final UTranslate tmp = change instanceof UTranslate ? this.getTranslate().compose((UTranslate) change)
: this.getTranslate();
final LimitFinder result = new LimitFinder(this.getStringBounder(), tmp, this.minmax);
2022-10-05 20:32:57 +00:00
if (instanceOfAny(change, UAntiAliasing.class, UBackground.class, UClip.class, HColor.class, UHidden.class,
UStroke.class, UTranslate.class, CopyForegroundColorToBackgroundColor.class) == false)
2022-03-19 12:48:23 +00:00
throw new UnsupportedOperationException(change.getClass().toString());
result.clip = change instanceof UClip ? ((UClip) change).translate(result.getTranslate()) : this.clip;
return result;
2013-12-10 19:36:50 +00:00
}
private final MinMaxMutable minmax;
2022-03-19 12:48:23 +00:00
private UClip clip;
public static LimitFinder create(StringBounder stringBounder, boolean initToZero) {
final LimitFinder result = new LimitFinder(stringBounder, new UTranslate(), MinMaxMutable.getEmpty(initToZero));
result.clip = null;
return result;
}
private LimitFinder(StringBounder stringBounder, UTranslate translate, MinMaxMutable minmax) {
super(stringBounder, translate);
this.minmax = minmax;
2013-12-10 19:36:50 +00:00
}
2015-04-07 18:18:37 +00:00
private void addPoint(double x, double y) {
2022-03-19 12:48:23 +00:00
if (clip == null || clip.isInside(x, y))
2015-04-07 18:18:37 +00:00
minmax.addPoint(x, y);
2022-03-19 12:48:23 +00:00
2015-04-07 18:18:37 +00:00
}
2013-12-10 19:36:50 +00:00
public void draw(UShape shape) {
final double x = getTranslate().getDx();
final double y = getTranslate().getDy();
2013-12-10 19:36:50 +00:00
if (shape instanceof UText) {
drawText(x, y, (UText) shape);
} else if (shape instanceof ULine) {
drawULine(x, y, (ULine) shape);
} else if (shape instanceof UEllipse) {
drawEllipse(x, y, (UEllipse) shape);
} else if (shape instanceof UPolygon) {
drawUPolygon(x, y, (UPolygon) shape);
} else if (shape instanceof UPath) {
drawUPath(x, y, (UPath) shape);
} else if (shape instanceof URectangle) {
drawRectangle(x, y, (URectangle) shape);
2015-04-07 18:18:37 +00:00
} else if (shape instanceof DotPath) {
drawDotPath(x, y, (DotPath) shape);
2013-12-10 19:36:50 +00:00
} else if (shape instanceof UImage) {
drawImage(x, y, (UImage) shape);
2016-08-25 20:45:37 +00:00
} else if (shape instanceof UImageSvg) {
drawImageSvg(x, y, (UImageSvg) shape);
2016-07-25 19:25:28 +00:00
} else if (shape instanceof UComment) {
2013-12-10 19:36:50 +00:00
} else if (shape instanceof UEmpty) {
drawEmpty(x, y, (UEmpty) shape);
2015-04-07 18:18:37 +00:00
} else if (shape instanceof TextBlock) {
final TextBlock tb = (TextBlock) shape;
2013-12-10 19:36:50 +00:00
tb.drawU(this);
} else if (shape instanceof UCenteredCharacter) {
// To be done
2020-04-05 15:13:04 +00:00
} else if (shape instanceof CenteredText) {
// Ignored
} else if (shape instanceof SpecialText) {
// Ignored
2022-10-05 20:32:57 +00:00
} else if (shape instanceof CopyForegroundColorToBackgroundColor) {
// Ignored
2015-04-07 18:18:37 +00:00
} else if (shape instanceof UPixel) {
addPoint(x, y);
2013-12-10 19:36:50 +00:00
} else {
throw new UnsupportedOperationException(shape.getClass().getName());
}
}
private void drawEmpty(double x, double y, UEmpty shape) {
2015-04-07 18:18:37 +00:00
addPoint(x, y);
addPoint(x + shape.getWidth(), y + shape.getHeight());
2013-12-10 19:36:50 +00:00
}
private void drawUPath(double x, double y, UPath shape) {
2015-04-07 18:18:37 +00:00
addPoint(x + shape.getMinX(), y + shape.getMinY());
addPoint(x + shape.getMaxX(), y + shape.getMaxY());
2013-12-10 19:36:50 +00:00
}
2016-05-11 21:31:47 +00:00
private final static double HACK_X_FOR_POLYGON = 10;
2013-12-10 19:36:50 +00:00
private void drawUPolygon(double x, double y, UPolygon shape) {
2015-04-07 18:18:37 +00:00
if (shape.getPoints().size() == 0) {
return;
}
2016-05-11 21:31:47 +00:00
addPoint(x + shape.getMinX() - HACK_X_FOR_POLYGON, y + shape.getMinY());
addPoint(x + shape.getMaxX() + HACK_X_FOR_POLYGON, y + shape.getMaxY());
2013-12-10 19:36:50 +00:00
}
private void drawULine(double x, double y, ULine shape) {
2015-04-07 18:18:37 +00:00
addPoint(x, y);
addPoint(x + shape.getDX(), y + shape.getDY());
2013-12-10 19:36:50 +00:00
}
private void drawRectangle(double x, double y, URectangle shape) {
2020-06-14 20:35:42 +00:00
addPoint(x - 1, y - 1);
addPoint(x + shape.getWidth() - 1 + shape.getDeltaShadow() * 2,
y + shape.getHeight() - 1 + shape.getDeltaShadow() * 2);
2015-04-07 18:18:37 +00:00
}
private void drawDotPath(double x, double y, DotPath shape) {
final MinMax shapeMinMax = shape.getMinMax();
addPoint(x + shapeMinMax.getMinX(), y + shapeMinMax.getMinY());
addPoint(x + shapeMinMax.getMaxX(), y + shapeMinMax.getMaxY());
2013-12-10 19:36:50 +00:00
}
private void drawImage(double x, double y, UImage shape) {
2015-04-07 18:18:37 +00:00
addPoint(x, y);
2017-03-15 19:13:31 +00:00
addPoint(x + shape.getWidth() - 1, y + shape.getHeight() - 1);
2013-12-10 19:36:50 +00:00
}
2016-08-25 20:45:37 +00:00
private void drawImageSvg(double x, double y, UImageSvg shape) {
addPoint(x, y);
2017-03-15 19:13:31 +00:00
addPoint(x + shape.getWidth() - 1, y + shape.getHeight() - 1);
2016-08-25 20:45:37 +00:00
}
2013-12-10 19:36:50 +00:00
private void drawEllipse(double x, double y, UEllipse shape) {
2015-04-07 18:18:37 +00:00
addPoint(x, y);
2020-06-14 20:35:42 +00:00
addPoint(x + shape.getWidth() - 1 + shape.getDeltaShadow() * 2,
y + shape.getHeight() - 1 + shape.getDeltaShadow() * 2);
2013-12-10 19:36:50 +00:00
}
private void drawText(double x, double y, UText text) {
2022-09-12 20:08:34 +00:00
final XDimension2D dim = getStringBounder().calculateDimension(text.getFontConfiguration().getFont(),
2022-03-19 12:48:23 +00:00
text.getText());
2013-12-10 19:36:50 +00:00
y -= dim.getHeight() - 1.5;
2015-04-07 18:18:37 +00:00
addPoint(x, y);
addPoint(x, y + dim.getHeight());
addPoint(x + dim.getWidth(), y);
addPoint(x + dim.getWidth(), y + dim.getHeight());
2013-12-10 19:36:50 +00:00
}
public ColorMapper getColorMapper() {
2022-09-18 17:08:06 +00:00
return ColorMapper.IDENTITY;
2013-12-10 19:36:50 +00:00
}
public double getMaxX() {
return minmax.getMaxX();
}
public double getMaxY() {
return minmax.getMaxY();
}
public double getMinX() {
return minmax.getMinX();
}
public double getMinY() {
return minmax.getMinY();
}
public MinMax getMinMax() {
2015-04-07 18:18:37 +00:00
if (minmax.isInfinity()) {
return MinMax.getEmpty(true);
}
2013-12-10 19:36:50 +00:00
return MinMax.fromMutable(minmax);
}
}