Skip to content

Commit a561fad

Browse files
committed
added svg producing to check that work result if needed
1 parent d44db6d commit a561fad

1 file changed

Lines changed: 118 additions & 0 deletions

File tree

jbbp/src/test/java/com/igormaznitsa/jbbp/it/RemarkableLinesParsingTest.java

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.igormaznitsa.jbbp.it;
22

33
import static com.igormaznitsa.jbbp.io.JBBPByteOrder.LITTLE_ENDIAN;
4+
import static java.lang.String.format;
45
import static org.junit.jupiter.api.Assertions.assertEquals;
56

67

@@ -9,6 +10,8 @@
910
import com.igormaznitsa.jbbp.io.JBBPOut;
1011
import com.igormaznitsa.jbbp.mapper.Bin;
1112
import com.igormaznitsa.jbbp.mapper.BinType;
13+
import java.io.PrintWriter;
14+
import java.util.UUID;
1215
import org.junit.jupiter.api.Test;
1316

1417
public class RemarkableLinesParsingTest extends AbstractParserIntegrationTest {
@@ -48,6 +51,10 @@ public void testV5_Remarkable1() throws Exception {
4851
assertEquals("reMarkable .lines file, version=5", parsed.header.trim());
4952
final byte[] written = JBBPOut.BeginBin().Bin(parsed).End().toByteArray();
5053
assertResource("remarkable1.rm", written);
54+
55+
// final StringWriter writer = new StringWriter();
56+
// parsed.printSvg(new PrintWriter(writer, true));
57+
// System.out.println(writer.toString());
5158
}
5259

5360
public static class RemarkableV5Body {
@@ -58,12 +65,58 @@ public static class RemarkableV5Body {
5865
@Bin(order = 3, arraySizeExpr = "nlayers")
5966
public Layer[] layers;
6067

68+
public void printSvg(final PrintWriter writer) {
69+
float maxX = Float.MIN_VALUE;
70+
float maxY = Float.MIN_VALUE;
71+
for (final Layer l : this.layers) {
72+
maxX = Math.max(maxX, l.findMaxX());
73+
maxY = Math.max(maxY, l.findMaxY());
74+
}
75+
76+
writer.print(format("<svg version=\"1.1\"" +
77+
" baseProfile=\"full\"" +
78+
" xmlns=\"http://www.w3.org/2000/svg\"" +
79+
" xmlns:xlink=\"http://www.w3.org/1999/xlink\"" +
80+
" xmlns:ev=\"http://www.w3.org/2001/xml-events\"" +
81+
" width=\"%f\" height=\"%f\">", maxX, maxY));
82+
writer.println();
83+
writer.print(format("<rect width=\"%f\" height=\"%f\" fill=\"darkgrey\"/>", maxX, maxY));
84+
writer.println();
85+
for (final Layer layer : layers) {
86+
layer.printSvg(writer);
87+
}
88+
writer.print("</svg>");
89+
}
90+
6191
public static class Layer {
6292
@Bin(order = 1, byteOrder = LITTLE_ENDIAN)
6393
public int nstrokes;
6494
@Bin(order = 2, arraySizeExpr = "nstrokes")
6595
public Stroke[] strokes;
6696

97+
public void printSvg(final PrintWriter writer) {
98+
for (final Stroke stroke : strokes) {
99+
stroke.printSvg(writer);
100+
}
101+
writer.println();
102+
}
103+
104+
public float findMaxX() {
105+
float result = Float.MIN_VALUE;
106+
for (final Stroke s : strokes) {
107+
result = Math.max(result, s.findMaxX());
108+
}
109+
return result;
110+
}
111+
112+
public float findMaxY() {
113+
float result = Float.MIN_VALUE;
114+
for (final Stroke s : strokes) {
115+
result = Math.max(result, s.findMaxY());
116+
}
117+
return result;
118+
}
119+
67120
public static class Stroke {
68121
@Bin(order = 1, byteOrder = LITTLE_ENDIAN)
69122
public int pen;
@@ -80,6 +133,67 @@ public static class Stroke {
80133
@Bin(order = 7, arraySizeExpr = "nsegments")
81134
public Segment[] segments;
82135

136+
private static String color2svg(final int index) {
137+
switch (index) {
138+
case 1:
139+
return "grey";
140+
case 2:
141+
return "white";
142+
default:
143+
return "black";
144+
}
145+
}
146+
147+
private static float pen2opacity(final int index) {
148+
switch (index) {
149+
case 3:
150+
case 7:
151+
case 13:
152+
case 16:
153+
return 0.9f;
154+
case 5:
155+
case 18:
156+
return 0.2f;
157+
case 8:
158+
return 0.0f;
159+
default:
160+
return 1.0f;
161+
}
162+
}
163+
164+
public float findMaxX() {
165+
float result = Float.MIN_VALUE;
166+
for (final Segment s : segments) {
167+
result = Math.max(result, s.x);
168+
}
169+
return result;
170+
}
171+
172+
public float findMaxY() {
173+
float result = Float.MIN_VALUE;
174+
for (final Segment s : segments) {
175+
result = Math.max(result, s.y);
176+
}
177+
return result;
178+
}
179+
180+
public void printSvg(final PrintWriter writer) {
181+
writer.print(format("<g id=\"%s\" style=\"stroke: %s; fill:none; opacity: %f\">",
182+
UUID.randomUUID().toString(), color2svg(this.color), pen2opacity(this.pen)));
183+
writer.println();
184+
writer.print(format("<polyline stroke-width=\"%f\" points=\"", this.width));
185+
String space = "";
186+
for (final Segment s : segments) {
187+
writer.print(space);
188+
s.printSvg(writer);
189+
space = " ";
190+
}
191+
writer.print("\"/>");
192+
writer.println();
193+
writer.print("</g>");
194+
writer.println();
195+
}
196+
83197
public static class Segment {
84198
@Bin(order = 1, byteOrder = LITTLE_ENDIAN)
85199
public float x;
@@ -93,6 +207,10 @@ public static class Segment {
93207
public float unknown1;
94208
@Bin(order = 6, byteOrder = LITTLE_ENDIAN)
95209
public float unknown2;
210+
211+
public void printSvg(final PrintWriter writer) {
212+
writer.print(format("%f,%f", x, y));
213+
}
96214
}
97215
}
98216
}

0 commit comments

Comments
 (0)