-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
executable file
·35 lines (26 loc) · 1.38 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/usr/bin/env python
# coding=utf-8
import inkex
import scour
from scour.scour import scourString, parse_args
options = parse_args(['--strip-xml-prolog', '--enable-viewboxing', '--no-line-breaks', '--remove-descriptive-elements', '--enable-comment-stripping', '--enable-id-stripping', '--shorten-ids'])
class CopyOptimize(inkex.Effect):
def __init__(self):
inkex.Effect.__init__(self)
def template(self):
width = self.document.getroot().get('width')
height = self.document.getroot().get('height')
viewbox = self.document.getroot().get('viewBox')
defs = self.document.getroot().defs.tostring().decode()
return f'<svg width="{width}" height="{height}" viewBox="{viewbox}" xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape" xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd" xmlns="http://www.w3.org/2000/svg" xmlns:svg="http://www.w3.org/2000/svg">' + defs
def effect(self):
elements = []
if len(self.svg.selection) == 0:
raise inkex.AbortExtension("None selected.")
else:
for elem in self.svg.selection:
elements.append(elem.tostring().decode())
svg_string = self.template() + ' '.join(elements[::-1]) + "</svg>"
inkex.utils.debug(scourString(svg_string, options))
if __name__ == '__main__':
CopyOptimize().run()