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

153 lines
5.0 KiB
Java
Raw Normal View History

2010-11-15 20:35:36 +00:00
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
2015-04-07 18:18:37 +00:00
* (C) Copyright 2009-2014, Arnaud Roques
2010-11-15 20:35:36 +00:00
*
* Project Info: http://plantuml.sourceforge.net
*
* 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
*
2015-07-11 09:32:49 +00:00
* Revision $Revision: 16518 $
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();
dummyStringBounder = StringBounderUtils.asStringBounder(gg);
}
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
}