b307544aca1b5f51af4a5a79c69cace8e7d95143
[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     def __init__(self, *args, **kwargs):
16         super(InkscapeDrawing, self).__init__(*args, **kwargs)
17
18         inkscape_attributes = {
19             'xmlns:inkscape': SVGAttribute('xmlns:inkscape',
20                                            anim=False,
21                                            types=[],
22                                            const=frozenset(['http://www.inkscape.org/namespaces/inkscape'])),
23             'xmlns:sodipodi': SVGAttribute('xmlns:sodipodi',
24                                            anim=False,
25                                            types=[],
26                                            const=frozenset(['http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd'])),
27             'inkscape:groupmode': SVGAttribute('inkscape:groupmode',
28                                                anim=False,
29                                                types=[],
30                                                const=frozenset(['layer'])),
31             'inkscape:label': SVGAttribute('inkscape:label',
32                                            anim=False,
33                                            types=frozenset(['string']),
34                                            const=[]),
35             'sodipodi:insensitive': SVGAttribute('sodipodi:insensitive',
36                                            anim=False,
37                                            types=frozenset(['string']),
38                                            const=[])
39         }
40
41         self.validator.attributes.update(inkscape_attributes)
42
43         elements = self.validator.elements
44
45         svg_attributes = set(elements['svg'].valid_attributes)
46         svg_attributes.add('xmlns:inkscape')
47         svg_attributes.add('xmlns:sodipodi')
48         elements['svg'].valid_attributes = frozenset(svg_attributes)
49
50         g_attributes = set(elements['g'].valid_attributes)
51         g_attributes.add('inkscape:groupmode')
52         g_attributes.add('inkscape:label')
53         g_attributes.add('sodipodi:insensitive')
54         elements['g'].valid_attributes = frozenset(g_attributes)
55
56         self['xmlns:inkscape'] = 'http://www.inkscape.org/namespaces/inkscape'
57         self['xmlns:sodipodi'] = 'http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd'
58
59     def layer(self, **kwargs):
60         """Create an inkscape layer.
61
62         An optional 'label' keyword argument can be passed to set a user
63         friendly name for the layer."""
64         label = kwargs.pop('label', None)
65
66         new_layer = self.g(**kwargs)
67         new_layer['inkscape:groupmode'] = 'layer'
68
69         if label:
70             new_layer['inkscape:label'] = label
71
72         return new_layer
73
74
75 def main():
76     svg = InkscapeDrawing('inkscape-test.svg', profile='full', size=(640, 480))
77
78     layer = svg.layer(label="Layer one")
79     layer["sodipodi:insensitive"] = "true"
80     svg.add(layer)
81
82     line = svg.line((100, 100), (300, 100),
83                     stroke=svgwrite.rgb(10, 10, 16, '%'),
84                     stroke_width='10')
85     layer.add(line)
86
87     text = svg.text('Test', insert=(100, 100), font_size='100', fill='red')
88     layer.add(text)
89
90     svg.save()
91
92
93 if __name__ == "__main__":
94     main()