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

352 lines
13 KiB
Java
Raw Normal View History

2013-12-10 19:36:50 +00:00
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
2019-01-16 18:34:41 +00:00
* (C) Copyright 2009-2020, 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
* Modified by : Arno Peterson
*
*
*/
package net.sourceforge.plantuml.svek.image;
import java.awt.geom.Dimension2D;
2017-05-10 19:51:15 +00:00
import java.util.Collection;
2018-05-01 17:26:04 +00:00
import java.util.HashSet;
import java.util.Set;
2013-12-10 19:36:50 +00:00
2017-04-05 17:37:42 +00:00
import net.sourceforge.plantuml.Dimension2DDouble;
2019-03-29 22:14:07 +00:00
import net.sourceforge.plantuml.Guillemet;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.ISkinParam;
import net.sourceforge.plantuml.SkinParamUtils;
import net.sourceforge.plantuml.Url;
2020-12-01 21:39:27 +00:00
import net.sourceforge.plantuml.UseStyle;
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;
2015-04-07 18:18:37 +00:00
import net.sourceforge.plantuml.cucadiagram.EntityPortion;
2018-05-01 17:26:04 +00:00
import net.sourceforge.plantuml.cucadiagram.IEntity;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.cucadiagram.ILeaf;
2017-05-10 19:51:15 +00:00
import net.sourceforge.plantuml.cucadiagram.Link;
2015-04-07 18:18:37 +00:00
import net.sourceforge.plantuml.cucadiagram.PortionShower;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.cucadiagram.Stereotype;
import net.sourceforge.plantuml.graphic.FontConfiguration;
import net.sourceforge.plantuml.graphic.HorizontalAlignment;
2018-11-26 18:46:22 +00:00
import net.sourceforge.plantuml.graphic.SkinParameter;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.graphic.StringBounder;
import net.sourceforge.plantuml.graphic.SymbolContext;
import net.sourceforge.plantuml.graphic.TextBlock;
import net.sourceforge.plantuml.graphic.TextBlockUtils;
import net.sourceforge.plantuml.graphic.USymbol;
2015-09-28 20:42:17 +00:00
import net.sourceforge.plantuml.graphic.color.ColorType;
2017-12-11 21:02:10 +00:00
import net.sourceforge.plantuml.graphic.color.Colors;
import net.sourceforge.plantuml.style.PName;
import net.sourceforge.plantuml.style.SName;
import net.sourceforge.plantuml.style.Style;
import net.sourceforge.plantuml.style.StyleSignature;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.svek.AbstractEntityImage;
2021-02-02 10:12:15 +00:00
import net.sourceforge.plantuml.svek.Bibliotekon;
2017-04-05 17:37:42 +00:00
import net.sourceforge.plantuml.svek.Margins;
2021-02-02 10:12:15 +00:00
import net.sourceforge.plantuml.svek.SvekNode;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.svek.ShapeType;
2021-02-02 10:12:15 +00:00
import net.sourceforge.plantuml.ugraphic.Shadowable;
2016-07-25 19:25:28 +00:00
import net.sourceforge.plantuml.ugraphic.UComment;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.ugraphic.UGraphic;
2020-10-12 20:56:58 +00:00
import net.sourceforge.plantuml.ugraphic.UGraphicStencil;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.ugraphic.UStroke;
2017-04-05 17:37:42 +00:00
import net.sourceforge.plantuml.ugraphic.UTranslate;
2020-03-18 10:50:02 +00:00
import net.sourceforge.plantuml.ugraphic.color.HColor;
2017-04-05 17:37:42 +00:00
import net.sourceforge.plantuml.utils.MathUtils;
2013-12-10 19:36:50 +00:00
2015-07-11 09:32:49 +00:00
public class EntityImageDescription extends AbstractEntityImage {
2013-12-10 19:36:50 +00:00
2015-09-28 20:42:17 +00:00
private final ShapeType shapeType;
2013-12-10 19:36:50 +00:00
final private Url url;
2015-04-07 18:18:37 +00:00
private final TextBlock asSmall;
2013-12-10 19:36:50 +00:00
2015-09-28 20:42:17 +00:00
private final TextBlock name;
2017-04-05 17:37:42 +00:00
private final TextBlock desc;
private TextBlock stereo;
private final boolean hideText;
2017-05-10 19:51:15 +00:00
private final Collection<Link> links;
2017-06-05 11:27:21 +00:00
private final boolean useRankSame;
2018-10-21 19:44:14 +00:00
private final boolean fixCircleLabelOverlapping;
2021-02-02 10:12:15 +00:00
private final Bibliotekon bibliotekon;
private final SymbolContext ctx;
2015-09-28 20:42:17 +00:00
2017-05-10 19:51:15 +00:00
public EntityImageDescription(ILeaf entity, ISkinParam skinParam, PortionShower portionShower,
2021-02-02 10:12:15 +00:00
Collection<Link> links, SName styleName, UStroke forceStroke, Bibliotekon bibliotekon) {
2015-09-28 20:42:17 +00:00
super(entity, entity.getColors(skinParam).mute(skinParam));
2020-09-30 20:57:58 +00:00
this.useRankSame = getSkinParam().useRankSame();
2021-02-02 10:12:15 +00:00
this.bibliotekon = bibliotekon;
2020-09-30 20:57:58 +00:00
this.fixCircleLabelOverlapping = getSkinParam().fixCircleLabelOverlapping();
2018-01-28 22:08:15 +00:00
2017-05-10 19:51:15 +00:00
this.links = links;
2013-12-10 19:36:50 +00:00
final Stereotype stereotype = entity.getStereotype();
2017-04-05 17:37:42 +00:00
USymbol symbol = getUSymbol(entity);
2018-05-06 19:59:19 +00:00
if (symbol == USymbol.FOLDER) {
this.shapeType = ShapeType.FOLDER;
2021-02-02 10:12:15 +00:00
} else if (symbol == USymbol.HEXAGON) {
this.shapeType = ShapeType.HEXAGON;
2018-05-06 19:59:19 +00:00
} else if (symbol == USymbol.INTERFACE) {
2020-09-30 20:57:58 +00:00
this.shapeType = getSkinParam().fixCircleLabelOverlapping() ? ShapeType.RECTANGLE_WITH_CIRCLE_INSIDE
2018-08-26 12:09:50 +00:00
: ShapeType.RECTANGLE;
2018-05-06 19:59:19 +00:00
} else {
this.shapeType = ShapeType.RECTANGLE;
}
2017-04-05 17:37:42 +00:00
this.hideText = symbol == USymbol.INTERFACE;
2013-12-10 19:36:50 +00:00
2015-04-07 18:18:37 +00:00
this.url = entity.getUrl99();
2013-12-10 19:36:50 +00:00
2017-12-11 21:02:10 +00:00
final Colors colors = entity.getColors(skinParam);
2020-03-18 10:50:02 +00:00
HColor backcolor = colors.getColor(ColorType.BACK);
final HColor forecolor;
final double roundCorner;
final double diagonalCorner;
2020-06-14 20:35:42 +00:00
final double deltaShadow;
final UStroke stroke;
2020-12-01 21:39:27 +00:00
final FontConfiguration fcTitle;
final FontConfiguration fcStereo;
Style style = null;
2021-02-02 10:12:15 +00:00
final HorizontalAlignment defaultAlign;
2020-12-01 21:39:27 +00:00
if (UseStyle.useBetaStyle()) {
final StyleSignature tmp = StyleSignature.of(SName.root, SName.element, styleName,
symbol.getSkinParameter().getStyleName());
style = tmp.with(stereotype).getMergedStyle(getSkinParam().getCurrentStyleBuilder());
final Style styleStereo = tmp.withStereotype(stereotype)
.getMergedStyle(getSkinParam().getCurrentStyleBuilder());
forecolor = style.value(PName.LineColor).asColor(skinParam.getIHtmlColorSet());
if (backcolor == null) {
backcolor = style.value(PName.BackGroundColor).asColor(skinParam.getIHtmlColorSet());
}
roundCorner = style.value(PName.RoundCorner).asDouble();
diagonalCorner = style.value(PName.DiagonalCorner).asDouble();
2020-06-14 20:35:42 +00:00
deltaShadow = style.value(PName.Shadowing).asDouble();
stroke = style.getStroke();
2020-12-01 21:39:27 +00:00
fcTitle = style.getFontConfiguration(getSkinParam().getIHtmlColorSet());
fcStereo = styleStereo.getFontConfiguration(getSkinParam().getIHtmlColorSet());
2021-02-02 10:12:15 +00:00
defaultAlign = style.getHorizontalAlignment();
} else {
forecolor = SkinParamUtils.getColor(getSkinParam(), stereotype, symbol.getColorParamBorder());
if (backcolor == null) {
backcolor = SkinParamUtils.getColor(getSkinParam(), getStereo(), symbol.getColorParamBack());
}
roundCorner = symbol.getSkinParameter().getRoundCorner(getSkinParam(), stereotype);
diagonalCorner = symbol.getSkinParameter().getDiagonalCorner(getSkinParam(), stereotype);
2020-06-14 20:35:42 +00:00
deltaShadow = getSkinParam().shadowing2(getEntity().getStereotype(), symbol.getSkinParameter()) ? 3 : 0;
if (forceStroke == null) {
stroke = colors.muteStroke(symbol.getSkinParameter().getStroke(getSkinParam(), stereotype));
} else {
stroke = forceStroke;
}
2020-12-01 21:39:27 +00:00
fcTitle = new FontConfiguration(getSkinParam(), symbol.getFontParam(), stereotype);
fcStereo = new FontConfiguration(getSkinParam(), symbol.getFontParamStereotype(), stereotype);
2021-02-02 10:12:15 +00:00
defaultAlign = HorizontalAlignment.LEFT;
2013-12-10 19:36:50 +00:00
}
2017-04-05 17:37:42 +00:00
2016-12-21 22:10:29 +00:00
assert getStereo() == stereotype;
2020-10-12 20:56:58 +00:00
2021-02-02 10:12:15 +00:00
ctx = new SymbolContext(backcolor, forecolor).withStroke(stroke).withShadow(deltaShadow).withCorner(roundCorner,
diagonalCorner);
2015-04-07 18:18:37 +00:00
2020-09-30 20:57:58 +00:00
final Display codeDisplay = Display.getWithNewlines(entity.getCodeGetName());
if ((entity.getDisplay().equals(codeDisplay) && symbol.getSkinParameter() == SkinParameter.PACKAGE)
|| entity.getDisplay().isWhite()) {
desc = TextBlockUtils.empty(getSkinParam().minClassWidth(), 0);
} else {
2021-02-02 10:12:15 +00:00
final HorizontalAlignment align = getSkinParam().getDefaultTextAlignment(defaultAlign);
desc = BodyFactory.create3(entity.getDisplay(), symbol.getFontParam(), getSkinParam(), align, fcTitle,
2020-12-14 18:31:06 +00:00
getSkinParam().wrapWidth());
2020-09-30 20:57:58 +00:00
}
2017-04-05 17:37:42 +00:00
stereo = TextBlockUtils.empty(0, 0);
2015-04-07 18:18:37 +00:00
2019-03-01 22:16:29 +00:00
if (stereotype != null && stereotype.getSprite(getSkinParam()) != null) {
2019-05-24 19:59:31 +00:00
// symbol = symbol.withStereoAlignment(HorizontalAlignment.RIGHT);
2019-03-01 22:16:29 +00:00
stereo = stereotype.getSprite(getSkinParam());
2019-03-29 22:14:07 +00:00
} else if (stereotype != null && stereotype.getLabel(Guillemet.DOUBLE_COMPARATOR) != null
2015-04-07 18:18:37 +00:00
&& portionShower.showPortion(EntityPortion.STEREOTYPE, entity)) {
2020-12-01 21:39:27 +00:00
stereo = Display.getWithNewlines(stereotype.getLabel(getSkinParam().guillemet())).create(fcStereo,
2015-09-28 20:42:17 +00:00
HorizontalAlignment.CENTER, getSkinParam());
2013-12-10 19:36:50 +00:00
}
2020-12-14 18:31:06 +00:00
name = BodyFactory.create2(codeDisplay, symbol.getFontParam(), getSkinParam(), HorizontalAlignment.CENTER,
stereotype, entity, style);
2015-07-11 09:32:49 +00:00
2017-04-05 17:37:42 +00:00
if (hideText) {
asSmall = symbol.asSmall(TextBlockUtils.empty(0, 0), TextBlockUtils.empty(0, 0), TextBlockUtils.empty(0, 0),
ctx, skinParam.getStereotypeAlignment());
2017-04-05 17:37:42 +00:00
} else {
2019-05-24 19:59:31 +00:00
asSmall = symbol.asSmall(name, desc, stereo, ctx, skinParam.getStereotypeAlignment());
2017-04-05 17:37:42 +00:00
}
}
private USymbol getUSymbol(ILeaf entity) {
2020-06-21 20:31:45 +00:00
final USymbol result = entity.getUSymbol() == null ? getSkinParam().componentStyle().toUSymbol()
: entity.getUSymbol();
2017-04-05 17:37:42 +00:00
if (result == null) {
throw new IllegalArgumentException();
}
return result;
2013-12-10 19:36:50 +00:00
}
2015-09-28 20:42:17 +00:00
public Dimension2D getNameDimension(StringBounder stringBounder) {
2017-04-05 17:37:42 +00:00
if (hideText) {
return new Dimension2DDouble(0, 0);
}
2015-09-28 20:42:17 +00:00
return name.calculateDimension(stringBounder);
}
2013-12-10 19:36:50 +00:00
public Dimension2D calculateDimension(StringBounder stringBounder) {
2015-04-07 18:18:37 +00:00
return asSmall.calculateDimension(stringBounder);
2013-12-10 19:36:50 +00:00
}
2017-04-05 17:37:42 +00:00
@Override
public Margins getShield(StringBounder stringBounder) {
2017-12-11 21:02:10 +00:00
if (hideText == false) {
return Margins.NONE;
}
2018-05-01 17:26:04 +00:00
// if (useRankSame && hasSomeHorizontalLink((ILeaf) getEntity(), links)) {
// return Margins.NONE;
// }
if (isThereADoubleLink((ILeaf) getEntity(), links)) {
return Margins.NONE;
}
2018-10-21 19:44:14 +00:00
if (fixCircleLabelOverlapping == false && hasSomeHorizontalLinkVisible((ILeaf) getEntity(), links)) {
2017-12-11 21:02:10 +00:00
return Margins.NONE;
2017-04-05 17:37:42 +00:00
}
2017-12-11 21:02:10 +00:00
if (hasSomeHorizontalLinkDoubleDecorated((ILeaf) getEntity(), links)) {
return Margins.NONE;
}
final Dimension2D dimStereo = stereo.calculateDimension(stringBounder);
final Dimension2D dimDesc = desc.calculateDimension(stringBounder);
final Dimension2D dimSmall = asSmall.calculateDimension(stringBounder);
final double x = Math.max(dimStereo.getWidth(), dimDesc.getWidth());
double suppX = x - dimSmall.getWidth();
if (suppX < 1) {
suppX = 1;
}
final double y = MathUtils.max(1, dimDesc.getHeight(), dimStereo.getHeight());
return new Margins(suppX / 2, suppX / 2, y, y);
2017-04-05 17:37:42 +00:00
}
2017-06-05 11:27:21 +00:00
2018-05-01 17:26:04 +00:00
private boolean hasSomeHorizontalLinkVisible(ILeaf leaf, Collection<Link> links) {
2017-05-10 19:51:15 +00:00
for (Link link : links) {
2018-05-01 17:26:04 +00:00
if (link.getLength() == 1 && link.contains(leaf) && link.isInvis() == false) {
2017-05-10 19:51:15 +00:00
return true;
}
}
return false;
}
2018-05-01 17:26:04 +00:00
private boolean isThereADoubleLink(ILeaf leaf, Collection<Link> links) {
final Set<IEntity> others = new HashSet<IEntity>();
for (Link link : links) {
if (link.contains(leaf)) {
final IEntity other = link.getOther(leaf);
final boolean changed = others.add(other);
if (changed == false) {
return true;
}
}
}
return false;
}
2017-12-11 21:02:10 +00:00
private boolean hasSomeHorizontalLinkDoubleDecorated(ILeaf leaf, Collection<Link> links) {
for (Link link : links) {
if (link.getLength() == 1 && link.contains(leaf) && link.getType().isDoubleDecorated()) {
return true;
}
}
return false;
}
2013-12-10 19:36:50 +00:00
final public void drawU(UGraphic ug) {
2019-12-10 21:45:49 +00:00
ug.draw(new UComment("entity " + getEntity().getCodeGetName()));
2013-12-10 19:36:50 +00:00
if (url != null) {
ug.startUrl(url);
}
2021-02-02 10:12:15 +00:00
if (shapeType == ShapeType.HEXAGON) {
drawHexagon(ctx.apply(ug));
}
2015-04-07 18:18:37 +00:00
asSmall.drawU(ug);
2021-02-02 10:12:15 +00:00
2017-04-05 17:37:42 +00:00
if (hideText) {
final double space = 8;
final Dimension2D dimSmall = asSmall.calculateDimension(ug.getStringBounder());
final Dimension2D dimDesc = desc.calculateDimension(ug.getStringBounder());
2018-12-22 11:11:40 +00:00
final double posx1 = (dimSmall.getWidth() - dimDesc.getWidth()) / 2;
2020-12-01 21:39:27 +00:00
2020-10-12 20:56:58 +00:00
UGraphic ugDesc = ug.apply(new UTranslate(posx1, space + dimSmall.getHeight()));
ugDesc = UGraphicStencil.create(ugDesc, dimDesc);
desc.drawU(ugDesc);
2020-12-01 21:39:27 +00:00
2017-04-05 17:37:42 +00:00
final Dimension2D dimStereo = stereo.calculateDimension(ug.getStringBounder());
2018-12-22 11:11:40 +00:00
final double posx2 = (dimSmall.getWidth() - dimStereo.getWidth()) / 2;
stereo.drawU(ug.apply(new UTranslate(posx2, -space - dimStereo.getHeight())));
2017-04-05 17:37:42 +00:00
}
2015-04-07 18:18:37 +00:00
2013-12-10 19:36:50 +00:00
if (url != null) {
2020-05-17 21:15:50 +00:00
ug.closeUrl();
2013-12-10 19:36:50 +00:00
}
}
2021-02-02 10:12:15 +00:00
private void drawHexagon(UGraphic ug) {
if (bibliotekon == null) {
throw new IllegalStateException();
}
final SvekNode node = bibliotekon.getNode(getEntity());
final Shadowable hexagon = node.getPolygon();
hexagon.setDeltaShadow(ctx.getDeltaShadow());
ug.draw(hexagon);
}
2013-12-10 19:36:50 +00:00
public ShapeType getShapeType() {
2015-09-28 20:42:17 +00:00
return shapeType;
2013-12-10 19:36:50 +00:00
}
2018-12-22 11:11:40 +00:00
@Override
public double getOverscanX(StringBounder stringBounder) {
if (hideText) {
final Dimension2D dimSmall = asSmall.calculateDimension(stringBounder);
final Dimension2D dimDesc = desc.calculateDimension(stringBounder);
final Dimension2D dimStereo = stereo.calculateDimension(stringBounder);
final double posx1 = (dimSmall.getWidth() - dimDesc.getWidth()) / 2;
final double posx2 = (dimSmall.getWidth() - dimStereo.getWidth()) / 2;
return MathUtils.max(-posx1, -posx2, 0);
}
return 0;
}
2013-12-10 19:36:50 +00:00
}