4 # Purpose: create Inkscape layers with svgwrite
7 # Copyright (C) 2018 Antonio Ospite <ao2@ao2.it>
10 from svgwrite.data.types import SVGAttribute
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'
18 def __init__(self, *args, **kwargs):
19 super(InkscapeDrawing, self).__init__(*args, **kwargs)
21 inkscape_attributes = {
22 'xmlns:inkscape': SVGAttribute('xmlns:inkscape',
25 const=frozenset([self.INKSCAPE_NAMESPACE])),
26 'xmlns:sodipodi': SVGAttribute('xmlns:sodipodi',
29 const=frozenset([self.SODIPODI_NAMESPACE])),
30 'inkscape:groupmode': SVGAttribute('inkscape:groupmode',
33 const=frozenset(['layer'])),
34 'inkscape:label': SVGAttribute('inkscape:label',
36 types=frozenset(['string']),
38 'sodipodi:insensitive': SVGAttribute('sodipodi:insensitive',
40 types=frozenset(['string']),
44 self.validator.attributes.update(inkscape_attributes)
46 elements = self.validator.elements
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)
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)
59 self['xmlns:inkscape'] = self.INKSCAPE_NAMESPACE
60 self['xmlns:sodipodi'] = self.SODIPODI_NAMESPACE
62 def layer(self, **kwargs):
63 """Create an inkscape layer.
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)
69 new_layer = self.g(**kwargs)
70 new_layer['inkscape:groupmode'] = 'layer'
73 new_layer['inkscape:label'] = label
79 svg = InkscapeDrawing('inkscape-test.svg', profile='full', size=(640, 480))
81 layer = svg.layer(label="Layer one")
82 layer["sodipodi:insensitive"] = "true"
85 line = svg.line((100, 100), (300, 100),
86 stroke=svgwrite.rgb(10, 10, 16, '%'),
90 text = svg.text('Test', insert=(100, 100), font_size='100', fill='red')
96 if __name__ == "__main__":