inkstitch/lib/svg/clip.py

38 wiersze
1.2 KiB
Python
Czysty Zwykły widok Historia

2023-05-10 07:02:47 +00:00
# Authors: see git history
#
# Copyright (c) 2023 Authors
# Licensed under the GNU GPL version 3.0 or later. See the file LICENSE for details.
from shapely.geometry import MultiPolygon, Polygon
2024-11-12 18:07:24 +00:00
from shapely.validation import make_valid
2023-05-10 07:02:47 +00:00
from ..elements import EmbroideryElement
2024-11-12 18:07:24 +00:00
from ..utils import ensure_multi_polygon
from .tags import SVG_GROUP_TAG
2023-05-10 07:02:47 +00:00
def get_clip_path(node):
# get clip and apply node transform
2024-11-12 18:07:24 +00:00
clip = _clip_paths(node)
for group in node.iterancestors(SVG_GROUP_TAG):
group_clip = _clip_paths(group)
if clip and group_clip:
clip = clip.intersection(group_clip)
elif group_clip:
clip = group_clip
if clip:
return ensure_multi_polygon(clip)
def _clip_paths(node_or_group):
clip = node_or_group.clip
if clip is None:
return
transform = node_or_group.composed_transform()
2023-05-10 07:02:47 +00:00
clip.transform = transform
clip_element = EmbroideryElement(clip)
2024-05-13 14:49:31 +00:00
clip_paths = [path for path in clip_element.paths if len(path) > 3]
if clip_paths:
2024-11-12 18:07:24 +00:00
clip_paths.sort(key=lambda point_list: Polygon(point_list).area, reverse=True)
return make_valid(MultiPolygon([(clip_paths[0], clip_paths[1:])]))