2022-01-25 15:41:30 +00:00
|
|
|
# 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
|
|
|
|
|
2022-05-18 14:02:07 +00:00
|
|
|
from shapely import geometry as shgeo
|
2022-01-25 15:41:30 +00:00
|
|
|
|
2022-06-10 14:25:30 +00:00
|
|
|
import inkex
|
|
|
|
|
2022-05-18 14:02:07 +00:00
|
|
|
from .svg.tags import EMBROIDERABLE_TAGS
|
2022-01-25 15:41:30 +00:00
|
|
|
from .utils import cache, get_bundled_dir
|
|
|
|
|
2022-05-18 14:02:07 +00:00
|
|
|
MARKER = ['pattern', 'guide-line']
|
2022-01-25 15:41:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
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)]
|
2022-05-18 14:02:07 +00:00
|
|
|
style.append('marker-%s:url(#inkstitch-%s-marker)' % (position, marker))
|
2022-01-25 15:41:30 +00:00
|
|
|
node.set('style', ";".join(style))
|
2022-05-18 14:02:07 +00:00
|
|
|
|
|
|
|
|
2022-06-10 14:25:30 +00:00
|
|
|
def get_marker_elements(node, marker, get_fills=True, get_strokes=True, get_satins=False):
|
2022-05-18 14:02:07 +00:00
|
|
|
from .elements import EmbroideryElement
|
2022-06-21 17:59:26 +00:00
|
|
|
from .elements.fill_stitch import FillStitch
|
2022-06-10 14:25:30 +00:00
|
|
|
from .elements.satin_column import SatinColumn
|
2022-05-18 14:02:07 +00:00
|
|
|
from .elements.stroke import Stroke
|
|
|
|
|
|
|
|
fills = []
|
|
|
|
strokes = []
|
2022-06-10 14:25:30 +00:00
|
|
|
satins = []
|
2022-06-21 17:46:59 +00:00
|
|
|
# do not close marker-start:url(
|
|
|
|
# if the marker group has been copied and pasted in Inkscape it may have been duplicated with an updated id (e.g. -4)
|
|
|
|
xpath = "./parent::svg:g/*[contains(@style, 'marker-start:url(#inkstitch-%s-marker')]" % marker
|
2022-05-18 14:02:07 +00:00
|
|
|
markers = node.xpath(xpath, namespaces=inkex.NSS)
|
|
|
|
for marker in markers:
|
|
|
|
if marker.tag not in EMBROIDERABLE_TAGS:
|
|
|
|
continue
|
|
|
|
|
|
|
|
element = EmbroideryElement(marker)
|
|
|
|
fill = element.get_style('fill')
|
|
|
|
stroke = element.get_style('stroke')
|
|
|
|
|
|
|
|
if get_fills and fill is not None:
|
2022-06-21 17:59:26 +00:00
|
|
|
fill = FillStitch(marker).shape
|
|
|
|
fills.append(fill)
|
2022-05-18 14:02:07 +00:00
|
|
|
|
|
|
|
if get_strokes and stroke is not None:
|
|
|
|
stroke = Stroke(marker).paths
|
|
|
|
line_strings = [shgeo.LineString(path) for path in stroke]
|
|
|
|
strokes.append(shgeo.MultiLineString(line_strings))
|
|
|
|
|
2022-06-10 14:25:30 +00:00
|
|
|
if get_satins and stroke is not None:
|
|
|
|
satin = SatinColumn(marker)
|
|
|
|
if len(satin.rails) == 2:
|
|
|
|
satins.append(satin)
|
|
|
|
|
|
|
|
return {'fill': fills, 'stroke': strokes, 'satin': satins}
|
2022-05-18 14:02:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
def has_marker(node, marker=list()):
|
|
|
|
if not marker:
|
|
|
|
marker = MARKER
|
|
|
|
for m in marker:
|
|
|
|
style = node.get('style') or ''
|
2022-06-21 17:46:59 +00:00
|
|
|
if "marker-start:url(#inkstitch-%s-marker" % m in style:
|
2022-05-18 14:02:07 +00:00
|
|
|
return True
|
|
|
|
return False
|