Coding style fixes
[experiments/svgwrite_inkscape_drawing.git] / svgwrite_inkscape_drawing.py
1 #!/usr/bin/env python
2 #coding:utf-8
3 # Author:  ao2
4 # Purpose: create Inkscape layers with svgwrite
5 # Created: 26.01.2018
6 # License: MIT License
7 # Copyright (C) 2018  Antonio Ospite <ao2@ao2.it>
8
9 import svgwrite
10 from svgwrite.data.types import SVGAttribute
11
12
13 class InkscapeDrawing(svgwrite.Drawing):
14     """An svgwrite.Drawing subclass which supports Inkscape layers"""
15     INKSCAPE_NAMESPACE = 'http://www.inkscape.org/namespaces/inkscape'
16     SODIPODI_NAMESPACE = 'http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd'
17
18     def __init__(self, *args, **kwargs):
19         super(InkscapeDrawing, self).__init__(*args, **kwargs)
20
21         inkscape_attributes = {
22             'xmlns:inkscape': SVGAttribute('xmlns:inkscape',
23                                            anim=False,
24                                            types=[],
25                                            const=frozenset([self.INKSCAPE_NAMESPACE])),
26             'xmlns:sodipodi': SVGAttribute('xmlns:sodipodi',
27                                            anim=False,
28                                            types=[],
29                                            const=frozenset([self.SODIPODI_NAMESPACE])),
30             'inkscape:groupmode': SVGAttribute('inkscape:groupmode',
31                                                anim=False,
32                                                types=[],
33                                                const=frozenset(['layer'])),
34             'inkscape:label': SVGAttribute('inkscape:label',
35                                            anim=False,
36                                            types=frozenset(['string']),
37                                            const=[]),
38             'sodipodi:insensitive': SVGAttribute('sodipodi:insensitive',
39                                                  anim=False,
40                                                  types=frozenset(['string']),
41                                                  const=[])
42         }
43
44         self.validator.attributes.update(inkscape_attributes)
45
46         elements = self.validator.elements
47
48         svg_attributes = set(elements['svg'].valid_attributes)
49         svg_attributes.add('xmlns:inkscape')
50         svg_attributes.add('xmlns:sodipodi')
51         elements['svg'].valid_attributes = frozenset(svg_attributes)
52
53         g_attributes = set(elements['g'].valid_attributes)
54         g_attributes.add('inkscape:groupmode')
55         g_attributes.add('inkscape:label')
56         g_attributes.add('sodipodi:insensitive')
57         elements['g'].valid_attributes = frozenset(g_attributes)
58
59         self['xmlns:inkscape'] = self.INKSCAPE_NAMESPACE
60         self['xmlns:sodipodi'] = self.SODIPODI_NAMESPACE
61
62     def layer(self, **kwargs):
63         """Create an inkscape layer.
64
65         An optional 'label' keyword argument can be passed to set a user
66         friendly name for the layer."""
67         label = kwargs.pop('label', None)
68
69         new_layer = self.g(**kwargs)
70         new_layer['inkscape:groupmode'] = 'layer'
71
72         if label:
73             new_layer['inkscape:label'] = label
74
75         return new_layer
76
77
78 def main():
79     svg = InkscapeDrawing('inkscape-test.svg', profile='full', size=(640, 480))
80
81     layer = svg.layer(label="Layer one")
82     layer["sodipodi:insensitive"] = "true"
83     svg.add(layer)
84
85     line = svg.line((100, 100), (300, 100),
86                     stroke=svgwrite.rgb(10, 10, 16, '%'),
87                     stroke_width='10')
88     layer.add(line)
89
90     text = svg.text('Test', insert=(100, 100), font_size='100', fill='red')
91     layer.add(text)
92
93     svg.save()
94
95
96 if __name__ == "__main__":
97     main()