1
0
mirror of https://github.com/octoleo/plantuml.git synced 2024-06-03 09:00:48 +00:00

Replace UGraphicUtils with ImageBuilder.

This commit is contained in:
matthew16550 2021-03-25 21:49:53 +11:00
parent d9e7c134d7
commit 880441b1cf
4 changed files with 33 additions and 128 deletions

View File

@ -41,6 +41,7 @@ import java.util.Arrays;
import net.sourceforge.plantuml.AbstractPSystem;
import net.sourceforge.plantuml.CounterOutputStream;
import net.sourceforge.plantuml.FileFormatOption;
import net.sourceforge.plantuml.FileImageData;
import net.sourceforge.plantuml.StringUtils;
import net.sourceforge.plantuml.api.ImageDataSimple;
import net.sourceforge.plantuml.core.DiagramDescription;
@ -51,9 +52,8 @@ import net.sourceforge.plantuml.cucadiagram.dot.GraphvizUtils;
import net.sourceforge.plantuml.cucadiagram.dot.ProcessState;
import net.sourceforge.plantuml.graphic.GraphicStrings;
import net.sourceforge.plantuml.graphic.TextBlock;
import net.sourceforge.plantuml.ugraphic.UGraphicUtils;
import net.sourceforge.plantuml.ugraphic.color.ColorMapperIdentity;
import net.sourceforge.plantuml.ugraphic.color.HColorUtils;
import static net.sourceforge.plantuml.ugraphic.ImageBuilder.plainImageBuilder;
public class PSystemDot extends AbstractPSystem {
@ -75,9 +75,10 @@ public class PSystemDot extends AbstractPSystem {
if (graphviz.getExeState() != ExeState.OK) {
final TextBlock result = GraphicStrings
.createForError(Arrays.asList("There is an issue with your Dot/Graphviz installation"), false);
UGraphicUtils.writeImage(os, null, fileFormat, seed(), new ColorMapperIdentity(), HColorUtils.WHITE,
result);
return ImageDataSimple.error();
return plainImageBuilder(result, fileFormat)
.seed(seed())
.status(FileImageData.CRASH)
.write(os);
}
final CounterOutputStream counter = new CounterOutputStream(os);
final ProcessState state = graphviz.createFile3(counter);
@ -86,9 +87,10 @@ public class PSystemDot extends AbstractPSystem {
// }
if (counter.getLength() == 0 || state.differs(ProcessState.TERMINATED_OK())) {
final TextBlock result = GraphicStrings.createForError(Arrays.asList("GraphViz has crashed"), false);
UGraphicUtils.writeImage(os, null, fileFormat, seed(), new ColorMapperIdentity(), HColorUtils.WHITE,
result);
return ImageDataSimple.error();
return plainImageBuilder(result, fileFormat)
.seed(seed())
.status(FileImageData.CRASH)
.write(os);
}
return ImageDataSimple.ok();

View File

@ -47,7 +47,6 @@ import net.sourceforge.plantuml.Dimension2DDouble;
import net.sourceforge.plantuml.FileFormatOption;
import net.sourceforge.plantuml.UmlDiagram;
import net.sourceforge.plantuml.UmlDiagramType;
import net.sourceforge.plantuml.api.ImageDataSimple;
import net.sourceforge.plantuml.core.DiagramDescription;
import net.sourceforge.plantuml.core.ImageData;
import net.sourceforge.plantuml.golem.MinMaxGolem;
@ -60,16 +59,17 @@ import net.sourceforge.plantuml.golem.TilesField;
import net.sourceforge.plantuml.graphic.InnerStrategy;
import net.sourceforge.plantuml.graphic.StringBounder;
import net.sourceforge.plantuml.graphic.TextBlock;
import net.sourceforge.plantuml.style.ClockwiseTopRightBottomLeft;
import net.sourceforge.plantuml.ugraphic.MinMax;
import net.sourceforge.plantuml.ugraphic.UEllipse;
import net.sourceforge.plantuml.ugraphic.UGraphic;
import net.sourceforge.plantuml.ugraphic.UGraphicUtils;
import net.sourceforge.plantuml.ugraphic.ULine;
import net.sourceforge.plantuml.ugraphic.UShape;
import net.sourceforge.plantuml.ugraphic.UTranslate;
import net.sourceforge.plantuml.ugraphic.color.ColorMapperIdentity;
import net.sourceforge.plantuml.ugraphic.color.HColorUtils;
import static net.sourceforge.plantuml.ugraphic.ImageBuilder.plainImageBuilder;
public class FlowDiagram extends UmlDiagram implements TextBlock {
private static double SINGLE_SIZE_X = 100;
@ -123,9 +123,13 @@ public class FlowDiagram extends UmlDiagram implements TextBlock {
@Override
protected ImageData exportDiagramInternal(OutputStream os, int index, FileFormatOption fileFormatOption)
throws IOException {
UGraphicUtils.writeImage(os, null, fileFormatOption, seed(), new ColorMapperIdentity(), HColorUtils.WHITE,
this);
return ImageDataSimple.ok();
return plainImageBuilder(this, fileFormatOption)
.dimension(calculateDimension(fileFormatOption.getDefaultStringBounder(getSkinParam())))
.margin(getDefaultMargins())
.metadata(fileFormatOption.isWithMetadata() ? getMetadata() : null)
.seed(seed())
.write(os);
}
public void drawU(UGraphic ug) {
@ -212,4 +216,8 @@ public class FlowDiagram extends UmlDiagram implements TextBlock {
throw new UnsupportedOperationException();
}
@Override
public ClockwiseTopRightBottomLeft getDefaultMargins() {
return ClockwiseTopRightBottomLeft.same(0);
}
}

View File

@ -114,6 +114,7 @@ public class ImageBuilder {
private double borderCorner;
private UStroke borderStroke;
private ColorMapper colorMapper = new ColorMapperIdentity();
private Dimension2D dimension;
private int dpi = 96;
private final FileFormatOption fileFormatOption;
private boolean handwritten;
@ -168,6 +169,11 @@ public class ImageBuilder {
return backcolor(HColorUtils.BLACK);
}
public ImageBuilder dimension(Dimension2D dimension) {
this.dimension = dimension;
return this;
}
public ImageBuilder margin(ClockwiseTopRightBottomLeft margin) {
this.margin = margin;
return this;
@ -249,7 +255,8 @@ public class ImageBuilder {
private ImageData writeImageInternal(FileFormatOption fileFormatOption, OutputStream os,
Animation animationArg) throws IOException {
Dimension2D dim = getFinalDimension(fileFormatOption.getDefaultStringBounder(svgCharSizeHack));
Dimension2D dim = (dimension == null)
? getFinalDimension(fileFormatOption.getDefaultStringBounder(svgCharSizeHack)) : dimension;
double dx = 0;
double dy = 0;
if (animationArg != null) {

View File

@ -1,112 +0,0 @@
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
* (C) Copyright 2009-2020, Arnaud Roques
*
* Project Info: http://plantuml.com
*
* 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
*
* 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 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.
*
*
* Original Author: Arnaud Roques
*
*
*/
package net.sourceforge.plantuml.ugraphic;
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;
import net.sourceforge.plantuml.SvgCharSizeHack;
import net.sourceforge.plantuml.eps.EpsStrategy;
import net.sourceforge.plantuml.graphic.TextBlock;
import net.sourceforge.plantuml.png.PngIO;
import net.sourceforge.plantuml.svg.LengthAdjust;
import net.sourceforge.plantuml.ugraphic.color.ColorMapper;
import net.sourceforge.plantuml.ugraphic.color.HColor;
import net.sourceforge.plantuml.ugraphic.eps.UGraphicEps;
import net.sourceforge.plantuml.ugraphic.g2d.UGraphicG2d;
import net.sourceforge.plantuml.ugraphic.svg.UGraphicSvg;
public abstract class UGraphicUtils {
public static void writeImage(OutputStream os, String metadata, FileFormatOption fileFormatOption, long seed,
ColorMapper colorMapper, HColor background, TextBlock image) throws IOException {
final FileFormat fileFormat = fileFormatOption.getFileFormat();
if (fileFormat == FileFormat.PNG) {
final BufferedImage im = createImage(fileFormatOption.getWatermark(), colorMapper, background, image);
PngIO.write(im, os, fileFormatOption.isWithMetadata() ? metadata : null, 96);
} else if (fileFormat == FileFormat.SVG) {
final Dimension2D size = computeSize(colorMapper, background, image);
final UGraphicSvg svg = new UGraphicSvg(true, size, colorMapper, colorMapper.toRGB(background), false, 1.0,
fileFormatOption.getSvgLinkTarget(), fileFormatOption.getHoverColor(), seed,
fileFormatOption.getPreserveAspectRatio(), SvgCharSizeHack.NO_HACK,
LengthAdjust.defaultValue());
image.drawU(svg);
svg.createXml(os, fileFormatOption.isWithMetadata() ? metadata : null);
} else if (fileFormat == FileFormat.EPS) {
final UGraphicEps ug = new UGraphicEps(colorMapper, EpsStrategy.getDefault2());
image.drawU(ug);
os.write(ug.getEPSCode().getBytes());
} 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());
} else {
throw new UnsupportedOperationException();
}
}
private static BufferedImage createImage(String watermark, ColorMapper colorMapper, HColor background,
TextBlock image) {
final Dimension2D size = computeSize(colorMapper, background, image);
final EmptyImageBuilder builder = new EmptyImageBuilder(watermark, size.getWidth(), size.getHeight(),
colorMapper.toColor(background));
final BufferedImage im = builder.getBufferedImage();
final Graphics2D g2d = builder.getGraphics2D();
final UGraphicG2d ug = new UGraphicG2d(colorMapper, g2d, 1.0);
image.drawU(ug);
g2d.dispose();
return im;
}
private static Dimension2D computeSize(ColorMapper colorMapper, HColor background, TextBlock image) {
final EmptyImageBuilder builder = new EmptyImageBuilder(null, 10, 10, colorMapper.toColor(background));
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;
}
}