Skip to content

Commit

Permalink
🐛 Skip using dot in ASCII art when there is only one element.
Browse files Browse the repository at this point in the history
  • Loading branch information
arnaudroques committed Dec 13, 2024
1 parent 113265a commit 6e6204e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 44 deletions.
28 changes: 16 additions & 12 deletions src/net/sourceforge/plantuml/dot/CucaDiagramTxtMaker.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,21 +111,25 @@ public CucaDiagramTxtMaker(CucaDiagram diagram, FileFormat fileFormat) throws IO
blocks.put(ent, b);
}

final GraphvizSolverB solver = new GraphvizSolverB();
if (blocks.size() > 1) {
final GraphvizSolverB solver = new GraphvizSolverB();

final Collection<Path> paths = new ArrayList<>();
for (Link link : diagram.getLinks()) {
final Block b1 = blocks.get(link.getEntity1());
final Block b2 = blocks.get(link.getEntity2());
paths.add(new Path(b1, b2, null, link.getLength(), link.isInvis()));
}
solver.solve(root, paths);
for (Path p : paths) {
if (p.isInvis())
continue;
final Collection<Path> paths = new ArrayList<>();
for (Link link : diagram.getLinks()) {
final Block b1 = blocks.get(link.getEntity1());
final Block b2 = blocks.get(link.getEntity2());
paths.add(new Path(b1, b2, null, link.getLength(), link.isInvis()));
}
solver.solve(root, paths);
for (Path p : paths) {
if (p.isInvis())
continue;

drawDotPath(p.getDotPath(), globalUg.getCharArea(), getXPixelPerChar(), getYPixelPerChar());
}

drawDotPath(p.getDotPath(), globalUg.getCharArea(), getXPixelPerChar(), getYPixelPerChar());
}

for (Entity ent : diagram.leafs()) {
final Block b = blocks.get(ent);
final XPoint2D p = b.getPosition();
Expand Down
59 changes: 27 additions & 32 deletions src/net/sourceforge/plantuml/posimo/GraphvizSolverB.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@ public XDimension2D solve(Cluster root, Collection<Path> paths) throws IOExcepti
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final ProcessState state = graphviz.createFile3(baos);
baos.close();
if (state.differs(ProcessState.TERMINATED_OK())) {
if (state.differs(ProcessState.TERMINATED_OK()))
throw new IllegalStateException("Timeout2 " + state);
}

final byte[] result = baos.toByteArray();
final String s = new String(result, UTF_8);
// Log.println("result=" + s);
Expand All @@ -119,19 +119,19 @@ public XDimension2D solve(Cluster root, Collection<Path> paths) throws IOExcepti

final Pattern pGraph = Pattern.compile("(?m)\\<svg\\s+width=\"(\\d+)pt\"\\s+height=\"(\\d+)pt\"");
final Matcher mGraph = pGraph.matcher(s);
if (mGraph.find() == false) {
if (mGraph.find() == false)
throw new IllegalStateException();
}

final int width = Integer.parseInt(mGraph.group(1));
final int height = Integer.parseInt(mGraph.group(2));

final YDelta yDelta = new YDelta(height);
for (Block b : root.getRecursiveContents()) {
final String start = "b" + b.getUid();
final int p1 = s.indexOf("<title>" + start + "</title>");
if (p1 == -1) {
if (p1 == -1)
throw new IllegalStateException();
}

final List<XPoint2D> pointsList = extractPointsList(s, p1, yDelta);
b.setX(getMinX(pointsList));
b.setY(getMinY(pointsList));
Expand All @@ -141,9 +141,9 @@ public XDimension2D solve(Cluster root, Collection<Path> paths) throws IOExcepti
for (Cluster cl : root.getSubClusters()) {
final String start = "cluster" + cl.getUid();
final int p1 = s.indexOf("<title>" + start + "</title>");
if (p1 == -1) {
if (p1 == -1)
throw new IllegalStateException();
}

final List<XPoint2D> pointsList = extractPointsList(s, p1, yDelta);
cl.setX(getMinX(pointsList));
cl.setY(getMinY(pointsList));
Expand All @@ -159,9 +159,9 @@ public XDimension2D solve(Cluster root, Collection<Path> paths) throws IOExcepti
final String end = "b" + p.getEnd().getUid();
final String searched = "<title>" + start + "&#45;&gt;" + end + "</title>";
final int p1 = s.indexOf(searched);
if (p1 == -1) {
if (p1 == -1)
throw new IllegalStateException(searched);
}

final int p2 = s.indexOf(" d=\"", p1);
final int p3 = s.indexOf("\"", p2 + " d=\"".length());
final String points = s.substring(p2 + " d=\"".length(), p3);
Expand All @@ -187,60 +187,55 @@ static private List<XPoint2D> extractPointsList(final String svg, final int star

static private double getMaxX(List<XPoint2D> points) {
double result = points.get(0).x;
for (int i = 1; i < points.size(); i++) {
if (points.get(i).x > result) {
for (int i = 1; i < points.size(); i++)
if (points.get(i).x > result)
result = points.get(i).x;
}
}

return result;
}

static private double getMinX(List<XPoint2D> points) {
double result = points.get(0).x;
for (int i = 1; i < points.size(); i++) {
if (points.get(i).x < result) {
for (int i = 1; i < points.size(); i++)
if (points.get(i).x < result)
result = points.get(i).x;
}
}

return result;
}

static private double getMaxY(List<XPoint2D> points) {
double result = points.get(0).y;
for (int i = 1; i < points.size(); i++) {
if (points.get(i).y > result) {
for (int i = 1; i < points.size(); i++)
if (points.get(i).y > result)
result = points.get(i).y;
}
}

return result;
}

static private double getMinY(List<XPoint2D> points) {
double result = points.get(0).y;
for (int i = 1; i < points.size(); i++) {
if (points.get(i).y < result) {
for (int i = 1; i < points.size(); i++)
if (points.get(i).y < result)
result = points.get(i).y;
}
}

return result;
}

private void exportPng(final String dotString, SFile f) throws IOException {
final Graphviz graphviz = GraphvizUtils.create(null, dotString, "png");
try (OutputStream os = f.createBufferedOutputStream()) {
final ProcessState state = graphviz.createFile3(os);
if (state.differs(ProcessState.TERMINATED_OK())) {
if (state.differs(ProcessState.TERMINATED_OK()))
throw new IllegalStateException("Timeout3 " + state);
}

}
}

private Path getPath(Collection<Path> paths, int start, int end) {
for (Path p : paths) {
if (p.getStart().getUid() == start && p.getEnd().getUid() == end) {
for (Path p : paths)
if (p.getStart().getUid() == start && p.getEnd().getUid() == end)
return p;
}
}

throw new IllegalArgumentException();

}
Expand Down

0 comments on commit 6e6204e

Please sign in to comment.