1
0
mirror of https://github.com/octoleo/plantuml.git synced 2024-06-17 07:32:24 +00:00
plantuml/src/net/sourceforge/plantuml/ugraphic/UGraphicUtils.java

113 lines
4.5 KiB
Java
Raw Normal View History

2010-11-15 20:35:36 +00:00
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
2019-01-16 18:34:41 +00:00
* (C) Copyright 2009-2020, 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;
2020-12-06 21:43:09 +00:00
import net.sourceforge.plantuml.SvgCharSizeHack;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.eps.EpsStrategy;
import net.sourceforge.plantuml.graphic.TextBlock;
import net.sourceforge.plantuml.png.PngIO;
2021-01-10 20:52:19 +00:00
import net.sourceforge.plantuml.svg.LengthAdjust;
2020-03-18 10:50:02 +00:00
import net.sourceforge.plantuml.ugraphic.color.ColorMapper;
import net.sourceforge.plantuml.ugraphic.color.HColor;
2013-12-10 19:36:50 +00:00
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 {
2017-04-19 18:30:16 +00:00
public static void writeImage(OutputStream os, String metadata, FileFormatOption fileFormatOption, long seed,
2020-03-18 10:50:02 +00:00
ColorMapper colorMapper, HColor background, TextBlock image) throws IOException {
2013-12-10 19:36:50 +00:00
final FileFormat fileFormat = fileFormatOption.getFileFormat();
if (fileFormat == FileFormat.PNG) {
2020-04-26 18:31:41 +00:00
final BufferedImage im = createImage(fileFormatOption.getWatermark(), colorMapper, background, image);
2013-12-10 19:36:50 +00:00
PngIO.write(im, os, fileFormatOption.isWithMetadata() ? metadata : null, 96);
} else if (fileFormat == FileFormat.SVG) {
2017-06-05 11:27:21 +00:00
final Dimension2D size = computeSize(colorMapper, background, image);
2020-12-06 21:43:09 +00:00
final UGraphicSvg svg = new UGraphicSvg(true, size, colorMapper, colorMapper.toRGB(background), false, 1.0,
fileFormatOption.getSvgLinkTarget(), fileFormatOption.getHoverColor(), seed,
2021-01-10 20:52:19 +00:00
fileFormatOption.getPreserveAspectRatio(), SvgCharSizeHack.NO_HACK,
LengthAdjust.defaultValue());
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
2020-04-26 18:31:41 +00:00
private static BufferedImage createImage(String watermark, ColorMapper colorMapper, HColor background,
TextBlock image) {
2017-06-05 11:27:21 +00:00
final Dimension2D size = computeSize(colorMapper, background, image);
2010-11-15 20:35:36 +00:00
2020-04-26 18:31:41 +00:00
final EmptyImageBuilder builder = new EmptyImageBuilder(watermark, size.getWidth(), size.getHeight(),
colorMapper.toColor(background));
2013-12-10 19:36:50 +00:00
final BufferedImage im = builder.getBufferedImage();
2017-06-05 11:27:21 +00:00
final Graphics2D 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
}
2020-03-18 10:50:02 +00:00
private static Dimension2D computeSize(ColorMapper colorMapper, HColor background, TextBlock image) {
2020-04-26 18:31:41 +00:00
final EmptyImageBuilder builder = new EmptyImageBuilder(null, 10, 10, colorMapper.toColor(background));
2017-06-05 11:27:21 +00:00
final Graphics2D g2d = builder.getGraphics2D();
final UGraphicG2d tmp = new UGraphicG2d(colorMapper, g2d, 1.0);
final Dimension2D size = image.calculateDimension(tmp.getStringBounder());
g2d.dispose();
return size;
}
2013-12-10 19:36:50 +00:00
2010-11-15 20:35:36 +00:00
}