aboutsummaryrefslogtreecommitdiff
path: root/Tests/svgLib/path/parser_test.py
blob: d76e99524a76f490581e991fd8169b5caee39fc9 (plain)
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

from fontTools.misc.py23 import *
from fontTools.pens.recordingPen import RecordingPen
from fontTools.svgLib import parse_path

import pytest


@pytest.mark.parametrize(
    "pathdef, expected",
    [

        # Examples from the SVG spec

        (
            "M 100 100 L 300 100 L 200 300 z",
            [
                ("moveTo", ((100.0, 100.0),)),
                ("lineTo", ((300.0, 100.0),)),
                ("lineTo", ((200.0, 300.0),)),
                ("lineTo", ((100.0, 100.0),)),
                ("closePath", ()),
            ]
        ),
        # for Z command behavior when there is multiple subpaths
        (
            "M 0 0 L 50 20 M 100 100 L 300 100 L 200 300 z",
            [
                ("moveTo", ((0.0, 0.0),)),
                ("lineTo", ((50.0, 20.0),)),
                ("endPath", ()),
                ("moveTo", ((100.0, 100.0),)),
                ("lineTo", ((300.0, 100.0),)),
                ("lineTo", ((200.0, 300.0),)),
                ("lineTo", ((100.0, 100.0),)),
                ("closePath", ()),
            ]
        ),
        (
            "M100,200 C100,100 250,100 250,200 S400,300 400,200",
            [
                ("moveTo", ((100.0, 200.0),)),
                ("curveTo", ((100.0, 100.0),
                             (250.0, 100.0),
                             (250.0, 200.0))),
                ("curveTo", ((250.0, 300.0),
                             (400.0, 300.0),
                             (400.0, 200.0))),
                ("endPath", ()),
            ]
        ),
        (
            "M100,200 C100,100 400,100 400,200",
            [
                ("moveTo", ((100.0, 200.0),)),
                ("curveTo", ((100.0, 100.0),
                             (400.0, 100.0),
                             (400.0, 200.0))),
                ("endPath", ()),
            ]
        ),
        (
            "M100,500 C25,400 475,400 400,500",
            [
                ("moveTo", ((100.0, 500.0),)),
                ("curveTo", ((25.0, 400.0),
                             (475.0, 400.0),
                             (400.0, 500.0))),
                ("endPath", ()),
            ]
        ),
        (
            "M100,800 C175,700 325,700 400,800",
            [
                ("moveTo", ((100.0, 800.0),)),
                ("curveTo", ((175.0, 700.0),
                             (325.0, 700.0),
                             (400.0, 800.0))),
                ("endPath", ()),
            ]
        ),
        (
            "M600,200 C675,100 975,100 900,200",
            [
                ("moveTo", ((600.0, 200.0),)),
                ("curveTo", ((675.0, 100.0),
                             (975.0, 100.0),
                             (900.0, 200.0))),
                ("endPath", ()),
            ]
        ),
        (
            "M600,500 C600,350 900,650 900,500",
            [
                ("moveTo", ((600.0, 500.0),)),
                ("curveTo", ((600.0, 350.0),
                             (900.0, 650.0),
                             (900.0, 500.0))),
                ("endPath", ()),
            ]
        ),
        (
            "M600,800 C625,700 725,700 750,800 S875,900 900,800",
            [
                ("moveTo", ((600.0, 800.0),)),
                ("curveTo", ((625.0, 700.0),
                             (725.0, 700.0),
                             (750.0, 800.0))),
                ("curveTo", ((775.0, 900.0),
                             (875.0, 900.0),
                             (900.0, 800.0))),
                ("endPath", ()),
            ]
        ),
        (
            "M200,300 Q400,50 600,300 T1000,300",
            [
                ("moveTo", ((200.0, 300.0),)),
                ("qCurveTo", ((400.0, 50.0),
                              (600.0, 300.0))),
                ("qCurveTo", ((800.0, 550.0),
                              (1000.0, 300.0))),
                ("endPath", ()),
            ]
        ),
        # End examples from SVG spec

        # Relative moveto
        (
            "M 0 0 L 50 20 m 50 80 L 300 100 L 200 300 z",
            [
                ("moveTo", ((0.0, 0.0),)),
                ("lineTo", ((50.0, 20.0),)),
                ("endPath", ()),
                ("moveTo", ((100.0, 100.0),)),
                ("lineTo", ((300.0, 100.0),)),
                ("lineTo", ((200.0, 300.0),)),
                ("lineTo", ((100.0, 100.0),)),
                ("closePath", ()),
            ]
        ),
        # Initial smooth and relative curveTo
        (
            "M100,200 s 150,-100 150,0",
            [
                ("moveTo", ((100.0, 200.0),)),
                ("curveTo", ((100.0, 200.0),
                             (250.0, 100.0),
                             (250.0, 200.0))),
                ("endPath", ()),
            ]
        ),
        # Initial smooth and relative qCurveTo
        (
            "M100,200 t 150,0",
            [
                ("moveTo", ((100.0, 200.0),)),
                ("qCurveTo", ((100.0, 200.0),
                              (250.0, 200.0))),
                ("endPath", ()),
            ]
        ),
        # relative l command
        (
            "M 100 100 L 300 100 l -100 200 z",
            [
                ("moveTo", ((100.0, 100.0),)),
                ("lineTo", ((300.0, 100.0),)),
                ("lineTo", ((200.0, 300.0),)),
                ("lineTo", ((100.0, 100.0),)),
                ("closePath", ()),
            ]
        ),
        # relative q command
        (
            "M200,300 q200,-250 400,0",
            [
                ("moveTo", ((200.0, 300.0),)),
                ("qCurveTo", ((400.0, 50.0),
                              (600.0, 300.0))),
                ("endPath", ()),
            ]
        ),
        # absolute H command
        (
            "M 100 100 H 300 L 200 300 z",
            [
                ("moveTo", ((100.0, 100.0),)),
                ("lineTo", ((300.0, 100.0),)),
                ("lineTo", ((200.0, 300.0),)),
                ("lineTo", ((100.0, 100.0),)),
                ("closePath", ()),
            ]
        ),
        # relative h command
        (
            "M 100 100 h 200 L 200 300 z",
            [
                ("moveTo", ((100.0, 100.0),)),
                ("lineTo", ((300.0, 100.0),)),
                ("lineTo", ((200.0, 300.0),)),
                ("lineTo", ((100.0, 100.0),)),
                ("closePath", ()),
            ]
        ),
        # absolute V command
        (
            "M 100 100 V 300 L 200 300 z",
            [
                ("moveTo", ((100.0, 100.0),)),
                ("lineTo", ((100.0, 300.0),)),
                ("lineTo", ((200.0, 300.0),)),
                ("lineTo", ((100.0, 100.0),)),
                ("closePath", ()),
            ]
        ),
        # relative v command
        (
            "M 100 100 v 200 L 200 300 z",
            [
                ("moveTo", ((100.0, 100.0),)),
                ("lineTo", ((100.0, 300.0),)),
                ("lineTo", ((200.0, 300.0),)),
                ("lineTo", ((100.0, 100.0),)),
                ("closePath", ()),
            ]
        ),
    ]
)
def test_parse_path(pathdef, expected):
    pen = RecordingPen()
    parse_path(pathdef, pen)

    assert pen.value == expected


@pytest.mark.parametrize(
    "pathdef1, pathdef2",
    [
        # don't need spaces between numbers and commands
        (
            "M 100 100 L 200 200",
            "M100 100L200 200",
        ),
        # repeated implicit command
        (
            "M 100 200 L 200 100 L -100 -200",
            "M 100 200 L 200 100 -100 -200"
        ),
        # don't need spaces before a minus-sign
        (
            "M100,200c10-5,20-10,30-20",
            "M 100 200 c 10 -5 20 -10 30 -20"
        ),
        # closed paths have an implicit lineTo if they don't
        # end on the same point as the initial moveTo
        (
            "M 100 100 L 300 100 L 200 300 z",
            "M 100 100 L 300 100 L 200 300 L 100 100 z"
        )
    ]
)
def test_equivalent_paths(pathdef1, pathdef2):
    pen1 = RecordingPen()
    parse_path(pathdef1, pen1)

    pen2 = RecordingPen()
    parse_path(pathdef2, pen2)

    assert pen1.value == pen2.value


def test_exponents():
    # It can be e or E, the plus is optional, and a minimum of +/-3.4e38 must be supported.
    pen = RecordingPen()
    parse_path("M-3.4e38 3.4E+38L-3.4E-38,3.4e-38", pen)
    expected = [
        ("moveTo", ((-3.4e+38, 3.4e+38),)),
        ("lineTo", ((-3.4e-38, 3.4e-38),)),
        ("endPath", ()),
    ]

    assert pen.value == expected


def test_invalid_implicit_command():
    with pytest.raises(ValueError) as exc_info:
        parse_path("M 100 100 L 200 200 Z 100 200", RecordingPen())
    assert exc_info.match("Unallowed implicit command")


def test_arc_to_cubic_bezier():
    pen = RecordingPen()
    parse_path("M300,200 h-150 a150,150 0 1,0 150,-150 z", pen)
    expected = [
        ('moveTo', ((300.0, 200.0),)),
        ('lineTo', ((150.0, 200.0),)),
        (
            'curveTo',
            (
                (150.0, 282.842),
                (217.157, 350.0),
                (300.0, 350.0)
            )
        ),
        (
            'curveTo',
            (
                (382.842, 350.0),
                (450.0, 282.842),
                (450.0, 200.0)
            )
        ),
        (
            'curveTo',
            (
                (450.0, 117.157),
                (382.842, 50.0),
                (300.0, 50.0)
            )
        ),
        ('lineTo', ((300.0, 200.0),)),
        ('closePath', ())
    ]

    result = list(pen.value)
    assert len(result) == len(expected)
    for (cmd1, points1), (cmd2, points2) in zip(result, expected):
        assert cmd1 == cmd2
        assert len(points1) == len(points2)
        for pt1, pt2 in zip(points1, points2):
            assert pt1 == pytest.approx(pt2, rel=1e-5)



class ArcRecordingPen(RecordingPen):

    def arcTo(self, rx, ry, rotation, arc_large, arc_sweep, end_point):
        self.value.append(
            ("arcTo", (rx, ry, rotation, arc_large, arc_sweep, end_point))
        )


def test_arc_pen_with_arcTo():
    pen = ArcRecordingPen()
    parse_path("M300,200 h-150 a150,150 0 1,0 150,-150 z", pen)
    expected = [
        ('moveTo', ((300.0, 200.0),)),
        ('lineTo', ((150.0, 200.0),)),
        ('arcTo', (150.0, 150.0, 0.0, True, False, (300.0, 50.0))),
        ('lineTo', ((300.0, 200.0),)),
        ('closePath', ())
    ]

    assert pen.value == expected


@pytest.mark.parametrize(
    "path, expected",
    [
        (
            "M1-2A3-4-1.0 01.5.7",
            [
                ("moveTo", ((1.0, -2.0),)),
                ("arcTo", (3.0, -4.0, -1.0, False, True, (0.5, 0.7))),
                ("endPath", ()),
            ],
        ),
        (
            "M21.58 7.19a2.51 2.51 0 10-1.77-1.77",
            [
                ("moveTo", ((21.58, 7.19),)),
                ("arcTo", (2.51, 2.51, 0.0, True, False, (19.81, 5.42))),
                ("endPath", ()),
            ],
        ),
        (
            "M22 12a25.87 25.87 0 00-.42-4.81",
            [
                ("moveTo", ((22.0, 12.0),)),
                ("arcTo", (25.87, 25.87, 0.0, False, False, (21.58, 7.19))),
                ("endPath", ()),
            ],
        ),
        (
            "M0,0 A1.2 1.2 0 012 15.8",
            [
                ("moveTo", ((0.0, 0.0),)),
                ("arcTo", (1.2, 1.2, 0.0, False, True, (2.0, 15.8))),
                ("endPath", ()),
            ],
        ),
        (
            "M12 7a5 5 0 105 5 5 5 0 00-5-5",
            [

                ("moveTo", ((12.0, 7.0),)),
                ("arcTo", (5.0, 5.0, 0.0, True, False, (17.0, 12.0))),
                ("arcTo", (5.0, 5.0, 0.0, False, False, (12.0, 7.0))),
                ("endPath", ()),
            ],
        )
    ],
)
def test_arc_flags_without_spaces(path, expected):
    pen = ArcRecordingPen()
    parse_path(path, pen)
    assert pen.value == expected


@pytest.mark.parametrize(
    "path", ["A", "A0,0,0,0,0,0", "A 0 0 0 0 0 0 0 0 0 0 0 0 0"]
)
def test_invalid_arc_not_enough_args(path):
    pen = ArcRecordingPen()
    with pytest.raises(ValueError, match="Invalid arc command") as e:
        parse_path(path, pen)

    assert isinstance(e.value.__cause__, ValueError)
    assert "Not enough arguments" in str(e.value.__cause__)


def test_invalid_arc_argument_value():
    pen = ArcRecordingPen()
    with pytest.raises(ValueError, match="Invalid arc command") as e:
        parse_path("M0,0 A0,0,0,2,0,0,0", pen)

    cause = e.value.__cause__
    assert isinstance(cause, ValueError)
    assert "Invalid argument for 'large-arc-flag' parameter: '2'" in str(cause)

    pen = ArcRecordingPen()
    with pytest.raises(ValueError, match="Invalid arc command") as e:
        parse_path("M0,0 A0,0,0,0,-2.0,0,0", pen)

    cause = e.value.__cause__
    assert isinstance(cause, ValueError)
    assert "Invalid argument for 'sweep-flag' parameter: '-2.0'" in str(cause)