1
0
mirror of https://github.com/octoleo/plantuml.git synced 2024-06-26 03:42:36 +00:00
plantuml/src/net/sourceforge/plantuml/ugraphic/UGraphicUtils.java

129 lines
4.9 KiB
Java
Raw Normal View History

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
*
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.ugraphic;
2013-12-10 19:36:50 +00:00
import java.awt.Graphics2D;
import java.awt.geom.Dimension2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import net.sourceforge.plantuml.EmptyImageBuilder;
import net.sourceforge.plantuml.FileFormat;
import net.sourceforge.plantuml.FileFormatOption;
2016-05-31 19:41:55 +00:00
import net.sourceforge.plantuml.StringUtils;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.eps.EpsStrategy;
import net.sourceforge.plantuml.graphic.HtmlColor;
import net.sourceforge.plantuml.graphic.TextBlock;
import net.sourceforge.plantuml.png.PngIO;
import net.sourceforge.plantuml.ugraphic.eps.UGraphicEps;
import net.sourceforge.plantuml.ugraphic.g2d.UGraphicG2d;
import net.sourceforge.plantuml.ugraphic.svg.UGraphicSvg;
2010-11-15 20:35:36 +00:00
public abstract class UGraphicUtils {
2016-11-04 21:39:29 +00:00
// public static UDrawable translate(final UDrawable d, final double dx, final double dy) {
// return new UDrawable() {
// public void drawU(UGraphic ug) {
// d.drawU(ug.apply(new UTranslate(dx, dy)));
// }
// };
//
// }
2011-09-08 10:42:27 +00:00
2013-12-10 19:36:50 +00:00
public static void writeImage(OutputStream os, String metadata, FileFormatOption fileFormatOption,
ColorMapper colorMapper, HtmlColor background, TextBlock image) throws IOException {
final FileFormat fileFormat = fileFormatOption.getFileFormat();
if (fileFormat == FileFormat.PNG) {
final BufferedImage im = createImage(colorMapper, background, image);
PngIO.write(im, os, fileFormatOption.isWithMetadata() ? metadata : null, 96);
} else if (fileFormat == FileFormat.SVG) {
final UGraphicSvg svg = new UGraphicSvg(colorMapper, StringUtils.getAsHtml(colorMapper
2017-02-15 21:34:36 +00:00
.getMappedColor(background)), false, 1.0, fileFormatOption.getSvgLinkTarget(),
2017-04-05 17:37:42 +00:00
fileFormatOption.getHoverColor(), fileFormatOption.getRandom());
2013-12-10 19:36:50 +00:00
image.drawU(svg);
2017-04-05 17:37:42 +00:00
svg.createXml(os, fileFormatOption.isWithMetadata() ? metadata : null);
2013-12-10 19:36:50 +00:00
} else if (fileFormat == FileFormat.EPS) {
final UGraphicEps ug = new UGraphicEps(colorMapper, EpsStrategy.getDefault2());
image.drawU(ug);
os.write(ug.getEPSCode().getBytes());
2015-04-07 18:18:37 +00:00
} else if (fileFormat == FileFormat.EPS_TEXT) {
final UGraphicEps ug = new UGraphicEps(colorMapper, EpsStrategy.WITH_MACRO_AND_TEXT);
image.drawU(ug);
os.write(ug.getEPSCode().getBytes());
2013-12-10 19:36:50 +00:00
} else {
throw new UnsupportedOperationException();
}
}
2010-11-15 20:35:36 +00:00
2013-12-10 19:36:50 +00:00
private static BufferedImage createImage(ColorMapper colorMapper, HtmlColor background, TextBlock image) {
EmptyImageBuilder builder = new EmptyImageBuilder(10, 10, colorMapper.getMappedColor(background));
Graphics2D g2d = builder.getGraphics2D();
2010-11-15 20:35:36 +00:00
2013-12-10 19:36:50 +00:00
final UGraphicG2d tmp = new UGraphicG2d(colorMapper, g2d, 1.0);
final Dimension2D size = image.calculateDimension(tmp.getStringBounder());
g2d.dispose();
2011-01-23 19:36:52 +00:00
2013-12-10 19:36:50 +00:00
builder = new EmptyImageBuilder(size.getWidth(), size.getHeight(), colorMapper.getMappedColor(background));
final BufferedImage im = builder.getBufferedImage();
g2d = builder.getGraphics2D();
2011-04-19 16:50:40 +00:00
2013-12-10 19:36:50 +00:00
final UGraphicG2d ug = new UGraphicG2d(colorMapper, g2d, 1.0);
image.drawU(ug);
g2d.dispose();
return im;
2010-11-15 20:35:36 +00:00
}
2013-12-10 19:36:50 +00:00
// public static void writeImage(OutputStream os, UGraphic ug, String metadata, int dpi) throws IOException {
// if (ug instanceof UGraphicG2d) {
// final BufferedImage im = ((UGraphicG2d) ug).getBufferedImage();
// PngIO.write(im, os, metadata, dpi);
// } else if (ug instanceof UGraphicSvg) {
// final UGraphicSvg svg = (UGraphicSvg) ug;
// svg.createXml(os);
// } else if (ug instanceof UGraphicEps) {
// final UGraphicEps eps = (UGraphicEps) ug;
// os.write(eps.getEPSCode().getBytes());
// } else if (ug instanceof UGraphicHtml5) {
// final UGraphicHtml5 html5 = (UGraphicHtml5) ug;
// os.write(html5.generateHtmlCode().getBytes());
// } else {
// throw new UnsupportedOperationException();
// }
// }
2010-11-15 20:35:36 +00:00
}