1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
|
#include <cxxtest/TestSuite.h>
#include "libnr/n-art-bpath.h"
#include "svg/svg.h"
#include "2geom/coord.h"
#include <string>
#include <vector>
#include <glib/gmem.h>
class SvgPathTest : public CxxTest::TestSuite
{
private:
std::vector<std::string> rectanglesAbsoluteClosed;
std::vector<std::string> rectanglesRelativeClosed;
std::vector<std::string> rectanglesAbsoluteOpen;
std::vector<std::string> rectanglesRelativeOpen;
NArtBpath rectangleBpath[5+1];
public:
SvgPathTest() {
// Lots of ways to define the same rectangle
rectanglesAbsoluteClosed.push_back("M 1,2 L 4,2 L 4,8 L 1,8 L 1,2 Z");
rectanglesAbsoluteClosed.push_back("M 1,2 L 4,2 L 4,8 L 1,8 z");
rectanglesAbsoluteClosed.push_back("M 1,2 4,2 4,8 1,8 z");
rectanglesAbsoluteClosed.push_back("M 1,2 H 4 V 8 H 1 z");
rectanglesRelativeClosed.push_back("m 1,2 l 3,0 l 0,6 l -3,0 z");
rectanglesRelativeClosed.push_back("m 1,2 3,0 0,6 -3,0 z");
rectanglesRelativeClosed.push_back("m 1,2 h 3 v 6 h -3 z");
rectanglesAbsoluteOpen.push_back("M 1,2 L 4,2 L 4,8 L 1,8 L 1,2");
rectanglesAbsoluteOpen.push_back("M 1,2 4,2 4,8 1,8 1,2");
rectanglesAbsoluteOpen.push_back("M 1,2 H 4 V 8 H 1 V 2");
rectanglesRelativeOpen.push_back("m 1,2 l 3,0 l 0,6 l -3,0 l 0,-6");
rectanglesRelativeOpen.push_back("m 1,2 3,0 0,6 -3,0 0,-6");
rectanglesRelativeOpen.push_back("m 1,2 h 3 v 6 h -3 v -6");
rectangleBpath[0].code = NR_MOVETO;
rectangleBpath[0].x3 = 1;
rectangleBpath[0].y3 = 2;
rectangleBpath[1].code = NR_LINETO;
rectangleBpath[1].x3 = 4;
rectangleBpath[1].y3 = 2;
rectangleBpath[2].code = NR_LINETO;
rectangleBpath[2].x3 = 4;
rectangleBpath[2].y3 = 8;
rectangleBpath[3].code = NR_LINETO;
rectangleBpath[3].x3 = 1;
rectangleBpath[3].y3 = 8;
rectangleBpath[4].code = NR_LINETO;
rectangleBpath[4].x3 = 1;
rectangleBpath[4].y3 = 2;
rectangleBpath[5].code = NR_END;
// TODO: Also test some (smooth) cubic/quadratic beziers and elliptical arcs
}
void testReadRectanglesAbsoluteClosed()
{
rectangleBpath[0].code = NR_MOVETO;
for(size_t i=0; i<rectanglesAbsoluteClosed.size(); i++) {
NArtBpath * bpath = sp_svg_read_path(rectanglesAbsoluteClosed[i].c_str());
TS_ASSERT(bpathEqual(bpath,rectangleBpath));
g_free(bpath);
}
}
void testReadRectanglesRelativeClosed()
{
rectangleBpath[0].code = NR_MOVETO;
for(size_t i=0; i<rectanglesRelativeClosed.size(); i++) {
NArtBpath * bpath = sp_svg_read_path(rectanglesRelativeClosed[i].c_str());
TS_ASSERT(bpathEqual(bpath,rectangleBpath));
g_free(bpath);
}
}
void testReadRectanglesAbsoluteOpen()
{
rectangleBpath[0].code = NR_MOVETO_OPEN;
for(size_t i=0; i<rectanglesAbsoluteOpen.size(); i++) {
NArtBpath * bpath = sp_svg_read_path(rectanglesAbsoluteOpen[i].c_str());
TS_ASSERT(bpathEqual(bpath,rectangleBpath));
g_free(bpath);
}
}
void testReadRectanglesRelativeOpen()
{
rectangleBpath[0].code = NR_MOVETO_OPEN;
for(size_t i=0; i<rectanglesRelativeOpen.size(); i++) {
NArtBpath * bpath = sp_svg_read_path(rectanglesRelativeOpen[i].c_str());
TS_ASSERT(bpathEqual(bpath,rectangleBpath));
g_free(bpath);
}
}
void testReadConcatenatedPaths()
{
NArtBpath bpath_good[4*5+1];
for(size_t i=0; i<4; i++) {
memcpy(bpath_good+i*5,rectangleBpath,sizeof(rectangleBpath[0])*5);
}
bpath_good[0*5].code = NR_MOVETO;
bpath_good[1*5].code = NR_MOVETO_OPEN;
bpath_good[2*5].code = NR_MOVETO;
bpath_good[3*5].code = NR_MOVETO_OPEN;
bpath_good[4*5].code = NR_END;
for(size_t i=0; i<5; i++) {
bpath_good[1*5+i].x3 += bpath_good[0*5+4].x3;
bpath_good[1*5+i].y3 += bpath_good[0*5+4].y3;
}
for(size_t i=0; i<5; i++) {
bpath_good[2*5+i].x3 += bpath_good[1*5+4].x3;
bpath_good[2*5+i].y3 += bpath_good[1*5+4].y3;
}
std::string path_str = rectanglesAbsoluteClosed[0] + rectanglesRelativeOpen[0] + rectanglesRelativeClosed[0] + rectanglesAbsoluteOpen[0];
NArtBpath * bpath = sp_svg_read_path(path_str.c_str());
TS_ASSERT(bpathEqual(bpath,bpath_good));
g_free(bpath);
}
void testReadZeroLengthSubpaths() {
// Per the SVG 1.1 specification (section F5) zero-length subpaths are relevant
NArtBpath bpath_good[8+1];
bpath_good[0].code = NR_MOVETO_OPEN;
bpath_good[0].x3 = bpath_good[0].y3 = 0;
bpath_good[1].code = NR_MOVETO_OPEN;
bpath_good[1].x3 = bpath_good[1].y3 = 1;
bpath_good[2].code = NR_LINETO;
bpath_good[2].x3 = bpath_good[2].y3 = 2;
bpath_good[3].code = NR_MOVETO;
bpath_good[3].x3 = bpath_good[3].y3 = 3;
bpath_good[4].code = NR_MOVETO;
bpath_good[4].x3 = bpath_good[4].y3 = 4;
bpath_good[5].code = NR_LINETO;
bpath_good[5].x3 = bpath_good[5].y3 = 5;
bpath_good[6].code = NR_LINETO;
bpath_good[6].x3 = bpath_good[6].y3 = 4;
bpath_good[7].code = NR_MOVETO_OPEN;
bpath_good[7].x3 = bpath_good[7].y3 = 6;
bpath_good[8].code = NR_END;
{ // Test absolute version
char const * path_str = "M 0,0 M 1,1 L 2,2 M 3,3 z M 4,4 L 5,5 z M 6,6";
NArtBpath * bpath = sp_svg_read_path(path_str);
TS_ASSERT(bpathEqual(bpath,bpath_good));
g_free(bpath);
}
{ // Test relative version
char const * path_str = "m 0,0 m 1,1 l 1,1 m 1,1 z m 1,1 l 1,1 z m 2,2";
NArtBpath * bpath = sp_svg_read_path(path_str);
TS_ASSERT(bpathEqual(bpath,bpath_good));
g_free(bpath);
}
}
void testReadImplicitMoveto() {
NArtBpath bpath_good[6+1];
bpath_good[0].code = NR_MOVETO;
bpath_good[0].x3 = bpath_good[0].y3 = 1;
bpath_good[1].code = NR_LINETO;
bpath_good[1].x3 = bpath_good[1].y3 = 2;
bpath_good[2].code = NR_LINETO;
bpath_good[2].x3 = bpath_good[2].y3 = 1;
bpath_good[3].code = NR_MOVETO;
bpath_good[3].x3 = bpath_good[3].y3 = 1;
bpath_good[4].code = NR_LINETO;
bpath_good[4].x3 = bpath_good[4].y3 = 3;
bpath_good[5].code = NR_LINETO;
bpath_good[5].x3 = bpath_good[5].y3 = 1;
bpath_good[6].code = NR_END;
{ // Test absolute version
char const * path_str = "M 1,1 L 2,2 z L 3,3 z";
NArtBpath * bpath = sp_svg_read_path(path_str);
TS_ASSERT(bpathEqual(bpath,bpath_good));
g_free(bpath);
}
{ // Test relative version
char const * path_str = "M 1,1 L 2,2 z L 3,3 z";
NArtBpath * bpath = sp_svg_read_path(path_str);
TS_ASSERT(bpathEqual(bpath,bpath_good));
g_free(bpath);
}
}
void testReadFloatingPoint() {
NArtBpath bpath_good[5+1];
bpath_good[0].code = NR_MOVETO;
bpath_good[0].x3 = .01;
bpath_good[0].y3 = .02;
bpath_good[1].code = NR_LINETO;
bpath_good[1].x3 = .04;
bpath_good[1].y3 = .02;
bpath_good[2].code = NR_LINETO;
bpath_good[2].x3 = 1.5;
bpath_good[2].y3 = 1.6;
bpath_good[3].code = NR_LINETO;
bpath_good[3].x3 = .01;
bpath_good[3].y3 = .08;
bpath_good[4].code = NR_LINETO;
bpath_good[4].x3 = .01;
bpath_good[4].y3 = .02;
bpath_good[5].code = NR_END;
{ // Test decimals
char const * path_str = "M .01,.02 L.04.02 L1.5,1.6L0.01,0.08 .01.02 z";
NArtBpath * bpath = sp_svg_read_path(path_str);
TS_ASSERT(bpathEqual(bpath,bpath_good));
g_free(bpath);
}
{ // Test exponent
char const * path_str = "M 1e-2,.2e-1 L 0.004e1,0.0002e+2 L0150E-2,1.6e0L1.0e-2,80e-3 z";
NArtBpath * bpath = sp_svg_read_path(path_str);
TS_ASSERT(bpathEqual(bpath,bpath_good));
g_free(bpath);
}
}
void testReadImplicitSeparation() {
// Coordinates need not be separated by whitespace if they can still be read unambiguously
NArtBpath bpath_good[5+1];
bpath_good[0].code = NR_MOVETO;
bpath_good[0].x3 = .1;
bpath_good[0].y3 = .2;
bpath_good[1].code = NR_LINETO;
bpath_good[1].x3 = .4;
bpath_good[1].y3 = .2;
bpath_good[2].code = NR_LINETO;
bpath_good[2].x3 = .4;
bpath_good[2].y3 = .8;
bpath_good[3].code = NR_LINETO;
bpath_good[3].x3 = .1;
bpath_good[3].y3 = .8;
bpath_good[4].code = NR_LINETO;
bpath_good[4].x3 = .1;
bpath_good[4].y3 = .2;
bpath_good[5].code = NR_END;
{ // Test absolute
char const * path_str = "M .1.2+0.4.2e0.4e0+8e-1.1.8 z";
NArtBpath * bpath = sp_svg_read_path(path_str);
TS_ASSERT(bpathEqual(bpath,bpath_good));
g_free(bpath);
}
{ // Test relative
char const * path_str = "m .1.2+0.3.0e0.0e0+6e-1-.3.0 z";
NArtBpath * bpath = sp_svg_read_path(path_str);
TS_ASSERT(bpathEqual(bpath,bpath_good));
g_free(bpath);
}
}
void testReadErrorMisplacedCharacter() {
char const * path_str;
NArtBpath * bpath;
NArtBpath * bpath_good = rectangleBpath;
bpath_good[0].code = NR_MOVETO;
// Comma in the wrong place (commas may only appear between parameters)
path_str = "M 1,2 4,2 4,8 1,8 z , m 13,15";
bpath = sp_svg_read_path(path_str);
TS_ASSERT(bpathEqual(bpath,bpath_good));
g_free(bpath);
// Comma in the wrong place (commas may only appear between parameters)
path_str = "M 1,2 4,2 4,8 1,8 z m,13,15";
bpath = sp_svg_read_path(path_str);
TS_ASSERT(bpathEqual(bpath,bpath_good));
g_free(bpath);
// Period in the wrong place (no numbers after a 'z')
path_str = "M 1,2 4,2 4,8 1,8 z . m 13,15";
bpath = sp_svg_read_path(path_str);
TS_ASSERT(bpathEqual(bpath,bpath_good));
g_free(bpath);
// Sign in the wrong place (no numbers after a 'z')
path_str = "M 1,2 4,2 4,8 1,8 z + - m 13,15";
bpath = sp_svg_read_path(path_str);
TS_ASSERT(bpathEqual(bpath,bpath_good));
g_free(bpath);
// Digit in the wrong place (no numbers after a 'z')
path_str = "M 1,2 4,2 4,8 1,8 z 9809 m 13,15";
bpath = sp_svg_read_path(path_str);
TS_ASSERT(bpathEqual(bpath,bpath_good));
g_free(bpath);
// Digit in the wrong place (no numbers after a 'z')
path_str = "M 1,2 4,2 4,8 1,8 z 9809 876 m 13,15";
bpath = sp_svg_read_path(path_str);
TS_ASSERT(bpathEqual(bpath,bpath_good));
g_free(bpath);
}
void testReadErrorUnrecognizedCharacter() {
char const * path_str;
NArtBpath * bpath;
NArtBpath * bpath_good = rectangleBpath;
bpath_good[0].code = NR_MOVETO;
// Unrecognized character
path_str = "M 1,2 4,2 4,8 1,8 z&m 13,15";
bpath = sp_svg_read_path(path_str);
TS_ASSERT(bpathEqual(bpath,bpath_good));
g_free(bpath);
// Unrecognized character
path_str = "M 1,2 4,2 4,8 1,8 z m &13,15";
bpath = sp_svg_read_path(path_str);
TS_ASSERT(bpathEqual(bpath,bpath_good));
g_free(bpath);
}
void testReadErrorTypo() {
char const * path_str;
NArtBpath * bpath;
NArtBpath * bpath_good = rectangleBpath;
bpath_good[0].code = NR_MOVETO;
// Typo
path_str = "M 1,2 4,2 4,8 1,8 z j 13,15";
bpath = sp_svg_read_path(path_str);
TS_ASSERT(bpathEqual(bpath,bpath_good));
g_free(bpath);
bpath_good[0].code = NR_MOVETO_OPEN;
// Typo
path_str = "M 1,2 4,2 4,8 1,8 L 1,2 x m 13,15";
bpath = sp_svg_read_path(path_str);
TS_ASSERT(bpathEqual(bpath,bpath_good));
g_free(bpath);
}
void testReadErrorIllformedNumbers() {
char const * path_str;
NArtBpath * bpath;
NArtBpath * bpath_good = rectangleBpath;
bpath_good[0].code = NR_MOVETO;
// Double exponent
path_str = "M 1,2 4,2 4,8 1,8 z m 13e4e5,15";
bpath = sp_svg_read_path(path_str);
TS_ASSERT(bpathEqual(bpath,bpath_good));
g_free(bpath);
// Double sign
path_str = "M 1,2 4,2 4,8 1,8 z m +-13,15";
bpath = sp_svg_read_path(path_str);
TS_ASSERT(bpathEqual(bpath,bpath_good));
g_free(bpath);
// Double sign
path_str = "M 1,2 4,2 4,8 1,8 z m 13e+-12,15";
bpath = sp_svg_read_path(path_str);
TS_ASSERT(bpathEqual(bpath,bpath_good));
g_free(bpath);
// No digit
path_str = "M 1,2 4,2 4,8 1,8 z m .e12,15";
bpath = sp_svg_read_path(path_str);
TS_ASSERT(bpathEqual(bpath,bpath_good));
g_free(bpath);
// No digit
path_str = "M 1,2 4,2 4,8 1,8 z m .,15";
bpath = sp_svg_read_path(path_str);
TS_ASSERT(bpathEqual(bpath,bpath_good));
g_free(bpath);
// No digit
path_str = "M 1,2 4,2 4,8 1,8 z m +,15";
bpath = sp_svg_read_path(path_str);
TS_ASSERT(bpathEqual(bpath,bpath_good));
g_free(bpath);
// No digit
path_str = "M 1,2 4,2 4,8 1,8 z m +.e+,15";
bpath = sp_svg_read_path(path_str);
TS_ASSERT(bpathEqual(bpath,bpath_good));
g_free(bpath);
}
void testReadErrorJunk() {
char const * path_str;
NArtBpath * bpath;
NArtBpath * bpath_good = rectangleBpath;
bpath_good[0].code = NR_MOVETO;
// Junk
path_str = "M 1,2 4,2 4,8 1,8 z j 357 hkjh.,34e34 90ih6kj4 h5k6vlh4N.,6,45wikuyi3yere..3487 m 13,23";
bpath = sp_svg_read_path(path_str);
TS_ASSERT(bpathEqual(bpath,bpath_good));
g_free(bpath);
}
void testReadErrorStopReading() {
char const * path_str;
NArtBpath * bpath;
NArtBpath * bpath_good = rectangleBpath;
bpath_good[0].code = NR_MOVETO;
// Unrecognized parameter
path_str = "M 1,2 4,2 4,8 1,8 z m #$%,23,34";
bpath = sp_svg_read_path(path_str);
TS_ASSERT(bpathEqual(bpath,bpath_good));
g_free(bpath);
// Invalid parameter
path_str = "M 1,2 4,2 4,8 1,8 z m #$%,23,34";
bpath = sp_svg_read_path(path_str);
TS_ASSERT(bpathEqual(bpath,bpath_good));
g_free(bpath);
// Illformed parameter
path_str = "M 1,2 4,2 4,8 1,8 z m +-12,23,34";
bpath = sp_svg_read_path(path_str);
TS_ASSERT(bpathEqual(bpath,bpath_good));
g_free(bpath);
bpath_good[0].code = NR_MOVETO_OPEN;
// "Third" parameter
path_str = "M 1,2 4,2 4,8 1,8 1,2,3 M 12,23";
bpath = sp_svg_read_path(path_str);
TS_ASSERT(bpathEqual(bpath,bpath_good));
g_free(bpath);
}
void testRoundTrip() {
// This is the easiest way to (also) test writing path data, as a path can be written in more than one way.
NArtBpath * bpath;
NArtBpath * new_bpath;
char * path_str;
// Rectangle (closed)
bpath = sp_svg_read_path(rectanglesAbsoluteClosed[0].c_str());
path_str = sp_svg_write_path(bpath);
new_bpath = sp_svg_read_path(path_str);
TS_ASSERT(bpathEqual(bpath,new_bpath));
g_free(bpath); g_free(path_str); g_free(new_bpath);
// Rectangle (open)
bpath = sp_svg_read_path(rectanglesAbsoluteOpen[0].c_str());
path_str = sp_svg_write_path(bpath);
new_bpath = sp_svg_read_path(path_str);
TS_ASSERT(bpathEqual(bpath,new_bpath));
g_free(bpath); g_free(path_str); g_free(new_bpath);
// Concatenated rectangles
bpath = sp_svg_read_path((rectanglesAbsoluteClosed[0] + rectanglesRelativeOpen[0] + rectanglesRelativeClosed[0] + rectanglesAbsoluteOpen[0]).c_str());
path_str = sp_svg_write_path(bpath);
new_bpath = sp_svg_read_path(path_str);
TS_ASSERT(bpathEqual(bpath,new_bpath));
g_free(bpath); g_free(path_str); g_free(new_bpath);
// Zero-length subpaths
bpath = sp_svg_read_path("M 0,0 M 1,1 L 2,2 M 3,3 z M 4,4 L 5,5 z M 6,6");
path_str = sp_svg_write_path(bpath);
new_bpath = sp_svg_read_path(path_str);
TS_ASSERT(bpathEqual(bpath,new_bpath));
g_free(bpath); g_free(path_str); g_free(new_bpath);
// Floating-point
bpath = sp_svg_read_path("M .01,.02 L 0.04,0.02 L.04,.08L0.01,0.08 z""M 1e-2,.2e-1 L 0.004e1,0.0002e+2 L04E-2,.08e0L1.0e-2,80e-3 z");
path_str = sp_svg_write_path(bpath);
new_bpath = sp_svg_read_path(path_str);
TS_ASSERT(bpathEqual(bpath, new_bpath, 1e-17));
g_free(bpath); g_free(path_str); g_free(new_bpath);
}
private:
bool bpathEqual(NArtBpath const * a, NArtBpath const * b, double eps = 1e-16) {
while(a->code != NR_END && b->code == a->code) {
switch(a->code) {
case NR_MOVETO:
case NR_MOVETO_OPEN:
case NR_LINETO:
if (!Geom::are_near(a->x3,b->x3, eps) || !Geom::are_near(a->y3,b->y3, eps)) return false;
break;
case NR_CURVETO:
if (!Geom::are_near(a->x1,b->x1, eps) || !Geom::are_near(a->y1,b->y1, eps)) return false;
if (!Geom::are_near(a->x2,b->x2, eps) || !Geom::are_near(a->y2,b->y2, eps)) return false;
if (!Geom::are_near(a->x3,b->x3, eps) || !Geom::are_near(a->y3,b->y3, eps)) return false;
break;
default:
TS_FAIL("Unknown path code!");
}
a++;
b++;
}
return a->code == b->code;
}
};
/*
Local Variables:
mode:c++
c-file-style:"stroustrup"
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
indent-tabs-mode:nil
fill-column:99
End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :
|