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

201 lines
7.1 KiB
Java
Raw Normal View History

2010-11-15 20:35:36 +00:00
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
* (C) Copyright 2009, Arnaud Roques
*
* 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
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* 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
*
2011-09-07 20:41:58 +00:00
* Revision $Revision: 7144 $
2010-11-15 20:35:36 +00:00
*
*/
package net.sourceforge.plantuml.graphic;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Dimension2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.List;
import net.sourceforge.plantuml.Dimension2DDouble;
import net.sourceforge.plantuml.EmptyImageBuilder;
import net.sourceforge.plantuml.FileFormat;
2011-01-05 18:23:06 +00:00
import net.sourceforge.plantuml.FileFormatOption;
2011-08-08 17:48:29 +00:00
import net.sourceforge.plantuml.StringUtils;
import net.sourceforge.plantuml.directdot.DotText;
import net.sourceforge.plantuml.eps.EpsStrategy;
2010-11-15 20:35:36 +00:00
import net.sourceforge.plantuml.png.PngIO;
2011-08-08 17:48:29 +00:00
import net.sourceforge.plantuml.svek.IEntityImage;
import net.sourceforge.plantuml.svek.ShapeType;
import net.sourceforge.plantuml.ugraphic.ColorMapper;
import net.sourceforge.plantuml.ugraphic.ColorMapperIdentity;
import net.sourceforge.plantuml.ugraphic.UFont;
2010-11-15 20:35:36 +00:00
import net.sourceforge.plantuml.ugraphic.UGraphic;
import net.sourceforge.plantuml.ugraphic.UImage;
2011-08-08 17:48:29 +00:00
import net.sourceforge.plantuml.ugraphic.eps.UGraphicEps;
2010-11-15 20:35:36 +00:00
import net.sourceforge.plantuml.ugraphic.g2d.UGraphicG2d;
import net.sourceforge.plantuml.ugraphic.svg.UGraphicSvg;
import net.sourceforge.plantuml.ugraphic.txt.UGraphicTxt;
2011-08-08 17:48:29 +00:00
public class GraphicStrings implements IEntityImage {
2010-11-15 20:35:36 +00:00
2011-08-08 17:48:29 +00:00
private final HtmlColor background;
2010-11-15 20:35:36 +00:00
2011-08-08 17:48:29 +00:00
private final UFont font;
2010-11-15 20:35:36 +00:00
2011-08-08 17:48:29 +00:00
private final HtmlColor green;
2010-11-15 20:35:36 +00:00
private final List<String> strings;
private final BufferedImage image;
private final GraphicPosition position;
private final boolean disableTextAliasing;
2011-08-08 17:48:29 +00:00
private final ColorMapper colorMapper = new ColorMapperIdentity();
2010-11-15 20:35:36 +00:00
public GraphicStrings(List<String> strings) {
2011-08-08 17:48:29 +00:00
this(strings, new UFont("SansSerif", Font.BOLD, 14), HtmlColor.getColorIfValid("#33FF02"), HtmlColor.BLACK,
2010-11-15 20:35:36 +00:00
null, null, false);
}
public GraphicStrings(List<String> strings, BufferedImage image) {
2011-08-08 17:48:29 +00:00
this(strings, new UFont("SansSerif", Font.BOLD, 14), HtmlColor.getColorIfValid("#33FF02"), HtmlColor.BLACK,
2010-11-15 20:35:36 +00:00
image, null, false);
}
2011-08-08 17:48:29 +00:00
public GraphicStrings(List<String> strings, UFont font, HtmlColor green, HtmlColor background,
boolean disableTextAliasing) {
2010-11-15 20:35:36 +00:00
this(strings, font, green, background, null, null, disableTextAliasing);
}
2011-08-08 17:48:29 +00:00
public GraphicStrings(List<String> strings, UFont font, HtmlColor green, HtmlColor background, BufferedImage image,
2010-11-15 20:35:36 +00:00
GraphicPosition position, boolean disableTextAliasing) {
this.strings = strings;
this.font = font;
this.green = green;
this.background = background;
this.image = image;
this.position = position;
this.disableTextAliasing = disableTextAliasing;
}
2011-01-05 18:23:06 +00:00
public void writeImage(OutputStream os, FileFormatOption fileFormat) throws IOException {
2010-11-15 20:35:36 +00:00
writeImage(os, null, fileFormat);
}
2011-01-05 18:23:06 +00:00
public void writeImage(OutputStream os, String metadata, FileFormatOption fileFormatOption) throws IOException {
final FileFormat fileFormat = fileFormatOption.getFileFormat();
2010-11-15 20:35:36 +00:00
if (fileFormat == FileFormat.PNG) {
final BufferedImage im = createImage();
2011-01-05 18:23:06 +00:00
PngIO.write(im, os, metadata, 96);
2011-08-08 17:48:29 +00:00
} else if (fileFormat == FileFormat.SVG) {
final UGraphicSvg svg = new UGraphicSvg(colorMapper, StringUtils.getAsHtml(colorMapper
.getMappedColor(background)), false);
2010-11-15 20:35:36 +00:00
drawU(svg);
svg.createXml(os);
} else if (fileFormat == FileFormat.ATXT || fileFormat == FileFormat.UTXT) {
final UGraphicTxt txt = new UGraphicTxt();
drawU(txt);
txt.getCharArea().print(new PrintStream(os));
2011-08-08 17:48:29 +00:00
} else if (fileFormat == FileFormat.EPS) {
final UGraphicEps ug = new UGraphicEps(colorMapper, EpsStrategy.getDefault2());
drawU(ug);
os.write(ug.getEPSCode().getBytes());
} else if (fileFormat == FileFormat.DOT) {
final DotText dotText = new DotText(strings, HtmlColor.getColorIfValid("#33FF02"), HtmlColor.BLACK);
final StringBuilder sb = new StringBuilder();
dotText.generateDot(sb);
os.write(sb.toString().getBytes());
2010-11-15 20:35:36 +00:00
} else {
throw new UnsupportedOperationException();
}
}
private BufferedImage createImage() {
2011-08-08 17:48:29 +00:00
EmptyImageBuilder builder = new EmptyImageBuilder(10, 10, colorMapper.getMappedColor(background));
2010-11-15 20:35:36 +00:00
// BufferedImage im = builder.getBufferedImage();
Graphics2D g2d = builder.getGraphics2D();
2011-08-08 17:48:29 +00:00
final Dimension2D size = drawU(new UGraphicG2d(colorMapper, g2d, null, 1.0));
2010-11-15 20:35:36 +00:00
g2d.dispose();
2011-08-08 17:48:29 +00:00
builder = new EmptyImageBuilder(size.getWidth(), size.getHeight(), colorMapper.getMappedColor(background));
2010-11-15 20:35:36 +00:00
final BufferedImage im = builder.getBufferedImage();
g2d = builder.getGraphics2D();
if (disableTextAliasing) {
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
}
2011-08-08 17:48:29 +00:00
drawU(new UGraphicG2d(colorMapper, g2d, null, 1.0));
2010-11-15 20:35:36 +00:00
g2d.dispose();
return im;
}
public Dimension2D drawU(final UGraphic ug) {
2011-01-23 19:36:52 +00:00
final TextBlock textBlock = TextBlockUtils.create(strings, new FontConfiguration(font, green),
HorizontalAlignement.LEFT);
2010-11-15 20:35:36 +00:00
Dimension2D size = textBlock.calculateDimension(ug.getStringBounder());
textBlock.drawU(ug, 0, 0);
if (image != null) {
if (position == GraphicPosition.BOTTOM) {
ug.draw((size.getWidth() - image.getWidth()) / 2, size.getHeight(), new UImage(image));
size = new Dimension2DDouble(size.getWidth(), size.getHeight() + image.getHeight());
} else if (position == GraphicPosition.BACKGROUND_CORNER) {
ug.draw(size.getWidth() - image.getWidth(), size.getHeight() - image.getHeight(), new UImage(image));
}
}
return size;
}
2011-08-08 17:48:29 +00:00
public void drawU(UGraphic ug, double theoricalPosition, double theoricalPosition2) {
drawU(ug);
}
public Dimension2D getDimension(StringBounder stringBounder) {
final TextBlock textBlock = TextBlockUtils.create(strings, new FontConfiguration(font, green),
HorizontalAlignement.LEFT);
return textBlock.calculateDimension(stringBounder);
}
public ShapeType getShapeType() {
return ShapeType.RECTANGLE;
}
public HtmlColor getBackcolor() {
return background;
}
2011-09-07 20:41:58 +00:00
public int getShield() {
return 0;
}
2010-11-15 20:35:36 +00:00
}