1
0
mirror of https://github.com/octoleo/plantuml.git synced 2024-06-03 09:00:48 +00:00
plantuml/src/net/sourceforge/plantuml/svek/DotStringFactory.java

438 lines
14 KiB
Java
Raw Normal View History

2011-08-08 17:48:29 +00:00
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
2016-01-09 12:15:40 +00:00
* (C) Copyright 2009-2017, Arnaud Roques
2011-08-08 17:48:29 +00:00
*
* Project Info: http://plantuml.sourceforge.net
*
* 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
2011-08-08 17:48:29 +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.
*
* [Java is a trademark or registered trademark of Sun Microsystems, Inc.
* in the United States and other countries.]
*
* Original Author: Arnaud Roques
*
* Revision $Revision: 4236 $
*
*/
package net.sourceforge.plantuml.svek;
import java.awt.geom.Point2D;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
2011-09-07 20:41:58 +00:00
import net.sourceforge.plantuml.Log;
2016-01-09 12:15:40 +00:00
import net.sourceforge.plantuml.OptionFlags;
2011-08-08 17:48:29 +00:00
import net.sourceforge.plantuml.UmlDiagramType;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.cucadiagram.IGroup;
import net.sourceforge.plantuml.cucadiagram.Rankdir;
import net.sourceforge.plantuml.cucadiagram.dot.DotData;
2016-01-30 12:20:07 +00:00
import net.sourceforge.plantuml.cucadiagram.dot.DotSplines;
2011-08-08 17:48:29 +00:00
import net.sourceforge.plantuml.cucadiagram.dot.Graphviz;
import net.sourceforge.plantuml.cucadiagram.dot.GraphvizUtils;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.cucadiagram.dot.GraphvizVersion;
import net.sourceforge.plantuml.cucadiagram.dot.GraphvizVersions;
import net.sourceforge.plantuml.cucadiagram.dot.ProcessState;
2011-08-08 17:48:29 +00:00
import net.sourceforge.plantuml.graphic.StringBounder;
import net.sourceforge.plantuml.graphic.TextBlock;
2011-09-08 10:42:27 +00:00
import net.sourceforge.plantuml.posimo.Moveable;
2015-04-07 18:18:37 +00:00
import net.sourceforge.plantuml.StringUtils;
2011-08-08 17:48:29 +00:00
2011-09-08 10:42:27 +00:00
public class DotStringFactory implements Moveable {
2011-08-08 17:48:29 +00:00
2013-12-10 19:36:50 +00:00
private final Bibliotekon bibliotekon = new Bibliotekon();
2011-08-08 17:48:29 +00:00
final private Set<String> rankMin = new HashSet<String>();
private final ColorSequence colorSequence;
private final Cluster root;
2013-12-10 19:36:50 +00:00
2011-08-08 17:48:29 +00:00
private Cluster current;
2013-12-10 19:36:50 +00:00
private final DotData dotData;
2011-08-08 17:48:29 +00:00
private final StringBounder stringBounder;
2013-12-10 19:36:50 +00:00
public DotStringFactory(ColorSequence colorSequence, StringBounder stringBounder, DotData dotData) {
2011-08-08 17:48:29 +00:00
this.colorSequence = colorSequence;
2013-12-10 19:36:50 +00:00
this.dotData = dotData;
2011-08-08 17:48:29 +00:00
this.stringBounder = stringBounder;
2013-12-10 19:36:50 +00:00
this.root = new Cluster(colorSequence, dotData.getSkinParam(), dotData.getRootGroup());
2011-08-08 17:48:29 +00:00
this.current = root;
}
public void addShape(Shape shape) {
current.addShape(shape);
}
private void printMinRanking(StringBuilder sb) {
if (rankMin.size() == 0) {
return;
}
sb.append("{ rank = min;");
for (String id : rankMin) {
sb.append(id);
sb.append(";");
}
sb.append("}");
}
private double getHorizontalDzeta() {
double max = 0;
2013-12-10 19:36:50 +00:00
for (Line l : bibliotekon.allLines()) {
2011-08-08 17:48:29 +00:00
final double c = l.getHorizontalDzeta(stringBounder);
if (c > max) {
max = c;
}
}
return max / 10;
}
private double getVerticalDzeta() {
double max = 0;
2013-12-10 19:36:50 +00:00
for (Line l : bibliotekon.allLines()) {
2011-08-08 17:48:29 +00:00
final double c = l.getVerticalDzeta(stringBounder);
if (c > max) {
max = c;
}
}
return max / 10;
}
2013-12-10 19:36:50 +00:00
String createDotString(String... dotStrings) {
2011-08-08 17:48:29 +00:00
final StringBuilder sb = new StringBuilder();
double nodesep = getHorizontalDzeta();
if (nodesep < getMinNodeSep()) {
nodesep = getMinNodeSep();
}
2013-12-10 19:36:50 +00:00
if (dotData.getSkinParam().getNodesep() != 0) {
nodesep = dotData.getSkinParam().getNodesep();
}
2011-08-08 17:48:29 +00:00
final String nodesepInches = SvekUtils.pixelToInches(nodesep);
2013-12-10 19:36:50 +00:00
// Log.println("nodesep=" + nodesepInches);
2011-08-08 17:48:29 +00:00
double ranksep = getVerticalDzeta();
if (ranksep < getMinRankSep()) {
ranksep = getMinRankSep();
}
2013-12-10 19:36:50 +00:00
if (dotData.getSkinParam().getRanksep() != 0) {
ranksep = dotData.getSkinParam().getRanksep();
}
2011-08-08 17:48:29 +00:00
final String ranksepInches = SvekUtils.pixelToInches(ranksep);
2013-12-10 19:36:50 +00:00
// Log.println("ranksep=" + ranksepInches);
2011-08-08 17:48:29 +00:00
sb.append("digraph unix {");
SvekUtils.println(sb);
for (String s : dotStrings) {
if (s.startsWith("ranksep")) {
sb.append("ranksep=" + ranksepInches + ";");
} else if (s.startsWith("nodesep")) {
sb.append("nodesep=" + nodesepInches + ";");
} else {
sb.append(s);
}
SvekUtils.println(sb);
}
2015-04-07 18:18:37 +00:00
// sb.append("newrank=true;");
// SvekUtils.println(sb);
2011-08-08 17:48:29 +00:00
sb.append("remincross=true;");
SvekUtils.println(sb);
sb.append("searchsize=500;");
SvekUtils.println(sb);
2016-01-09 12:15:40 +00:00
if (OptionFlags.USE_COMPOUND) {
sb.append("compound=true;");
SvekUtils.println(sb);
}
2016-01-30 12:20:07 +00:00
final DotSplines dotSplines = dotData.getSkinParam().getDotSplines();
if (dotSplines == DotSplines.POLYLINE) {
sb.append("splines=polyline;");
SvekUtils.println(sb);
} else if (dotSplines == DotSplines.ORTHO) {
sb.append("splines=ortho;");
SvekUtils.println(sb);
}
2015-04-07 18:18:37 +00:00
if (dotData.getSkinParam().getRankdir() == Rankdir.LEFT_TO_RIGHT) {
2013-12-10 19:36:50 +00:00
sb.append("rankdir=LR;");
SvekUtils.println(sb);
}
manageMinMaxCluster(sb);
root.printCluster1(sb, bibliotekon.allLines());
for (Line line : bibliotekon.lines0()) {
2011-09-08 10:42:27 +00:00
line.appendLine(sb);
}
2011-08-08 17:48:29 +00:00
root.fillRankMin(rankMin);
2015-09-28 20:42:17 +00:00
root.printCluster2(sb, bibliotekon.allLines(), stringBounder, dotData.getDotMode(), getGraphvizVersion(),
dotData.getUmlDiagramType());
2011-08-08 17:48:29 +00:00
printMinRanking(sb);
2013-12-10 19:36:50 +00:00
for (Line line : bibliotekon.lines1()) {
2011-08-08 17:48:29 +00:00
line.appendLine(sb);
}
SvekUtils.println(sb);
sb.append("}");
return sb.toString();
}
2013-12-10 19:36:50 +00:00
private void manageMinMaxCluster(final StringBuilder sb) {
final List<String> minPointCluster = new ArrayList<String>();
final List<String> maxPointCluster = new ArrayList<String>();
for (Cluster cluster : bibliotekon.allCluster()) {
2015-04-07 18:18:37 +00:00
final String minPoint = cluster.getMinPoint(dotData.getUmlDiagramType());
2013-12-10 19:36:50 +00:00
if (minPoint != null) {
minPointCluster.add(minPoint);
}
2015-04-07 18:18:37 +00:00
final String maxPoint = cluster.getMaxPoint(dotData.getUmlDiagramType());
2013-12-10 19:36:50 +00:00
if (maxPoint != null) {
maxPointCluster.add(maxPoint);
}
}
if (minPointCluster.size() > 0) {
sb.append("{rank=min;");
for (String s : minPointCluster) {
sb.append(s);
sb.append(" [shape=point,width=.01,label=\"\"]");
sb.append(";");
}
sb.append("}");
SvekUtils.println(sb);
}
if (maxPointCluster.size() > 0) {
sb.append("{rank=max;");
for (String s : maxPointCluster) {
sb.append(s);
sb.append(" [shape=point,width=.01,label=\"\"]");
sb.append(";");
}
sb.append("}");
SvekUtils.println(sb);
}
}
2011-08-08 17:48:29 +00:00
private int getMinRankSep() {
2013-12-10 19:36:50 +00:00
if (dotData.getUmlDiagramType() == UmlDiagramType.ACTIVITY) {
// return 29;
return 40;
2011-08-08 17:48:29 +00:00
}
return 60;
}
private int getMinNodeSep() {
2013-12-10 19:36:50 +00:00
if (dotData.getUmlDiagramType() == UmlDiagramType.ACTIVITY) {
// return 15;
return 20;
2011-08-08 17:48:29 +00:00
}
2013-12-10 19:36:50 +00:00
return 35;
}
public GraphvizVersion getGraphvizVersion() {
final Graphviz graphviz = GraphvizUtils.create("foo;", "svg");
final File f = graphviz.getDotExe();
return GraphvizVersions.getInstance().getVersion(f);
2011-08-08 17:48:29 +00:00
}
2013-12-10 19:36:50 +00:00
public String getSvg(boolean trace, String... dotStrings) throws IOException {
2011-08-08 17:48:29 +00:00
final String dotString = createDotString(dotStrings);
2013-12-10 19:36:50 +00:00
if (trace) {
Log.info("Creating temporary file svek.dot");
SvekUtils.traceDotString(dotString);
}
2011-08-08 17:48:29 +00:00
final Graphviz graphviz = GraphvizUtils.create(dotString, "svg");
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
2013-12-10 19:36:50 +00:00
final ProcessState state = graphviz.createFile3(baos);
2011-08-08 17:48:29 +00:00
baos.close();
2015-04-07 18:18:37 +00:00
if (state.differs(ProcessState.TERMINATED_OK())) {
throw new IllegalStateException("Timeout4 " + state, state.getCause());
2013-12-10 19:36:50 +00:00
}
2011-08-08 17:48:29 +00:00
final byte[] result = baos.toByteArray();
final String s = new String(result, "UTF-8");
2013-12-10 19:36:50 +00:00
if (trace) {
Log.info("Creating temporary file svek.svg");
SvekUtils.traceSvgString(s);
}
2011-08-08 17:48:29 +00:00
return s;
}
public boolean illegalDotExe() {
final Graphviz graphviz = GraphvizUtils.create(null, "svg");
final File dotExe = graphviz.getDotExe();
return dotExe == null || dotExe.isFile() == false || dotExe.canRead() == false;
}
public File getDotExe() {
final Graphviz graphviz = GraphvizUtils.create(null, "svg");
return graphviz.getDotExe();
}
2013-12-10 19:36:50 +00:00
public ClusterPosition solve(String svg) throws IOException, InterruptedException {
if (svg.length() == 0) {
throw new EmptySvgException();
}
2011-08-08 17:48:29 +00:00
final Pattern pGraph = Pattern.compile("(?m)\\<svg\\s+width=\"(\\d+)pt\"\\s+height=\"(\\d+)pt\"");
final Matcher mGraph = pGraph.matcher(svg);
if (mGraph.find() == false) {
throw new IllegalStateException();
}
final int fullWidth = Integer.parseInt(mGraph.group(1));
final int fullHeight = Integer.parseInt(mGraph.group(2));
2013-12-10 19:36:50 +00:00
final MinFinder corner1 = new MinFinder();
for (Shape sh : bibliotekon.allShapes()) {
2011-08-08 17:48:29 +00:00
int idx = svg.indexOf("<title>" + sh.getUid() + "</title>");
2015-09-28 20:42:17 +00:00
if (sh.getType() == ShapeType.RECTANGLE || sh.getType() == ShapeType.FOLDER
|| sh.getType() == ShapeType.DIAMOND) {
2011-08-08 17:48:29 +00:00
final List<Point2D.Double> points = SvekUtils.extractPointsList(svg, idx, fullHeight);
final double minX = SvekUtils.getMinX(points);
final double minY = SvekUtils.getMinY(points);
2013-12-10 19:36:50 +00:00
corner1.manage(minX, minY);
sh.moveSvek(minX, minY);
2011-08-08 17:48:29 +00:00
} else if (sh.getType() == ShapeType.ROUND_RECTANGLE) {
2013-12-10 19:36:50 +00:00
final int idx2 = svg.indexOf("d=\"", idx + 1);
2011-08-08 17:48:29 +00:00
idx = svg.indexOf("points=\"", idx + 1);
2013-12-10 19:36:50 +00:00
final List<Point2D.Double> points;
2015-04-07 18:18:37 +00:00
if (idx2 != -1 && (idx == -1 || idx2 < idx)) {
2013-12-10 19:36:50 +00:00
// GraphViz 2.30
points = SvekUtils.extractD(svg, idx2, fullHeight);
} else {
points = SvekUtils.extractPointsList(svg, idx, fullHeight);
for (int i = 0; i < 3; i++) {
idx = svg.indexOf("points=\"", idx + 1);
points.addAll(SvekUtils.extractPointsList(svg, idx, fullHeight));
}
2011-08-08 17:48:29 +00:00
}
final double minX = SvekUtils.getMinX(points);
final double minY = SvekUtils.getMinY(points);
2013-12-10 19:36:50 +00:00
corner1.manage(minX, minY);
sh.moveSvek(minX, minY);
2015-04-07 18:18:37 +00:00
} else if (sh.getType() == ShapeType.OCTAGON) {
idx = svg.indexOf("points=\"", idx + 1);
final List<Point2D.Double> points = SvekUtils.extractPointsList(svg, idx, fullHeight);
final double minX = SvekUtils.getMinX(points);
final double minY = SvekUtils.getMinY(points);
corner1.manage(minX, minY);
sh.moveSvek(minX, minY);
sh.setOctagon(minX, minY, points);
2011-08-08 17:48:29 +00:00
} else if (sh.getType() == ShapeType.CIRCLE || sh.getType() == ShapeType.CIRCLE_IN_RECT
|| sh.getType() == ShapeType.OVAL) {
final double cx = SvekUtils.getValue(svg, idx, "cx");
final double cy = SvekUtils.getValue(svg, idx, "cy") + fullHeight;
final double rx = SvekUtils.getValue(svg, idx, "rx");
final double ry = SvekUtils.getValue(svg, idx, "ry");
2013-12-10 19:36:50 +00:00
sh.moveSvek(cx - rx, cy - ry);
2011-08-08 17:48:29 +00:00
} else {
throw new IllegalStateException(sh.getType().toString() + " " + sh.getUid());
}
}
2013-12-10 19:36:50 +00:00
for (Cluster cluster : bibliotekon.allCluster()) {
int idx = getClusterIndex(svg, cluster.getColor());
2011-08-08 17:48:29 +00:00
final List<Point2D.Double> points = SvekUtils.extractPointsList(svg, idx, fullHeight);
final double minX = SvekUtils.getMinX(points);
final double minY = SvekUtils.getMinY(points);
final double maxX = SvekUtils.getMaxX(points);
final double maxY = SvekUtils.getMaxY(points);
cluster.setPosition(minX, minY, maxX, maxY);
2013-12-10 19:36:50 +00:00
corner1.manage(minX, minY);
2011-08-08 17:48:29 +00:00
2013-12-10 19:36:50 +00:00
if (cluster.getTitleAndAttributeWidth() == 0 || cluster.getTitleAndAttributeHeight() == 0) {
2011-08-08 17:48:29 +00:00
continue;
}
2013-12-10 19:36:50 +00:00
idx = getClusterIndex(svg, cluster.getTitleColor());
2011-08-08 17:48:29 +00:00
final List<Point2D.Double> pointsTitle = SvekUtils.extractPointsList(svg, idx, fullHeight);
final double minXtitle = SvekUtils.getMinX(pointsTitle);
final double minYtitle = SvekUtils.getMinY(pointsTitle);
cluster.setTitlePosition(minXtitle, minYtitle);
}
2013-12-10 19:36:50 +00:00
for (Line line : bibliotekon.allLines()) {
2016-01-09 12:15:40 +00:00
line.solveLine(svg, fullHeight, corner1, dotData);
2011-08-08 17:48:29 +00:00
}
2011-09-07 20:41:58 +00:00
2013-12-10 19:36:50 +00:00
for (Line line : bibliotekon.allLines()) {
line.manageCollision(bibliotekon.allShapes());
2011-08-08 17:48:29 +00:00
}
2013-12-10 19:36:50 +00:00
corner1.manage(0, 0);
return new ClusterPosition(corner1.getMinX(), corner1.getMinY(), fullWidth, fullHeight);
// return new ClusterPosition(0, 0, fullWidth, fullHeight);
2011-08-08 17:48:29 +00:00
}
2013-12-10 19:36:50 +00:00
private int getClusterIndex(final String svg, int colorInt) {
2015-04-07 18:18:37 +00:00
final String colorString = StringUtils.goLowerCase(StringUtils.getAsHtml(colorInt));
2013-12-10 19:36:50 +00:00
final String keyTitle1 = "=\"" + colorString + "\"";
int idx = svg.indexOf(keyTitle1);
if (idx == -1) {
final String keyTitle2 = "stroke:" + colorString + ";";
idx = svg.indexOf(keyTitle2);
2011-09-08 10:42:27 +00:00
}
2013-12-10 19:36:50 +00:00
if (idx == -1) {
throw new IllegalStateException("Cannot find color " + colorString);
2011-09-08 10:42:27 +00:00
}
2013-12-10 19:36:50 +00:00
return idx;
2011-08-08 17:48:29 +00:00
}
2013-12-10 19:36:50 +00:00
public void openCluster(IGroup g, int titleAndAttributeWidth, int titleAndAttributeHeight, TextBlock title,
TextBlock stereo) {
this.current = current.createChild(g, titleAndAttributeWidth, titleAndAttributeHeight, title, stereo,
colorSequence, dotData.getSkinParam());
bibliotekon.addCluster(this.current);
2011-08-08 17:48:29 +00:00
}
public void closeCluster() {
if (current.getParent() == null) {
throw new IllegalStateException();
}
this.current = current.getParent();
}
2011-09-08 10:42:27 +00:00
public void moveSvek(double deltaX, double deltaY) {
2013-12-10 19:36:50 +00:00
for (Shape sh : bibliotekon.allShapes()) {
2011-09-08 10:42:27 +00:00
sh.moveSvek(deltaX, deltaY);
}
2013-12-10 19:36:50 +00:00
for (Line line : bibliotekon.allLines()) {
2011-09-08 10:42:27 +00:00
line.moveSvek(deltaX, deltaY);
}
2013-12-10 19:36:50 +00:00
for (Cluster cl : bibliotekon.allCluster()) {
2011-09-08 10:42:27 +00:00
cl.moveSvek(deltaX, deltaY);
}
}
2013-12-10 19:36:50 +00:00
public final Bibliotekon getBibliotekon() {
return bibliotekon;
}
2011-08-08 17:48:29 +00:00
}