1
0
mirror of https://github.com/octoleo/plantuml.git synced 2024-06-02 08:30:49 +00:00
plantuml/src/net/sourceforge/plantuml/EmbeddedDiagram.java

242 lines
7.1 KiB
Java
Raw Normal View History

2019-03-01 22:16:29 +00:00
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
2023-02-22 18:43:48 +00:00
* (C) Copyright 2009-2024, Arnaud Roques
2019-03-01 22:16:29 +00:00
*
2023-02-22 18:43:48 +00:00
* Project Info: https://plantuml.com
2022-08-17 17:34:24 +00:00
*
2019-03-01 22:16:29 +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
*
2019-03-01 22:16:29 +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
* 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
2022-08-17 17:34:24 +00:00
*
2019-03-01 22:16:29 +00:00
*
*/
package net.sourceforge.plantuml;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
2022-11-04 17:36:03 +00:00
import java.util.ArrayList;
2021-08-30 17:13:54 +00:00
import java.util.Arrays;
2022-11-04 17:36:03 +00:00
import java.util.Iterator;
2019-03-29 22:14:07 +00:00
import java.util.List;
2019-03-01 22:16:29 +00:00
2023-02-22 18:43:48 +00:00
import net.atmp.PixelImage;
2019-03-01 22:16:29 +00:00
import net.sourceforge.plantuml.core.Diagram;
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.UShape;
2023-02-22 18:43:48 +00:00
import net.sourceforge.plantuml.klimt.creole.Neutron;
import net.sourceforge.plantuml.klimt.creole.atom.Atom;
import net.sourceforge.plantuml.klimt.drawing.UGraphic;
2023-02-02 17:59:43 +00:00
import net.sourceforge.plantuml.klimt.font.StringBounder;
import net.sourceforge.plantuml.klimt.geom.HorizontalAlignment;
2023-02-22 18:43:48 +00:00
import net.sourceforge.plantuml.klimt.geom.XDimension2D;
import net.sourceforge.plantuml.klimt.shape.AbstractTextBlock;
import net.sourceforge.plantuml.klimt.shape.Line;
import net.sourceforge.plantuml.klimt.shape.UImage;
import net.sourceforge.plantuml.klimt.shape.UImageSvg;
2022-08-17 17:34:24 +00:00
import net.sourceforge.plantuml.log.Logme;
2019-03-01 22:16:29 +00:00
import net.sourceforge.plantuml.preproc.Defines;
import net.sourceforge.plantuml.security.SImageIO;
2023-02-22 18:43:48 +00:00
import net.sourceforge.plantuml.style.ISkinSimple;
2023-02-02 17:59:43 +00:00
import net.sourceforge.plantuml.text.StringLocated;
2019-03-01 22:16:29 +00:00
2022-11-04 17:36:03 +00:00
public class EmbeddedDiagram extends AbstractTextBlock implements Line, Atom {
2023-02-28 21:22:51 +00:00
// ::remove file when __HAXE__
2022-11-04 17:36:03 +00:00
public static final String EMBEDDED_START = "{{";
public static final String EMBEDDED_END = "}}";
public static String getEmbeddedType(CharSequence cs) {
if (cs == null)
return null;
final String s = StringUtils.trin(cs.toString());
if (s.startsWith(EMBEDDED_START) == false)
return null;
if (s.equals(EMBEDDED_START))
return "uml";
if (s.equals(EMBEDDED_START + "ditaa"))
return "ditaa";
2022-11-04 17:36:03 +00:00
if (s.equals(EMBEDDED_START + "uml"))
return "uml";
if (s.equals(EMBEDDED_START + "wbs"))
return "wbs";
if (s.equals(EMBEDDED_START + "mindmap"))
return "mindmap";
if (s.equals(EMBEDDED_START + "gantt"))
return "gantt";
if (s.equals(EMBEDDED_START + "json"))
return "json";
if (s.equals(EMBEDDED_START + "yaml"))
return "yaml";
if (s.equals(EMBEDDED_START + "wire"))
return "wire";
if (s.equals(EMBEDDED_START + "creole"))
return "creole";
if (s.equals(EMBEDDED_START + "board"))
return "board";
if (s.equals(EMBEDDED_START + "ebnf"))
return "ebnf";
if (s.equals(EMBEDDED_START + "regex"))
return "regex";
if (s.equals(EMBEDDED_START + "files"))
return "files";
if (s.equals(EMBEDDED_START + "chronology"))
return "chronology";
2022-11-04 17:36:03 +00:00
return null;
}
public static EmbeddedDiagram createAndSkip(String type, Iterator<CharSequence> it, ISkinSimple skinParam) {
final List<String> result = new ArrayList<String>();
result.add("@start" + type);
int nested = 1;
while (it.hasNext()) {
final CharSequence s2 = it.next();
if (EmbeddedDiagram.getEmbeddedType(StringUtils.trinNoTrace(s2)) != null)
// if (StringUtils.trinNoTrace(s2).startsWith(EmbeddedDiagram.EMBEDDED_START))
nested++;
else if (StringUtils.trinNoTrace(s2).equals(EmbeddedDiagram.EMBEDDED_END)) {
nested--;
if (nested == 0)
break;
}
result.add(s2.toString());
}
result.add("@end" + type);
return EmbeddedDiagram.from(skinParam, result);
2022-03-13 12:22:51 +00:00
}
2022-11-04 17:36:03 +00:00
private final List<StringLocated> list;
private final ISkinSimple skinParam;
private BufferedImage image;
private EmbeddedDiagram(ISkinSimple skinParam, List<StringLocated> system) {
this.list = system;
2022-03-13 12:22:51 +00:00
this.skinParam = skinParam;
2022-11-04 17:36:03 +00:00
}
public static EmbeddedDiagram from(ISkinSimple skinParam, List<String> strings) {
return new EmbeddedDiagram(skinParam, BlockUml.convert(strings));
}
2022-03-13 12:22:51 +00:00
public double getStartingAltitude(StringBounder stringBounder) {
return 0;
}
2022-09-12 20:08:34 +00:00
public XDimension2D calculateDimension(StringBounder stringBounder) {
2022-03-13 12:22:51 +00:00
try {
2023-02-02 17:59:43 +00:00
if (stringBounder.matchesProperty("SVG")) {
2023-01-16 19:06:31 +00:00
final String imageSvg = getImageSvg();
final UImageSvg svg = new UImageSvg(imageSvg, 1);
return new XDimension2D(svg.getWidth(), svg.getHeight());
}
2022-03-13 12:22:51 +00:00
final BufferedImage im = getImage();
2022-09-12 20:08:34 +00:00
return new XDimension2D(im.getWidth(), im.getHeight());
2022-03-13 12:22:51 +00:00
} catch (IOException e) {
2022-08-17 17:34:24 +00:00
Logme.error(e);
2022-03-13 12:22:51 +00:00
} catch (InterruptedException e) {
2022-08-17 17:34:24 +00:00
Logme.error(e);
2022-03-13 12:22:51 +00:00
}
2022-09-12 20:08:34 +00:00
return new XDimension2D(42, 42);
2022-03-13 12:22:51 +00:00
}
public void drawU(UGraphic ug) {
try {
final boolean isSvg = ug.matchesProperty("SVG");
if (isSvg) {
final String imageSvg = getImageSvg();
final UImageSvg svg = new UImageSvg(imageSvg, 1);
ug.draw(svg);
return;
}
final BufferedImage im = getImage();
final UShape image = new UImage(new PixelImage(im, AffineTransformType.TYPE_BILINEAR));
ug.draw(image);
} catch (IOException e) {
2022-08-17 17:34:24 +00:00
Logme.error(e);
2022-03-13 12:22:51 +00:00
} catch (InterruptedException e) {
2022-08-17 17:34:24 +00:00
Logme.error(e);
2022-03-13 12:22:51 +00:00
}
}
private String getImageSvg() throws IOException, InterruptedException {
final Diagram system = getSystem();
final ByteArrayOutputStream os = new ByteArrayOutputStream();
system.exportDiagram(os, 0, new FileFormatOption(FileFormat.SVG));
os.close();
return new String(os.toByteArray());
}
private BufferedImage getImage() throws IOException, InterruptedException {
2022-04-10 19:24:55 +00:00
if (image == null)
2022-03-13 12:22:51 +00:00
image = getImageSlow();
2022-04-10 19:24:55 +00:00
2022-03-13 12:22:51 +00:00
return image;
}
private BufferedImage getImageSlow() throws IOException, InterruptedException {
final Diagram system = getSystem();
final ByteArrayOutputStream os = new ByteArrayOutputStream();
system.exportDiagram(os, 0, new FileFormatOption(FileFormat.PNG));
os.close();
return SImageIO.read(os.toByteArray());
}
public HorizontalAlignment getHorizontalAlignment() {
return HorizontalAlignment.LEFT;
}
private Diagram getSystem() throws IOException, InterruptedException {
2022-11-04 17:36:03 +00:00
final BlockUml blockUml = new BlockUml(list, Defines.createEmpty(), skinParam, null, null);
2022-03-13 12:22:51 +00:00
return blockUml.getDiagram();
}
2019-03-01 22:16:29 +00:00
@Override
public List<Neutron> getNeutrons() {
return Arrays.asList(Neutron.create(this));
}
}