plantuml/src/net/sourceforge/plantuml/svek/image/EntityImageNote.java

333 lines
12 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
*
2017-03-15 19:13:31 +00:00
* If you like this project or if you find it useful, you can support us at:
*
2017-03-15 19:13:31 +00:00
* 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
*
2013-12-10 19:36:50 +00:00
*
*/
package net.sourceforge.plantuml.svek.image;
2022-02-16 18:59:44 +00:00
import java.util.EnumMap;
import java.util.Map;
import java.util.Objects;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.ColorParam;
2019-03-29 22:14:07 +00:00
import net.sourceforge.plantuml.CornerParam;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.ISkinParam;
import net.sourceforge.plantuml.SkinParamBackcolored;
2021-09-19 17:05:24 +00:00
import net.sourceforge.plantuml.UmlDiagramType;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.Url;
2022-09-12 20:08:34 +00:00
import net.sourceforge.plantuml.awt.geom.XDimension2D;
import net.sourceforge.plantuml.awt.geom.XLine2D;
import net.sourceforge.plantuml.awt.geom.XPoint2D;
2022-11-07 19:27:11 +00:00
import net.sourceforge.plantuml.baraye.IEntity;
import net.sourceforge.plantuml.baraye.ILeaf;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.creole.Stencil;
2020-12-14 18:31:06 +00:00
import net.sourceforge.plantuml.cucadiagram.BodyFactory;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.cucadiagram.Display;
import net.sourceforge.plantuml.cucadiagram.Stereotype;
2015-05-03 15:36:36 +00:00
import net.sourceforge.plantuml.graphic.FontConfiguration;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.graphic.HorizontalAlignment;
import net.sourceforge.plantuml.graphic.StringBounder;
import net.sourceforge.plantuml.graphic.TextBlock;
import net.sourceforge.plantuml.graphic.TextBlockEmpty;
2015-09-28 20:42:17 +00:00
import net.sourceforge.plantuml.graphic.color.ColorType;
import net.sourceforge.plantuml.graphic.color.Colors;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.posimo.DotPath;
import net.sourceforge.plantuml.skin.rose.Rose;
2019-08-26 17:07:21 +00:00
import net.sourceforge.plantuml.style.PName;
import net.sourceforge.plantuml.style.SName;
import net.sourceforge.plantuml.style.Style;
2022-11-19 14:57:30 +00:00
import net.sourceforge.plantuml.style.StyleSignature;
2022-03-01 18:11:51 +00:00
import net.sourceforge.plantuml.style.StyleSignatureBasic;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.svek.AbstractEntityImage;
import net.sourceforge.plantuml.svek.ShapeType;
2021-04-25 20:59:17 +00:00
import net.sourceforge.plantuml.svek.SvekLine;
2021-03-07 12:23:24 +00:00
import net.sourceforge.plantuml.svek.SvekNode;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.ugraphic.UGraphic;
import net.sourceforge.plantuml.ugraphic.UGraphicStencil;
2021-03-07 12:23:24 +00:00
import net.sourceforge.plantuml.ugraphic.UGroupType;
2018-01-28 22:08:15 +00:00
import net.sourceforge.plantuml.ugraphic.UPath;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.ugraphic.UStroke;
import net.sourceforge.plantuml.ugraphic.UTranslate;
2020-03-18 10:50:02 +00:00
import net.sourceforge.plantuml.ugraphic.color.HColor;
2022-12-17 11:01:10 +00:00
import net.sourceforge.plantuml.utils.Direction;
2013-12-10 19:36:50 +00:00
public class EntityImageNote extends AbstractEntityImage implements Stencil {
2020-03-18 10:50:02 +00:00
private final HColor noteBackgroundColor;
private final HColor borderColor;
2019-08-26 17:07:21 +00:00
private final double shadowing;
2013-12-10 19:36:50 +00:00
private final int marginX1 = 6;
private final int marginX2 = 15;
private final int marginY = 5;
2022-11-19 14:57:30 +00:00
2013-12-10 19:36:50 +00:00
private final ISkinParam skinParam;
2022-02-12 17:27:51 +00:00
private final Style style;
2013-12-10 19:36:50 +00:00
private final TextBlock textBlock;
2021-09-19 17:05:24 +00:00
public EntityImageNote(ILeaf entity, ISkinParam skinParam, UmlDiagramType umlDiagramType) {
2015-09-28 20:42:17 +00:00
super(entity, getSkin(getISkinParam(skinParam, entity), entity));
this.skinParam = getISkinParam(skinParam, entity);
2013-12-10 19:36:50 +00:00
final Display strings = entity.getDisplay();
2022-05-21 09:41:00 +00:00
this.style = getDefaultStyleDefinition(umlDiagramType.getStyleName())
.getMergedStyle(skinParam.getCurrentStyleBuilder());
if (entity.getColors().getColor(ColorType.BACK) == null)
2022-09-18 17:08:06 +00:00
this.noteBackgroundColor = style.value(PName.BackGroundColor).asColor(skinParam.getIHtmlColorSet());
2022-05-21 09:41:00 +00:00
else
this.noteBackgroundColor = entity.getColors().getColor(ColorType.BACK);
2022-02-10 18:16:18 +00:00
2022-09-18 17:08:06 +00:00
this.borderColor = style.value(PName.LineColor).asColor(skinParam.getIHtmlColorSet());
2022-05-21 09:41:00 +00:00
this.shadowing = style.value(PName.Shadowing).asDouble();
2021-11-10 21:15:26 +00:00
2022-09-18 17:08:06 +00:00
final FontConfiguration fontConfiguration = style.getFontConfiguration(skinParam.getIHtmlColorSet());
2022-05-21 09:41:00 +00:00
final HorizontalAlignment horizontalAlignment = style.getHorizontalAlignment();
2013-12-10 19:36:50 +00:00
2022-02-10 18:16:18 +00:00
if (strings.size() == 1 && strings.get(0).length() == 0)
2013-12-10 19:36:50 +00:00
textBlock = new TextBlockEmpty();
2022-02-10 18:16:18 +00:00
else
2022-05-21 09:41:00 +00:00
textBlock = BodyFactory.create3(strings, getSkinParam(), horizontalAlignment, fontConfiguration,
2023-01-09 19:13:37 +00:00
style.wrapWidth(), style);
2022-02-10 18:16:18 +00:00
2013-12-10 19:36:50 +00:00
}
2015-09-28 20:42:17 +00:00
private static ISkinParam getISkinParam(ISkinParam skinParam, IEntity entity) {
2022-02-10 18:16:18 +00:00
if (entity.getColors() != null)
2021-11-15 18:27:27 +00:00
return entity.getColors().mute(skinParam);
2022-02-10 18:16:18 +00:00
2015-09-28 20:42:17 +00:00
return skinParam;
}
2015-05-31 18:56:03 +00:00
static ISkinParam getSkin(ISkinParam skinParam, IEntity entity) {
2013-12-10 19:36:50 +00:00
final Stereotype stereotype = entity.getStereotype();
2021-11-15 18:27:27 +00:00
HColor back = entity.getColors().getColor(ColorType.BACK);
2022-02-10 18:16:18 +00:00
if (back != null)
2013-12-10 19:36:50 +00:00
return new SkinParamBackcolored(skinParam, back);
2022-02-10 18:16:18 +00:00
2013-12-10 19:36:50 +00:00
back = getColorStatic(skinParam, ColorParam.noteBackground, stereotype);
2022-02-10 18:16:18 +00:00
if (back != null)
2013-12-10 19:36:50 +00:00
return new SkinParamBackcolored(skinParam, back);
2022-02-10 18:16:18 +00:00
2013-12-10 19:36:50 +00:00
return skinParam;
}
2020-03-18 10:50:02 +00:00
private static HColor getColorStatic(ISkinParam skinParam, ColorParam colorParam, Stereotype stereo) {
2013-12-10 19:36:50 +00:00
final Rose rose = new Rose();
2019-01-16 18:34:41 +00:00
return rose.getHtmlColor(skinParam, stereo, colorParam);
2013-12-10 19:36:50 +00:00
}
final public double getPreferredWidth(StringBounder stringBounder) {
final double result = getTextWidth(stringBounder);
return result;
}
final public double getPreferredHeight(StringBounder stringBounder) {
return getTextHeight(stringBounder);
}
2022-09-12 20:08:34 +00:00
private XDimension2D getSize(StringBounder stringBounder, final TextBlock textBlock) {
2013-12-10 19:36:50 +00:00
return textBlock.calculateDimension(stringBounder);
}
final protected double getTextHeight(StringBounder stringBounder) {
final TextBlock textBlock = getTextBlock();
2022-09-12 20:08:34 +00:00
final XDimension2D size = getSize(stringBounder, textBlock);
2013-12-10 19:36:50 +00:00
return size.getHeight() + 2 * marginY;
}
final protected TextBlock getTextBlock() {
return textBlock;
}
final protected double getPureTextWidth(StringBounder stringBounder) {
final TextBlock textBlock = getTextBlock();
2022-09-12 20:08:34 +00:00
final XDimension2D size = getSize(stringBounder, textBlock);
2013-12-10 19:36:50 +00:00
return size.getWidth();
}
final public double getTextWidth(StringBounder stringBounder) {
return getPureTextWidth(stringBounder) + marginX1 + marginX2;
}
2022-09-12 20:08:34 +00:00
public XDimension2D calculateDimension(StringBounder stringBounder) {
2013-12-10 19:36:50 +00:00
final double height = getPreferredHeight(stringBounder);
final double width = getPreferredWidth(stringBounder);
2022-09-12 20:08:34 +00:00
return new XDimension2D(width, height);
2013-12-10 19:36:50 +00:00
}
2022-11-19 14:57:30 +00:00
private StyleSignature getDefaultStyleDefinition(SName sname) {
return StyleSignatureBasic.of(SName.root, SName.element, sname, SName.note).withTOBECHANGED(getStereo());
2019-08-26 17:07:21 +00:00
}
2013-12-10 19:36:50 +00:00
final public void drawU(UGraphic ug) {
final Url url = getEntity().getUrl99();
2022-02-16 18:59:44 +00:00
final Map<UGroupType, String> typeIDent = new EnumMap<>(UGroupType.class);
typeIDent.put(UGroupType.CLASS, "elem " + getEntity().getCode() + " selected");
typeIDent.put(UGroupType.ID, "elem_" + getEntity().getCode());
ug.startGroup(typeIDent);
2022-02-10 18:16:18 +00:00
if (url != null)
2013-12-10 19:36:50 +00:00
ug.startUrl(url);
2022-02-10 18:16:18 +00:00
2016-12-01 20:29:25 +00:00
final UGraphic ug2 = UGraphicStencil.create(ug, this, new UStroke());
2013-12-10 19:36:50 +00:00
if (opaleLine == null || opaleLine.isOpale() == false) {
drawNormal(ug2);
} else {
final StringBounder stringBounder = ug.getStringBounder();
DotPath path = opaleLine.getDotPath();
2020-02-18 21:24:31 +00:00
path.moveSvek(-node.getMinX(), -node.getMinY());
2022-09-12 20:08:34 +00:00
XPoint2D p1 = path.getStartPoint();
XPoint2D p2 = path.getEndPoint();
2013-12-10 19:36:50 +00:00
final double textWidth = getTextWidth(stringBounder);
final double textHeight = getTextHeight(stringBounder);
2022-09-12 20:08:34 +00:00
final XPoint2D center = new XPoint2D(textWidth / 2, textHeight / 2);
2013-12-10 19:36:50 +00:00
if (p1.distance(center) > p2.distance(center)) {
path = path.reverse();
p1 = path.getStartPoint();
2015-09-28 20:42:17 +00:00
// p2 = path.getEndPoint();
2013-12-10 19:36:50 +00:00
}
final Direction strategy = getOpaleStrategy(textWidth, textHeight, p1);
2022-09-12 20:08:34 +00:00
final XPoint2D pp1 = path.getStartPoint();
final XPoint2D pp2 = path.getEndPoint();
final XPoint2D newRefpp2 = move(pp2, node.getMinX(), node.getMinY());
final XPoint2D projection = move(other.projection(newRefpp2, stringBounder), -node.getMinX(),
2020-02-18 21:24:31 +00:00
-node.getMinY());
2022-02-12 17:27:51 +00:00
final Opale opale = new Opale(shadowing, borderColor, noteBackgroundColor, textBlock, true, getStroke());
2018-01-28 22:08:15 +00:00
opale.setRoundCorner(getRoundCorner());
2015-09-28 20:42:17 +00:00
opale.setOpale(strategy, pp1, projection);
2016-09-29 19:51:18 +00:00
final UGraphic stroked = applyStroke(ug2);
2021-11-15 18:27:27 +00:00
opale.drawU(Colors.applyStroke(stroked, getEntity().getColors()));
2013-12-10 19:36:50 +00:00
}
2022-02-10 18:16:18 +00:00
if (url != null)
2020-05-17 21:15:50 +00:00
ug.closeUrl();
ug.closeGroup();
2013-12-10 19:36:50 +00:00
}
2018-01-28 22:08:15 +00:00
private double getRoundCorner() {
2018-04-06 20:36:30 +00:00
return skinParam.getRoundCorner(CornerParam.DEFAULT, null);
2018-01-28 22:08:15 +00:00
}
2022-09-12 20:08:34 +00:00
private static XPoint2D move(XPoint2D pt, double dx, double dy) {
return new XPoint2D(pt.getX() + dx, pt.getY() + dy);
2015-09-28 20:42:17 +00:00
}
2013-12-10 19:36:50 +00:00
private void drawNormal(UGraphic ug) {
final StringBounder stringBounder = ug.getStringBounder();
2018-01-28 22:08:15 +00:00
final UPath polygon = Opale.getPolygonNormal(getTextWidth(stringBounder), getTextHeight(stringBounder),
getRoundCorner());
2022-02-10 18:16:18 +00:00
2022-05-21 09:41:00 +00:00
polygon.setDeltaShadow(this.shadowing);
2022-02-10 18:16:18 +00:00
2020-04-19 16:04:39 +00:00
ug = ug.apply(noteBackgroundColor.bg()).apply(borderColor);
2016-09-29 19:51:18 +00:00
final UGraphic stroked = applyStroke(ug);
stroked.draw(polygon);
2018-01-28 22:08:15 +00:00
ug.draw(Opale.getCorner(getTextWidth(stringBounder), getRoundCorner()));
2013-12-10 19:36:50 +00:00
getTextBlock().drawU(ug.apply(new UTranslate(marginX1, marginY)));
}
2016-09-29 19:51:18 +00:00
private UGraphic applyStroke(UGraphic ug) {
2022-05-21 09:41:00 +00:00
return ug.apply(style.getStroke());
2022-02-12 17:27:51 +00:00
2016-09-29 19:51:18 +00:00
}
2022-02-12 17:27:51 +00:00
private UStroke getStroke() {
2022-05-21 09:41:00 +00:00
return style.getStroke();
2022-02-12 17:27:51 +00:00
}
2022-09-12 20:08:34 +00:00
private Direction getOpaleStrategy(double width, double height, XPoint2D pt) {
final double d1 = getOrthoDistance(new XLine2D(width, 0, width, height), pt);
final double d2 = getOrthoDistance(new XLine2D(0, height, width, height), pt);
final double d3 = getOrthoDistance(new XLine2D(0, 0, 0, height), pt);
final double d4 = getOrthoDistance(new XLine2D(0, 0, width, 0), pt);
2022-02-10 18:16:18 +00:00
if (d3 <= d1 && d3 <= d2 && d3 <= d4)
2013-12-10 19:36:50 +00:00
return Direction.LEFT;
2022-02-10 18:16:18 +00:00
if (d1 <= d2 && d1 <= d3 && d1 <= d4)
2013-12-10 19:36:50 +00:00
return Direction.RIGHT;
2022-02-10 18:16:18 +00:00
if (d4 <= d1 && d4 <= d2 && d4 <= d3)
2013-12-10 19:36:50 +00:00
return Direction.UP;
2022-02-10 18:16:18 +00:00
if (d2 <= d1 && d2 <= d3 && d2 <= d4)
2013-12-10 19:36:50 +00:00
return Direction.DOWN;
2022-02-10 18:16:18 +00:00
2013-12-10 19:36:50 +00:00
return null;
}
2021-06-27 16:50:40 +00:00
2022-09-12 20:08:34 +00:00
private static double getOrthoDistance(XLine2D seg, XPoint2D pt) {
2022-02-10 18:16:18 +00:00
if (isHorizontal(seg))
2021-05-14 08:42:57 +00:00
return Math.abs(seg.getP1().getY() - pt.getY());
2022-02-10 18:16:18 +00:00
if (isVertical(seg))
2021-05-14 08:42:57 +00:00
return Math.abs(seg.getP1().getX() - pt.getX());
2022-02-10 18:16:18 +00:00
2021-05-14 08:42:57 +00:00
throw new IllegalArgumentException();
}
2021-06-27 16:50:40 +00:00
2022-09-12 20:08:34 +00:00
private static boolean isHorizontal(XLine2D seg) {
2021-05-14 08:42:57 +00:00
return seg.getP1().getY() == seg.getP2().getY();
}
2022-09-12 20:08:34 +00:00
private static boolean isVertical(XLine2D seg) {
2021-05-14 08:42:57 +00:00
return seg.getP1().getX() == seg.getP2().getX();
}
2013-12-10 19:36:50 +00:00
public ShapeType getShapeType() {
return ShapeType.RECTANGLE;
}
2021-04-20 20:19:49 +00:00
private SvekLine opaleLine;
2021-02-02 10:12:15 +00:00
private SvekNode node;
private SvekNode other;
2013-12-10 19:36:50 +00:00
2021-04-20 20:19:49 +00:00
public void setOpaleLine(SvekLine line, SvekNode node, SvekNode other) {
2013-12-10 19:36:50 +00:00
this.opaleLine = line;
2020-02-18 21:24:31 +00:00
this.node = node;
2021-05-14 08:42:57 +00:00
this.other = Objects.requireNonNull(other);
2013-12-10 19:36:50 +00:00
}
public double getStartingX(StringBounder stringBounder, double y) {
return 0;
}
public double getEndingX(StringBounder stringBounder, double y) {
return calculateDimension(stringBounder).getWidth();
}
}