diff options
| author | Lex Neva <lexelby@users.noreply.github.com> | 2018-03-30 20:37:11 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2018-03-30 20:37:11 -0400 |
| commit | 084c5555f21a6e927e4de98fdb66c4c408626f58 (patch) | |
| tree | 91e2da4493f4dcc5243114d3f73296b4a0166543 /embroider_simulate.py | |
| parent | 1279b3ec47fa9c7eb6540e255b67389644fb0b38 (diff) | |
print through web browser (#127)
* spawn a web server and open a printable view of the design in the user's web browser
* configurable inclusion of client and operator views
* editable fields for color names, client, title, and purchase order number
* groundwork laid to save these parameters back into the SVG
* major refactor of codebase to support printing
* code is organized logically into modules
* added inkstitch logo and branding guidelines
* l10n text extraction now handled by babel
* removed legacy embroider_update extension
* partial fix for #125
Diffstat (limited to 'embroider_simulate.py')
| -rw-r--r-- | embroider_simulate.py | 83 |
1 files changed, 26 insertions, 57 deletions
diff --git a/embroider_simulate.py b/embroider_simulate.py index d1407249..c7c3f6bd 100644 --- a/embroider_simulate.py +++ b/embroider_simulate.py @@ -5,16 +5,18 @@ import wx import inkex import simplestyle import colorsys +from itertools import izip import inkstitch +from inkstitch.extensions import InkstitchExtension from inkstitch import PIXELS_PER_MM -from embroider import _, patches_to_stitches, get_elements, elements_to_patches +from inkstitch.stitch_plan import patches_to_stitch_plan +from inkstitch.svg import color_block_to_point_lists class EmbroiderySimulator(wx.Frame): def __init__(self, *args, **kwargs): - stitch_file = kwargs.pop('stitch_file', None) - patches = kwargs.pop('patches', None) + stitch_plan = kwargs.pop('stitch_plan', None) self.on_close_hook = kwargs.pop('on_close', None) self.frame_period = kwargs.pop('frame_period', 80) self.stitches_per_frame = kwargs.pop('stitches_per_frame', 1) @@ -32,7 +34,7 @@ class EmbroiderySimulator(wx.Frame): self.panel = wx.Panel(self, wx.ID_ANY) self.panel.SetFocus() - self.load(stitch_file, patches) + self.load(stitch_plan) if self.target_duration: self.adjust_speed(self.target_duration) @@ -54,13 +56,10 @@ class EmbroiderySimulator(wx.Frame): self.Bind(wx.EVT_CLOSE, self.on_close) - def load(self, stitch_file=None, patches=None): - if stitch_file: - self.mirror = True - self.segments = self._parse_stitch_file(stitch_file) - elif patches: + def load(self, stitch_plan=None): + if stitch_plan: self.mirror = False - self.segments = self._patches_to_segments(patches) + self.segments = self._stitch_plan_to_segments(stitch_plan) else: return @@ -114,55 +113,21 @@ class EmbroiderySimulator(wx.Frame): return string def color_to_pen(self, color): - # python colorsys module uses floats from 0 to 1.0 - color = [value / 255.0 for value in color] - - hls = list(colorsys.rgb_to_hls(*color)) - - # Our background is white. If the color is too close to white, then - # it won't be visible. Capping lightness should make colors visible - # without changing them too much. - hls[1] = min(hls[1], 0.85) - - color = colorsys.hls_to_rgb(*hls) - - # convert back to values in the range of 0-255 - color = [value * 255 for value in color] - - return wx.Pen(color) - - def _patches_to_segments(self, patches): - stitches = patches_to_stitches(patches) + return wx.Pen(color.visible_on_white.rgb) + def _stitch_plan_to_segments(self, stitch_plan): segments = [] - last_pos = None - last_color = None - pen = None - trimming = False + for color_block in stitch_plan: + pen = self.color_to_pen(color_block.color) - for stitch in stitches: - if stitch.trim: - trimming = True - last_pos = None - continue - - if trimming: - if stitch.jump: + for point_list in color_block_to_point_lists(color_block): + # if there's only one point, there's nothing to do, so skip + if len(point_list) < 2: continue - else: - trimming = False - - pos = (stitch.x, stitch.y) - - if stitch.color == last_color: - if last_pos: - segments.append(((last_pos, pos), pen)) - else: - pen = self.color_to_pen(simplestyle.parseColor(stitch.color)) - last_pos = pos - last_color = stitch.color + for start, end in izip(point_list[:-1], point_list[1:]): + segments.append(((start, end), pen)) return segments @@ -292,18 +257,22 @@ class EmbroiderySimulator(wx.Frame): except IndexError: self.timer.Stop() -class SimulateEffect(inkex.Effect): +class SimulateEffect(InkstitchExtension): def __init__(self): - inkex.Effect.__init__(self) + InkstitchExtension.__init__(self) self.OptionParser.add_option("-P", "--path", action="store", type="string", dest="path", default=".", help="Directory in which to store output file") def effect(self): - patches = elements_to_patches(get_elements(self)) + if not self.get_elements(): + return + + patches = self.elements_to_patches(self.elements) + stitch_plan = patches_to_stitch_plan(patches) app = wx.App() - frame = EmbroiderySimulator(None, -1, _("Embroidery Simulation"), wx.DefaultPosition, size=(1000, 1000), patches=patches) + frame = EmbroiderySimulator(None, -1, _("Embroidery Simulation"), wx.DefaultPosition, size=(1000, 1000), stitch_plan=stitch_plan) app.SetTopWindow(frame) frame.Show() wx.CallAfter(frame.go) |
