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

327 lines
11 KiB
Java
Raw Normal View History

2013-12-10 19:36:50 +00:00
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
2023-02-22 18:43:48 +00:00
* (C) Copyright 2009-2024, Arnaud Roques
2013-12-10 19:36:50 +00:00
*
2023-02-22 18:43:48 +00:00
* Project Info: https://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:
*
2023-02-22 18:43:48 +00:00
* https://plantuml.com/patreon (only 1$ per month!)
* https://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
2023-02-22 18:43:48 +00:00
import net.sourceforge.plantuml.abel.Entity;
2020-12-14 18:31:06 +00:00
import net.sourceforge.plantuml.cucadiagram.BodyFactory;
2023-02-02 17:59:43 +00:00
import net.sourceforge.plantuml.klimt.UGroupType;
import net.sourceforge.plantuml.klimt.UPath;
import net.sourceforge.plantuml.klimt.UStroke;
import net.sourceforge.plantuml.klimt.UTranslate;
import net.sourceforge.plantuml.klimt.color.ColorType;
2023-02-22 18:43:48 +00:00
import net.sourceforge.plantuml.klimt.color.Colors;
2023-02-02 17:59:43 +00:00
import net.sourceforge.plantuml.klimt.color.HColor;
2023-02-22 18:43:48 +00:00
import net.sourceforge.plantuml.klimt.creole.Display;
import net.sourceforge.plantuml.klimt.creole.Stencil;
import net.sourceforge.plantuml.klimt.drawing.UGraphic;
import net.sourceforge.plantuml.klimt.drawing.UGraphicStencil;
2023-02-02 17:59:43 +00:00
import net.sourceforge.plantuml.klimt.font.FontConfiguration;
import net.sourceforge.plantuml.klimt.font.StringBounder;
import net.sourceforge.plantuml.klimt.geom.HorizontalAlignment;
2023-02-22 18:43:48 +00:00
import net.sourceforge.plantuml.klimt.geom.XDimension2D;
import net.sourceforge.plantuml.klimt.geom.XLine2D;
import net.sourceforge.plantuml.klimt.geom.XPoint2D;
import net.sourceforge.plantuml.klimt.shape.DotPath;
import net.sourceforge.plantuml.klimt.shape.TextBlock;
import net.sourceforge.plantuml.klimt.shape.TextBlockEmpty;
import net.sourceforge.plantuml.skin.ColorParam;
import net.sourceforge.plantuml.skin.CornerParam;
import net.sourceforge.plantuml.skin.SkinParamBackcolored;
import net.sourceforge.plantuml.skin.UmlDiagramType;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.skin.rose.Rose;
2023-02-22 18:43:48 +00:00
import net.sourceforge.plantuml.stereo.Stereotype;
import net.sourceforge.plantuml.style.ISkinParam;
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;
2023-02-02 17:59:43 +00:00
import net.sourceforge.plantuml.url.Url;
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;
2023-02-06 21:04:53 +00:00
public EntityImageNote(Entity 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
}
2023-02-06 21:04:53 +00:00
private static ISkinParam getISkinParam(ISkinParam skinParam, Entity 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;
}
2023-02-06 21:04:53 +00:00
static ISkinParam getSkin(ISkinParam skinParam, Entity 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);
2023-02-06 21:04:53 +00:00
typeIDent.put(UGroupType.CLASS, "elem " + getEntity().getName() + " selected");
typeIDent.put(UGroupType.ID, "elem_" + getEntity().getName());
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();
2023-02-26 18:51:17 +00:00
final UTranslate force1 = getMagneticBorder().getForceAt(stringBounder, path.getStartPoint());
final UTranslate force2 = other.getMagneticBorder().getForceAt(stringBounder, path.getEndPoint());
2020-02-18 21:24:31 +00:00
path.moveSvek(-node.getMinX(), -node.getMinY());
2023-02-26 18:51:17 +00:00
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);
2023-02-26 18:51:17 +00:00
if (path.getStartPoint().distance(center) > path.getEndPoint().distance(center))
2013-12-10 19:36:50 +00:00
path = path.reverse();
2023-02-26 18:51:17 +00:00
final Direction strategy = getOpaleStrategy(textWidth, textHeight, path.getStartPoint());
final XPoint2D pp1 = force1.getTranslated(path.getStartPoint());
final XPoint2D pp2 = force2.getTranslated(path.getEndPoint());
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());
2023-02-26 18:51:17 +00:00
opale.setOpale(strategy, pp1, pp2);
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
}
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();
}
}