From 8c53f8ce8863fcd9ecf4df12d3644c8596fde061 Mon Sep 17 00:00:00 2001 From: Sven Lafebre Date: Mon, 16 Nov 2026 12:23:10 +0100 Subject: [PATCH 1/3] Fix #8093: Pathfinder takes a clip group's members where its mask shows them Pathfinder collected a clip group's members as if it were a plain group, so Divide cut the hidden art too and the result lost the mask. Each member is now cut to the group's clipping region first (nested clip groups by each region in turn): its area is intersected with the region, and Divide's cutting lines keep only their parts inside it. A member the mask hides gives no piece, a group among other objects takes the style of its top member that shows, and a selection the masks hide altogether is refused with a reason. This applies to every object.pathfinder.* operation, which also covers Merge on masked art (#3367). --- crates/engine/src/cmd/pathops.rs | 143 ++++++++++-- crates/engine/src/tests.rs | 1 + crates/engine/src/tests/pathfinder_clip.rs | 243 +++++++++++++++++++++ docs/target-app-parity.md | 1 + 4 files changed, 371 insertions(+), 17 deletions(-) create mode 100644 crates/engine/src/tests/pathfinder_clip.rs diff --git a/crates/engine/src/cmd/pathops.rs b/crates/engine/src/cmd/pathops.rs index 5b7f22abd9..75bb06bd0e 100644 --- a/crates/engine/src/cmd/pathops.rs +++ b/crates/engine/src/cmd/pathops.rs @@ -9,7 +9,7 @@ use serde_json::{Value, json}; use vectorcraft_color::BlendMode; use vectorcraft_doc::appearance::{AppearanceItem, StrokeLayer}; use vectorcraft_doc::{AnchorRef, Appearance, Document, Node, NodeId, NodeKind, SegmentRef, TextObject}; -use vectorcraft_geom::{FillRule, PathData}; +use vectorcraft_geom::{FillRule, PathData, SubPath}; use vectorcraft_pathops as po; use vectorcraft_pathops::{BoolOp, PathfinderOp}; @@ -352,6 +352,92 @@ pub(crate) fn pf_leaves(d: &Document, n: &Node, out: &mut Vec) { } } +/// The region a clip group's members show through: its clipping path's outline +/// ([`vectorcraft_render::effects::clip_outline`]), cut by the clip groups around it. +struct Clip { + path: PathData, + rule: FillRule, +} + +/// Does `path` have an anchor or handle that isn't a finite number? +fn has_invalid_coordinates(path: &PathData) -> bool { + path.subpaths.iter().flat_map(|sp| &sp.anchors).any(|a| [a.p, a.h_in, a.h_out].iter().any(|q| !(q.x.is_finite() && q.y.is_finite()))) +} + +/// A Pathfinder operation's leaf shape ([`pf_leaves`]) with the region its clip groups show it +/// through, `None` outside any clip group. +type ClippedLeaf = (Node, Option>); + +/// [`pf_leaves`], each with the region the clip groups it is in show it through (#8093). A clip +/// group's clipping path is not a leaf, and the members of one whose clipping path clips by nothing +/// are hidden, as on the canvas, and left out. +fn clipped_pf_leaves(d: &Document, n: &Node, clip: Option<&Arc>, precision: f64, out: &mut Vec) -> Result<()> { + let (NodeKind::Group { children, clip: clips } | NodeKind::Layer { children, clip: clips, .. }) = &n.kind else { + let mut lv = vec![]; + leaves(n, &mut lv); + out.extend(lv.into_iter().map(|l| (l, clip.cloned()))); + return Ok(()); + }; + let hook = |c: &Node| vectorcraft_render::effects::outline_object(Some(d), c); + if let Some(art) = vectorcraft_render::effects::compound_shape_art(Some(d), n, Some(&hook)) { + out.push((art, clip.cloned())); + return Ok(()); + } + if !*clips { + return children.iter().try_for_each(|c| clipped_pf_leaves(d, c, clip, precision, out)); + } + let Some((first, rest)) = children.split_first() else { return Ok(()) }; + let Some((outline, rule)) = vectorcraft_render::effects::clip_outline(first) else { return Ok(()) }; + let mut region = Clip { path: PathData::from_bezpath(&outline), rule }; + if has_invalid_coordinates(®ion.path) { + return Err(EngineError::Other("paths contain invalid coordinates".into())); + } + if let Some(outer) = clip { + let path = po::try_boolean(®ion.path, region.rule, &outer.path, outer.rule, BoolOp::Intersect, precision) + .map_err(|e| EngineError::Other(e.to_string()))?; + region = Clip { path, rule: FillRule::NonZero }; + } + let region = Arc::new(region); + rest.iter().try_for_each(|c| clipped_pf_leaves(d, c, Some(®ion), precision, out)) +} + +/// `path`, filled by `rule`, where `clip` shows it: its area intersected with the region, filled +/// non-zero, and with `lines` (Divide, whose open contours cut) its open contours' parts inside the +/// region, so their hidden parts cut nothing. +fn clipped(path: PathData, rule: FillRule, clip: &Clip, lines: bool, precision: f64) -> Result<(PathData, FillRule)> { + if has_invalid_coordinates(&path) { + // Left as it is for the operation to refuse ("paths contain invalid coordinates"). + return Ok((path, rule)); + } + let (Some(within), Some(bounds)) = (clip.path.control_bounds(), path.control_bounds()) else { return Ok((PathData::default(), rule)) }; + if within.intersect(bounds).is_zero_area() && !bounds.is_zero_area() { + // Wholly outside the region (a line's bounds may be flat, so only an area's are trusted). + return Ok((PathData::default(), rule)); + } + let (open, closed): (Vec, Vec) = + if lines { path.subpaths.into_iter().partition(|sp| !sp.closed) } else { (vec![], path.subpaths) }; + let mut subs = vec![]; + if !closed.is_empty() { + let area = po::try_boolean(&PathData::new(closed), rule, &clip.path, clip.rule, BoolOp::Intersect, precision) + .map_err(|e| EngineError::Other(e.to_string()))?; + subs.extend(area.subpaths); + } + if !open.is_empty() { + let open = PathData::new(open); + // Each segment is sampled at least every 1/16 of the region's shorter side (and 24 times), + // and the crossings found are refined: a part of the region narrower than that, which a + // line crosses between two samples, can be missed. + let step = (within.width().min(within.height()) / 16.0).max(precision); + let outline = clip.path.to_bezpath(); + let outside = |q| !vectorcraft_geom::hit::fill_contains(&outline, clip.rule, q); + match super::draw2::erase_outline(&open, step, &outside) { + Some(pieces) => subs.extend(pieces.into_iter().flat_map(|p| p.subpaths)), + None => subs.extend(open.subpaths), + } + } + Ok((PathData::new(subs), FillRule::NonZero)) +} + /// How many shapes a Pathfinder operation takes from `n` selected alone: its leaf shapes, however /// deep its groups nest (an expanded stroke is a group inside the group, #1635). Inside a compound /// shape, a blend, an envelope or another live object and a symbol instance count as one shape @@ -598,9 +684,9 @@ fn run_pf(s: &mut Session, p: &Value, op: PathfinderOp) -> Result { let mut styles: Vec