Skip to content

Commit be2be31

Browse files
committed
fix: fixed tikz export to retain aspect ratio
1 parent cc8c6af commit be2be31

4 files changed

Lines changed: 83 additions & 16 deletions

File tree

src/main/java/org/variantsync/diffdetective/show/variation/DiffTreeApp.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,20 @@
3838
import java.util.Map;
3939

4040
public class DiffTreeApp extends App {
41-
private final static double DEFAULT_NODE_RADIUS = 50;
41+
public final static double TIKZ_NODE_RADIUS = 6.5;
42+
public final static int TIKZ_FONT_SIZE = 5;
43+
public final static double DEFAULT_NODE_RADIUS = 50;
44+
public final static int DEFAULT_FONT_SIZE = 30;
45+
4246
private final static Format DEFAULT_FORMAT =
4347
new Format(
44-
new ShowNodeFormat(),
48+
new PaperNodeFormat(),
4549
// There is a bug in the exporter currently that accidentally switches direction so as a workaround we revert it here.
4650
new DefaultEdgeLabelFormat(EdgeLabelFormat.Direction.ParentToChild)
4751
);
4852

4953
private final static List<DiffNodeLabelFormat> AVAILABLE_FORMATS = List.of(
54+
new PaperNodeFormat(),
5055
new ShowNodeFormat(),
5156
new LabelOnlyDiffNodeFormat(),
5257
new EditClassesDiffNodeFormat(),
@@ -175,9 +180,11 @@ private void saveTikz() {
175180
var unbufferedOutput = Files.newOutputStream(targetFile.toPath());
176181
var output = new BufferedOutputStream(unbufferedOutput)
177182
) {
183+
final double millimetersPerPixel = TIKZ_NODE_RADIUS / resolution.x();
178184
final Vec2 flipY = new Vec2(1.0, -1.0);
179-
final double nodeRadiusInTikz = 6.5;
180-
final Vec2 scaleToTikz = Vec2.all(nodeRadiusInTikz).dividedBy(resolution);
185+
final Vec2 scaleToTikz =
186+
Vec2.all(millimetersPerPixel);
187+
// Vec2.all(TIKZ_NODE_RADIUS).dividedBy(resolution);
181188

182189
tikzExporter.exportDiffTree(
183190
diffTree,
@@ -187,7 +194,8 @@ private void saveTikz() {
187194
pos = pos.scale(scaleToTikz);
188195
return pos;
189196
},
190-
output
197+
output,
198+
true
191199
);
192200
} catch (IOException e) {
193201
JOptionPane.showMessageDialog(

src/main/java/org/variantsync/diffdetective/show/variation/GraphNodeGraphics.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class GraphNodeGraphics extends EntityGraphics {
1818
double textbox_borderwidth_absolute = 2;
1919
double textbox_insets_absolute = 12;
2020
int textbox_borderarcwidth_absolute = 7;
21-
Font basic = new Font(null, Font.PLAIN, 20);
21+
Font basic = new Font(null, Font.PLAIN, DiffTreeApp.DEFAULT_FONT_SIZE);
2222

2323
public GraphNodeGraphics(NodeView node) {
2424
this.node = node;

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

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
import java.util.Map;
1717
import java.util.function.Function;
1818
import java.util.stream.Collectors;
19+
import java.util.stream.Stream;
20+
1921

2022
/**
2123
* Exporter for TikZ pictures which can be embedded into a LaTeX document.
@@ -69,13 +71,15 @@ public void exportDiffTree(
6971
exportDiffTree(
7072
diffTree,
7173
positions::get,
72-
destination);
74+
destination,
75+
true);
7376
}
7477

7578
public void exportDiffTree(
7679
DiffTree diffTree,
7780
Function<DiffNode, Vec2> nodeLayout,
78-
OutputStream destination
81+
OutputStream destination,
82+
boolean escape
7983
) {
8084
// Start tikz picture.
8185
var output = new PrintStream(destination);
@@ -101,24 +105,25 @@ public void exportDiffTree(
101105
output.format("%n\t\\draw[vtdarrow]");
102106
format.forEachEdge(diffTree, (edge) -> {
103107
output.format("%n\t\t(node_%d) edge[%s] (node_%d)",
104-
edge.from().getID(),
105-
edge.style().tikzStyle(),
106-
edge.to().getID());
108+
edge.from().getID(),
109+
edge.style().tikzStyle(),
110+
edge.to().getID());
107111
});
108112
output.println();
109113
output.format("%n\t;");
110114
output.println();
111115

112116
// Draw node labels. We do this last so that they are on top of edges and nodes.
113117
format.forEachNode(diffTree, (node) -> {
114-
String escapedLabel =
118+
Stream<String> labels =
115119
format
116120
.getNodeFormat()
117121
.toMultilineLabel(node)
118-
.stream()
119-
.map(LaTeX::escape)
120-
.collect(Collectors
121-
.joining(" \\\\ "));
122+
.stream();
123+
if (escape) {
124+
labels = labels.map(LaTeX::escape);
125+
}
126+
String escapedLabel = labels.collect(Collectors.joining(" \\\\ "));
122127

123128
output.format("\t\\node[textbox] at (%s) {%s};%n",
124129
node.getID(),
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package org.variantsync.diffdetective.variation.diff.serialize.nodeformat;
2+
3+
import org.prop4j.NodeWriter;
4+
import org.variantsync.diffdetective.util.LaTeX;
5+
import org.variantsync.diffdetective.util.StringUtils;
6+
import org.variantsync.diffdetective.variation.NodeType;
7+
import org.variantsync.diffdetective.variation.diff.DiffNode;
8+
9+
import java.util.Arrays;
10+
import java.util.List;
11+
12+
public class PaperNodeFormat implements DiffNodeLabelFormat {
13+
public static final int MAX_ARTIFACT_LABEL_LENGTH = 12;
14+
15+
@Override
16+
public String toLabel(DiffNode node) {
17+
if (node.isRoot()) {
18+
return "r";
19+
}
20+
21+
String s = "";
22+
23+
if (node.isAnnotation()) {
24+
// if (node.getNodeType() == NodeType.ELSE) {
25+
// s += "ELSE";
26+
// } else {
27+
s += node.getFormula().toString(NodeWriter.logicalSymbols);
28+
// }
29+
} else {
30+
final int lineNoFrom = node.getFromLine().inDiff();
31+
final int lineNoTo = node.getToLine().inDiff();
32+
s += lineNoFrom;
33+
if (lineNoFrom != lineNoTo - 1) {
34+
s += "-" + lineNoTo;
35+
}
36+
37+
s += ": ";
38+
39+
40+
String label = node.getLabel().toString().trim();
41+
if (label.length() > MAX_ARTIFACT_LABEL_LENGTH) {
42+
label = StringUtils.clamp(MAX_ARTIFACT_LABEL_LENGTH - 3, label) + "...";
43+
}
44+
s += label; //LaTeX.escape(label);
45+
}
46+
47+
return s;
48+
}
49+
50+
@Override
51+
public List<String> toMultilineLabel(DiffNode node) {
52+
return Arrays.stream(toLabel(node).split(StringUtils.LINEBREAK)).toList();
53+
}
54+
}

0 commit comments

Comments
 (0)