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
|
#!/usr/bin/env python
# coding=utf-8
"""
Test the element API base classes and basic functionality
"""
from lxml import etree
from inkex.elements import (
load_svg, ShapeElement, Group, Rectangle, Tspan, TextElement, Line,
)
from inkex.transforms import Transform
from inkex.styles import Style
from inkex.utils import FragmentError
from inkex.tester import TestCase
from inkex.tester.svg import svg_file
class FakeShape(ShapeElement): # pylint: disable=abstract-method
"""A protend shape"""
tag_name = 'fake'
class SvgTestCase(TestCase):
"""Test SVG"""
source_file = 'complextransform.test.svg'
def setUp(self):
super(SvgTestCase, self).setUp()
self.svg = svg_file(self.data_file('svg', self.source_file))
class OverridenElementTestCase(SvgTestCase):
"""Test element overriding functionality"""
def test_tag_names(self):
"""
Test tag names for custom and unknown tags
"""
doc = load_svg("""
<svg xmlns="http://www.w3.org/2000/svg" xmlns:x="http://x.com/x">
<g id="good"></g>
<badsvg id="bad">Unknown SVG tag</badsvg>
<x:othertag id="ugly"></x:othertag>
</svg>""")
svg = doc.getroot()
good = svg.getElementById("good")
self.assertEqual(good.TAG, "g")
bad = svg.getElementById("bad")
self.assertEqual(bad.TAG, "badsvg")
ugly = svg.getElementById("ugly")
self.assertEqual(ugly.TAG, "othertag")
def test_reference_count(self):
"""
Test inkex.element.BaseElement-derived object type is preserved on adding to group
See https://gitlab.com/inkscape/extensions/-/issues/81 for details
"""
grp = Group()
for _ in range(10):
rect = Rectangle()
grp.add(rect)
for elem in grp:
self.assertEqual(type(elem), Rectangle)
def test_abstract_raises(self):
"""Abstract classes cannot be instantiated"""
self.assertRaises(NotImplementedError, FakeShape().get_path)
self.assertRaises(AttributeError, FakeShape().set_path, None)
def test_add(self):
"""Can add single or multiple elements with passthrough"""
elem = self.svg.getElementById('D')
group = elem.add(Group(id='foo'))
self.assertEqual(group.get('id'), 'foo')
groups = elem.add(Group(id='f1'), Group(id='f2'))
self.assertEqual(len(groups), 2)
self.assertEqual(groups[0].get('id'), 'f1')
self.assertEqual(groups[1].get('id'), 'f2')
def test_creation(self):
"""Create elements with attributes"""
group = Group().update(inkscape__label='Foo')
self.assertEqual(group.get('inkscape:label'), 'Foo')
group = Group().update(inkscape__label='Bar')
self.assertEqual(group.label, 'Bar')
def test_tostring(self):
"""Elements can be printed as strings"""
self.assertEqual(Group().tostring(), b'<g/>')
elem = Group(id='bar')
path = elem.add(Tspan(id='foo'))
elem.transform.add_translate(50, 50)
path.style['fill'] = 'red'
self.assertEqual(elem.tostring(), \
b'<g transform="translate(50, 50)"><tspan id="foo" style="fill:red"/></g>')
class AttributeHandelingTestCase(SvgTestCase):
"""Test how attributes are handled"""
def test_chained_multiple_attrs(self):
"""Set multiple attributes at a time"""
group = Group().update(
attr1='A',
attr2='B'
).update(
attr3='C',
attr4='D'
)
self.assertEqual(group.get('attr1'), 'A')
self.assertEqual(group.get('attr2'), 'B')
self.assertEqual(group.get('attr3'), 'C')
self.assertEqual(group.get('attr4'), 'D')
# remove attributes, setting them to None
group.update(
attr1=None,
attr4=None
)
self.assertEqual(group.get('attr1'), None)
self.assertEqual(group.get('attr2'), 'B')
self.assertEqual(group.get('attr3'), 'C')
self.assertEqual(group.get('attr4'), None)
self.assertEqual(group.pop('attr2'), 'B')
self.assertEqual(group.pop('attr3'), 'C')
def test_set_wrapped_attribute(self):
"""Remove wrapped attribute using .set()"""
group = Group().update(
transform=Transform(scale=2)
)
self.assertEqual(group.transform.matrix[0][0], 2)
self.assertEqual(group.transform.matrix[1][1], 2)
group.update(
transform=None
)
self.assertEqual(group.transform, Transform())
def test_pop_wrapped_attribute(self):
"""Remove wrapped attribute using .pop()"""
group = Group()
self.assertEqual(group.pop('transform'), Transform())
group.update(
transform=Transform(scale=2)
)
self.assertEqual(group.pop('transform'), Transform(scale=2))
self.assertEqual(group.pop('transform'), Transform())
self.assertRaises(AttributeError, getattr, group, 'foo')
def test_pop_regular_attribute(self):
"""Remove wrapped attribute using .pop()"""
group = Group()
self.assertEqual(group.get('attr1'), None)
group.update(
attr1="42"
)
self.assertEqual(group.pop('attr1'), "42")
self.assertEqual(group.pop('attr1'), None)
def test_update_consistant(self):
"""Update doesn't keep callbacks around"""
elem = self.svg.getElementById('D')
tr_a = Transform(translate=(10, 10))
tr_b = Transform(translate=(-20, 15))
elem.transform = tr_a
elem.transform = tr_b
self.assertEqual(str(elem.transform), 'translate(-20, 15)')
tr_a.add_translate(10, 10)
self.assertEqual(str(elem.transform), 'translate(-20, 15)')
elem.set('transform', None)
self.assertEqual(elem.get('transform'), None)
def test_in_place_style(self):
"""Do styles update when we set them"""
elem = self.svg.getElementById('D')
elem.style['fill'] = 'purpleberry'
self.assertEqual(elem.get('style'), 'fill:purpleberry')
elem.style = {'marker': 'flag'}
self.assertEqual(elem.get('style'), 'marker:flag')
elem.style = Style(stroke='gammon')
self.assertEqual(elem.get('style'), 'stroke:gammon')
elem.style.update('grape:2;strawberry:nice;')
self.assertEqual(elem.get('style'), 'stroke:gammon;grape:2;strawberry:nice')
def test_random_id(self):
"""Test setting a random id"""
elem = self.svg.getElementById('D')
elem.set_random_id('Thing')
self.assertEqual(elem.get('id'), 'Thing5815')
elem.set_random_id('Thing', size=2)
self.assertEqual(elem.get('id'), 'Thing85')
elem.set_random_id()
self.assertEqual(elem.get('id'), 'path5392')
# No document root, no random id allowed
self.assertRaises(FragmentError, elem.copy().set_random_id)
def test_random_ids(self):
"""Test setting a tree of ids"""
elem = self.svg.getElementById('D')
self.svg.set_random_ids(prefix='TreeItem')
self.assertEqual(self.svg.get('id'), 'TreeItem5815')
self.assertEqual(self.svg[0].get('id'), 'TreeItem8555')
self.assertEqual(elem.get('id'), 'TreeItem2036')
def test_set_id_backlinks(self):
"""Changing an id can update backlinks"""
elem = self.svg.getElementById('path1')
elem.set_id('plant54', True)
self.assertEqual(self.svg.getElementById('G').get('xlink:href'), '#plant54')
self.assertEqual(self.svg.getElementById('G').href, elem)
self.assertEqual(str(self.svg.getElementById('B').style), 'fill:#eee;joker:url(#plant54)')
def test_get_element_by_name(self):
"""Get elements by name"""
self.assertEqual(self.svg.getElementByName('Key').get('id'), 'K')
self.assertEqual(self.svg.getElementByName('Elm', 'svg:g').get('id'), 'L')
self.assertEqual(self.svg.getElementByName('Mine').get('id'), 'M')
self.assertEqual(self.svg.getElementByName('doesntexist'), None)
self.assertEqual(self.svg.getElementByName('Key', 'rect'), None)
class TransformationTestCase(SvgTestCase):
"""Test transformative functions"""
def test_bounding_box(self):
"""Elements can have bounding boxes"""
elem = self.svg.getElementById('D')
self.assertEqual(tuple(elem.bounding_box()), ((60.0, 100.0), (130.0, 170.00)))
self.assertTrue(elem.bounding_box().center.is_close((80.0, 150.0)))
self.assertEqual(tuple(TextElement(x='10', y='5').bounding_box()), ((10, 10), (5, 5)))
group = Group(elem)
self.assertEqual(elem.bounding_box(), group.bounding_box())
def test_transform(self):
"""In-place modified transforms are retained"""
elem = self.svg.getElementById('D')
self.assertEqual(str(elem.transform), 'translate(30, 10)')
elem.transform.add_translate(-10, 10)
self.assertEqual(str(elem.transform), 'translate(20, 20)')
def test_scale(self):
"""In-place scaling from blank transform"""
elem = self.svg.getElementById('F')
self.assertEqual(elem.transform, Transform())
self.assertEqual(elem.get('transform'), None)
elem.transform.add_scale(1.0666666666666667, 1.0666666666666667)
self.assertEqual(elem.get('transform'), Transform(scale=1.06667))
self.assertIn(b'transform', etree.tostring(elem))
def test_in_place_transforms(self):
"""Do style and transforms update correctly"""
elem = self.svg.getElementById('D')
self.assertEqual(type(elem.transform), Transform)
self.assertEqual(type(elem.style), Style)
self.assertTrue(elem.transform)
elem.transform = Transform()
self.assertEqual(elem.transform, Transform())
self.assertEqual(elem.get('transform'), None)
self.assertNotIn(b'transform', etree.tostring(elem))
elem.transform.add_translate(10, 10)
self.assertIn(b'transform', etree.tostring(elem))
elem.transform.add_translate(-10, -10)
self.assertNotIn(b'transform', etree.tostring(elem))
class RelationshipTestCase(SvgTestCase):
"""Test relationships between elements"""
def test_findall(self):
"""Findall elements in svg"""
groups = self.svg.findall('svg:g')
self.assertEqual(len(groups), 1)
def test_copy(self):
"""Test copying elements"""
elem = self.svg.getElementById('D')
cpy = elem.copy()
self.assertFalse(cpy.getparent())
self.assertFalse(cpy.get('id'))
def test_duplicate(self):
"""Test duplicating elements"""
elem = self.svg.getElementById('D')
dup = elem.duplicate()
self.assertTrue(dup.get('id'))
self.assertNotEqual(elem.get('id'), dup.get('id'))
self.assertEqual(elem.getparent(), dup.getparent())
def test_replace_with(self):
"""Replacing nodes in a tree"""
rect = self.svg.getElementById('E')
path = rect.to_path_element()
rect.replace_with(path)
self.assertEqual(rect.getparent(), None)
self.assertEqual(path.getparent(), self.svg.getElementById('C'))
def test_descendants(self):
"""Elements can walk their descendants"""
self.assertEqual(tuple(self.svg.descendants().ids), (
'mydoc', 'path1', 'base', 'metadata7',
'A', 'B', 'C', 'D', 'E', 'F', 'G',
'H', 'I', 'J', 'K', 'L', 'M',
))
get = self.svg.getElementById
self.assertEqual(tuple(get('L').descendants().ids), ('L', 'M'))
self.assertEqual(tuple(get('M').descendants().ids), ('M',))
def test_ancestors(self):
"""Element descendants of elements"""
get = self.svg.getElementById
self.assertEqual(tuple(get('M').ancestors().ids), ('L', 'K', 'A', 'mydoc'))
self.assertEqual(tuple(get('M').ancestors(stop_at=[None]).ids), ('L', 'K', 'A', 'mydoc'))
self.assertEqual(tuple(get('M').ancestors(stop_at=[get('K')]).ids), ('L', 'K'))
self.assertEqual(tuple(get('M').ancestors(stop_at=[get('L')]).ids), ('L',))
def test_luca(self):
"""Test last common ancestor"""
get = self.svg.getElementById
self.assertEqual(tuple(get('M').ancestors(get('M')).ids), ('L',))
self.assertEqual(tuple(get('G').ancestors(get('H')).ids), ('C',))
self.assertEqual(tuple(get('M').ancestors(get('H')).ids), ('L', 'K', 'A'))
|