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

171 lines
6.0 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
import net.sourceforge.plantuml.Dimension2DDouble;
2022-05-04 17:52:00 +00:00
import net.sourceforge.plantuml.awt.geom.Dimension2D;
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) {
if (verticalAlignment == VerticalAlignment.TOP) {
return addTop(original, text, horizontal);
}
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();
2013-12-10 19:36:50 +00:00
final Dimension2D dimOriginal = original.calculateDimension(stringBounder);
2011-08-08 17:48:29 +00:00
final Dimension2D dimText1 = getTextDim(text1, stringBounder);
final Dimension2D dimText2 = getTextDim(text2, stringBounder);
2013-12-10 19:36:50 +00:00
final Dimension2D 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
}
}
private Dimension2D getTextDim(TextBlock text, StringBounder stringBounder) {
if (text == null) {
return new Dimension2DDouble(0, 0);
}
return text.calculateDimension(stringBounder);
}
2013-12-10 19:36:50 +00:00
private double getTextX(final Dimension2D dimText, final Dimension2D dimTotal, HorizontalAlignment h) {
if (h == HorizontalAlignment.CENTER) {
2011-08-08 17:48:29 +00:00
return (dimTotal.getWidth() - dimText.getWidth()) / 2;
2013-12-10 19:36:50 +00:00
} else if (h == HorizontalAlignment.LEFT) {
2011-08-08 17:48:29 +00:00
return 0;
2013-12-10 19:36:50 +00:00
} else if (h == HorizontalAlignment.RIGHT) {
2011-08-08 17:48:29 +00:00
return dimTotal.getWidth() - dimText.getWidth();
} else {
throw new IllegalStateException();
}
}
2020-03-18 10:50:02 +00:00
public HColor getBackcolor() {
2015-04-07 18:18:37 +00:00
if (original instanceof TextBlockBackcolored) {
return ((TextBlockBackcolored) original).getBackcolor();
}
throw new UnsupportedOperationException();
2011-08-08 17:48:29 +00:00
}
2013-12-10 19:36:50 +00:00
public Dimension2D calculateDimension(StringBounder stringBounder) {
final Dimension2D dimOriginal = original.calculateDimension(stringBounder);
2021-11-09 17:47:19 +00:00
final Dimension2D dim1 = getTextDim(text1, stringBounder);
final Dimension2D dim2 = getTextDim(text2, stringBounder);
final Dimension2D dimText = Dimension2DDouble.mergeTB(dim1, dim2);
2011-08-08 17:48:29 +00:00
return Dimension2DDouble.mergeTB(dimOriginal, dimText);
}
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
}