From be93c858a4b7bdd771521502c3c1195a01b32fbf Mon Sep 17 00:00:00 2001 From: Kaalleen <36401965+kaalleen@users.noreply.github.com> Date: Tue, 25 Jan 2022 16:41:30 +0100 Subject: markers and lettering (#1559) --- lib/extensions/selection_to_pattern.py | 43 +++------------------------------- lib/lettering/font.py | 9 +++++++ lib/marker.py | 37 +++++++++++++++++++++++++++++ symbols/marker.svg | 37 +++++++++++++++++++++++++++++ 4 files changed, 86 insertions(+), 40 deletions(-) create mode 100644 lib/marker.py create mode 100644 symbols/marker.svg diff --git a/lib/extensions/selection_to_pattern.py b/lib/extensions/selection_to_pattern.py index 41f89a83..b426e5db 100644 --- a/lib/extensions/selection_to_pattern.py +++ b/lib/extensions/selection_to_pattern.py @@ -4,10 +4,10 @@ # Licensed under the GNU GPL version 3.0 or later. See the file LICENSE for details. import inkex -from lxml import etree from ..i18n import _ -from ..svg.tags import EMBROIDERABLE_TAGS, SVG_DEFS_TAG +from ..marker import set_marker +from ..svg.tags import EMBROIDERABLE_TAGS from .base import InkstitchExtension @@ -23,41 +23,4 @@ class SelectionToPattern(InkstitchExtension): for pattern in self.get_nodes(): if pattern.tag in EMBROIDERABLE_TAGS: - self.set_marker(pattern) - - def set_marker(self, node): - xpath = ".//marker[@id='inkstitch-pattern-marker']" - pattern_marker = self.document.xpath(xpath) - - if not pattern_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 = """ - - - - - """ # noqa: E501 - defs.append(etree.fromstring(marker)) - - # attach marker to node - style = node.get('style') or '' - style = style.split(";") - style = [i for i in style if not i.startswith('marker-start')] - style.append('marker-start:url(#inkstitch-pattern-marker)') - node.set('style', ";".join(style)) + set_marker(pattern, 'start', 'pattern') diff --git a/lib/lettering/font.py b/lib/lettering/font.py index 09b772c3..f0aac11e 100644 --- a/lib/lettering/font.py +++ b/lib/lettering/font.py @@ -15,6 +15,7 @@ from ..elements import nodes_to_elements from ..exceptions import InkstitchException from ..extensions.lettering_custom_font_dir import get_custom_font_dir from ..i18n import _, get_languages +from ..marker import MARKER, ensure_marker from ..stitches.auto_satin import auto_satin from ..svg.tags import (CONNECTION_END, CONNECTION_START, INKSCAPE_LABEL, SVG_PATH_TAG, SVG_USE_TAG, XLINK_HREF) @@ -218,7 +219,9 @@ class Font(object): style += inkex.Style("stroke-width:0.5px") element.set('style', '%s' % style.to_str()) + # make sure necessary marker and command symbols are in the defs section self._ensure_command_symbols(destination_group) + self._ensure_marker_symbols(destination_group) return destination_group @@ -336,6 +339,12 @@ class Font(object): for command in commands: ensure_symbol(group.getroottree().getroot(), command) + def _ensure_marker_symbols(self, group): + for marker in MARKER: + xpath = ".//*[contains(@style, 'marker-start:url(#inkstitch-%s-marker)')]" % marker + if group.xpath(xpath, namespaces=inkex.NSS): + ensure_marker(group.getroottree().getroot(), marker) + def _apply_auto_satin(self, group, trim): """Apply Auto-Satin to an SVG XML node tree with an svg:g at its root. diff --git a/lib/marker.py b/lib/marker.py new file mode 100644 index 00000000..4f262abe --- /dev/null +++ b/lib/marker.py @@ -0,0 +1,37 @@ +# Authors: see git history +# +# Copyright (c) 2022 Authors +# Licensed under the GNU GPL version 3.0 or later. See the file LICENSE for details. + +from copy import deepcopy +from os import path + +import inkex + +from .utils import cache, get_bundled_dir + +MARKER = ['pattern'] + + +def ensure_marker(svg, marker): + marker_path = ".//*[@id='inkstitch-%s-marker']" % marker + if svg.defs.find(marker_path) is None: + svg.defs.append(deepcopy(_marker_svg().defs.find(marker_path))) + + +@cache +def _marker_svg(): + marker_path = path.join(get_bundled_dir("symbols"), "marker.svg") + with open(marker_path) as marker_file: + return inkex.load_svg(marker_file).getroot() + + +def set_marker(node, position, marker): + ensure_marker(node.getroottree().getroot(), marker) + + # attach marker to node + style = node.get('style') or '' + style = style.split(";") + style = [i for i in style if not i.startswith('marker-%s' % position)] + style.append('marker-%s:url(#inkstitch-pattern-marker)' % position) + node.set('style', ";".join(style)) diff --git a/symbols/marker.svg b/symbols/marker.svg new file mode 100644 index 00000000..b32327bc --- /dev/null +++ b/symbols/marker.svg @@ -0,0 +1,37 @@ + + + + + + + + + + + + + -- cgit v1.2.3