Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Namespaced Attributes #657

Merged
merged 2 commits into from Apr 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 20 additions & 10 deletions 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
Expand Down Expand Up @@ -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.
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions lib/elements/auto_fill.py
Expand Up @@ -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',
Expand Down
24 changes: 19 additions & 5 deletions lib/elements/element.py
Expand Up @@ -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


Expand Down Expand Up @@ -72,6 +72,16 @@ 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):
return self.node.get('id')
Expand All @@ -85,13 +95,16 @@ 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]

@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
Expand Down Expand Up @@ -131,7 +144,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):
Expand Down
9 changes: 5 additions & 4 deletions 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):
Expand Down Expand Up @@ -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",
}
)
8 changes: 4 additions & 4 deletions lib/extensions/remove_embroidery_settings.py
Expand Up @@ -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:
Expand Down Expand Up @@ -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]
29 changes: 16 additions & 13 deletions 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):
Expand Down Expand Up @@ -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)
Expand All @@ -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)

Expand Down
8 changes: 5 additions & 3 deletions lib/svg/rendering.py
Expand Up @@ -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:
# _______
Expand Down Expand Up @@ -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,
Expand All @@ -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)

Expand Down
59 changes: 58 additions & 1 deletion 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'

Expand All @@ -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')