File: test_filters.py

package info (click to toggle)
svgwrite 1.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 2,092 kB
  • sloc: python: 12,603; makefile: 117; sh: 5
file content (414 lines) | stat: -rw-r--r-- 17,649 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/env python
#coding:utf-8
# Author:  mozman --<mozman@gmx.at>
# Purpose: test filters module
# Created: 04.11.2010
# Copyright (C) 2010, Manfred Moitzi
# License: MIT License

import unittest

from svgwrite import filters

class TestFilter(unittest.TestCase):
    def test_constructor(self):
        f = filters.Filter(profile='full', debug=True)
        self.assertEqual(f.tostring(), '<filter />')

    def test_constructor_params(self):
        f = filters.Filter(start=(10,20), size=(30,40), inherit='#test', profile='full', debug=True)
        self.assertEqual(f.tostring(), '<filter height="40" width="30" x="10" xlink:href="#test" y="20" />')

    def test_filterRes(self):
        f = filters.Filter(resolution=300, profile='full', debug=True)
        self.assertEqual(f.tostring(), '<filter filterRes="300" />')
        f = filters.Filter(resolution=(300, 400), profile='full', debug=True)
        self.assertEqual(f.tostring(), '<filter filterRes="300 400" />')
        f = filters.Filter(resolution="300 400", profile='full', debug=True)
        self.assertEqual(f.tostring(), '<filter filterRes="300 400" />')


class MockFRI(filters._FilterRequireInput):
    elementname = 'feBlend'


class TestFilterRequireInput(unittest.TestCase):
    def test_constructor(self):
        f = MockFRI('input')
        self.assertEqual(f.tostring(), '<feBlend in="input" />')

    def test_constructor_with_all_params(self):
        f = MockFRI('input', start=(10, 20), size=(30, 40), result='output')
        self.assertEqual(f.tostring(), '<feBlend height="40" in="input" result="output" width="30" x="10" y="20" />')

class Test_feBlend(unittest.TestCase):
    def test_constructor(self):
        f = filters.Filter()
        f.feBlend('input1', in2='input2', mode='normal')
        self.assertEqual(f.tostring(), '<filter><feBlend in="input1" in2="input2" mode="normal" /></filter>')

class Test_feColorMatrix(unittest.TestCase):
    def test_constructor(self):
        f = filters.Filter()
        f.feColorMatrix('input1', type_='matrix', values='0 0 0')
        self.assertEqual(f.tostring(), '<filter><feColorMatrix in="input1" type="matrix" values="0 0 0" /></filter>')

class Test_feComponentTransfer(unittest.TestCase):
    def test_constructor(self):
        f = filters.Filter()
        f.feComponentTransfer('input1')
        self.assertEqual(f.tostring(), '<filter><feComponentTransfer in="input1" /></filter>')

    def test_add_FuncR(self):
        f = filters.Filter()
        fct = f.feComponentTransfer('input1')
        func = fct.feFuncR('identity')
        self.assertEqual(fct.tostring(), '<feComponentTransfer in="input1"><feFuncR type="identity" /></feComponentTransfer>')
        self.assertEqual(func.tostring(), '<feFuncR type="identity" />')

    def test_add_FuncG(self):
        f = filters.Filter()
        fct = f.feComponentTransfer('input1')
        func = fct.feFuncG('table')
        self.assertEqual(func.tostring(), '<feFuncG type="table" />')

    def test_add_FuncB(self):
        f = filters.Filter()
        fct = f.feComponentTransfer('input1')
        func = fct.feFuncB('discrete')
        self.assertEqual(func.tostring(), '<feFuncB type="discrete" />')

    def test_add_FuncA(self):
        f = filters.Filter()
        fct = f.feComponentTransfer('input1')
        func = fct.feFuncA('gamma')
        self.assertEqual(func.tostring(), '<feFuncA type="gamma" />')

    def test_FuncR_all_params(self):
        f = filters.Filter()
        fct = f.feComponentTransfer('input1')
        func = fct.feFuncR('identity', tableValues="1,2", slope=1, intercept=0,
                           amplitude=0, exponent=1, offset=0)
        self.assertEqual(func.tostring(), '<feFuncR amplitude="0" exponent="1" '\
                         'intercept="0" offset="0" slope="1" '\
                         'tableValues="1,2" type="identity" />')


class Test_feComposite(unittest.TestCase):
    def test_constructor(self):
        f = filters.Filter()
        pf = f.feComposite('input1', in2='input2')
        self.assertEqual(pf.tostring(), '<feComposite in="input1" in2="input2" />')

    def test_operator(self):
        f = filters.Filter()
        self.assertEqual(f.feComposite('input1', operator='over').tostring(),
                         '<feComposite in="input1" operator="over" />')
        self.assertEqual(f.feComposite('input1', operator='in').tostring(),
                         '<feComposite in="input1" operator="in" />')
        self.assertEqual(f.feComposite('input1', operator='out').tostring(),
                         '<feComposite in="input1" operator="out" />')
        self.assertEqual(f.feComposite('input1', operator='xor').tostring(),
                         '<feComposite in="input1" operator="xor" />')
        self.assertEqual(f.feComposite('input1', operator='arithmetic').tostring(),
                         '<feComposite in="input1" operator="arithmetic" />')

    def test_k1k2k3k4(self):
        f = filters.Filter()
        pf = f.feComposite('input1', k1=1, k2=2, k3=3, k4=4)
        self.assertEqual(pf.tostring(), '<feComposite in="input1" k1="1" k2="2" k3="3" k4="4" />')


class Test_feConvolveMatrix(unittest.TestCase):
    def test_constructor(self):
        f = filters.Filter()
        pf = f.feConvolveMatrix('input1')
        self.assertEqual(pf.tostring(), '<feConvolveMatrix in="input1" />')

    def test_order(self):
        f = filters.Filter()
        pf = f.feConvolveMatrix('input1', order='1 2')
        self.assertEqual(pf.tostring(), '<feConvolveMatrix in="input1" order="1 2" />')

    def test_kernelMatrix(self):
        f = filters.Filter()
        pf = f.feConvolveMatrix('input1', kernelMatrix='1 2 3 4')
        self.assertEqual(pf.tostring(), '<feConvolveMatrix in="input1" kernelMatrix="1 2 3 4" />')

    def test_divisor_and_bias(self):
        f = filters.Filter()
        pf = f.feConvolveMatrix('input1', divisor=2.5, bias=0.5)
        self.assertEqual(pf.tostring(), '<feConvolveMatrix bias="0.5" divisor="2.5" in="input1" />')

    def test_targetX_and_targetY(self):
        f = filters.Filter()
        pf = f.feConvolveMatrix('input1', targetX=1, targetY=2)
        self.assertEqual(pf.tostring(), '<feConvolveMatrix in="input1" targetX="1" targetY="2" />')

    def test_edgeMode(self):
        f = filters.Filter()
        self.assertEqual(f.feConvolveMatrix('input1', edgeMode='duplicate').tostring(),
                         '<feConvolveMatrix edgeMode="duplicate" in="input1" />')
        self.assertEqual(f.feConvolveMatrix('input1', edgeMode='wrap').tostring(),
                         '<feConvolveMatrix edgeMode="wrap" in="input1" />')
        self.assertEqual(f.feConvolveMatrix('input1', edgeMode='none').tostring(),
                         '<feConvolveMatrix edgeMode="none" in="input1" />')

    def test_preserveAlpha(self):
        f = filters.Filter()
        self.assertEqual(f.feConvolveMatrix('input1', preserveAlpha='true').tostring(),
                         '<feConvolveMatrix in="input1" preserveAlpha="true" />')
        self.assertEqual(f.feConvolveMatrix('input1', preserveAlpha='false').tostring(),
                         '<feConvolveMatrix in="input1" preserveAlpha="false" />')

    def test_kernelUnitLength(self):
        f = filters.Filter()
        self.assertEqual(f.feConvolveMatrix('input1', kernelUnitLength=2).tostring(),
                         '<feConvolveMatrix in="input1" kernelUnitLength="2" />')


class Test_feDiffuseLighting(unittest.TestCase):
    def test_constructor(self):
        f = filters.Filter()
        self.assertEqual(f.feDiffuseLighting('input1').tostring(),
                         '<feDiffuseLighting in="input1" />')

    def test_surfaceScale(self):
        f = filters.Filter()
        self.assertEqual(f.feDiffuseLighting('input1', surfaceScale=1).tostring(),
                         '<feDiffuseLighting in="input1" surfaceScale="1" />')

    def test_diffuseConstant(self):
        f = filters.Filter()
        self.assertEqual(f.feDiffuseLighting('input1', diffuseConstant=1).tostring(),
                         '<feDiffuseLighting diffuseConstant="1" in="input1" />')

    def test_kernelUnitLength(self):
        f = filters.Filter()
        self.assertEqual(f.feDiffuseLighting('input1', kernelUnitLength=2).tostring(),
                         '<feDiffuseLighting in="input1" kernelUnitLength="2" />')

    def test_lighting_color(self):
        f = filters.Filter()
        self.assertEqual(f.feDiffuseLighting('input1', lighting_color='yellow').tostring(),
                         '<feDiffuseLighting in="input1" lighting-color="yellow" />')


class Test_feDisplacementMap(unittest.TestCase):
    def test_constructor(self):
        f = filters.Filter()
        self.assertEqual(f.feDisplacementMap('input1', in2="input2").tostring(),
                         '<feDisplacementMap in="input1" in2="input2" />')

    def test_scale(self):
        f = filters.Filter()
        self.assertEqual(f.feDisplacementMap('input1', scale=2).tostring(),
                         '<feDisplacementMap in="input1" scale="2" />')

    def test_xChannelSelector_yChannelSelector(self):
        f = filters.Filter()
        self.assertEqual(f.feDisplacementMap('input1', xChannelSelector="R", yChannelSelector="G").tostring(),
                         '<feDisplacementMap in="input1" xChannelSelector="R" yChannelSelector="G" />')


class Test_feFlood(unittest.TestCase):
    def test_constructor(self):
        f = filters.Filter()
        self.assertEqual(f.feFlood().tostring(),
                         '<feFlood />')

    def test_flood_color(self):
        f = filters.Filter()
        self.assertEqual(f.feFlood(flood_color="red").tostring(),
                         '<feFlood flood-color="red" />')

    def test_flood_opacity(self):
        f = filters.Filter()
        self.assertEqual(f.feFlood(flood_opacity=0.5).tostring(),
                         '<feFlood flood-opacity="0.5" />')


class Test_feGaussianBlur(unittest.TestCase):
    def test_constructor(self):
        f = filters.Filter()
        self.assertEqual(f.feGaussianBlur("input1").tostring(),
                         '<feGaussianBlur in="input1" />')

    def test_stdDeviation(self):
        f = filters.Filter()
        self.assertEqual(f.feGaussianBlur("input1", stdDeviation="1 2").tostring(),
                         '<feGaussianBlur in="input1" stdDeviation="1 2" />')


class Test_feImage(unittest.TestCase):
    def test_constructor(self):
        f = filters.Filter()
        self.assertEqual(f.feImage("./image.png").tostring(),
                         '<feImage xlink:href="./image.png" />')


class Test_feMerge(unittest.TestCase):
    def test_constructor(self):
        f = filters.Filter()
        self.assertEqual(f.feMerge(['Layer1', 'Layer2']).tostring(),
                         '<feMerge><feMergeNode in="Layer1" /><feMergeNode in="Layer2" /></feMerge>')

    def test_layers(self):
        f = filters.Filter()
        merge = f.feMerge(['Layer1', 'Layer2'])
        merge.feMergeNode(('Layer3', ))
        self.assertEqual(merge.tostring(),
                         '<feMerge><feMergeNode in="Layer1" /><feMergeNode in="Layer2" />'
                         '<feMergeNode in="Layer3" /></feMerge>')


class Test_feMorphology(unittest.TestCase):
    def test_constructor(self):
        f = filters.Filter()
        self.assertEqual(f.feMorphology("input1").tostring(),
                         '<feMorphology in="input1" />')

    def test_operator(self):
        f = filters.Filter()
        self.assertEqual(f.feMorphology("input1", operator="erode").tostring(),
                         '<feMorphology in="input1" operator="erode" />')
        self.assertEqual(f.feMorphology("input1", operator="dilate").tostring(),
                         '<feMorphology in="input1" operator="dilate" />')

    def test_radius(self):
        f = filters.Filter()
        self.assertEqual(f.feMorphology("input1", radius="1,2").tostring(),
                         '<feMorphology in="input1" radius="1,2" />')


class Test_feOffset(unittest.TestCase):
    def test_constructor(self):
        f = filters.Filter()
        self.assertEqual(f.feOffset("input1").tostring(),
                         '<feOffset in="input1" />')

    def test_dx_dy(self):
        f = filters.Filter()
        self.assertEqual(f.feOffset("input1", dx=10, dy=20).tostring(),
                         '<feOffset dx="10" dy="20" in="input1" />')


class Test_feSpecularLighting(unittest.TestCase):
    def test_constructor(self):
        f = filters.Filter()
        self.assertEqual(f.feSpecularLighting("input1").tostring(),
                         '<feSpecularLighting in="input1" />')

    def test_surfaceScale(self):
        f = filters.Filter()
        self.assertEqual(f.feSpecularLighting("input1", surfaceScale=7).tostring(),
                         '<feSpecularLighting in="input1" surfaceScale="7" />')

    def test_specularExponent(self):
        f = filters.Filter()
        self.assertEqual(f.feSpecularLighting("input1", specularExponent=7).tostring(),
                         '<feSpecularLighting in="input1" specularExponent="7" />')

    def test_kernelUnitLength(self):
        f = filters.Filter()
        self.assertEqual(f.feSpecularLighting("input1", kernelUnitLength="1,2").tostring(),
                         '<feSpecularLighting in="input1" kernelUnitLength="1,2" />')

    def test_lighting_color(self):
        f = filters.Filter()
        self.assertEqual(f.feSpecularLighting('input1', lighting_color='yellow').tostring(),
                         '<feSpecularLighting in="input1" lighting-color="yellow" />')


class Test_feTile(unittest.TestCase):
    def test_constructor(self):
        f = filters.Filter()
        self.assertEqual(f.feTile("input1").tostring(),
                         '<feTile in="input1" />')


class Test_feTurbulence(unittest.TestCase):
    def test_constructor(self):
        f = filters.Filter()
        self.assertEqual(f.feTurbulence().tostring(),
                         '<feTurbulence />')

    def test_baseFrequency(self):
        f = filters.Filter()
        self.assertEqual(f.feTurbulence(baseFrequency="1,2").tostring(),
                         '<feTurbulence baseFrequency="1,2" />')

    def test_numOctaves(self):
        f = filters.Filter()
        self.assertEqual(f.feTurbulence(numOctaves="1").tostring(),
                         '<feTurbulence numOctaves="1" />')

    def test_seed(self):
        f = filters.Filter()
        self.assertEqual(f.feTurbulence(seed="1").tostring(),
                         '<feTurbulence seed="1" />')

    def test_stitchTiles(self):
        f = filters.Filter()
        self.assertEqual(f.feTurbulence(stitchTiles="stitch").tostring(),
                         '<feTurbulence stitchTiles="stitch" />')

    def test_type(self):
        f = filters.Filter()
        self.assertEqual(f.feTurbulence(type_="fractalNoise").tostring(),
                         '<feTurbulence type="fractalNoise" />')
        self.assertEqual(f.feTurbulence(type_="turbulence").tostring(),
                         '<feTurbulence type="turbulence" />')


class TestDistantLight(unittest.TestCase):
    def test_constructor(self):
        f = filters.Filter()
        fp = f.feDiffuseLighting('input1')
        ls = fp.feDistantLight()
        self.assertEqual(ls.tostring(), '<feDistantLight />')
        self.assertEqual(fp.tostring(), '<feDiffuseLighting in="input1"><feDistantLight /></feDiffuseLighting>')
        self.assertEqual(f.tostring(), '<filter><feDiffuseLighting in="input1"><feDistantLight /></feDiffuseLighting></filter>')

    def test_all_parmeters(self):
        f = filters.Filter()
        ls = f.feDiffuseLighting('input1').feDistantLight(1, 2)
        self.assertEqual(ls.tostring(), '<feDistantLight azimuth="1" elevation="2" />')


class TestPointLight(unittest.TestCase):
    def test_constructor(self):
        f = filters.Filter()
        fp = f.feDiffuseLighting('input1')
        ls = fp.fePointLight()
        self.assertEqual(ls.tostring(), '<fePointLight />')
        self.assertEqual(fp.tostring(), '<feDiffuseLighting in="input1"><fePointLight /></feDiffuseLighting>')
        self.assertEqual(f.tostring(), '<filter><feDiffuseLighting in="input1"><fePointLight /></feDiffuseLighting></filter>')

    def test_all_parmeters(self):
        f = filters.Filter()
        ls = f.feDiffuseLighting('input1').fePointLight(source=(1,2,3))
        self.assertEqual(ls.tostring(), '<fePointLight x="1" y="2" z="3" />')


class TestSpotLight(unittest.TestCase):
    def test_constructor(self):
        f = filters.Filter()
        fp = f.feDiffuseLighting('input1')
        ls = fp.feSpotLight()
        self.assertEqual(ls.tostring(), '<feSpotLight />')
        self.assertEqual(fp.tostring(), '<feDiffuseLighting in="input1"><feSpotLight /></feDiffuseLighting>')
        self.assertEqual(f.tostring(), '<filter><feDiffuseLighting in="input1"><feSpotLight /></feDiffuseLighting></filter>')

    def test_all_parmeters(self):
        f = filters.Filter()
        ls = f.feDiffuseLighting('input1').feSpotLight(source=(1, 2, 3), target=(4, 5, 6),
                                                       specularExponent=2,
                                                       limitingConeAngle=15)
        self.assertEqual(ls.tostring(), '<feSpotLight limitingConeAngle="15" '
                         'pointsAtX="4" pointsAtY="5" pointsAtZ="6" '
                         'specularExponent="2" x="1" y="2" z="3" />')


if __name__ == '__main__':
    unittest.main()