add handling for <polyline>

pull/5/head
Lex Neva 2017-10-22 20:23:24 +01:00
rodzic 6a742200b5
commit d40bf67180
1 zmienionych plików z 98 dodań i 20 usunięć

Wyświetl plik

@ -48,9 +48,13 @@ dbg = open("/tmp/embroider-debug.txt", "w")
PyEmb.dbg = dbg PyEmb.dbg = dbg
SVG_PATH_TAG = inkex.addNS('path', 'svg') SVG_PATH_TAG = inkex.addNS('path', 'svg')
SVG_POLYLINE_TAG = inkex.addNS('polyline', 'svg')
SVG_DEFS_TAG = inkex.addNS('defs', 'svg') SVG_DEFS_TAG = inkex.addNS('defs', 'svg')
SVG_GROUP_TAG = inkex.addNS('g', 'svg') SVG_GROUP_TAG = inkex.addNS('g', 'svg')
EMBROIDERABLE_TAGS = (SVG_PATH_TAG, SVG_POLYLINE_TAG)
class Param(object): class Param(object):
def __init__(self, name, description, unit=None, values=[], type=None, group=None, inverse=False, default=None): def __init__(self, name, description, unit=None, values=[], type=None, group=None, inverse=False, default=None):
self.name = name self.name = name
@ -158,6 +162,11 @@ class EmbroideryElement(object):
style = simplestyle.parseStyle(self.node.get("style")) style = simplestyle.parseStyle(self.node.get("style"))
return style_name in style return style_name in style
@property
def path(self):
return cubicsuperpath.parsePath(self.node.get("d"))
@cache @cache
def parse_path(self): def parse_path(self):
# A CSP is a "cubic superpath". # A CSP is a "cubic superpath".
@ -187,9 +196,7 @@ class EmbroideryElement(object):
# In a path, each element in the 3-tuple is itself a tuple of (x, y). # In a path, each element in the 3-tuple is itself a tuple of (x, y).
# Tuples all the way down. Hasn't anyone heard of using classes? # Tuples all the way down. Hasn't anyone heard of using classes?
path = cubicsuperpath.parsePath(self.node.get("d")) path = self.path
# print >> sys.stderr, pformat(path)
# start with the identity transform # start with the identity transform
transform = [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0]] transform = [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0]]
@ -1554,7 +1561,78 @@ class SatinColumn(EmbroideryElement):
return patches return patches
class Polyline(EmbroideryElement):
# Handle a <polyline> element, which is treated as a set of points to
# stitch exactly.
#
# <polyline> elements are pretty rare in SVG, from what I can tell.
# Anything you can do with a <polyline> can also be done with a <p>, and
# much more.
#
# Notably, EmbroiderModder2 uses <polyline> elements when converting from
# common machine embroidery file formats to SVG. Handling those here lets
# users use File -> Import to pull in existing designs they may have
# obtained, for example purchased fonts.
@property
def points(self):
# example: "1,2 0,0 1.5,3 4,2"
points = self.node.get('points')
points = points.split(" ")
points = [[float(coord) for coord in point.split(",")] for point in points]
return points
@property
def path(self):
# A polyline is a series of connected line segments described by their
# points. In order to make use of the existing logic for incorporating
# svg transforms that is in our superclass, we'll convert the polyline
# to a degenerate cubic superpath in which the bezier handles are on
# the segment endpoints.
path = [[[point[:], point[:], point[:]] for point in self.points]]
return path
@property
@cache
def csp(self):
csp = self.parse_path()
return csp
@property
def color(self):
# EmbroiderModder2 likes to use the `stroke` property directly instead
# of CSS.
return self.get_style("stroke") or self.node.get("stroke")
@property
def stitches(self):
# For a <polyline>, we'll stitch the points exactly as they exist in
# the SVG, with no stitch spacing interpolation, flattening, etc.
# See the comments in the parent class's parse_path method for a
# description of the CSP data structure.
stitches = [point for handle_before, point, handle_after in self.csp[0]]
return stitches
def to_patches(self, last_patch):
patch = Patch(color=self.color)
for stitch in self.stitches:
patch.add_stitch(PyEmb.Point(*stitch))
return [patch]
def detect_classes(node): def detect_classes(node):
if node.tag == SVG_POLYLINE_TAG:
return [Polyline]
else:
element = EmbroideryElement(node) element = EmbroideryElement(node)
if element.get_boolean_param("satin_column"): if element.get_boolean_param("satin_column"):
@ -1590,7 +1668,7 @@ def descendants(node):
for child in node: for child in node:
nodes.extend(descendants(child)) nodes.extend(descendants(child))
if node.tag == SVG_PATH_TAG: if node.tag in EMBROIDERABLE_TAGS:
nodes.append(node) nodes.append(node)
return nodes return nodes
@ -1732,7 +1810,7 @@ class Embroider(inkex.Effect):
self.patches = [] self.patches = []
def handle_node(self, node): def handle_node(self, node):
print >> dbg, "handling node", node.get('id'), node.get('tag') print >> dbg, "handling node", node.get('id'), node.tag
nodes = descendants(node) nodes = descendants(node)
for node in nodes: for node in nodes:
classes = detect_classes(node) classes = detect_classes(node)