2018-03-31 00:37:11 +00:00
|
|
|
import sys
|
|
|
|
from copy import deepcopy
|
2018-05-02 01:21:07 +00:00
|
|
|
from shapely import geometry as shgeo
|
2018-03-31 00:37:11 +00:00
|
|
|
|
2018-05-02 01:21:07 +00:00
|
|
|
from ..i18n import _
|
2018-03-31 00:37:11 +00:00
|
|
|
from ..utils import cache
|
2018-06-21 19:41:06 +00:00
|
|
|
from ..svg import PIXELS_PER_MM, convert_length, get_doc_size, apply_transforms
|
|
|
|
from ..commands import find_commands
|
2018-03-31 00:37:11 +00:00
|
|
|
|
|
|
|
# inkscape-provided utilities
|
|
|
|
import simpletransform
|
|
|
|
import simplestyle
|
|
|
|
import cubicsuperpath
|
|
|
|
from cspsubdiv import cspsubdiv
|
|
|
|
|
2018-05-02 01:21:07 +00:00
|
|
|
|
2018-03-31 00:37:11 +00:00
|
|
|
class Patch:
|
|
|
|
"""A raw collection of stitches with attached instructions."""
|
|
|
|
|
2018-04-07 02:09:26 +00:00
|
|
|
def __init__(self, color=None, stitches=None, trim_after=False, stop_after=False, stitch_as_is=False):
|
2018-03-31 00:37:11 +00:00
|
|
|
self.color = color
|
|
|
|
self.stitches = stitches or []
|
|
|
|
self.trim_after = trim_after
|
|
|
|
self.stop_after = stop_after
|
2018-04-07 02:09:26 +00:00
|
|
|
self.stitch_as_is = stitch_as_is
|
2018-03-31 00:37:11 +00:00
|
|
|
|
|
|
|
def __add__(self, other):
|
|
|
|
if isinstance(other, Patch):
|
|
|
|
return Patch(self.color, self.stitches + other.stitches)
|
|
|
|
else:
|
|
|
|
raise TypeError("Patch can only be added to another Patch")
|
|
|
|
|
2018-06-05 00:19:37 +00:00
|
|
|
def __len__(self):
|
|
|
|
# This method allows `len(patch)` and `if patch:
|
|
|
|
return len(self.stitches)
|
|
|
|
|
2018-03-31 00:37:11 +00:00
|
|
|
def add_stitch(self, stitch):
|
|
|
|
self.stitches.append(stitch)
|
|
|
|
|
|
|
|
def reverse(self):
|
|
|
|
return Patch(self.color, self.stitches[::-1])
|
|
|
|
|
|
|
|
|
|
|
|
class Param(object):
|
|
|
|
def __init__(self, name, description, unit=None, values=[], type=None, group=None, inverse=False, default=None, tooltip=None, sort_index=0):
|
|
|
|
self.name = name
|
|
|
|
self.description = description
|
|
|
|
self.unit = unit
|
|
|
|
self.values = values or [""]
|
|
|
|
self.type = type
|
|
|
|
self.group = group
|
|
|
|
self.inverse = inverse
|
|
|
|
self.default = default
|
|
|
|
self.tooltip = tooltip
|
|
|
|
self.sort_index = sort_index
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return "Param(%s)" % vars(self)
|
|
|
|
|
|
|
|
|
|
|
|
# Decorate a member function or property with information about
|
|
|
|
# the embroidery parameter it corresponds to
|
|
|
|
def param(*args, **kwargs):
|
|
|
|
p = Param(*args, **kwargs)
|
|
|
|
|
|
|
|
def decorator(func):
|
|
|
|
func.param = p
|
|
|
|
return func
|
|
|
|
|
|
|
|
return decorator
|
|
|
|
|
|
|
|
|
|
|
|
class EmbroideryElement(object):
|
|
|
|
def __init__(self, node):
|
|
|
|
self.node = node
|
|
|
|
|
|
|
|
@property
|
|
|
|
def id(self):
|
|
|
|
return self.node.get('id')
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get_params(cls):
|
|
|
|
params = []
|
|
|
|
for attr in dir(cls):
|
|
|
|
prop = getattr(cls, attr)
|
|
|
|
if isinstance(prop, property):
|
|
|
|
# The 'param' attribute is set by the 'param' decorator defined above.
|
|
|
|
if hasattr(prop.fget, 'param'):
|
|
|
|
params.append(prop.fget.param)
|
|
|
|
|
|
|
|
return params
|
|
|
|
|
|
|
|
@cache
|
|
|
|
def get_param(self, param, default):
|
|
|
|
value = self.node.get("embroider_" + param, "").strip()
|
|
|
|
|
|
|
|
return value or default
|
|
|
|
|
|
|
|
@cache
|
|
|
|
def get_boolean_param(self, param, default=None):
|
|
|
|
value = self.get_param(param, default)
|
|
|
|
|
|
|
|
if isinstance(value, bool):
|
|
|
|
return value
|
|
|
|
else:
|
|
|
|
return value and (value.lower() in ('yes', 'y', 'true', 't', '1'))
|
|
|
|
|
|
|
|
@cache
|
|
|
|
def get_float_param(self, param, default=None):
|
|
|
|
try:
|
|
|
|
value = float(self.get_param(param, default))
|
|
|
|
except (TypeError, ValueError):
|
|
|
|
value = default
|
|
|
|
|
|
|
|
if value is None:
|
|
|
|
return value
|
|
|
|
|
|
|
|
if param.endswith('_mm'):
|
|
|
|
value = value * PIXELS_PER_MM
|
|
|
|
|
|
|
|
return value
|
|
|
|
|
|
|
|
@cache
|
|
|
|
def get_int_param(self, param, default=None):
|
|
|
|
try:
|
|
|
|
value = int(self.get_param(param, default))
|
|
|
|
except (TypeError, ValueError):
|
|
|
|
return default
|
|
|
|
|
|
|
|
if param.endswith('_mm'):
|
|
|
|
value = int(value * PIXELS_PER_MM)
|
|
|
|
|
|
|
|
return value
|
|
|
|
|
|
|
|
def set_param(self, name, value):
|
|
|
|
self.node.set("embroider_%s" % name, str(value))
|
|
|
|
|
|
|
|
@cache
|
2018-06-10 19:13:51 +00:00
|
|
|
def get_style(self, style_name, default=None):
|
2018-03-31 00:37:11 +00:00
|
|
|
style = simplestyle.parseStyle(self.node.get("style"))
|
|
|
|
if (style_name not in style):
|
2018-06-10 19:13:51 +00:00
|
|
|
return default
|
2018-03-31 00:37:11 +00:00
|
|
|
value = style[style_name]
|
|
|
|
if value == 'none':
|
|
|
|
return None
|
|
|
|
return value
|
|
|
|
|
|
|
|
@cache
|
|
|
|
def has_style(self, style_name):
|
|
|
|
style = simplestyle.parseStyle(self.node.get("style"))
|
|
|
|
return style_name in style
|
|
|
|
|
2018-05-02 01:21:07 +00:00
|
|
|
@property
|
|
|
|
@cache
|
|
|
|
def stroke_scale(self):
|
|
|
|
svg = self.node.getroottree().getroot()
|
|
|
|
doc_width, doc_height = get_doc_size(svg)
|
|
|
|
viewbox = svg.get('viewBox', '0 0 %s %s' % (doc_width, doc_height))
|
|
|
|
viewbox = viewbox.strip().replace(',', ' ').split()
|
|
|
|
return doc_width / float(viewbox[2])
|
|
|
|
|
2018-03-31 00:37:11 +00:00
|
|
|
@property
|
|
|
|
@cache
|
|
|
|
def stroke_width(self):
|
2018-06-10 19:13:51 +00:00
|
|
|
width = self.get_style("stroke-width", "1")
|
2018-03-31 00:37:11 +00:00
|
|
|
|
|
|
|
if width is None:
|
|
|
|
return 1.0
|
|
|
|
|
|
|
|
width = convert_length(width)
|
2018-05-02 01:21:07 +00:00
|
|
|
return width * self.stroke_scale
|
2018-03-31 00:37:11 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def path(self):
|
|
|
|
# A CSP is a "cubic superpath".
|
|
|
|
#
|
|
|
|
# A "path" is a sequence of strung-together bezier curves.
|
|
|
|
#
|
|
|
|
# A "superpath" is a collection of paths that are all in one object.
|
|
|
|
#
|
|
|
|
# The "cubic" bit in "cubic superpath" is because the bezier curves
|
|
|
|
# inkscape uses involve cubic polynomials.
|
|
|
|
#
|
|
|
|
# Each path is a collection of tuples, each of the form:
|
|
|
|
#
|
|
|
|
# (control_before, point, control_after)
|
|
|
|
#
|
|
|
|
# A bezier curve segment is defined by an endpoint, a control point,
|
|
|
|
# a second control point, and a final endpoint. A path is a bunch of
|
|
|
|
# bezier curves strung together. One could represent a path as a set
|
|
|
|
# of four-tuples, but there would be redundancy because the ending
|
|
|
|
# point of one bezier is the starting point of the next. Instead, a
|
|
|
|
# path is a set of 3-tuples as shown above, and one must construct
|
|
|
|
# each bezier curve by taking the appropriate endpoints and control
|
|
|
|
# points. Bleh. It should be noted that a straight segment is
|
|
|
|
# represented by having the control point on each end equal to that
|
|
|
|
# end's point.
|
|
|
|
#
|
|
|
|
# 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?
|
|
|
|
|
2018-06-21 19:41:06 +00:00
|
|
|
return cubicsuperpath.parsePath(self.node.get("d"))
|
2018-03-31 00:37:11 +00:00
|
|
|
|
2018-06-21 19:41:06 +00:00
|
|
|
@cache
|
|
|
|
def parse_path(self):
|
|
|
|
return apply_transforms(self.path, self.node)
|
2018-03-31 00:37:11 +00:00
|
|
|
|
2018-06-21 19:41:06 +00:00
|
|
|
@property
|
|
|
|
@cache
|
|
|
|
def commands(self):
|
|
|
|
return find_commands(self.node)
|
2018-03-31 00:37:11 +00:00
|
|
|
|
2018-06-23 02:19:57 +00:00
|
|
|
@cache
|
|
|
|
def get_commands(self, command):
|
|
|
|
return [c for c in self.commands if c.command == command]
|
|
|
|
|
2018-06-30 18:16:56 +00:00
|
|
|
@cache
|
|
|
|
def has_command(self, command):
|
|
|
|
return len(self.get_commands(command)) > 0
|
|
|
|
|
2018-06-23 02:19:57 +00:00
|
|
|
@cache
|
|
|
|
def get_command(self, command):
|
|
|
|
commands = self.get_commands(command)
|
|
|
|
|
|
|
|
if len(commands) == 1:
|
|
|
|
return commands[0]
|
|
|
|
elif len(commands) > 1:
|
|
|
|
raise ValueError(_("%(id)s has more than one command of type '%(command)s' linked to it") %
|
|
|
|
dict(id=self.node.get(id), command=command))
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
2018-04-03 02:11:57 +00:00
|
|
|
def strip_control_points(self, subpath):
|
|
|
|
return [point for control_before, point, control_after in subpath]
|
|
|
|
|
2018-03-31 00:37:11 +00:00
|
|
|
def flatten(self, path):
|
|
|
|
"""approximate a path containing beziers with a series of points"""
|
|
|
|
|
|
|
|
path = deepcopy(path)
|
|
|
|
cspsubdiv(path, 0.1)
|
|
|
|
|
2018-04-03 02:11:57 +00:00
|
|
|
return [self.strip_control_points(subpath) for subpath in path]
|
2018-03-31 00:37:11 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def trim_after(self):
|
|
|
|
return self.get_boolean_param('trim_after', False)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def stop_after(self):
|
|
|
|
return self.get_boolean_param('stop_after', False)
|
|
|
|
|
|
|
|
def to_patches(self, last_patch):
|
|
|
|
raise NotImplementedError("%s must implement to_patches()" % self.__class__.__name__)
|
|
|
|
|
|
|
|
def embroider(self, last_patch):
|
|
|
|
patches = self.to_patches(last_patch)
|
|
|
|
|
|
|
|
if patches:
|
2018-06-30 18:18:45 +00:00
|
|
|
patches[-1].trim_after = self.has_command("trim") or self.trim_after
|
|
|
|
patches[-1].stop_after = self.has_command("stop") or self.stop_after
|
2018-03-31 00:37:11 +00:00
|
|
|
|
|
|
|
return patches
|
|
|
|
|
|
|
|
def fatal(self, message):
|
|
|
|
print >> sys.stderr, "error:", message
|
|
|
|
sys.exit(1)
|