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

217 lines
8.7 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-04-07 18:18:37 +00:00
* Revision $Revision: 15817 $
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;
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;
2015-04-07 18:18:37 +00:00
import net.sourceforge.plantuml.ISkinSimple;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.SpriteContainer;
2015-04-07 18:18:37 +00:00
import net.sourceforge.plantuml.creole.CreoleParser;
import net.sourceforge.plantuml.creole.Sheet;
import net.sourceforge.plantuml.creole.SheetBlock1;
import net.sourceforge.plantuml.creole.SheetBlock2;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.cucadiagram.Display;
2010-11-15 20:35:36 +00:00
import net.sourceforge.plantuml.cucadiagram.Stereotype;
2011-09-07 20:41:58 +00:00
import net.sourceforge.plantuml.posimo.Positionable;
import net.sourceforge.plantuml.posimo.PositionableImpl;
2010-11-15 20:35:36 +00:00
import net.sourceforge.plantuml.sequencediagram.MessageNumber;
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;
2015-04-07 18:18:37 +00:00
import net.sourceforge.plantuml.ugraphic.UStroke;
2010-11-15 20:35:36 +00:00
public class TextBlockUtils {
2013-12-10 19:36:50 +00:00
public static TextBlock create(Display texts, FontConfiguration fontConfiguration,
2015-04-07 18:18:37 +00:00
HorizontalAlignment horizontalAlignment, ISkinSimple spriteContainer) {
return create(texts, fontConfiguration, horizontalAlignment, spriteContainer, false);
}
public static TextBlock create(Display texts, FontConfiguration fontConfiguration,
HorizontalAlignment horizontalAlignment, ISkinSimple spriteContainer, boolean modeSimpleLine) {
2013-12-10 19:36:50 +00:00
if (texts == null) {
return empty(0, 0);
2010-11-15 20:35:36 +00:00
}
2015-04-07 18:18:37 +00:00
return create(texts, fontConfiguration, horizontalAlignment, spriteContainer, 0, modeSimpleLine, null, null);
2013-12-10 19:36:50 +00:00
}
public static TextBlock create(Display texts, FontConfiguration fontConfiguration,
2015-04-07 18:18:37 +00:00
HorizontalAlignment horizontalAlignment, ISkinSimple spriteContainer, double maxMessageSize,
boolean modeSimpleLine, UFont fontForStereotype, HtmlColor htmlColorForStereotype) {
if (texts.getNaturalHorizontalAlignment() != null) {
horizontalAlignment = texts.getNaturalHorizontalAlignment();
}
2013-12-10 19:36:50 +00:00
if (texts.size() > 0) {
if (texts.get(0) instanceof Stereotype) {
2015-04-07 18:18:37 +00:00
return createStereotype(texts, fontConfiguration, horizontalAlignment, spriteContainer, 0,
fontForStereotype, htmlColorForStereotype);
2013-12-10 19:36:50 +00:00
}
if (texts.get(texts.size() - 1) instanceof Stereotype) {
return createStereotype(texts, fontConfiguration, horizontalAlignment, spriteContainer,
2015-04-07 18:18:37 +00:00
texts.size() - 1, fontForStereotype, htmlColorForStereotype);
2013-12-10 19:36:50 +00:00
}
if (texts.get(0) instanceof MessageNumber) {
return createMessageNumber(texts, fontConfiguration, horizontalAlignment, spriteContainer,
maxMessageSize);
}
2010-11-15 20:35:36 +00:00
}
2015-04-07 18:18:37 +00:00
return getCreole(texts, fontConfiguration, horizontalAlignment, spriteContainer, maxMessageSize, modeSimpleLine);
}
private static TextBlock getCreole(Display texts, FontConfiguration fontConfiguration,
HorizontalAlignment horizontalAlignment, ISkinSimple spriteContainer, double maxMessageSize,
boolean modeSimpleLine) {
final Sheet sheet = new CreoleParser(fontConfiguration, horizontalAlignment, spriteContainer, modeSimpleLine)
.createSheet(texts);
final SheetBlock1 sheetBlock1 = new SheetBlock1(sheet, maxMessageSize, spriteContainer == null ? 0
: spriteContainer.getPadding());
return new SheetBlock2(sheetBlock1, sheetBlock1, new UStroke(1.5));
2010-11-15 20:35:36 +00:00
}
2013-12-10 19:36:50 +00:00
private static TextBlock createMessageNumber(Display texts, FontConfiguration fontConfiguration,
2015-04-07 18:18:37 +00:00
HorizontalAlignment horizontalAlignment, ISkinSimple spriteContainer, double maxMessageSize) {
TextBlock tb1 = getCreole(texts.subList(0, 1), fontConfiguration, horizontalAlignment, spriteContainer,
maxMessageSize, false);
tb1 = TextBlockUtils.withMargin(tb1, 0, 4, 0, 0);
final TextBlock tb2 = getCreole(texts.subList(1, texts.size()), fontConfiguration, horizontalAlignment,
spriteContainer, maxMessageSize, false);
return TextBlockUtils.mergeLR(tb1, tb2, VerticalAlignment.CENTER);
2010-11-15 20:35:36 +00:00
}
2013-12-10 19:36:50 +00:00
private static TextBlock createStereotype(Display texts, FontConfiguration fontConfiguration,
2015-04-07 18:18:37 +00:00
HorizontalAlignment horizontalAlignment, SpriteContainer spriteContainer, int position,
UFont fontForStereotype, HtmlColor htmlColorForStereotype) {
2013-12-10 19:36:50 +00:00
final Stereotype stereotype = (Stereotype) texts.get(position);
2010-11-15 20:35:36 +00:00
if (stereotype.isSpotted()) {
2013-12-10 19:36:50 +00:00
final CircledCharacter circledCharacter = new CircledCharacter(stereotype.getCharacter(),
stereotype.getRadius(), stereotype.getCircledFont(), stereotype.getHtmlColor(), null,
fontConfiguration.getColor());
2015-04-07 18:18:37 +00:00
if (stereotype.getLabel(false) == null) {
2011-01-23 19:36:52 +00:00
return new TextBlockSpotted(circledCharacter, texts.subList(1, texts.size()), fontConfiguration,
2013-12-10 19:36:50 +00:00
horizontalAlignment, spriteContainer);
2010-11-15 20:35:36 +00:00
}
2013-12-10 19:36:50 +00:00
return new TextBlockSpotted(circledCharacter, texts, fontConfiguration, horizontalAlignment,
spriteContainer);
2010-11-15 20:35:36 +00:00
}
2015-04-07 18:18:37 +00:00
return new TextBlockSimple(texts, fontConfiguration, horizontalAlignment, spriteContainer, 0, fontForStereotype, htmlColorForStereotype);
2010-11-15 20:35:36 +00:00
}
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) {
2011-09-07 20:41:58 +00:00
return new TextBlock() {
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
}
2010-11-15 20:35:36 +00:00
}