File: test_path.py

package info (click to toggle)
python-svgelements 1.7.2-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 764 kB
  • sloc: python: 11,859; sh: 6; makefile: 3
file content (291 lines) | stat: -rw-r--r-- 11,467 bytes parent folder | download
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
import unittest

from svgelements import *


class TestPath(unittest.TestCase):
    """Tests of the SVG Path element."""

    def test_subpaths(self):
        path = Path("M0,0 50,50 100,100Z M0,100 50,50, 100,0")
        for i, p in enumerate(path.as_subpaths()):
            if i == 0:
                self.assertEqual(p.d(), "M 0,0 L 50,50 L 100,100 Z")
            elif i == 1:
                self.assertEqual(p.d(), "M 0,100 L 50,50 L 100,0")
            self.assertLessEqual(i, 1)

    def test_subpath_degenerate(self):
        path = Path("")
        for i, p in enumerate(path.as_subpaths()):
            pass

    def test_subpaths_no_move(self):
        path = Path("M0,0 50,0 50,50 0,50 Z L0,100 100,100 100,0")
        for i, p in enumerate(path.as_subpaths()):
            if i == 0:
                self.assertEqual(p.d(), "M 0,0 L 50,0 L 50,50 L 0,50 Z")
            elif i == 1:
                self.assertEqual(p.d(), "L 0,100 L 100,100 L 100,0")
            self.assertLessEqual(i, 1)
        path = Path("M0,0ZZZZZ")
        subpaths = list(path.as_subpaths())
        self.assertEqual(len(subpaths), 5)

    def test_count_subpaths(self):
        path = Path("M0,0 50,50 100,100Z M0,100 50,50, 100,0")
        self.assertEqual(path.count_subpaths(), 2)

    def test_subpath(self):
        path = Path("M0,0 50,50 100,100Z M0,100 50,50, 100,0")
        subpath = path.subpath(0)
        self.assertEqual(subpath.d(), "M 0,0 L 50,50 L 100,100 Z")
        subpath = path.subpath(1)
        self.assertEqual(subpath.d(), "M 0,100 L 50,50 L 100,0")

    def test_move_quad_smooth(self):
        path = Path()
        path.move((4, 4), (20, 20), (25, 25), 6 + 3j)
        path.quad((20, 33), (100, 100))
        path.smooth_quad((13, 45), (16, 16), (34, 56), "z").closed()
        self.assertEqual(path.d(), "M 4,4 L 20,20 L 25,25 L 6,3 Q 20,33 100,100 T 13,45 T 16,16 T 34,56 T 4,4 Z")

    def test_move_cubic_smooth(self):
        path = Path()
        path.move((4, 4), (20, 20), (25, 25), 6 + 3j)
        path.cubic((20, 33), (25, 25), (100, 100))
        path.smooth_cubic((13, 45), (16, 16), (34, 56), "z").closed()
        self.assertEqual(path.d(), "M 4,4 L 20,20 L 25,25 L 6,3 C 20,33 25,25 100,100 S 13,45 16,16 S 34,56 4,4 Z")

    def test_convex_hull(self):
        pts = (3, 4), (4, 6), (18, -2), (9, 0)
        hull = [e for e in Point.convex_hull(pts)]
        self.assertEqual([(3, 4), (9, 0), (18, -2), (4, 6)], hull)

        # bounding box and a bunch of random numbers that must be inside.
        pts = [(100, 100), (100, -100), (-100, -100), (-100, 100)]
        from random import randint
        for i in range(50):
            pts.append((randint(-99, 99), randint(-99, 99)))
        hull = [e for e in Point.convex_hull(pts)]
        for p in hull:
            self.assertEqual(abs(p[0]), 100)
            self.assertEqual(abs(p[1]), 100)

    def test_reverse_path_q(self):
        path = Path("M1,0 22,7 Q 17,17 91,2")
        path.reverse()
        self.assertEqual(path, Path("M 91,2 Q 17,17 22,7 L 1,0"))

    def test_reverse_path_multi_move(self):
        path = Path("M1,0 M2,0 M3,0")
        path.reverse()
        self.assertEqual(path, "M3,0 M2,0 M1,0")
        path = Path("M1,0z M2,0z M3,0z")
        path.reverse()
        self.assertEqual(path, "M3,0 Z M2,0 Z M1,0 Z")

    def test_reverse_path_multipath(self):
        path = Path("M1,0 22,7 Q 17,17 91,2M0,0zM20,20z")
        path.reverse()
        self.assertEqual(path, Path("M20,20zM0,0zM 91,2 Q 17,17 22,7 L 1,0"))

    def test_path_mult_sideeffect(self):
        path = Path("M1,1 10,10 Q 17,17 91,2 T 9,9 C 40,40 20,0, 9,9 S 60,50 0,0 A 25,25 -30 0,1 30,30 z")
        q = path * "scale(2)"
        self.assertEqual(path, "M1,1 10,10 Q 17,17 91,2 T 9,9 C 40,40 20,0, 9,9 S 60,50 0,0 A 25,25 -30 0,1 30,30 z")

    def test_subpath_imult_sideeffect(self):
        path = Path("M1,1 10,10 Q 17,17 91,2 T 9,9 C 40,40 20,0, 9,9 S 60,50 0,0 A 25,25 -30 0,1 30,30 zM50,50z")
        self.assertEqual(
            path,
            "M1,1 10,10 Q 17,17 91,2 T 9,9 C 40,40 20,0, 9,9 S 60,50 0,0 A 25,25 -30 0,1 30,30 zM50,50z")
        for p in path.as_subpaths():
            p *= "scale(2)"
        self.assertEqual(
            path,
            "M 2,2 L 20,20 Q 34,34 182,4 T 18,18 C 80,80 40,0 18,18 S 120,100 0,0 A 50,50 -30 0,1 60,60 ZM100,100z")

    def test_subpath_reverse(self):
        #Issue 45
        p = Path("M0,0 1,1")
        p.reverse()
        self.assertEqual(p, "M1,1 0,0")

        p = Path("M0,0 M1,1")
        p.reverse()
        self.assertEqual(p, "M1,1 M0,0")

        p = Path("M1,1 L5,5M2,1 L6,5M3,1 L7,5")
        subpaths = list(p.as_subpaths())
        subpaths[1].reverse()
        self.assertEqual("M 1,1 L 5,5 M 6,5 L 2,1 M 3,1 L 7,5", str(p))
        subpaths[1].reverse()
        self.assertEqual("M 1,1 L 5,5 M 2,1 L 6,5 M 3,1 L 7,5", str(p))

        p = Path("M1,1 L5,5M2,1 L6,5ZM3,1 L7,5")
        subpaths = list(p.as_subpaths())
        subpaths[1].reverse()
        self.assertEqual("M 6,5 L 2,1 Z", str(subpaths[1]))
        self.assertEqual("M 1,1 L 5,5 M 6,5 L 2,1 Z M 3,1 L 7,5", str(p))

        p = Path("M1,1 L5,5M2,1 6,5 100,100 200,200 ZM3,1 L7,5")
        subpaths = list(p.as_subpaths())
        subpaths[1].reverse()
        self.assertEqual("M 1,1 L 5,5 M 200,200 L 100,100 L 6,5 L 2,1 Z M 3,1 L 7,5", str(p))

    def test_validation_delete(self):
        p = Path("M1,1 M2,2 M3,3 M4,4")
        del p[2]
        self.assertEqual(p, "M1,1 M2,2 M4,4")
        p = Path("M0,0 L 1,1 L 2,2 L 3,3 L 4,4z")
        del p[3]
        self.assertEqual(p, "M0,0 L 1,1 L 2,2 L 4,4z")
        p = Path("M0,0 L 1,1 L 2,2 M 3,3 L 4,4z")
        del p[3]
        self.assertEqual(p, "M0,0 L 1,1 L 2,2 L 4,4z")

    def test_validation_insert(self):
        p = Path("M1,1 M2,2 M4,4")
        p.insert(2, "M3,3")
        self.assertEqual(p, "M1,1 M2,2 M3,3 M4,4")
        p = Path("M0,0 L 1,1 L 2,2 L 4,4")
        p.insert(3, "L3,3")
        self.assertEqual(p, "M0,0 L 1,1 L 2,2 L 3,3 L 4,4")

    def test_validation_append(self):
        p = Path("M1,1 M2,2 M3,3")
        p.append("M4,4")
        self.assertEqual(p, "M1,1 M2,2 M3,3 M4,4")
        p = Path("M0,0 L 1,1 L 2,2 L 3,3")
        p.append("L4,4")
        self.assertEqual(p, "M0,0 L 1,1 L 2,2 L 3,3 L 4,4")
        p.append("Z")
        self.assertEqual(p, "M0,0 L 1,1 L 2,2 L 3,3 L 4,4 Z")

        p = Path("M1,1 M2,2")
        p.append("M3,3 M4,4")
        self.assertEqual(p, "M1,1 M2,2 M3,3 M4,4")
        p = Path("M0,0 L 1,1 L 2,2")
        p.append("L 3,3 L4,4")
        self.assertEqual(p, "M0,0 L 1,1 L 2,2 L 3,3 L 4,4")
        p.append("Z")
        self.assertEqual(p, "M0,0 L 1,1 L 2,2 L 3,3 L 4,4 Z")

    def test_validation_extend(self):
        p = Path("M1,1 M2,2")
        p.extend(Path("M3,3 M4,4"))
        self.assertEqual(p, "M1,1 M2,2 M3,3 M4,4")
        p = Path("M0,0 L 1,1 L 2,2")
        p.extend(Path("L 3,3 L4,4"))
        self.assertEqual(p, "M0,0 L 1,1 L 2,2 L 3,3 L 4,4")
        p.extend(Path("Z"))
        self.assertEqual(p, "M0,0 L 1,1 L 2,2 L 3,3 L 4,4 Z")

        p = Path("M1,1 M2,2")
        p.extend("M3,3 M4,4")
        self.assertEqual(p, "M1,1 M2,2 M3,3 M4,4")
        p = Path("M0,0 L 1,1 L 2,2")
        p.extend("L 3,3 L4,4")
        self.assertEqual(p, "M0,0 L 1,1 L 2,2 L 3,3 L 4,4")
        p.extend("Z")
        self.assertEqual(p, "M0,0 L 1,1 L 2,2 L 3,3 L 4,4 Z")

    def test_validation_setitem(self):
        p = Path("M1,1 M2,2 M3,3 M4,4")
        p[2] = Line(None, (3,3))
        self.assertEqual(p, "M1,1 M2,2 L3,3 M4,4")
        p = Path("M0,0 L 1,1 L 2,2 L 3,3 L 4,4z")
        p[3] = Move(None, (3,3))
        self.assertEqual(p, "M0,0 L 1,1 L 2,2 M3,3 L 4,4z")

    def test_validation_setitem_str(self):
        p = Path("M1,1 M2,2 M3,3 M4,4")
        p[2] = "L3,3"
        self.assertEqual(p, Path("M1,1 M2,2 L3,3 M4,4"))
        p = Path("M0,0 L 1,1 L 2,2 L 3,3 L 4,4z")
        p[3] = "M3,3"
        self.assertEqual(p, Path("M0,0 L 1,1 L 2,2 M3,3 L 4,4z"))

    def test_arc_start_t(self):
        m = Path("m 0,0 a 5.01,5.01 180 0,0 0,10 z"
                 "m 0,0 a 65,65 180 0,0 65,66 z")
        for a in m:
            if isinstance(a, Arc):
                start_t = a.get_start_t()
                a_start = a.point_at_t(start_t)
                self.assertEqual(a.start, a_start)
                self.assertEqual(a.end, a.point_at_t(a.get_end_t()))

    def test_relative_roundabout(self):
        m = Path("m 0,0 a 5.01,5.01 180 0,0 0,10 z")
        self.assertEqual(m.d(), "m 0,0 a 5.01,5.01 180 0,0 0,10 z")
        m = Path("M0,0 1,1 z")
        self.assertEqual(m.d(), "M 0,0 L 1,1 z")
        self.assertEqual(m.d(relative=True), "m 0,0 l 1,1 z")
        self.assertEqual(m.d(relative=False), "M 0,0 L 1,1 Z")
        m = Path("M 4,4 L 20,20 L 25,25 L 6,3 Q 20,33 100,100 Q 180,167 13,45 T 16,16 T 34,56 T 4,4 z")
        self.assertEqual(m.d(), "M 4,4 L 20,20 L 25,25 L 6,3 Q 20,33 100,100 Q 180,167 13,45 T 16,16 T 34,56 T 4,4 z")
        self.assertEqual(m.d(smooth=False), "M 4,4 L 20,20 L 25,25 L 6,3 Q 20,33 100,100 Q 180,167 13,45 Q -154,-77 16,16 Q 186,109 34,56 Q -118,3 4,4 z")
        self.assertEqual(m.d(smooth=True), "M 4,4 L 20,20 L 25,25 L 6,3 Q 20,33 100,100 T 13,45 T 16,16 T 34,56 T 4,4 z")
        self.assertEqual(m.d(), "M 4,4 L 20,20 L 25,25 L 6,3 Q 20,33 100,100 Q 180,167 13,45 T 16,16 T 34,56 T 4,4 z")

    def test_path_z_termination(self):
        m = Path("M 4,4 L 20,20 L 25,25 L 6,3 Q 20,33 Z")
        self.assertEqual(m.d(), "M 4,4 L 20,20 L 25,25 L 6,3 Q 20,33 4,4 Z")
        m = Path("M 4,4 L 20,20 L 25,25 L 6,3 Q 20,33 100,100 T Z")
        self.assertEqual(m.d(), "M 4,4 L 20,20 L 25,25 L 6,3 Q 20,33 100,100 T 4,4 Z")
        m = Path("M 4,4 L 20,20 L 25,25 L 6,3 Q 20,33 100,100 T z")
        self.assertEqual(m.d(), "M 4,4 L 20,20 L 25,25 L 6,3 Q 20,33 100,100 T 4,4 z")
        m = Path("m 0,0 1,1 A 5.01,5.01 180 0,0 z")
        self.assertEqual(m.d(), "m 0,0 l 1,1 A 5.01,5.01 180 0,0 0,0 z")
        m = Path("m0,0z")
        self.assertEqual(m.d(), "m 0,0 z")
        m = Path("M0,0Lz")
        self.assertEqual(m.d(), "M 0,0 L 0,0 z")

    def test_path_setitem_slice(self):
        m = Path("M0,0 1,1 z")
        m[1:] = 'L2,2z'
        self.assertEqual(m.d(), "M 0,0 L 2,2 z")
        self.assertTrue(m._is_valid())
        del m[1]
        self.assertEqual(m.d(), "M 0,0 z")
        self.assertTrue(m._is_valid())

        m = Path("M0,0z")
        m[:] = 'M1,1z'
        self.assertEqual(m.d(), "M 1,1 z")
        self.assertTrue(m._is_valid())

        m = Path("M0,0z")
        del m[:]
        self.assertEqual(m, '')
        self.assertTrue(m._is_valid())

        m = Path("M0,0z")
        m[0] = "M1,1"
        self.assertEqual(m.d(), "M 1,1 z")
        self.assertTrue(m._is_valid())
        m[1] = "z"
        self.assertTrue(m._is_valid())

        m = Path("M0,0z")
        del m[1]
        self.assertEqual(m.d(), "M 0,0")
        self.assertTrue(m._is_valid())

        m = Path("M0,0 1,1 z")
        m[3:] = "M5,5z"
        self.assertEqual(m.d(), "M 0,0 L 1,1 z M 5,5 z")
        self.assertTrue(m._is_valid())

        m = Path("M0,0 1,1 z")
        m[-1:] = "M5,5z"
        self.assertEqual(m.d(), "M 0,0 L 1,1 M 5,5 z")
        self.assertTrue(m._is_valid())

        m = Path("M0,0 1,1 z")
        def m_assign():
            m[-1] = 'M5,5z'
        self.assertRaises(ValueError, m_assign)