2010-11-15 20:35:36 +00:00
|
|
|
/* ========================================================================
|
|
|
|
* PlantUML : a free UML diagram generator
|
|
|
|
* ========================================================================
|
|
|
|
*
|
2016-01-09 12:15:40 +00:00
|
|
|
* (C) Copyright 2009-2017, 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
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* [Java is a trademark or registered trademark of Sun Microsystems, Inc.
|
|
|
|
* in the United States and other countries.]
|
|
|
|
*
|
|
|
|
* Original Author: Arnaud Roques
|
|
|
|
*
|
2016-04-04 19:05:10 +00:00
|
|
|
* Revision $Revision: 19267 $
|
2010-11-15 20:35:36 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
package net.sourceforge.plantuml.graphic;
|
|
|
|
|
2013-12-10 19:36:50 +00:00
|
|
|
import java.awt.Font;
|
|
|
|
import java.awt.FontMetrics;
|
2011-09-07 20:41:58 +00:00
|
|
|
import java.awt.Graphics2D;
|
2013-12-10 19:36:50 +00:00
|
|
|
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;
|
2013-12-10 19:36:50 +00:00
|
|
|
import java.awt.image.BufferedImage;
|
2010-11-15 20:35:36 +00:00
|
|
|
|
2013-12-10 19:36:50 +00:00
|
|
|
import net.sourceforge.plantuml.Dimension2DDouble;
|
2011-09-07 20:41:58 +00:00
|
|
|
import net.sourceforge.plantuml.posimo.Positionable;
|
|
|
|
import net.sourceforge.plantuml.posimo.PositionableImpl;
|
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;
|
2010-11-15 20:35:36 +00:00
|
|
|
|
|
|
|
public class TextBlockUtils {
|
|
|
|
|
2011-09-07 20:41:58 +00:00
|
|
|
public static TextBlock withMargin(TextBlock textBlock, double marginX, double marginY) {
|
|
|
|
return new TextBlockMarged(textBlock, marginX, marginX, marginY, marginY);
|
|
|
|
}
|
|
|
|
|
2013-12-10 19:36:50 +00:00
|
|
|
public static TextBlock withMinWidth(TextBlock textBlock, double minWidth, HorizontalAlignment horizontalAlignment) {
|
|
|
|
return new TextBlockMinWidth(textBlock, minWidth, horizontalAlignment);
|
|
|
|
}
|
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, marginX1, marginX2, marginY1, marginY2);
|
2011-09-07 20:41:58 +00:00
|
|
|
}
|
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
|
|
|
public static TextBlock mergeLR(TextBlock b1, TextBlock b2, VerticalAlignment verticallAlignment) {
|
|
|
|
return new TextBlockHorizontal(b1, b2, verticallAlignment);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static TextBlock mergeTB(TextBlock b1, TextBlock b2, HorizontalAlignment horizontalAlignment) {
|
|
|
|
return new TextBlockVertical2(b1, b2, horizontalAlignment);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static MinMax getMinMax(TextBlock tb, StringBounder stringBounder) {
|
|
|
|
final LimitFinder limitFinder = new LimitFinder(stringBounder, false);
|
|
|
|
tb.drawU(limitFinder);
|
|
|
|
return limitFinder.getMinMax();
|
|
|
|
}
|
|
|
|
|
|
|
|
private static final Graphics2D gg;
|
|
|
|
private static final StringBounder dummyStringBounder;
|
|
|
|
|
|
|
|
static {
|
|
|
|
final BufferedImage imDummy = new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB);
|
|
|
|
gg = imDummy.createGraphics();
|
2016-04-04 19:05:10 +00:00
|
|
|
dummyStringBounder = StringBounderUtils.asStringBounder();
|
2013-12-10 19:36:50 +00:00
|
|
|
}
|
|
|
|
|
2015-08-05 20:17:01 +00:00
|
|
|
public static boolean isEmpty(TextBlock text) {
|
|
|
|
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 StringBounder getDummyStringBounder() {
|
|
|
|
return dummyStringBounder;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static FontRenderContext getFontRenderContext() {
|
|
|
|
return gg.getFontRenderContext();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static MinMax getMinMax(TextBlock tb) {
|
|
|
|
return getMinMax(tb, dummyStringBounder);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Dimension2D getDimension(TextBlock tb) {
|
|
|
|
return tb.calculateDimension(dummyStringBounder);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static LineMetrics getLineMetrics(UFont font, String text) {
|
|
|
|
return font.getLineMetrics(gg, text);
|
2011-09-07 20:41:58 +00:00
|
|
|
}
|
|
|
|
|
2013-12-10 19:36:50 +00:00
|
|
|
public static FontMetrics getFontMetrics(Font font) {
|
|
|
|
return 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Rectangle2D getInnerPosition(String member, StringBounder stringBounder) {
|
|
|
|
if (display.startsWith(member)) {
|
|
|
|
final Dimension2D dim = calculateDimension(stringBounder);
|
|
|
|
return new Rectangle2D.Double(0, 0, dim.getWidth(), dim.getHeight());
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2010-11-15 20:35:36 +00:00
|
|
|
}
|