From e0cdfd17044824bc82ac3de7592826c0237ebb8d Mon Sep 17 00:00:00 2001 From: Kaalleen Date: Mon, 13 Apr 2020 11:58:54 +0200 Subject: [PATCH 1/2] namespaced attributes --- lib/commands.py | 30 ++++++---- lib/elements/auto_fill.py | 4 +- lib/elements/element.py | 20 +++++-- lib/extensions/convert_to_satin.py | 9 +-- lib/extensions/remove_embroidery_settings.py | 8 +-- lib/stitches/auto_satin.py | 29 +++++----- lib/svg/rendering.py | 8 ++- lib/svg/tags.py | 59 +++++++++++++++++++- 8 files changed, 125 insertions(+), 42 deletions(-) diff --git a/lib/commands.py b/lib/commands.py index c7b8698d82..b92d79cff2 100644 --- a/lib/commands.py +++ b/lib/commands.py @@ -1,19 +1,22 @@ -from copy import deepcopy import os -from random import random import sys +from copy import deepcopy +from random import random + +from shapely import geometry as shgeo import cubicsuperpath import inkex -from shapely import geometry as shgeo import simpletransform -from .i18n import _, N_ -from .svg import apply_transforms, get_node_transform, get_correction_transform, get_document, generate_unique_id -from .svg.tags import SVG_DEFS_TAG, SVG_GROUP_TAG, SVG_PATH_TAG, SVG_USE_TAG, SVG_SYMBOL_TAG, \ - CONNECTION_START, CONNECTION_END, CONNECTOR_TYPE, XLINK_HREF, INKSCAPE_LABEL -from .utils import cache, get_bundled_dir, Point - +from .i18n import N_, _ +from .svg import (apply_transforms, generate_unique_id, + get_correction_transform, get_document, get_node_transform) +from .svg.tags import (CONNECTION_END, CONNECTION_START, CONNECTOR_TYPE, + INKSCAPE_LABEL, INKSTITCH_ATTRIBS, SVG_DEFS_TAG, + SVG_GROUP_TAG, SVG_PATH_TAG, SVG_SYMBOL_TAG, + SVG_USE_TAG, XLINK_HREF) +from .utils import Point, cache, get_bundled_dir COMMANDS = { # L10N command attached to an object @@ -346,7 +349,7 @@ def get_command_pos(element, index, total): def remove_legacy_param(element, command): if command == "trim" or command == "stop": # If they had the old "TRIM after" or "STOP after" attributes set, - # automatically delete them. THe new commands will do the same + # automatically delete them. The new commands will do the same # thing. # # If we didn't delete these here, then things would get confusing. @@ -359,6 +362,13 @@ def remove_legacy_param(element, command): if attribute in element.node.attrib: del element.node.attrib[attribute] + # Attributes have changed to be namespaced. + # Let's check for them as well, they might have automatically changed. + attribute = INKSTITCH_ATTRIBS["%s_after" % command] + + if attribute in element.node.attrib: + del element.node.attrib[attribute] + def add_commands(element, commands): document = get_document(element.node) diff --git a/lib/elements/auto_fill.py b/lib/elements/auto_fill.py index 04da328811..b574c8bf14 100644 --- a/lib/elements/auto_fill.py +++ b/lib/elements/auto_fill.py @@ -53,9 +53,9 @@ def running_stitch_length(self): return max(self.get_float_param("running_stitch_length_mm", 1.5), 0.01) @property - @param('fill_underlay', _('Underlay'), type='toggle', group=_('AutoFill Underlay'), default=False) + @param('fill_underlay', _('Underlay'), type='toggle', group=_('AutoFill Underlay'), default=True) def fill_underlay(self): - return self.get_boolean_param("fill_underlay", default=False) + return self.get_boolean_param("fill_underlay", default=True) @property @param('fill_underlay_angle', diff --git a/lib/elements/element.py b/lib/elements/element.py index 04f15b6e2b..5e1c82c5a1 100644 --- a/lib/elements/element.py +++ b/lib/elements/element.py @@ -8,7 +8,7 @@ from ..commands import find_commands from ..i18n import _ from ..svg import PIXELS_PER_MM, apply_transforms, convert_length, get_doc_size -from ..svg.tags import INKSCAPE_LABEL +from ..svg.tags import INKSCAPE_LABEL, INKSTITCH_ATTRIBS from ..utils import cache @@ -72,6 +72,10 @@ class EmbroideryElement(object): def __init__(self, node): self.node = node + for attrib in self.node.attrib: + if attrib.startswith('embroider_'): + self.replace_legacy_param(attrib) + @property def id(self): return self.node.get('id') @@ -85,13 +89,18 @@ def get_params(cls): # The 'param' attribute is set by the 'param' decorator defined above. if hasattr(prop.fget, 'param'): params.append(prop.fget.param) - return params + def replace_legacy_param(self, param): + value = self.node.get(param, "").strip() + self.set_param(param[10:], value) + del self.node.attrib[param] + # defaut setting has changed for fill_underlay + self.set_param('fill_underlay', False) + @cache def get_param(self, param, default): - value = self.node.get("embroider_" + param, "").strip() - + value = self.node.get(INKSTITCH_ATTRIBS[param], "").strip() return value or default @cache @@ -131,7 +140,8 @@ def get_int_param(self, param, default=None): return value def set_param(self, name, value): - self.node.set("embroider_%s" % name, str(value)) + param = INKSTITCH_ATTRIBS[name] + self.node.set(param, str(value)) @cache def get_style(self, style_name, default=None): diff --git a/lib/extensions/convert_to_satin.py b/lib/extensions/convert_to_satin.py index f392465995..e2b287dd03 100644 --- a/lib/extensions/convert_to_satin.py +++ b/lib/extensions/convert_to_satin.py @@ -1,17 +1,18 @@ import math from itertools import chain, groupby -import inkex import numpy from numpy import diff, setdiff1d, sign from shapely import geometry as shgeo -from .base import InkstitchExtension +import inkex + from ..elements import Stroke from ..i18n import _ from ..svg import PIXELS_PER_MM, get_correction_transform -from ..svg.tags import SVG_PATH_TAG +from ..svg.tags import INKSTITCH_ATTRIBS, SVG_PATH_TAG from ..utils import Point +from .base import InkstitchExtension class SelfIntersectionError(Exception): @@ -309,6 +310,6 @@ def satin_to_svg_node(self, rails, rungs, correction_transform, path_style): "style": path_style, "transform": correction_transform, "d": d, - "embroider_satin_column": "true", + INKSTITCH_ATTRIBS['satin_column']: "true", } ) diff --git a/lib/extensions/remove_embroidery_settings.py b/lib/extensions/remove_embroidery_settings.py index d87a216a97..d39c7e9445 100644 --- a/lib/extensions/remove_embroidery_settings.py +++ b/lib/extensions/remove_embroidery_settings.py @@ -30,11 +30,11 @@ def remove_params(self): if not self.selected: xpath = ".//svg:path" elements = self.find_elements(xpath) - self.remove_embroider_attributes(elements) + self.remove_inkstitch_attributes(elements) else: for node in self.selected: elements = self.get_selected_elements(node) - self.remove_embroider_attributes(elements) + self.remove_inkstitch_attributes(elements) def remove_commands(self): if not self.selected: @@ -83,8 +83,8 @@ def remove_elements(self, xpath): def remove_element(self, element): element.getparent().remove(element) - def remove_embroider_attributes(self, elements): + def remove_inkstitch_attributes(self, elements): for element in elements: for attrib in element.attrib: - if attrib.startswith('embroider_'): + if attrib.startswith(inkex.NSS['inkstitch'], 1): del element.attrib[attrib] diff --git a/lib/stitches/auto_satin.py b/lib/stitches/auto_satin.py index 4ce356ce3b..9edff53c7f 100644 --- a/lib/stitches/auto_satin.py +++ b/lib/stitches/auto_satin.py @@ -1,20 +1,23 @@ -from itertools import chain, izip import math +from itertools import chain, izip -import cubicsuperpath -import inkex +import networkx as nx from shapely import geometry as shgeo from shapely.geometry import Point as ShapelyPoint -import simplestyle -import networkx as nx +import cubicsuperpath +import inkex +import simplestyle from ..commands import add_commands -from ..elements import Stroke, SatinColumn +from ..elements import SatinColumn, Stroke from ..i18n import _ -from ..svg import PIXELS_PER_MM, line_strings_to_csp, get_correction_transform, generate_unique_id -from ..svg.tags import SVG_PATH_TAG, SVG_GROUP_TAG, INKSCAPE_LABEL -from ..utils import Point as InkstitchPoint, cut, cache +from ..svg import (PIXELS_PER_MM, generate_unique_id, get_correction_transform, + line_strings_to_csp) +from ..svg.tags import (INKSCAPE_LABEL, INKSTITCH_ATTRIBS, SVG_GROUP_TAG, + SVG_PATH_TAG) +from ..utils import Point as InkstitchPoint +from ..utils import cache, cut class SatinSegment(object): @@ -209,9 +212,9 @@ def __init__(self, path_or_stroke, original_element=None): self.original_element = original_element self.style = original_element.node.get('style', '') self.running_stitch_length = \ - original_element.node.get('embroider_running_stitch_length_mm', '') or \ - original_element.node.get('embroider_center_walk_underlay_stitch_length_mm', '') or \ - original_element.node.get('embroider_contour_underlay_stitch_length_mm', '') + original_element.node.get(INKSTITCH_ATTRIBS['running_stitch_length_mm'], '') or \ + original_element.node.get(INKSTITCH_ATTRIBS['center_walk_underlay_stitch_length_mm'], '') or \ + original_element.node.get(INKSTITCH_ATTRIBS['contour_underlay_stitch_length_mm'], '') def to_element(self): node = inkex.etree.Element(SVG_PATH_TAG) @@ -222,7 +225,7 @@ def to_element(self): style['stroke-dasharray'] = "0.5,0.5" style = simplestyle.formatStyle(style) node.set("style", style) - node.set("embroider_running_stitch_length_mm", self.running_stitch_length) + node.set(INKSTITCH_ATTRIBS['running_stitch_length_mm'], self.running_stitch_length) stroke = Stroke(node) diff --git a/lib/svg/rendering.py b/lib/svg/rendering.py index 2711f12a08..5860ceefcf 100644 --- a/lib/svg/rendering.py +++ b/lib/svg/rendering.py @@ -5,10 +5,11 @@ import simplestyle import simpletransform -from .tags import INKSCAPE_GROUPMODE, INKSCAPE_LABEL, SVG_DEFS_TAG, SVG_GROUP_TAG, SVG_PATH_TAG -from .units import PIXELS_PER_MM, get_viewbox_transform from ..i18n import _ from ..utils import Point, cache +from .tags import (INKSCAPE_GROUPMODE, INKSCAPE_LABEL, INKSTITCH_ATTRIBS, + SVG_DEFS_TAG, SVG_GROUP_TAG, SVG_PATH_TAG) +from .units import PIXELS_PER_MM, get_viewbox_transform # The stitch vector path looks like this: # _______ @@ -198,6 +199,7 @@ def color_block_to_paths(color_block, svg, destination, visual_commands): add_commands(Stroke(destination[-1]), ["trim"]) color = color_block.color.visible_on_white.to_hex_str() + path = inkex.etree.Element(SVG_PATH_TAG, { 'style': simplestyle.formatStyle({ 'stroke': color, @@ -206,7 +208,7 @@ def color_block_to_paths(color_block, svg, destination, visual_commands): }), 'd': "M" + " ".join(" ".join(str(coord) for coord in point) for point in point_list), 'transform': get_correction_transform(svg), - 'embroider_manual_stitch': 'true' + INKSTITCH_ATTRIBS['manual_stitch']: 'true' }) destination.append(path) diff --git a/lib/svg/tags.py b/lib/svg/tags.py index 55af113a3f..3e4445131d 100644 --- a/lib/svg/tags.py +++ b/lib/svg/tags.py @@ -1,5 +1,6 @@ import inkex + # This is used below and added to the document in ../extensions/base.py. inkex.NSS['inkstitch'] = 'http://inkstitch.org/namespace' @@ -13,15 +14,71 @@ SVG_SYMBOL_TAG = inkex.addNS('symbol', 'svg') SVG_USE_TAG = inkex.addNS('use', 'svg') +EMBROIDERABLE_TAGS = (SVG_PATH_TAG, SVG_POLYLINE_TAG) + INKSCAPE_LABEL = inkex.addNS('label', 'inkscape') INKSCAPE_GROUPMODE = inkex.addNS('groupmode', 'inkscape') CONNECTION_START = inkex.addNS('connection-start', 'inkscape') CONNECTION_END = inkex.addNS('connection-end', 'inkscape') CONNECTOR_TYPE = inkex.addNS('connector-type', 'inkscape') + XLINK_HREF = inkex.addNS('href', 'xlink') + SODIPODI_NAMEDVIEW = inkex.addNS('namedview', 'sodipodi') SODIPODI_GUIDE = inkex.addNS('guide', 'sodipodi') SODIPODI_ROLE = inkex.addNS('role', 'sodipodi') + INKSTITCH_LETTERING = inkex.addNS('lettering', 'inkstitch') -EMBROIDERABLE_TAGS = (SVG_PATH_TAG, SVG_POLYLINE_TAG) +INKSTITCH_ATTRIBS = {} +# Fill +inkstitch_attribs = [ + 'ties', + 'trim_after', + 'stop_after', + # fill + 'angle', + 'auto_fill', + 'expand_mm', + 'fill_underlay', + 'fill_underlay_angle', + 'fill_underlay_inset_mm', + 'fill_underlay_max_stitch_length_mm', + 'fill_underlay_row_spacing_mm', + 'fill_underlay_skip_last', + 'max_stitch_length_mm', + 'row_spacing_mm', + 'end_row_spacing_mm', + 'skip_last', + 'staggers', + 'underlay_underpath', + 'underpath', + 'flip', + 'expand_mm', + # stroke + 'manual_stitch', + 'bean_stitch_repeats', + 'repeats', + 'running_stitch_length_mm', + # satin column + 'satin_column', + 'satin_column', + 'running_stitch_length_mm', + 'center_walk_underlay', + 'center_walk_underlay_stitch_length_mm', + 'contour_underlay', + 'contour_underlay_stitch_length_mm', + 'contour_underlay_inset_mm', + 'zigzag_underlay', + 'zigzag_spacing_mm', + 'zigzag_underlay_inset_mm', + 'zigzag_underlay_spacing_mm', + 'e_stitch', + 'pull_compensation_mm', + 'stroke_first', + # Legacy + 'embroider_trim_after', + 'embroider_stop_after' + ] +for attrib in inkstitch_attribs: + INKSTITCH_ATTRIBS[attrib] = inkex.addNS(attrib, 'inkstitch') From 701f077a6293687dc85e8f46c2746415e6e20c78 Mon Sep 17 00:00:00 2001 From: Kaalleen Date: Mon, 13 Apr 2020 22:42:55 +0200 Subject: [PATCH 2/2] fill_underlay correction --- lib/elements/element.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/elements/element.py b/lib/elements/element.py index 5e1c82c5a1..65d239c371 100644 --- a/lib/elements/element.py +++ b/lib/elements/element.py @@ -72,9 +72,15 @@ class EmbroideryElement(object): def __init__(self, node): self.node = node + legacy_attribs = False for attrib in self.node.attrib: if attrib.startswith('embroider_'): + # update embroider_ attributes to namespaced attributes self.replace_legacy_param(attrib) + legacy_attribs = True + if legacy_attribs and not self.get_param('fill_underlay', ""): + # defaut setting for fill_underlay has changed + self.set_param('fill_underlay', False) @property def id(self): @@ -95,8 +101,6 @@ def replace_legacy_param(self, param): value = self.node.get(param, "").strip() self.set_param(param[10:], value) del self.node.attrib[param] - # defaut setting has changed for fill_underlay - self.set_param('fill_underlay', False) @cache def get_param(self, param, default):