plantuml/src/net/sourceforge/plantuml/svek/DecorateEntityImage.java

170 lines
5.9 KiB
Java
Raw Normal View History

2011-08-08 17:48:29 +00:00
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
2022-03-07 19:33:46 +00:00
* (C) Copyright 2009-2023, Arnaud Roques
2011-08-08 17:48:29 +00:00
*
2016-03-06 16:47:34 +00:00
* Project Info: http://plantuml.com
2011-08-08 17:48:29 +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
*
2011-08-08 17:48:29 +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
2013-12-10 19:36:50 +00:00
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
2011-08-08 17:48:29 +00:00
* 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.svek;
2021-11-09 17:47:19 +00:00
import java.util.Objects;
2011-08-08 17:48:29 +00:00
2022-09-12 20:08:34 +00:00
import net.sourceforge.plantuml.awt.geom.XDimension2D;
2015-06-07 10:23:10 +00:00
import net.sourceforge.plantuml.graphic.AbstractTextBlock;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.graphic.HorizontalAlignment;
2011-08-08 17:48:29 +00:00
import net.sourceforge.plantuml.graphic.StringBounder;
import net.sourceforge.plantuml.graphic.TextBlock;
2015-04-07 18:18:37 +00:00
import net.sourceforge.plantuml.graphic.VerticalAlignment;
2017-11-20 16:10:36 +00:00
import net.sourceforge.plantuml.ugraphic.MinMax;
2011-08-08 17:48:29 +00:00
import net.sourceforge.plantuml.ugraphic.UGraphic;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.ugraphic.UTranslate;
2020-03-18 10:50:02 +00:00
import net.sourceforge.plantuml.ugraphic.color.HColor;
2011-08-08 17:48:29 +00:00
2015-06-07 10:23:10 +00:00
public class DecorateEntityImage extends AbstractTextBlock implements TextBlockBackcolored {
2011-08-08 17:48:29 +00:00
2015-04-07 18:18:37 +00:00
private final TextBlock original;
2013-12-10 19:36:50 +00:00
private final HorizontalAlignment horizontal1;
2011-08-08 17:48:29 +00:00
private final TextBlock text1;
2013-12-10 19:36:50 +00:00
private final HorizontalAlignment horizontal2;
2011-08-08 17:48:29 +00:00
private final TextBlock text2;
2011-09-07 20:41:58 +00:00
private double deltaX;
private double deltaY;
2018-05-01 17:26:04 +00:00
public static TextBlock addTop(TextBlock original, TextBlock text, HorizontalAlignment horizontal) {
2013-12-10 19:36:50 +00:00
return new DecorateEntityImage(original, text, horizontal, null, null);
2011-08-08 17:48:29 +00:00
}
2018-05-01 17:26:04 +00:00
public static TextBlock addBottom(TextBlock original, TextBlock text, HorizontalAlignment horizontal) {
2013-12-10 19:36:50 +00:00
return new DecorateEntityImage(original, null, null, text, horizontal);
}
2018-05-01 17:26:04 +00:00
public static TextBlock add(TextBlock original, TextBlock text, HorizontalAlignment horizontal,
2015-04-07 18:18:37 +00:00
VerticalAlignment verticalAlignment) {
2022-10-05 20:32:57 +00:00
if (verticalAlignment == VerticalAlignment.TOP)
2015-04-07 18:18:37 +00:00
return addTop(original, text, horizontal);
2022-10-05 20:32:57 +00:00
2015-04-07 18:18:37 +00:00
return addBottom(original, text, horizontal);
}
2018-05-01 17:26:04 +00:00
public static TextBlock addTopAndBottom(TextBlock original, TextBlock text1, HorizontalAlignment horizontal1,
TextBlock text2, HorizontalAlignment horizontal2) {
return new DecorateEntityImage(original, text1, horizontal1, text2, horizontal2);
}
private DecorateEntityImage(TextBlock original, TextBlock text1, HorizontalAlignment horizontal1, TextBlock text2,
2015-04-07 18:18:37 +00:00
HorizontalAlignment horizontal2) {
2021-11-09 17:47:19 +00:00
this.original = Objects.requireNonNull(original);
2011-08-08 17:48:29 +00:00
this.horizontal1 = horizontal1;
this.text1 = text1;
this.horizontal2 = horizontal2;
this.text2 = text2;
}
2013-12-10 19:36:50 +00:00
public void drawU(UGraphic ug) {
2011-08-08 17:48:29 +00:00
final StringBounder stringBounder = ug.getStringBounder();
2022-09-12 20:08:34 +00:00
final XDimension2D dimOriginal = original.calculateDimension(stringBounder);
final XDimension2D dimText1 = getTextDim(text1, stringBounder);
final XDimension2D dimText2 = getTextDim(text2, stringBounder);
final XDimension2D dimTotal = calculateDimension(stringBounder);
2011-08-08 17:48:29 +00:00
2013-12-10 19:36:50 +00:00
final double yImage = dimText1.getHeight();
2011-08-08 17:48:29 +00:00
final double yText2 = yImage + dimOriginal.getHeight();
final double xImage = (dimTotal.getWidth() - dimOriginal.getWidth()) / 2;
if (text1 != null) {
final double xText1 = getTextX(dimText1, dimTotal, horizontal1);
2020-03-18 10:50:02 +00:00
text1.drawU(ug.apply(UTranslate.dx(xText1)));
2011-08-08 17:48:29 +00:00
}
2013-12-10 19:36:50 +00:00
original.drawU(ug.apply(new UTranslate(xImage, yImage)));
2011-09-07 20:41:58 +00:00
deltaX = xImage;
deltaY = yImage;
2011-08-08 17:48:29 +00:00
if (text2 != null) {
final double xText2 = getTextX(dimText2, dimTotal, horizontal2);
2013-12-10 19:36:50 +00:00
text2.drawU(ug.apply(new UTranslate(xText2, yText2)));
2011-08-08 17:48:29 +00:00
}
}
2022-09-12 20:08:34 +00:00
private XDimension2D getTextDim(TextBlock text, StringBounder stringBounder) {
2022-10-05 20:32:57 +00:00
if (text == null)
2022-09-12 20:08:34 +00:00
return new XDimension2D(0, 0);
2022-10-05 20:32:57 +00:00
2011-08-08 17:48:29 +00:00
return text.calculateDimension(stringBounder);
}
2022-09-12 20:08:34 +00:00
private double getTextX(final XDimension2D dimText, final XDimension2D dimTotal, HorizontalAlignment h) {
2022-10-05 20:32:57 +00:00
if (h == HorizontalAlignment.CENTER)
2011-08-08 17:48:29 +00:00
return (dimTotal.getWidth() - dimText.getWidth()) / 2;
2022-10-05 20:32:57 +00:00
else if (h == HorizontalAlignment.LEFT)
2011-08-08 17:48:29 +00:00
return 0;
2022-10-05 20:32:57 +00:00
else if (h == HorizontalAlignment.RIGHT)
2011-08-08 17:48:29 +00:00
return dimTotal.getWidth() - dimText.getWidth();
2022-10-05 20:32:57 +00:00
else
2011-08-08 17:48:29 +00:00
throw new IllegalStateException();
2022-10-05 20:32:57 +00:00
2011-08-08 17:48:29 +00:00
}
2020-03-18 10:50:02 +00:00
public HColor getBackcolor() {
2022-10-05 20:32:57 +00:00
if (original instanceof TextBlockBackcolored)
2015-04-07 18:18:37 +00:00
return ((TextBlockBackcolored) original).getBackcolor();
2022-10-05 20:32:57 +00:00
2015-04-07 18:18:37 +00:00
throw new UnsupportedOperationException();
2011-08-08 17:48:29 +00:00
}
2022-09-12 20:08:34 +00:00
public XDimension2D calculateDimension(StringBounder stringBounder) {
final XDimension2D dimOriginal = original.calculateDimension(stringBounder);
final XDimension2D dim1 = getTextDim(text1, stringBounder);
final XDimension2D dim2 = getTextDim(text2, stringBounder);
final XDimension2D dimText = XDimension2D.mergeTB(dim1, dim2);
return XDimension2D.mergeTB(dimOriginal, dimText);
2011-08-08 17:48:29 +00:00
}
2018-05-01 17:26:04 +00:00
2017-11-20 16:10:36 +00:00
@Override
public MinMax getMinMax(StringBounder stringBounder) {
return MinMax.fromDim(calculateDimension(stringBounder));
}
2011-08-08 17:48:29 +00:00
2011-09-07 20:41:58 +00:00
public final double getDeltaX() {
if (original instanceof DecorateEntityImage) {
return deltaX + ((DecorateEntityImage) original).deltaX;
}
return deltaX;
}
public final double getDeltaY() {
if (original instanceof DecorateEntityImage) {
return deltaY + ((DecorateEntityImage) original).deltaY;
}
return deltaY;
}
2011-08-08 17:48:29 +00:00
}