plantuml/src/net/sourceforge/plantuml/UmlDiagram.java

342 lines
11 KiB
Java
Raw Normal View History

2010-11-15 20:35:36 +00:00
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
2023-02-22 18:43:48 +00:00
* (C) Copyright 2009-2024, Arnaud Roques
2010-11-15 20:35:36 +00:00
*
2023-02-22 18:43:48 +00:00
* Project Info: https://plantuml.com
2022-08-17 17:34:24 +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:
2022-08-17 17:34:24 +00:00
*
2023-02-22 18:43:48 +00:00
* https://plantuml.com/patreon (only 1$ per month!)
* https://plantuml.com/paypal
2022-08-17 17:34:24 +00:00
*
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;
2023-02-22 18:43:48 +00:00
import static net.atmp.ImageBuilder.plainImageBuilder;
2021-05-23 15:35:13 +00:00
2018-09-23 12:15:14 +00:00
import java.awt.Color;
2013-12-10 19:36:50 +00:00
import java.awt.geom.AffineTransform;
2011-04-19 16:50:40 +00:00
import java.awt.image.BufferedImage;
2020-06-21 20:31:45 +00:00
import java.io.BufferedOutputStream;
2013-12-10 19:36:50 +00:00
import java.io.ByteArrayOutputStream;
2020-06-21 20:31:45 +00:00
import java.io.File;
2011-04-19 16:50:40 +00:00
import java.io.FileNotFoundException;
2020-06-21 20:31:45 +00:00
import java.io.FileOutputStream;
2011-04-19 16:50:40 +00:00
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
2010-11-15 20:35:36 +00:00
import java.util.List;
2022-09-20 20:35:41 +00:00
import java.util.Map;
2010-11-15 20:35:36 +00:00
2023-02-22 18:43:48 +00:00
import net.atmp.PixelImage;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.api.ImageDataSimple;
2015-05-31 18:56:03 +00:00
import net.sourceforge.plantuml.command.CommandExecutionResult;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.core.Diagram;
import net.sourceforge.plantuml.core.ImageData;
2015-04-07 18:18:37 +00:00
import net.sourceforge.plantuml.core.UmlSource;
2018-05-06 19:59:19 +00:00
import net.sourceforge.plantuml.cucadiagram.DisplaySection;
2023-02-22 18:43:48 +00:00
import net.sourceforge.plantuml.dot.UnparsableGraphvizException;
import net.sourceforge.plantuml.file.SuggestedFile;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.flashcode.FlashCodeFactory;
import net.sourceforge.plantuml.flashcode.FlashCodeUtils;
2015-04-07 18:18:37 +00:00
import net.sourceforge.plantuml.fun.IconLoader;
2023-02-22 18:43:48 +00:00
import net.sourceforge.plantuml.klimt.AffineTransformType;
2023-02-02 17:59:43 +00:00
import net.sourceforge.plantuml.klimt.UTranslate;
2023-02-22 18:43:48 +00:00
import net.sourceforge.plantuml.klimt.drawing.UGraphic;
2023-02-02 17:59:43 +00:00
import net.sourceforge.plantuml.klimt.font.FontParam;
2023-02-22 18:43:48 +00:00
import net.sourceforge.plantuml.klimt.geom.GraphicPosition;
import net.sourceforge.plantuml.klimt.geom.XDimension2D;
import net.sourceforge.plantuml.klimt.shape.GraphicStrings;
2023-02-26 18:51:17 +00:00
import net.sourceforge.plantuml.klimt.shape.TextBlock;
2023-02-22 18:43:48 +00:00
import net.sourceforge.plantuml.klimt.shape.UDrawable;
import net.sourceforge.plantuml.klimt.shape.UImage;
2022-08-17 17:34:24 +00:00
import net.sourceforge.plantuml.log.Logme;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.mjpeg.MJPEGGenerator;
2011-08-08 17:48:29 +00:00
import net.sourceforge.plantuml.pdf.PdfConverter;
2020-05-30 15:20:23 +00:00
import net.sourceforge.plantuml.security.SFile;
2022-08-17 17:34:24 +00:00
import net.sourceforge.plantuml.security.SImageIO;
2020-05-30 15:20:23 +00:00
import net.sourceforge.plantuml.security.SecurityUtils;
2023-02-22 18:43:48 +00:00
import net.sourceforge.plantuml.skin.UmlDiagramType;
2020-12-01 21:39:27 +00:00
import net.sourceforge.plantuml.style.NoStyleAvailableException;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.svek.EmptySvgException;
2015-04-07 18:18:37 +00:00
import net.sourceforge.plantuml.svek.GraphvizCrash;
2022-12-17 11:01:10 +00:00
import net.sourceforge.plantuml.utils.Log;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.version.Version;
2010-11-15 20:35:36 +00:00
2019-04-21 20:40:01 +00:00
public abstract class UmlDiagram extends TitledDiagram implements Diagram, Annotated, WithSprite {
2010-11-15 20:35:36 +00:00
private boolean rotation;
private int minwidth = Integer.MAX_VALUE;
2022-03-19 12:48:23 +00:00
// public UmlDiagram(ThemeStyle style, UmlSource source, UmlDiagramType type) {
// super(style, source, type);
// }
2019-03-01 22:16:29 +00:00
2022-09-20 20:35:41 +00:00
public UmlDiagram(UmlSource source, UmlDiagramType type, Map<String, String> orig) {
2022-09-18 17:08:06 +00:00
super(source, type, orig);
2019-03-01 22:16:29 +00:00
}
2010-11-15 20:35:36 +00:00
final public int getMinwidth() {
return minwidth;
}
final public void setMinwidth(int minwidth) {
this.minwidth = minwidth;
}
final public boolean isRotation() {
return rotation;
}
final public void setRotation(boolean rotation) {
this.rotation = rotation;
}
2018-05-06 19:59:19 +00:00
public final DisplaySection getFooterOrHeaderTeoz(FontParam param) {
2022-02-10 18:16:18 +00:00
if (param == FontParam.FOOTER)
2015-05-31 18:56:03 +00:00
return getFooter();
2022-02-10 18:16:18 +00:00
if (param == FontParam.HEADER)
2015-05-31 18:56:03 +00:00
return getHeader();
2022-02-10 18:16:18 +00:00
2015-05-31 18:56:03 +00:00
throw new IllegalArgumentException();
}
2022-02-10 18:16:18 +00:00
2016-12-01 20:29:25 +00:00
@Override
2021-03-23 13:06:33 +00:00
final protected ImageData exportDiagramNow(OutputStream os, int index, FileFormatOption fileFormatOption)
2011-04-19 16:50:40 +00:00
throws IOException {
2013-12-10 19:36:50 +00:00
2017-10-07 09:46:53 +00:00
fileFormatOption = fileFormatOption.withTikzFontDistortion(getSkinParam().getTikzFontDistortion());
2016-09-29 19:51:18 +00:00
2023-02-06 21:04:53 +00:00
// ::comment when CORE
2022-02-10 18:16:18 +00:00
if (fileFormatOption.getFileFormat() == FileFormat.PDF)
2015-04-07 18:18:37 +00:00
return exportDiagramInternalPdf(os, index);
// ::done
2015-04-07 18:18:37 +00:00
2013-12-10 19:36:50 +00:00
try {
2015-04-07 18:18:37 +00:00
final ImageData imageData = exportDiagramInternal(os, index, fileFormatOption);
2022-09-12 20:08:34 +00:00
this.lastInfo = new XDimension2D(imageData.getWidth(), imageData.getHeight());
2013-12-10 19:36:50 +00:00
return imageData;
2020-12-01 21:39:27 +00:00
} catch (NoStyleAvailableException e) {
2022-08-17 17:34:24 +00:00
// Logme.error(e);
2021-03-23 13:06:33 +00:00
exportDiagramError(os, e, fileFormatOption, null);
2013-12-10 19:36:50 +00:00
} catch (UnparsableGraphvizException e) {
2022-08-17 17:34:24 +00:00
Logme.error(e);
2021-03-23 13:06:33 +00:00
exportDiagramError(os, e.getCause(), fileFormatOption, e.getGraphvizVersion());
2021-02-02 10:12:15 +00:00
} catch (Throwable e) {
2022-08-17 17:34:24 +00:00
// Logme.error(e);
2021-03-23 13:06:33 +00:00
exportDiagramError(os, e, fileFormatOption, null);
2013-12-10 19:36:50 +00:00
}
2018-05-01 17:26:04 +00:00
return ImageDataSimple.error();
2011-04-19 16:50:40 +00:00
}
2021-03-23 13:06:33 +00:00
private void exportDiagramError(OutputStream os, Throwable exception, FileFormatOption fileFormat,
2015-08-05 20:17:01 +00:00
String graphvizVersion) throws IOException {
2021-03-23 13:06:33 +00:00
exportDiagramError(os, exception, fileFormat, seed(), getMetadata(), getFlashData(),
2017-07-03 17:59:53 +00:00
getFailureText1(exception, graphvizVersion, getFlashData()));
2015-08-05 20:17:01 +00:00
}
2015-04-07 18:18:37 +00:00
2017-04-19 18:30:16 +00:00
public static void exportDiagramError(OutputStream os, Throwable exception, FileFormatOption fileFormat, long seed,
2015-08-05 20:17:01 +00:00
String metadata, String flash, List<String> strings) throws IOException {
2016-11-18 21:12:09 +00:00
2023-02-06 21:04:53 +00:00
// ::comment when CORE
2016-11-18 21:12:09 +00:00
if (fileFormat.getFileFormat() == FileFormat.ATXT || fileFormat.getFileFormat() == FileFormat.UTXT) {
exportDiagramErrorText(os, exception, strings);
return;
}
// ::done
2016-11-18 21:12:09 +00:00
2015-05-31 18:56:03 +00:00
strings.addAll(CommandExecutionResult.getStackTrace(exception));
2020-05-17 21:15:50 +00:00
2020-12-01 21:39:27 +00:00
BufferedImage im2 = null;
2023-02-06 21:04:53 +00:00
// ::comment when CORE
2020-12-01 21:39:27 +00:00
if (flash != null) {
2020-05-17 21:15:50 +00:00
final FlashCodeUtils utils = FlashCodeFactory.getFlashCodeUtils();
2020-12-01 21:39:27 +00:00
try {
im2 = utils.exportFlashcode(flash, Color.BLACK, Color.WHITE);
} catch (Throwable e) {
Log.error("Issue in flashcode generation " + e);
2022-08-17 17:34:24 +00:00
// Logme.error(e);
2020-12-01 21:39:27 +00:00
}
2022-02-10 18:16:18 +00:00
if (im2 != null)
2020-05-17 21:15:50 +00:00
GraphvizCrash.addDecodeHint(strings);
2015-04-07 18:18:37 +00:00
}
// ::done
2020-12-01 21:39:27 +00:00
final BufferedImage im = im2;
2023-02-26 18:51:17 +00:00
final TextBlock graphicStrings = GraphicStrings.createBlackOnWhite(strings, IconLoader.getRandom(),
2015-04-07 18:18:37 +00:00
GraphicPosition.BACKGROUND_CORNER_TOP_RIGHT);
final UDrawable drawable = (im == null) ? graphicStrings : new UDrawable() {
2022-02-10 18:16:18 +00:00
public void drawU(UGraphic ug) {
graphicStrings.drawU(ug);
final double height = graphicStrings.calculateDimension(ug.getStringBounder()).getHeight();
ug = ug.apply(UTranslate.dy(height));
ug.draw(new UImage(new PixelImage(im, AffineTransformType.TYPE_NEAREST_NEIGHBOR)).scale(3));
}
};
plainImageBuilder(drawable, fileFormat).metadata(metadata).seed(seed).write(os);
2015-04-07 18:18:37 +00:00
}
2023-02-06 21:04:53 +00:00
// ::comment when CORE
2016-11-18 21:12:09 +00:00
private static void exportDiagramErrorText(OutputStream os, Throwable exception, List<String> strings) {
2020-05-30 15:20:23 +00:00
final PrintWriter pw = SecurityUtils.createPrintWriter(os);
2016-11-18 21:12:09 +00:00
exception.printStackTrace(pw);
pw.println();
pw.println();
for (String s : strings) {
s = s.replaceAll("\\</?\\w+?\\>", "");
pw.println(s);
}
pw.flush();
}
2023-02-02 17:59:43 +00:00
// ::done
2016-11-18 21:12:09 +00:00
2016-03-06 16:47:34 +00:00
public String getFlashData() {
2015-04-07 18:18:37 +00:00
final UmlSource source = getSource();
2022-02-10 18:16:18 +00:00
if (source == null)
2016-05-31 19:41:55 +00:00
return "";
2022-02-10 18:16:18 +00:00
2016-05-31 19:41:55 +00:00
return source.getPlainString();
2015-04-07 18:18:37 +00:00
}
2017-07-03 17:59:53 +00:00
static private List<String> getFailureText1(Throwable exception, String graphvizVersion, String textDiagram) {
final List<String> strings = GraphvizCrash.anErrorHasOccured(exception, textDiagram);
2013-12-10 19:36:50 +00:00
strings.add("PlantUML (" + Version.versionString() + ") cannot parse result from dot/GraphViz.");
2022-02-10 18:16:18 +00:00
if (exception instanceof EmptySvgException)
2013-12-10 19:36:50 +00:00
strings.add("Because dot/GraphViz returns an empty string.");
2022-02-10 18:16:18 +00:00
2016-05-11 21:31:47 +00:00
GraphvizCrash.checkOldVersionWarning(strings);
2013-12-10 19:36:50 +00:00
if (graphvizVersion != null) {
strings.add(" ");
strings.add("GraphViz version used : " + graphvizVersion);
}
2016-05-11 21:31:47 +00:00
GraphvizCrash.pleaseGoTo(strings);
2015-04-07 18:18:37 +00:00
GraphvizCrash.addProperties(strings);
strings.add(" ");
2016-05-11 21:31:47 +00:00
GraphvizCrash.thisMayBeCaused(strings);
2013-12-10 19:36:50 +00:00
strings.add(" ");
2016-05-11 21:31:47 +00:00
GraphvizCrash.youShouldSendThisDiagram(strings);
2013-12-10 19:36:50 +00:00
strings.add(" ");
2015-04-07 18:18:37 +00:00
return strings;
2013-12-10 19:36:50 +00:00
}
2017-07-03 17:59:53 +00:00
public static List<String> getFailureText2(Throwable exception, String textDiagram) {
final List<String> strings = GraphvizCrash.anErrorHasOccured(exception, textDiagram);
2015-08-05 20:17:01 +00:00
strings.add("PlantUML (" + Version.versionString() + ") has crashed.");
2016-05-11 21:31:47 +00:00
GraphvizCrash.checkOldVersionWarning(strings);
2015-08-05 20:17:01 +00:00
strings.add(" ");
2016-05-11 21:31:47 +00:00
GraphvizCrash.youShouldSendThisDiagram(strings);
2015-08-05 20:17:01 +00:00
strings.add(" ");
return strings;
}
2023-02-06 21:04:53 +00:00
// ::comment when CORE
2013-12-10 19:36:50 +00:00
private void exportDiagramInternalMjpeg(OutputStream os) throws IOException {
2020-05-30 15:20:23 +00:00
final SFile f = new SFile("c:/test.avi");
2013-12-10 19:36:50 +00:00
final int nb = 150;
final double framerate = 30;
final MJPEGGenerator m = new MJPEGGenerator(f, 640, 480, framerate, nb);
for (int i = 0; i < nb; i++) {
final AffineTransform at = new AffineTransform();
final double coef = (nb - 1 - i) * 1.0 / nb;
at.setToShear(coef, coef);
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
2020-04-26 18:31:41 +00:00
// exportDiagramTOxxBEREMOVED(baos, null, 0, new
// FileFormatOption(FileFormat.PNG, at));
2013-12-10 19:36:50 +00:00
baos.close();
final BufferedImage im = SImageIO.read(baos.toByteArray());
2013-12-10 19:36:50 +00:00
m.addImage(im);
}
m.finishAVI();
}
2015-04-07 18:18:37 +00:00
private ImageData exportDiagramInternalPdf(OutputStream os, int index) throws IOException {
2020-06-21 20:31:45 +00:00
final File svg = FileUtils.createTempFileLegacy("pdf", ".svf");
final File pdfFile = FileUtils.createTempFileLegacy("pdf", ".pdf");
final ImageData result;
try (OutputStream fos = new BufferedOutputStream(new FileOutputStream(svg))) {
result = exportDiagram(fos, index, new FileFormatOption(FileFormat.SVG));
}
2011-08-08 17:48:29 +00:00
PdfConverter.convert(svg, pdfFile);
FileUtils.copyToStream(pdfFile, os);
2013-12-10 19:36:50 +00:00
return result;
2011-08-08 17:48:29 +00:00
}
2017-04-19 18:30:16 +00:00
final protected void exportCmap(SuggestedFile suggestedFile, int index, final ImageData cmapdata)
throws FileNotFoundException {
final String name = changeName(suggestedFile.getFile(index).getAbsolutePath());
2020-05-30 15:20:23 +00:00
final SFile cmapFile = new SFile(name);
try (PrintWriter pw = cmapFile.createPrintWriter()) {
2022-02-10 18:16:18 +00:00
if (PSystemUtils.canFileBeWritten(cmapFile) == false)
2013-12-10 19:36:50 +00:00
return;
2022-02-10 18:16:18 +00:00
2013-12-10 19:36:50 +00:00
pw.print(cmapdata.getCMapData(cmapFile.getName().substring(0, cmapFile.getName().length() - 6)));
2011-04-19 16:50:40 +00:00
}
}
static String changeName(String name) {
return name.replaceAll("(?i)\\.\\w{3}$", ".cmapx");
}
// ::done
private XDimension2D lastInfo;
protected abstract ImageData exportDiagramInternal(OutputStream os, int index, FileFormatOption fileFormatOption)
throws IOException;
2011-04-19 16:50:40 +00:00
2013-12-10 19:36:50 +00:00
@Override
public String getWarningOrError() {
2022-02-10 18:16:18 +00:00
if (lastInfo == null)
2013-12-10 19:36:50 +00:00
return null;
2022-02-10 18:16:18 +00:00
2013-12-10 19:36:50 +00:00
final double actualWidth = lastInfo.getWidth();
2022-02-10 18:16:18 +00:00
if (actualWidth == 0)
2013-12-10 19:36:50 +00:00
return null;
2022-02-10 18:16:18 +00:00
2013-12-10 19:36:50 +00:00
final String value = getSkinParam().getValue("widthwarning");
2022-02-10 18:16:18 +00:00
if (value == null)
2013-12-10 19:36:50 +00:00
return null;
2022-02-10 18:16:18 +00:00
if (value.matches("\\d+") == false)
2013-12-10 19:36:50 +00:00
return null;
2022-02-10 18:16:18 +00:00
2013-12-10 19:36:50 +00:00
final int widthwarning = Integer.parseInt(value);
2022-02-10 18:16:18 +00:00
if (actualWidth > widthwarning)
2013-12-10 19:36:50 +00:00
return "The image is " + ((int) actualWidth) + " pixel width. (Warning limit is " + widthwarning + ")";
2022-02-10 18:16:18 +00:00
2013-12-10 19:36:50 +00:00
return null;
2011-04-19 16:50:40 +00:00
}
2019-06-26 19:24:49 +00:00
public void setHideEmptyDescription(boolean hideEmptyDescription) {
}
2010-11-15 20:35:36 +00:00
}