diff options
| author | Kaalleen <36401965+kaalleen@users.noreply.github.com> | 2024-11-12 19:07:24 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-11-12 19:07:24 +0100 |
| commit | 45dda2616d4d636759136e2c65998670ea06855c (patch) | |
| tree | 0dd33c7ab51cb69b0b6128df0095c5f84e876f2b /lib/svg | |
| parent | 7c99a138d12bcd5f9f4cdbfd3ff7c2b15ec890e4 (diff) | |
Clipped groups (#3261)
Diffstat (limited to 'lib/svg')
| -rw-r--r-- | lib/svg/clip.py | 26 |
1 files changed, 21 insertions, 5 deletions
diff --git a/lib/svg/clip.py b/lib/svg/clip.py index f2c77222..3fcffa4e 100644 --- a/lib/svg/clip.py +++ b/lib/svg/clip.py @@ -4,18 +4,34 @@ # Licensed under the GNU GPL version 3.0 or later. See the file LICENSE for details. from shapely.geometry import MultiPolygon, Polygon +from shapely.validation import make_valid from ..elements import EmbroideryElement +from ..utils import ensure_multi_polygon +from .tags import SVG_GROUP_TAG def get_clip_path(node): # get clip and apply node transform - clip = node.clip - transform = node.composed_transform() + 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() clip.transform = transform clip_element = EmbroideryElement(clip) clip_paths = [path for path in clip_element.paths if len(path) > 3] - clip_paths.sort(key=lambda point_list: Polygon(point_list).area, reverse=True) if clip_paths: - return MultiPolygon([(clip_paths[0], clip_paths[1:])]) - return MultiPolygon() + clip_paths.sort(key=lambda point_list: Polygon(point_list).area, reverse=True) + return make_valid(MultiPolygon([(clip_paths[0], clip_paths[1:])])) |
