Skip to content

Commit 4c4b868

Browse files
committed
feat: tikz export with custom layouts
1 parent 2f05969 commit 4c4b868

1 file changed

Lines changed: 32 additions & 9 deletions

File tree

  • src/main/java/org/variantsync/diffdetective/variation/diff/serialize

src/main/java/org/variantsync/diffdetective/variation/diff/serialize/TikzExporter.java

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
package org.variantsync.diffdetective.variation.diff.serialize;
22

3+
import org.variantsync.diffdetective.show.engine.geom.Vec2;
4+
import org.variantsync.diffdetective.util.LaTeX;
5+
import org.variantsync.diffdetective.variation.diff.DiffNode;
6+
import org.variantsync.diffdetective.variation.diff.DiffTree;
7+
38
import java.io.BufferedInputStream;
4-
import java.io.BufferedReader;
59
import java.io.IOException;
6-
import java.io.InputStreamReader;
710
import java.io.OutputStream;
811
import java.io.PrintStream;
912
import java.nio.file.Files;
1013
import java.nio.file.Path;
14+
import java.util.HashMap;
1115
import java.util.Locale;
12-
import java.util.regex.Pattern;
16+
import java.util.Map;
17+
import java.util.function.Function;
1318
import java.util.stream.Collectors;
1419

15-
import org.variantsync.diffdetective.util.LaTeX;
16-
import org.variantsync.diffdetective.variation.diff.DiffTree;
17-
1820
/**
1921
* Exporter for TikZ pictures which can be embedded into a LaTeX document.
2022
*
@@ -55,13 +57,34 @@ public void exportDiffTree(
5557
GraphvizExporter.LayoutAlgorithm algorithm,
5658
OutputStream destination
5759
) throws IOException {
60+
final Map<Integer, DiffNode> ids = new HashMap<>();
61+
diffTree.forAll(node -> ids.put(node.getID(), node));
62+
63+
// Convert the layout information received by Graphviz to coordinates used by TikZ.
64+
final Map<DiffNode, Vec2> positions = new HashMap<>();
65+
GraphvizExporter.layoutNodesIn(diffTree, format, algorithm, (id, x, y) ->
66+
positions.put(ids.get(id), new Vec2(x, y))
67+
);
68+
69+
exportDiffTree(
70+
diffTree,
71+
positions::get,
72+
destination);
73+
}
74+
75+
public void exportDiffTree(
76+
DiffTree diffTree,
77+
Function<DiffNode, Vec2> nodeLayout,
78+
OutputStream destination
79+
) {
5880
// Start tikz picture.
5981
var output = new PrintStream(destination);
6082
output.println("\\begin{tikzpicture}");
6183

6284
// Convert the layout information received by Graphviz to coordinates used by TikZ.
63-
GraphvizExporter.layoutNodesIn(diffTree, format, algorithm, (id, x, y) -> {
64-
output.format("\t\\coordinate (%s) at (%s,%s);%n", id, x, y);
85+
diffTree.forAll(node -> {
86+
final Vec2 position = nodeLayout.apply(node);
87+
output.format("\t\\coordinate (%s) at (%s,%s);%n", node.getID(), position.x(), position.y());
6588
});
6689

6790
// Add all TikZ nodes positioned at the Graphviz coordinates.
@@ -72,7 +95,7 @@ public void exportDiffTree(
7295
node.getID(),
7396
node.getID());
7497
});
75-
output.println("");
98+
output.println();
7699

77100
// Add all TikZ edges positioned.
78101
output.format("%n\t\\draw[vtdarrow]");

0 commit comments

Comments
 (0)