diff --git a/crates/engine/src/cmd/pathops.rs b/crates/engine/src/cmd/pathops.rs index 5b7f22abd9..70de82fa65 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,94 @@ 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: their region is empty. +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 mut region = match vectorcraft_render::effects::clip_outline(first) { + Some((outline, rule)) => Clip { path: PathData::from_bezpath(&outline), rule }, + None => Clip { path: PathData::default(), rule: FillRule::NonZero }, + }; + if has_invalid_coordinates(®ion.path) { + return Err(EngineError::Other("paths contain invalid coordinates".into())); + } + if let Some(outer) = clip.filter(|_| !region.path.is_empty()) { + 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 @@ -591,6 +679,10 @@ fn run_pf(s: &mut Session, p: &Value, op: PathfinderOp) -> Result { } let shape_mode = matches!(op, PathfinderOp::Unite | PathfinderOp::MinusFront | PathfinderOp::Intersect | PathfinderOp::Exclude | PathfinderOp::MinusBack); + // An operand its clipping masks hide altogether is an empty shape for the operations whose + // result it changes (Intersect gives nothing, Minus Front loses its back object, Minus Back its + // front one, Crop its cropping shape); the others leave it out, which gives the same art. + let keeps_hidden = matches!(op, PathfinderOp::Intersect | PathfinderOp::MinusFront | PathfinderOp::MinusBack | PathfinderOp::Crop); let label = pf_label(op); let options = pf_options(s, p, &format!("object.pathfinder.{}", pf_id(op)))?; let ids = s.edit(label, |d, sel| { @@ -598,63 +690,95 @@ fn run_pf(s: &mut Session, p: &Value, op: PathfinderOp) -> Result { let mut styles: Vec