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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
|
import inkex
import re
import json
from copy import deepcopy
from collections import MutableMapping
from ..svg.tags import *
from ..elements import AutoFill, Fill, Stroke, SatinColumn, Polyline, EmbroideryElement
from ..utils import cache
from ..commands import is_command
SVG_METADATA_TAG = inkex.addNS("metadata", "svg")
def strip_namespace(tag):
"""Remove xml namespace from a tag name.
>>> {http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd}namedview
<<< namedview
"""
match = re.match('^\{[^}]+\}(.+)$', tag)
if match:
return match.group(1)
else:
return tag
class InkStitchMetadata(MutableMapping):
"""Helper class to get and set inkstitch-specific metadata attributes.
Operates on a document and acts like a dict. Setting an item adds or
updates a metadata element in the document. Getting an item retrieves
a metadata element's text contents or None if an element by that name
doesn't exist.
"""
def __init__(self, document):
self.document = document
self.metadata = self._get_or_create_metadata()
def _get_or_create_metadata(self):
metadata = self.document.find(SVG_METADATA_TAG)
if metadata is None:
metadata = inkex.etree.SubElement(self.document.getroot(), SVG_METADATA_TAG)
# move it so that it goes right after the first element, sodipodi:namedview
self.document.getroot().remove(metadata)
self.document.getroot().insert(1, metadata)
return metadata
# Because this class inherints from MutableMapping, all we have to do is
# implement these five methods and we get a full dict-like interface.
def __setitem__(self, name, value):
item = self._find_item(name)
item.text = json.dumps(value)
def _find_item(self, name, create=True):
tag = inkex.addNS(name, "inkstitch")
item = self.metadata.find(tag)
if item is None and create:
item = inkex.etree.SubElement(self.metadata, tag)
return item
def __getitem__(self, name):
item = self._find_item(name)
try:
return json.loads(item.text)
except (ValueError, TypeError):
return None
def __delitem__(self, name):
item = self._find_item(name, create=False)
if item is not None:
self.metadata.remove(item)
def __iter__(self):
for child in self.metadata:
if child.prefix == "inkstitch":
yield strip_namespace(child.tag)
def __len__(self):
i = 0
for i, item in enumerate(self):
pass
return i + 1
class InkstitchExtension(inkex.Effect):
"""Base class for Inkstitch extensions. Not intended for direct use."""
def hide_all_layers(self):
for g in self.document.getroot().findall(SVG_GROUP_TAG):
if g.get(INKSCAPE_GROUPMODE) == "layer":
g.set("style", "display:none")
def no_elements_error(self):
if self.selected:
inkex.errormsg(_("No embroiderable paths selected."))
else:
inkex.errormsg(_("No embroiderable paths found in document."))
inkex.errormsg(_("Tip: use Path -> Object to Path to convert non-paths."))
def descendants(self, node):
nodes = []
element = EmbroideryElement(node)
if element.has_command('ignore'):
return []
if element.has_style('display') and element.get_style('display') is None:
return []
if node.tag == SVG_DEFS_TAG:
return []
for child in node:
nodes.extend(self.descendants(child))
if node.tag in EMBROIDERABLE_TAGS:
nodes.append(node)
return nodes
def get_nodes(self):
"""Get all XML nodes, or just those selected
effect is an instance of a subclass of inkex.Effect.
"""
if self.selected:
nodes = []
for node in self.document.getroot().iter():
if node.get("id") in self.selected:
nodes.extend(self.descendants(node))
else:
nodes = self.descendants(self.document.getroot())
return nodes
def detect_classes(self, node):
if node.tag == SVG_POLYLINE_TAG:
return [Polyline]
else:
element = EmbroideryElement(node)
if element.get_boolean_param("satin_column"):
return [SatinColumn]
else:
classes = []
if element.get_style("fill", "black"):
if element.get_boolean_param("auto_fill", True):
classes.append(AutoFill)
else:
classes.append(Fill)
if element.get_style("stroke"):
if not is_command(element.node):
classes.append(Stroke)
if element.get_boolean_param("stroke_first", False):
classes.reverse()
return classes
def get_elements(self):
self.elements = []
for node in self.get_nodes():
classes = self.detect_classes(node)
self.elements.extend(cls(node) for cls in classes)
if self.elements:
return True
else:
self.no_elements_error()
return False
def elements_to_patches(self, elements):
patches = []
for element in elements:
if patches:
last_patch = patches[-1]
else:
last_patch = None
patches.extend(element.embroider(last_patch))
return patches
def get_inkstitch_metadata(self):
return InkStitchMetadata(self.document)
def get_base_file_name(self):
svg_filename = self.document.getroot().get(inkex.addNS('docname', 'sodipodi'), "embroidery.svg")
if svg_filename.endswith('.svg'):
svg_filename = svg_filename[:-4]
return svg_filename
def parse(self):
"""Override inkex.Effect to add Ink/Stitch xml namespace"""
# SVG parsers don't actually look for anything at this URL. They just
# care that it's unique. That defines a "namespace" of element and
# attribute names to disambiguate conflicts with element and
# attribute names other XML namespaces.
#
# Updating inkex.NSS here allows us to pass 'inkstitch' into
# inkex.addNS().
inkex.NSS['inkstitch'] = 'http://inkstitch.org/namespace'
# call the superclass's method first
inkex.Effect.parse(self)
# This is the only way I could find to add a namespace to an existing
# element tree at the top without getting ugly prefixes like "ns0".
inkex.etree.cleanup_namespaces(self.document,
top_nsmap=inkex.NSS,
keep_ns_prefixes=inkex.NSS.keys())
self.original_document = deepcopy(self.document)
|