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 'xmlns:sodipodi': SVGAttribute('xmlns:sodipodi',
26 const=frozenset(['http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd'])),
27 'inkscape:groupmode': SVGAttribute('inkscape:groupmode',
30 const=frozenset(['layer'])),
31 'inkscape:label': SVGAttribute('inkscape:label',
33 types=frozenset(['string']),
35 'sodipodi:insensitive': SVGAttribute('sodipodi:insensitive',
37 types=frozenset(['string']),
41 self.validator.attributes.update(inkscape_attributes)
43 elements = self.validator.elements
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)
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)
56 self['xmlns:inkscape'] = 'http://www.inkscape.org/namespaces/inkscape'
57 self['xmlns:sodipodi'] = 'http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd'
59 def layer(self, **kwargs):
60 """Create an inkscape layer.
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)
66 new_layer = self.g(**kwargs)
67 new_layer['inkscape:groupmode'] = 'layer'
70 new_layer['inkscape:label'] = label
76 svg = InkscapeDrawing('inkscape-test.svg', profile='full', size=(640, 480))
78 layer = svg.layer(label="Layer one")
79 layer["sodipodi:insensitive"] = "true"
82 line = svg.line((100, 100), (300, 100),
83 stroke=svgwrite.rgb(10, 10, 16, '%'),
87 text = svg.text('Test', insert=(100, 100), font_size='100', fill='red')
93 if __name__ == "__main__":