diff options
| author | Kaalleen <36401965+kaalleen@users.noreply.github.com> | 2021-12-09 15:05:21 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-12-09 15:05:21 +0100 |
| commit | 36f7610cc0844a034ba068b549332cab3ef7b033 (patch) | |
| tree | e63e66d8313df795d26efb42833f103f3554971a /lib/extensions | |
| parent | 41ace3a9e53b52b4271554d7aedd88d533ee5f5e (diff) | |
Force lock stitches option/extension and some typos (#1471)
Diffstat (limited to 'lib/extensions')
| -rw-r--r-- | lib/extensions/__init__.py | 2 | ||||
| -rw-r--r-- | lib/extensions/lettering_force_lock_stitches.py | 86 | ||||
| -rw-r--r-- | lib/extensions/reorder.py | 2 | ||||
| -rw-r--r-- | lib/extensions/stitch_plan_preview.py | 2 |
4 files changed, 90 insertions, 2 deletions
diff --git a/lib/extensions/__init__.py b/lib/extensions/__init__.py index ec21b592..2d344578 100644 --- a/lib/extensions/__init__.py +++ b/lib/extensions/__init__.py @@ -24,6 +24,7 @@ from .lettering import Lettering from .lettering_custom_font_dir import LetteringCustomFontDir from .lettering_generate_json import LetteringGenerateJson from .lettering_remove_kerning import LetteringRemoveKerning +from .lettering_force_lock_stitches import LetteringForceLockStitches from .letters_to_font import LettersToFont from .object_commands import ObjectCommands from .output import Output @@ -56,6 +57,7 @@ __all__ = extensions = [StitchPlanPreview, LetteringGenerateJson, LetteringRemoveKerning, LetteringCustomFontDir, + LetteringForceLockStitches, LettersToFont, Troubleshoot, RemoveEmbroiderySettings, diff --git a/lib/extensions/lettering_force_lock_stitches.py b/lib/extensions/lettering_force_lock_stitches.py new file mode 100644 index 00000000..62d7ae14 --- /dev/null +++ b/lib/extensions/lettering_force_lock_stitches.py @@ -0,0 +1,86 @@ +# Authors: see git history +# +# Copyright (c) 2021 Authors +# Licensed under the GNU GPL version 3.0 or later. See the file LICENSE for details. + +import inkex +from shapely.geometry import Point + +from ..i18n import _ +from ..svg import PIXELS_PER_MM +from ..svg.tags import INKSTITCH_ATTRIBS +from .base import InkstitchExtension + + +class LetteringForceLockStitches(InkstitchExtension): + ''' + This extension helps font creators to add the force lock stitches attribute to the last objects of each glyph + Font creators to add forced lock stitches on glyphs with accents / spaces. + ''' + + def __init__(self, *args, **kwargs): + InkstitchExtension.__init__(self, *args, **kwargs) + self.arg_parser.add_argument("-a", "--max_distance", type=float, default=3, dest="max_distance") + self.arg_parser.add_argument("-i", "--min_distance", type=float, default=1, dest="min_distance") + self.arg_parser.add_argument("-l", "--last_element", type=inkex.Boolean, dest="last_element") + + def effect(self): + if self.options.max_distance < self.options.min_distance: + inkex.errormssg(_("The maximum value is smaller than the minimum value.")) + + # Set glyph layers to be visible. We don't want them to be ignored by self.elements + self._update_layer_visibility('inline') + + # mark last elements of a glyph + xpath = ".//svg:g[@inkscape:groupmode='layer']//svg:path[last()]" + last_elements = self.document.xpath(xpath, namespaces=inkex.NSS) + for last_element in last_elements: + last_element.set('lastglyphelement', str(True)) + + # find last point of an element + if not self.get_elements(): + return + + previous_element = None + last_stitch = None + for element in self.elements: + stitch_group = element.to_stitch_groups(None) + # if the distance of the last stitch of the previous object to the first stitch of this objects + # lies within the user defined distance range, set the force_lock_stitches-attribute. + if last_stitch: + first_stitch = stitch_group[0].stitches[0] + first_stitch = Point(first_stitch.x, first_stitch.y) + self._set_force_attribute(first_stitch, last_stitch, previous_element) + + # if this is the last element of a glyph, we don't want to compare it to the next element + if element.node.get('lastglyphelement', False): + previous_element = None + last_stitch = None + else: + previous_element = element + last_stitch = stitch_group[-1].stitches[-1] + last_stitch = Point(last_stitch.x, last_stitch.y) + + # remove last element attributes again + # set force lock stitches attribute if needed + for last_element in last_elements: + last_element.attrib.pop('lastglyphelement') + if self.options.last_element: + last_element.set(INKSTITCH_ATTRIBS['force_lock_stitches'], True) + + # hide glyph layers again + self._update_layer_visibility('none') + + def _set_force_attribute(self, first_stitch, last_stitch, previous_element): + distance_mm = first_stitch.distance(last_stitch) / PIXELS_PER_MM + + if distance_mm < self.options.max_distance and distance_mm > self.options.min_distance: + previous_element.node.set(INKSTITCH_ATTRIBS['force_lock_stitches'], True) + + def _update_layer_visibility(self, display): + xpath = ".//svg:g[@inkscape:groupmode='layer']" + layers = self.document.xpath(xpath, namespaces=inkex.NSS) + for layer in layers: + display_style = 'display:%s' % display + style = inkex.Style(layer.get('style', '')) + inkex.Style(display_style) + layer.set('style', style) diff --git a/lib/extensions/reorder.py b/lib/extensions/reorder.py index 933c1d70..be478e39 100644 --- a/lib/extensions/reorder.py +++ b/lib/extensions/reorder.py @@ -7,7 +7,7 @@ from .base import InkstitchExtension class Reorder(InkstitchExtension): - # Remove selected objects from the document and readd them in the order they + # Remove selected objects from the document and re-add them in the order they # were selected. def effect(self): diff --git a/lib/extensions/stitch_plan_preview.py b/lib/extensions/stitch_plan_preview.py index 04168665..e5e570fb 100644 --- a/lib/extensions/stitch_plan_preview.py +++ b/lib/extensions/stitch_plan_preview.py @@ -41,7 +41,7 @@ class StitchPlanPreview(InkstitchExtension): # apply options layer = svg.find(".//*[@id='__inkstitch_stitch_plan__']") - # update layer visibilty 0 = unchanged, 1 = hidden, 2 = lower opacity + # update layer visibility 0 = unchanged, 1 = hidden, 2 = lower opacity if self.options.layer_visibility == 1: self.hide_all_layers() layer.set('style', None) |
