forked from mnesarco/inkscape-parametric
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparametric.py
executable file
·196 lines (161 loc) · 6.39 KB
/
parametric.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#! /usr/bin/python
'''
Copyright (C) 2019 Frank Martinez, mnesarco <at> gmail.com
This file is part of inkscape-parametric.
inkscape-parametric is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
inkscape-parametric is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with inkscape-parametric. If not, see <https://www.gnu.org/licenses/>.
'''
import inkex, simplestyle, simplepath, math, copy, sys
from io import StringIO
etree = inkex.etree
class SvgObject(object):
"""
This class is a wrapper of the svg node, attributes are mapped
to lxml node attributes
"""
def __init__(self, lxml):
object.__setattr__(self, 'e', lxml)
def getE(self):
return object.__getattribute__(self, 'e')
def __getattr__(self, name):
return self.e.attrib[name]
def __setattr__(self, name, value):
self.e.attrib[name] = str(value)
def isgroup(self):
return self.getE().tag == 'g'
def gettag(self):
return inkex.etree.QName(self.getE())
def text(self, value = None):
if value is None:
return self.getE().text
else:
self.getE().text = value
class SvgDoc(object):
"""
This class is a wrapper of the svg document.
"""
def __init__(self, lxml):
self.lxml = lxml
def findById(self, id):
"""
Returns a wrapped svg object by its id
"""
node = self.lxml.find('//*[@id="%s"]' % str(id), namespaces=inkex.NSS)
if node == None:
return None
else:
return SvgObject(node)
class Parametric(inkex.EffectExtension):
"""
This extension adds parametric functionality to the svg document.
1 - Any python code inside <parametric:script></parametric:script> will be
executed.
2 - All attributes in the parametric namespace will be evaluated (python expression) and
the result will be set to the equivalent attribute without namespace.
"""
namespace = "//fdmtech.com/inkscape/parametric"
prefix = "parametric"
def __init__(self):
inkex.Effect.__init__(self)
def getroot(self):
return self.document.getroot()
def isparametric(self):
return self.getroot().nsmap.has_key(self.prefix)
def parse(self, filename=None):
"""Parse document in specified file or on stdin"""
# First try to open the file from the function argument
if filename is not None:
try:
stream = open(filename, 'r')
except IOError:
errormsg(_("Unable to open specified file: %s") % filename)
sys.exit()
# If it wasn't specified, try to open the file specified as
# an object member
elif self.svg_file is not None:
try:
stream = open(self.svg_file, 'r')
except IOError:
errormsg(_("Unable to open object member file: %s") % self.svg_file)
sys.exit()
# Finally, if the filename was not specified anywhere, use
# standard input stream
else:
stream = sys.stdin
data = stream.read()
stream.close()
p = inkex.etree.XMLParser(huge_tree=True)
reparsed = False
try:
# Parse already parametric svg document
self.document = inkex.etree.parse(io.StringIO(data), parser=p)
except:
# Parse not parametric svg document with 'invalid' parametric attributes
reparsed = True
data = self.add_parametric(data)
self.document = inkex.etree.parse(io.StringIO(data), parser=p)
# Parse non parametric svg document
if not self.isparametric() and not reparsed:
data = self.add_parametric(data)
self.document = inkex.etree.parse(io.StringIO(data), parser=p)
# Add parametric script if not exists
root = self.getroot()
script = root.find('.//{%s}script' % self.namespace)
if script == None:
node = inkex.etree.SubElement(root, '{%s}script' % self.namespace, nsmap={self.prefix : self.namespace})
node.text = '""" You can write python code here """'
self.original_document = copy.deepcopy(self.document)
def add_parametric(self, data):
"""
Adds parametric namespace and script to the document
"""
r = data.replace('<svg', "<svg\n xmlns:{0}=\"{1}\" {0}:enabled=\"true\" ".format(self.prefix, self.namespace))
r = r.replace('</svg>', (
'<parametric:script>'
'""" You can write python code here """'
'</parametric:script>'
'</svg>'))
return r
def output(self):
self.document.write(sys.stdout)
def evalScript(self, ctx):
root = self.getroot()
script = root.find('.//{%s}script' % self.namespace)
if script != None:
try:
exec(script.text, ctx, ctx)
except Exception as e:
inkex.errormsg(str(e))
def evalAttributes(self, ctx):
root = self.getroot()
px = "{%s}" % self.namespace
pxn = len(px)
errors = ""
for node in self.document.xpath("//*[@*[namespace-uri()='%s']]" % self.namespace):
if node != root:
for att in node.items():
if att[0].startswith(px):
name = att[0][pxn:]
try:
value = str(eval(att[1], ctx, ctx))
node.attrib[name] = value
except Exception as e:
errors = errors + str(e) + "\n"
if errors != "":
inkex.errormsg(errors)
def effect(self):
ctx = {'_svg' : SvgDoc(self.document)}
self.evalScript(ctx)
self.evalAttributes(ctx)
if __name__ == '__main__':
e = Parametric()
e.run()
# vim: expandtab shiftwidth=4 tabstop=4 softtabstop=4 fileencoding=utf-8 textwidth=99