plantuml/src/net/sourceforge/plantuml/klimt/shape/TextBlockUtils.java

237 lines
7.6 KiB
Java
Raw Normal View History

2010-11-15 20:35:36 +00:00
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
2023-02-22 18:43:48 +00:00
* (C) Copyright 2009-2024, Arnaud Roques
2010-11-15 20:35:36 +00:00
*
2023-02-22 18:43:48 +00:00
* Project Info: https://plantuml.com
2010-11-15 20:35:36 +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:
*
2023-02-22 18:43:48 +00:00
* https://plantuml.com/patreon (only 1$ per month!)
* https://plantuml.com/paypal
2017-03-15 19:13:31 +00:00
*
2010-11-15 20:35:36 +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
2010-11-15 20:35:36 +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
*
*
*/
2023-02-22 18:43:48 +00:00
package net.sourceforge.plantuml.klimt.shape;
2010-11-15 20:35:36 +00:00
2023-02-22 18:43:48 +00:00
import net.atmp.InnerStrategy;
2023-02-02 17:59:43 +00:00
import net.sourceforge.plantuml.klimt.UStroke;
import net.sourceforge.plantuml.klimt.color.HColor;
2023-02-22 18:43:48 +00:00
import net.sourceforge.plantuml.klimt.drawing.LimitFinder;
import net.sourceforge.plantuml.klimt.drawing.UGraphic;
2023-02-02 17:59:43 +00:00
import net.sourceforge.plantuml.klimt.font.StringBounder;
import net.sourceforge.plantuml.klimt.geom.HorizontalAlignment;
2023-02-26 18:51:17 +00:00
import net.sourceforge.plantuml.klimt.geom.MagneticBorder;
2023-02-02 17:59:43 +00:00
import net.sourceforge.plantuml.klimt.geom.MinMax;
import net.sourceforge.plantuml.klimt.geom.Positionable;
import net.sourceforge.plantuml.klimt.geom.PositionableImpl;
2023-02-22 18:43:48 +00:00
import net.sourceforge.plantuml.klimt.geom.VerticalAlignment;
import net.sourceforge.plantuml.klimt.geom.XDimension2D;
import net.sourceforge.plantuml.klimt.geom.XPoint2D;
import net.sourceforge.plantuml.klimt.geom.XRectangle2D;
import net.sourceforge.plantuml.style.ClockwiseTopRightBottomLeft;
2010-11-15 20:35:36 +00:00
public class TextBlockUtils {
2020-05-17 21:15:50 +00:00
public static final TextBlock EMPTY_TEXT_BLOCK = TextBlockUtils.empty(0, 0);
2020-04-26 18:31:41 +00:00
public static TextBlock bordered(TextBlock textBlock, UStroke stroke, HColor borderColor, HColor backgroundColor,
2022-06-27 16:33:45 +00:00
double cornersize, ClockwiseTopRightBottomLeft margins, String id) {
return new TextBlockBordered(textBlock, stroke, borderColor, backgroundColor, cornersize, margins, id);
2016-11-04 21:39:29 +00:00
}
public static TextBlock withMargin(TextBlock textBlock, double marginX, double marginY) {
2022-04-10 19:24:55 +00:00
if (marginX == 0 && marginY == 0)
2020-12-14 18:31:06 +00:00
return textBlock;
2022-04-10 19:24:55 +00:00
return new TextBlockMarged(textBlock, marginY, marginX, marginY, marginX);
}
public static TextBlock withMargin(TextBlock textBlock, ClockwiseTopRightBottomLeft margins) {
return new TextBlockMarged(textBlock, margins);
2013-12-10 19:36:50 +00:00
}
2010-11-15 20:35:36 +00:00
2013-12-10 19:36:50 +00:00
public static TextBlock withMargin(TextBlock textBlock, double marginX1, double marginX2, double marginY1,
double marginY2) {
return new TextBlockMarged(textBlock, marginY1, marginX2, marginY2, marginX1);
2011-09-07 20:41:58 +00:00
}
2020-02-18 21:24:31 +00:00
public static TextBlock withMinWidth(TextBlock textBlock, double minWidth,
HorizontalAlignment horizontalAlignment) {
2016-11-04 21:39:29 +00:00
return new TextBlockMinWidth(textBlock, minWidth, horizontalAlignment);
}
2013-12-10 19:36:50 +00:00
public static TextBlock empty(final double width, final double height) {
2015-06-07 10:23:10 +00:00
return new AbstractTextBlock() {
2013-12-10 19:36:50 +00:00
public void drawU(UGraphic ug) {
2011-09-07 20:41:58 +00:00
}
2022-09-12 20:08:34 +00:00
public XDimension2D calculateDimension(StringBounder stringBounder) {
return new XDimension2D(width, height);
2011-09-07 20:41:58 +00:00
}
2022-09-12 20:08:34 +00:00
public XRectangle2D getInnerPosition(String member, StringBounder stringBounder, InnerStrategy strategy) {
return null;
}
2011-09-07 20:41:58 +00:00
};
}
2022-09-12 20:08:34 +00:00
public static Positionable asPositionable(TextBlock textBlock, StringBounder stringBounder, XPoint2D pt) {
2013-12-10 19:36:50 +00:00
return new PositionableImpl(pt, textBlock.calculateDimension(stringBounder));
}
2022-09-12 20:08:34 +00:00
public static Positionable asPositionable(XDimension2D dim, StringBounder stringBounder, XPoint2D pt) {
2020-02-18 21:24:31 +00:00
return new PositionableImpl(pt, dim);
}
2013-12-10 19:36:50 +00:00
public static TextBlock mergeLR(TextBlock b1, TextBlock b2, VerticalAlignment verticallAlignment) {
2022-10-18 20:57:44 +00:00
if (b1 == EMPTY_TEXT_BLOCK)
2020-05-17 21:15:50 +00:00
return b2;
2022-10-18 20:57:44 +00:00
if (b2 == EMPTY_TEXT_BLOCK)
2020-05-17 21:15:50 +00:00
return b1;
2022-10-18 20:57:44 +00:00
2013-12-10 19:36:50 +00:00
return new TextBlockHorizontal(b1, b2, verticallAlignment);
}
public static TextBlock mergeTB(TextBlock b1, TextBlock b2, HorizontalAlignment horizontalAlignment) {
2022-10-18 20:57:44 +00:00
if (b1 == EMPTY_TEXT_BLOCK)
2020-05-17 21:15:50 +00:00
return b2;
2022-10-18 20:57:44 +00:00
if (b2 == EMPTY_TEXT_BLOCK)
2020-05-17 21:15:50 +00:00
return b1;
2022-10-18 20:57:44 +00:00
2013-12-10 19:36:50 +00:00
return new TextBlockVertical2(b1, b2, horizontalAlignment);
}
2020-09-19 15:43:24 +00:00
public static TextBlock mergeTB(TextBlock b1, UImage image, HorizontalAlignment horizontalAlignment) {
2022-10-18 20:57:44 +00:00
if (b1 == EMPTY_TEXT_BLOCK)
2020-09-19 15:43:24 +00:00
throw new IllegalArgumentException();
2022-10-18 20:57:44 +00:00
2020-09-19 15:43:24 +00:00
return new TextBlockVertical2(b1, image, horizontalAlignment);
}
2013-12-10 19:36:50 +00:00
2020-02-18 21:24:31 +00:00
// public static TextBlockBackcolored mergeColoredTB(TextBlockBackcolored b1,
// TextBlockBackcolored b2,
2019-05-24 19:59:31 +00:00
// HorizontalAlignment horizontalAlignment) {
// return addBackcolor(mergeTB(b1, b2, horizontalAlignment), b1.getBackcolor());
// }
2020-04-26 18:31:41 +00:00
public static MinMax getMinMax(UDrawable tb, StringBounder stringBounder, boolean initToZero) {
2022-03-19 12:48:23 +00:00
final LimitFinder limitFinder = LimitFinder.create(stringBounder, initToZero);
2013-12-10 19:36:50 +00:00
tb.drawU(limitFinder);
return limitFinder.getMinMax();
}
2016-08-25 20:45:37 +00:00
public static boolean isEmpty(TextBlock text, StringBounder dummyStringBounder) {
2015-08-05 20:17:01 +00:00
if (text == null) {
return true;
}
2022-09-12 20:08:34 +00:00
final XDimension2D dim = text.calculateDimension(dummyStringBounder);
2015-08-05 20:17:01 +00:00
return dim.getHeight() == 0 && dim.getWidth() == 0;
}
2015-06-07 10:23:10 +00:00
public static TextBlock fullInnerPosition(final TextBlock bloc, final String display) {
return new TextBlock() {
public void drawU(UGraphic ug) {
bloc.drawU(ug);
}
2022-09-12 20:08:34 +00:00
public XDimension2D calculateDimension(StringBounder stringBounder) {
2015-06-07 10:23:10 +00:00
return bloc.calculateDimension(stringBounder);
}
2018-09-23 12:15:14 +00:00
2017-11-20 16:10:36 +00:00
public MinMax getMinMax(StringBounder stringBounder) {
return bloc.getMinMax(stringBounder);
}
2015-06-07 10:23:10 +00:00
2022-09-12 20:08:34 +00:00
public XRectangle2D getInnerPosition(String member, StringBounder stringBounder, InnerStrategy strategy) {
2017-04-05 17:37:42 +00:00
if (strategy.check(display, member)) {
2022-09-12 20:08:34 +00:00
final XDimension2D dim = calculateDimension(stringBounder);
return new XRectangle2D(0, 0, dim.getWidth(), dim.getHeight());
2015-06-07 10:23:10 +00:00
}
return null;
}
2017-04-05 17:37:42 +00:00
2023-02-26 18:51:17 +00:00
public MagneticBorder getMagneticBorder() {
return bloc.getMagneticBorder();
}
public HColor getBackcolor() {
return bloc.getBackcolor();
}
2015-06-07 10:23:10 +00:00
};
}
2023-02-26 18:51:17 +00:00
public static TextBlock addBackcolor(final TextBlock text, final HColor backColor) {
return new TextBlock() {
2018-09-23 12:15:14 +00:00
public void drawU(UGraphic ug) {
text.drawU(ug);
}
public MinMax getMinMax(StringBounder stringBounder) {
return text.getMinMax(stringBounder);
}
2022-09-12 20:08:34 +00:00
public XRectangle2D getInnerPosition(String member, StringBounder stringBounder, InnerStrategy strategy) {
2018-09-23 12:15:14 +00:00
return text.getInnerPosition(member, stringBounder, strategy);
}
2022-09-12 20:08:34 +00:00
public XDimension2D calculateDimension(StringBounder stringBounder) {
2018-09-23 12:15:14 +00:00
return text.calculateDimension(stringBounder);
}
2020-03-18 10:50:02 +00:00
public HColor getBackcolor() {
2018-09-23 12:15:14 +00:00
return backColor;
}
2023-02-26 18:51:17 +00:00
public MagneticBorder getMagneticBorder() {
return text.getMagneticBorder();
}
2018-09-23 12:15:14 +00:00
};
}
public static TextBlock fromUImage(final UImage image) {
2023-02-26 18:51:17 +00:00
return new AbstractTextBlock() {
2018-09-23 12:15:14 +00:00
public void drawU(UGraphic ug) {
ug.draw(image);
}
2022-09-12 20:08:34 +00:00
public XDimension2D calculateDimension(StringBounder stringBounder) {
return new XDimension2D(image.getWidth(), image.getHeight());
2018-09-23 12:15:14 +00:00
}
public MinMax getMinMax(StringBounder stringBounder) {
return MinMax.fromMax(image.getWidth(), image.getHeight());
}
};
}
2010-11-15 20:35:36 +00:00
}