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

271 lines
9.2 KiB
Java
Raw Normal View History

2010-11-15 20:35:36 +00:00
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
2019-01-16 18:34:41 +00:00
* (C) Copyright 2009-2020, Arnaud Roques
2010-11-15 20:35:36 +00:00
*
2016-03-06 16:47:34 +00:00
* Project Info: http://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:
*
* http://plantuml.com/patreon (only 1$ per month!)
* http://plantuml.com/paypal
*
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
*
*
*/
package net.sourceforge.plantuml.graphic;
2013-12-10 19:36:50 +00:00
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.font.FontRenderContext;
import java.awt.font.LineMetrics;
2011-09-07 20:41:58 +00:00
import java.awt.geom.Dimension2D;
import java.awt.geom.Point2D;
2015-06-07 10:23:10 +00:00
import java.awt.geom.Rectangle2D;
2010-11-15 20:35:36 +00:00
2016-11-04 21:39:29 +00:00
import net.sourceforge.plantuml.ColorParam;
2018-07-27 21:56:46 +00:00
import net.sourceforge.plantuml.CornerParam;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.Dimension2DDouble;
2020-05-30 15:20:23 +00:00
import net.sourceforge.plantuml.FileFormat;
2016-11-04 21:39:29 +00:00
import net.sourceforge.plantuml.ISkinParam;
import net.sourceforge.plantuml.LineParam;
2019-07-14 20:09:26 +00:00
import net.sourceforge.plantuml.SkinParam;
2016-11-04 21:39:29 +00:00
import net.sourceforge.plantuml.cucadiagram.Display;
2011-09-07 20:41:58 +00:00
import net.sourceforge.plantuml.posimo.Positionable;
import net.sourceforge.plantuml.posimo.PositionableImpl;
2016-11-04 21:39:29 +00:00
import net.sourceforge.plantuml.skin.rose.Rose;
import net.sourceforge.plantuml.style.ClockwiseTopRightBottomLeft;
2018-09-23 12:15:14 +00:00
import net.sourceforge.plantuml.svek.TextBlockBackcolored;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.ugraphic.LimitFinder;
import net.sourceforge.plantuml.ugraphic.MinMax;
import net.sourceforge.plantuml.ugraphic.UFont;
2011-09-07 20:41:58 +00:00
import net.sourceforge.plantuml.ugraphic.UGraphic;
2018-09-23 12:15:14 +00:00
import net.sourceforge.plantuml.ugraphic.UImage;
2016-11-04 21:39:29 +00:00
import net.sourceforge.plantuml.ugraphic.UStroke;
2020-03-18 10:50:02 +00:00
import net.sourceforge.plantuml.ugraphic.color.HColor;
import net.sourceforge.plantuml.ugraphic.color.HColorUtils;
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,
double cornersize) {
2016-11-04 21:39:29 +00:00
return new TextBlockBordered(textBlock, stroke, borderColor, backgroundColor, cornersize);
2011-09-07 20:41:58 +00:00
}
2020-04-26 18:31:41 +00:00
public static TextBlock bordered(TextBlock textBlock, UStroke stroke, HColor borderColor, HColor backgroundColor,
double cornersize, double marginX, double marginY) {
2019-07-14 20:09:26 +00:00
return new TextBlockBordered(textBlock, stroke, borderColor, backgroundColor, cornersize, marginX, marginY);
}
public static TextBlock bordered(TextBlock textBlock, UStroke stroke, HColor borderColor, HColor backgroundColor,
double cornersize, ClockwiseTopRightBottomLeft margins) {
return new TextBlockBordered(textBlock, stroke, borderColor, backgroundColor, cornersize, margins);
}
2016-11-04 21:39:29 +00:00
public static TextBlock title(FontConfiguration font, Display stringsToDisplay, ISkinParam skinParam) {
2019-08-26 17:07:21 +00:00
if (SkinParam.USE_STYLES()) {
throw new UnsupportedOperationException();
}
2016-11-04 21:39:29 +00:00
UStroke stroke = skinParam.getThickness(LineParam.titleBorder, null);
final Rose rose = new Rose();
2020-03-18 10:50:02 +00:00
HColor borderColor = rose.getHtmlColor(skinParam, ColorParam.titleBorder);
final HColor backgroundColor = rose.getHtmlColor(skinParam, ColorParam.titleBackground);
2016-11-04 21:39:29 +00:00
final TextBlockTitle result = new TextBlockTitle(font, stringsToDisplay, skinParam);
if (stroke == null && borderColor == null) {
return result;
}
if (stroke == null) {
stroke = new UStroke(1.5);
}
if (borderColor == null) {
2020-03-18 10:50:02 +00:00
borderColor = HColorUtils.BLACK;
2016-11-04 21:39:29 +00:00
}
2018-04-06 20:36:30 +00:00
final double corner = skinParam.getRoundCorner(CornerParam.titleBorder, null);
2016-11-04 21:39:29 +00:00
return withMargin(bordered(result, stroke, borderColor, backgroundColor, corner), 2, 2);
}
public static TextBlock withMargin(TextBlock textBlock, double marginX, double marginY) {
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
}
2013-12-10 19:36:50 +00:00
public Dimension2D calculateDimension(StringBounder stringBounder) {
return new Dimension2DDouble(width, height);
2011-09-07 20:41:58 +00:00
}
};
}
2013-12-10 19:36:50 +00:00
public static Positionable asPositionable(TextBlock textBlock, StringBounder stringBounder, Point2D pt) {
return new PositionableImpl(pt, textBlock.calculateDimension(stringBounder));
}
2020-02-18 21:24:31 +00:00
public static Positionable asPositionable(Dimension2D dim, StringBounder stringBounder, Point2D pt) {
return new PositionableImpl(pt, dim);
}
2013-12-10 19:36:50 +00:00
public static TextBlock mergeLR(TextBlock b1, TextBlock b2, VerticalAlignment verticallAlignment) {
2020-05-17 21:15:50 +00:00
if (b1 == EMPTY_TEXT_BLOCK) {
return b2;
}
if (b2 == EMPTY_TEXT_BLOCK) {
return b1;
}
2013-12-10 19:36:50 +00:00
return new TextBlockHorizontal(b1, b2, verticallAlignment);
}
public static TextBlock mergeTB(TextBlock b1, TextBlock b2, HorizontalAlignment horizontalAlignment) {
2020-05-17 21:15:50 +00:00
if (b1 == EMPTY_TEXT_BLOCK) {
return b2;
}
if (b2 == EMPTY_TEXT_BLOCK) {
return b1;
}
2013-12-10 19:36:50 +00:00
return new TextBlockVertical2(b1, b2, horizontalAlignment);
}
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) {
final LimitFinder limitFinder = new LimitFinder(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;
}
final Dimension2D dim = text.calculateDimension(dummyStringBounder);
return dim.getHeight() == 0 && dim.getWidth() == 0;
}
2013-12-10 19:36:50 +00:00
public static FontRenderContext getFontRenderContext() {
2020-05-30 15:20:23 +00:00
return FileFormat.gg.getFontRenderContext();
2013-12-10 19:36:50 +00:00
}
public static LineMetrics getLineMetrics(UFont font, String text) {
2020-05-30 15:20:23 +00:00
return font.getLineMetrics(FileFormat.gg, text);
2011-09-07 20:41:58 +00:00
}
2013-12-10 19:36:50 +00:00
public static FontMetrics getFontMetrics(Font font) {
2020-05-30 15:20:23 +00:00
return FileFormat.gg.getFontMetrics(font);
2011-09-07 20:41:58 +00:00
}
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);
}
public Dimension2D calculateDimension(StringBounder stringBounder) {
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
2017-04-05 17:37:42 +00:00
public Rectangle2D getInnerPosition(String member, StringBounder stringBounder, InnerStrategy strategy) {
if (strategy.check(display, member)) {
2015-06-07 10:23:10 +00:00
final Dimension2D dim = calculateDimension(stringBounder);
return new Rectangle2D.Double(0, 0, dim.getWidth(), dim.getHeight());
}
return null;
}
2017-04-05 17:37:42 +00:00
2015-06-07 10:23:10 +00:00
};
}
2020-03-18 10:50:02 +00:00
public static TextBlockBackcolored addBackcolor(final TextBlock text, final HColor backColor) {
2018-09-23 12:15:14 +00:00
return new TextBlockBackcolored() {
public void drawU(UGraphic ug) {
text.drawU(ug);
}
public MinMax getMinMax(StringBounder stringBounder) {
return text.getMinMax(stringBounder);
}
public Rectangle2D getInnerPosition(String member, StringBounder stringBounder, InnerStrategy strategy) {
return text.getInnerPosition(member, stringBounder, strategy);
}
public Dimension2D calculateDimension(StringBounder stringBounder) {
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;
}
};
}
public static TextBlock fromUImage(final UImage image) {
return new TextBlock() {
public void drawU(UGraphic ug) {
ug.draw(image);
}
public Dimension2D calculateDimension(StringBounder stringBounder) {
return new Dimension2DDouble(image.getWidth(), image.getHeight());
}
public MinMax getMinMax(StringBounder stringBounder) {
return MinMax.fromMax(image.getWidth(), image.getHeight());
}
public Rectangle2D getInnerPosition(String member, StringBounder stringBounder, InnerStrategy strategy) {
return null;
}
};
}
2010-11-15 20:35:36 +00:00
}