diff options
Diffstat (limited to 'lib/extensions')
| -rw-r--r-- | lib/extensions/__init__.py | 2 | ||||
| -rw-r--r-- | lib/extensions/base.py | 12 | ||||
| -rw-r--r-- | lib/extensions/input.py | 63 | ||||
| -rw-r--r-- | lib/extensions/lettering.py | 2 | ||||
| -rw-r--r-- | lib/extensions/letters_to_font.py | 81 | ||||
| -rw-r--r-- | lib/extensions/stitch_plan_preview.py | 62 | ||||
| -rw-r--r-- | lib/extensions/troubleshoot.py | 6 |
7 files changed, 162 insertions, 66 deletions
diff --git a/lib/extensions/__init__.py b/lib/extensions/__init__.py index 83a522f2..ec21b592 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 .letters_to_font import LettersToFont from .object_commands import ObjectCommands from .output import Output from .params import Params @@ -55,6 +56,7 @@ __all__ = extensions = [StitchPlanPreview, LetteringGenerateJson, LetteringRemoveKerning, LetteringCustomFontDir, + LettersToFont, Troubleshoot, RemoveEmbroiderySettings, Cleanup, diff --git a/lib/extensions/base.py b/lib/extensions/base.py index 828e3685..a065b5cc 100644 --- a/lib/extensions/base.py +++ b/lib/extensions/base.py @@ -10,6 +10,7 @@ from collections.abc import MutableMapping import inkex from lxml import etree +from lxml.etree import Comment from stringcase import snakecase from ..commands import is_command, layer_commands @@ -19,7 +20,8 @@ from ..i18n import _ from ..patterns import is_pattern from ..svg import generate_unique_id from ..svg.tags import (CONNECTOR_TYPE, EMBROIDERABLE_TAGS, INKSCAPE_GROUPMODE, - NOT_EMBROIDERABLE_TAGS, SVG_DEFS_TAG, SVG_GROUP_TAG) + NOT_EMBROIDERABLE_TAGS, SVG_CLIPPATH_TAG, SVG_DEFS_TAG, + SVG_GROUP_TAG, SVG_MASK_TAG) SVG_METADATA_TAG = inkex.addNS("metadata", "svg") @@ -129,6 +131,10 @@ class InkstitchExtension(inkex.Effect): def descendants(self, node, selected=False, troubleshoot=False): # noqa: C901 nodes = [] + + if node.tag == Comment: + return [] + element = EmbroideryElement(node) if element.has_command('ignore_object'): @@ -141,7 +147,9 @@ class InkstitchExtension(inkex.Effect): if (node.tag in EMBROIDERABLE_TAGS or node.tag == SVG_GROUP_TAG) and element.get_style('display', 'inline') is None: return [] - if node.tag == SVG_DEFS_TAG: + # defs, masks and clippaths can contain embroiderable elements + # but should never be rendered directly. + if node.tag in [SVG_DEFS_TAG, SVG_MASK_TAG, SVG_CLIPPATH_TAG]: return [] # command connectors with a fill color set, will glitch into the elements list diff --git a/lib/extensions/input.py b/lib/extensions/input.py index a8b8bee3..066b9003 100644 --- a/lib/extensions/input.py +++ b/lib/extensions/input.py @@ -3,70 +3,13 @@ # Copyright (c) 2010 Authors # Licensed under the GNU GPL version 3.0 or later. See the file LICENSE for details. -import os -import sys - -import inkex from lxml import etree -import pyembroidery - -from ..i18n import _ -from ..stitch_plan import StitchPlan -from ..svg import PIXELS_PER_MM, render_stitch_plan -from ..svg.tags import INKSCAPE_LABEL +from ..stitch_plan import generate_stitch_plan class Input(object): def run(self, args): embroidery_file = args[0] - self.validate_file_path(embroidery_file) - - pattern = pyembroidery.read(embroidery_file) - stitch_plan = StitchPlan() - color_block = None - - for raw_stitches, thread in pattern.get_as_colorblocks(): - color_block = stitch_plan.new_color_block(thread) - for x, y, command in raw_stitches: - if command == pyembroidery.STITCH: - color_block.add_stitch(x * PIXELS_PER_MM / 10.0, y * PIXELS_PER_MM / 10.0) - if len(color_block) > 0: - if command == pyembroidery.TRIM: - color_block.add_stitch(trim=True) - elif command == pyembroidery.STOP: - color_block.add_stitch(stop=True) - color_block = stitch_plan.new_color_block(thread) - - stitch_plan.delete_empty_color_blocks() - - if stitch_plan.last_color_block: - if stitch_plan.last_color_block.last_stitch: - if stitch_plan.last_color_block.last_stitch.stop: - # ending with a STOP command is redundant, so remove it - del stitch_plan.last_color_block[-1] - - extents = stitch_plan.extents - svg = inkex.SvgDocumentElement("svg", nsmap=inkex.NSS, attrib={ - "width": str(extents[0] * 2), - "height": str(extents[1] * 2), - "viewBox": "0 0 %s %s" % (extents[0] * 2, extents[1] * 2), - }) - render_stitch_plan(svg, stitch_plan) - - # rename the Stitch Plan layer so that it doesn't get overwritten by Embroider - layer = svg.find(".//*[@id='__inkstitch_stitch_plan__']") - layer.set(INKSCAPE_LABEL, os.path.basename(embroidery_file)) - layer.attrib.pop('id') - - # Shift the design so that its origin is at the center of the canvas - # Note: this is NOT the same as centering the design in the canvas! - layer.set('transform', 'translate(%s,%s)' % (extents[0], extents[1])) - - print(etree.tostring(svg).decode('utf-8')) - - def validate_file_path(self, path): - # Check if the file exists - if not os.path.isfile(path): - inkex.errormsg(_('File does not exist and cannot be opened. Please correct the file path and try again.\r%s') % path) - sys.exit(1) + stitch_plan = generate_stitch_plan(embroidery_file) + print(etree.tostring(stitch_plan).decode('utf-8')) diff --git a/lib/extensions/lettering.py b/lib/extensions/lettering.py index 312a47ce..35509eb6 100644 --- a/lib/extensions/lettering.py +++ b/lib/extensions/lettering.py @@ -174,7 +174,7 @@ class LetteringFrame(wx.Frame): image.Rescale(300, 20, quality=wx.IMAGE_QUALITY_HIGH) self.font_chooser.Append(font.marked_custom_font_name, wx.Bitmap(image)) else: - self.font_chooser.Append(font.name) + self.font_chooser.Append(font.marked_custom_font_name) def get_font_descriptions(self): return {font.name: font.description for font in self.fonts.values()} diff --git a/lib/extensions/letters_to_font.py b/lib/extensions/letters_to_font.py new file mode 100644 index 00000000..158d0d9f --- /dev/null +++ b/lib/extensions/letters_to_font.py @@ -0,0 +1,81 @@ +# 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 os +from pathlib import Path + +import inkex +from inkex import errormsg + +from ..commands import ensure_symbol +from ..i18n import _ +from ..stitch_plan import generate_stitch_plan +from ..svg import get_correction_transform +from ..svg.tags import INKSCAPE_GROUPMODE, INKSCAPE_LABEL, SVG_PATH_TAG +from .base import InkstitchExtension + + +class LettersToFont(InkstitchExtension): + ''' + This extension will create a json file to store a custom directory path for additional user fonts + ''' + def __init__(self, *args, **kwargs): + InkstitchExtension.__init__(self, *args, **kwargs) + self.arg_parser.add_argument("-d", "--font-dir", type=str, default="", dest="font_dir") + self.arg_parser.add_argument("-f", "--file-format", type=str, default="", dest="file_format") + self.arg_parser.add_argument("-c", "--import-commands", type=inkex.Boolean, default=False, dest="import_commands") + + def effect(self): + font_dir = self.options.font_dir + file_format = self.options.file_format + + if not os.path.isdir(font_dir): + errormsg(_("Font directory not found. Please specify an existing directory.")) + + glyphs = list(Path(font_dir).rglob(file_format)) + if not glyphs: + glyphs = list(Path(font_dir).rglob(file_format.lower())) + + document = self.document.getroot() + for glyph in glyphs: + letter = self.get_glyph_element(glyph) + label = "GlyphLayer-%s" % letter.get(INKSCAPE_LABEL, ' ').split('.')[0][-1] + group = inkex.Group(attrib={ + INKSCAPE_LABEL: label, + INKSCAPE_GROUPMODE: "layer", + "transform": get_correction_transform(document, child=True) + }) + + # remove color block groups if we import without commands + # there will only be one object per color block anyway + if not self.options.import_commands: + for element in letter.iter(SVG_PATH_TAG): + group.insert(0, element) + else: + group.insert(0, letter) + + document.insert(0, group) + group.set('style', 'display:none') + + # users may be confused if they get an empty document + # make last letter visible again + group.set('style', None) + + # In most cases trims are inserted with the imported letters. + # Let's make sure the trim symbol exists in the defs section + ensure_symbol(document, 'trim') + + self.insert_baseline(document) + + def get_glyph_element(self, glyph): + stitch_plan = generate_stitch_plan(str(glyph), self.options.import_commands) + # we received a stitch plan wrapped in an svg document, we only need the stitch_plan group + # this group carries the name of the file, so we can search for it. + stitch_plan = stitch_plan.xpath('.//*[@inkscape:label="%s"]' % os.path.basename(glyph), namespaces=inkex.NSS)[0] + stitch_plan.attrib.pop(INKSCAPE_GROUPMODE) + return stitch_plan + + def insert_baseline(self, document): + document.namedview.new_guide(position=0.0, name="baseline") diff --git a/lib/extensions/stitch_plan_preview.py b/lib/extensions/stitch_plan_preview.py index c50fa738..04168665 100644 --- a/lib/extensions/stitch_plan_preview.py +++ b/lib/extensions/stitch_plan_preview.py @@ -3,12 +3,23 @@ # Copyright (c) 2010 Authors # Licensed under the GNU GPL version 3.0 or later. See the file LICENSE for details. +from inkex import Boolean, Style +from lxml import etree + from ..stitch_plan import stitch_groups_to_stitch_plan from ..svg import render_stitch_plan +from ..svg.tags import (INKSCAPE_GROUPMODE, SVG_DEFS_TAG, SVG_GROUP_TAG, + SVG_PATH_TAG) from .base import InkstitchExtension class StitchPlanPreview(InkstitchExtension): + def __init__(self, *args, **kwargs): + InkstitchExtension.__init__(self, *args, **kwargs) + self.arg_parser.add_argument("-s", "--move-to-side", type=Boolean, default=True, dest="move_to_side") + self.arg_parser.add_argument("-v", "--layer-visibility", type=int, default=0, dest="layer_visibility") + self.arg_parser.add_argument("-n", "--needle-points", type=Boolean, default=False, dest="needle_points") + def effect(self): # delete old stitch plan svg = self.document.getroot() @@ -27,6 +38,53 @@ class StitchPlanPreview(InkstitchExtension): stitch_plan = stitch_groups_to_stitch_plan(patches, collapse_len=collapse_len) render_stitch_plan(svg, stitch_plan, realistic) - # translate stitch plan to the right side of the canvas + # apply options layer = svg.find(".//*[@id='__inkstitch_stitch_plan__']") - layer.set('transform', 'translate(%s)' % svg.get('viewBox', '0 0 800 0').split(' ')[2]) + + # update layer visibilty 0 = unchanged, 1 = hidden, 2 = lower opacity + if self.options.layer_visibility == 1: + self.hide_all_layers() + layer.set('style', None) + elif self.options.layer_visibility == 2: + for g in self.document.getroot().findall(SVG_GROUP_TAG): + style = g.specified_style() + # check groupmode and exclude stitch_plan layer + # exclude objects which are not displayed at all or already have opacity < 0.4 + if (g.get(INKSCAPE_GROUPMODE) == "layer" and not g == layer and + float(style.get('opacity', 1)) > 0.4 and not style.get('display', 'inline') == 'none'): + style += Style('opacity:0.4') + g.set("style", style) + + # translate stitch plan to the right side of the canvas + if self.options.move_to_side: + layer.set('transform', 'translate(%s)' % svg.get('viewBox', '0 0 800 0').split(' ')[2]) + else: + layer.set('transform', None) + + # display needle points + if self.options.needle_points: + markers = 'marker-mid:url(#inkstitch-needle-point);marker-start:url(#inkstitch-needle-point);marker-end:url(#inkstitch-needle-point)' + for element in layer.iterdescendants(SVG_PATH_TAG): + style = ';'.join([element.get('style'), markers]) + element.set('style', style) + self.ensure_marker() + + def ensure_marker(self): + xpath = ".//svg:marker[@id='inkstitch-needle-point']" + point_marker = self.document.getroot().xpath(xpath) + + if not point_marker: + # get or create def element + defs = self.document.find(SVG_DEFS_TAG) + if defs is None: + defs = etree.SubElement(self.document, SVG_DEFS_TAG) + + # insert marker + marker = """<marker + orient="auto" + id="inkstitch-needle-point"> + <circle + cx="0" cy="0" r="1.5" + style="fill:context-stroke;opacity:0.8;" /> + </marker>""" + defs.append(etree.fromstring(marker)) diff --git a/lib/extensions/troubleshoot.py b/lib/extensions/troubleshoot.py index bf7faf76..c4b386d9 100644 --- a/lib/extensions/troubleshoot.py +++ b/lib/extensions/troubleshoot.py @@ -130,7 +130,11 @@ class Troubleshoot(InkstitchExtension): def add_descriptions(self, problem_types): svg = self.document.getroot() - text_x = str(float(svg.get('viewBox', '0 0 800 0').split(' ')[2]) + 5.0) + + # We could use svg.viewport_width, but then we would need to do unit conversions, + # so let's stay with parsing the viewbox by ourselves + # viewbox values are either separated through white space or commas + text_x = str(float(svg.get('viewBox', '0 0 800 0').replace(",", " ").split()[2]) + 5.0) text_container = inkex.TextElement(attrib={ "x": text_x, |
