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 def __init__(self, *args, **kwargs):
16 super(InkscapeDrawing, self).__init__(*args, **kwargs)
18 inkscape_attributes = {
19 'xmlns:inkscape': SVGAttribute('xmlns:inkscape',
22 const=frozenset(['http://www.inkscape.org/namespaces/inkscape'])),
23 'inkscape:groupmode': SVGAttribute('inkscape:groupmode',
26 const=frozenset(['layer'])),
27 'inkscape:label': SVGAttribute('inkscape:label',
29 types=frozenset(['string']),
33 self.validator.attributes.update(inkscape_attributes)
35 elements = self.validator.elements
37 svg_attributes = set(elements['svg'].valid_attributes)
38 svg_attributes.add('xmlns:inkscape')
39 elements['svg'].valid_attributes = frozenset(svg_attributes)
41 g_attributes = set(elements['g'].valid_attributes)
42 g_attributes.add('inkscape:groupmode')
43 g_attributes.add('inkscape:label')
44 elements['g'].valid_attributes = frozenset(g_attributes)
46 self['xmlns:inkscape'] = 'http://www.inkscape.org/namespaces/inkscape'
48 def layer(self, **kwargs):
49 """Create an inkscape layer.
51 An optional 'label' keyword argument can be passed to set a user
52 friendly name for the layer."""
53 label = kwargs.pop('label', None)
55 new_layer = self.g(**kwargs)
56 new_layer['inkscape:groupmode'] = 'layer'
59 new_layer['inkscape:label'] = label
65 svg = InkscapeDrawing('inkscape-test.svg', profile='full', size=(640, 480))
67 layer = svg.layer(label="Layer one")
70 line = svg.line((100, 100), (300, 100),
71 stroke=svgwrite.rgb(10, 10, 16, '%'),
75 text = svg.text('Test', insert=(100, 100), font_size='100', fill='red')
81 if __name__ == "__main__":