Merge pull request #8905 from dotCooCoo/fix/pathfinder-intersect-empty-operand

Pathfinder: an Intersect effect with a group that has no area at its precision draws nothing
This commit is contained in:
BFlat authored and GitHub committed 2026-11-21 10:17:57 +07:00
commit c176da5d0e
2 files changed
+62 -1

No files matched your search

+19 -1
View File
@@ -644,13 +644,19 @@ pub fn pathfinder_children(doc: Option<&Document>, group: &Node, hook: Option<Ou
// (style source, shape) per stack entry; keys index `sources`.
let mut sources: Vec<Node> = vec![];
let mut shapes: Vec<po::Shape> = vec![];
// Keys of the group operands whose art has no area left at the precision.
let mut collapsed: Vec<u64> = vec![];
for mut lv in all {
if shape_mode && lv.len() > 1 {
let parts: Vec<(PathData, FillRule)> = lv.iter().filter_map(geometry).collect();
let refs: Vec<(&PathData, FillRule)> = parts.iter().map(|(p, r)| (p, *r)).collect();
let key = sources.len() as u64;
if let Some(top) = lv.pop() {
shapes.push(po::Shape::new(po::unite_all_with(&refs, options.precision), FillRule::NonZero, key));
let united = po::unite_all_with(&refs, options.precision);
if united.is_empty() && !parts.is_empty() {
collapsed.push(key);
}
shapes.push(po::Shape::new(united, FillRule::NonZero, key));
sources.push(top);
}
} else {
@@ -662,6 +668,18 @@ pub fn pathfinder_children(doc: Option<&Document>, group: &Node, hook: Option<Ou
}
}
}
// An operand with no area still takes part: Intersect, and Minus Front or Minus Back when
// it is the shape the others are cut from, then leave nothing.
let is_collapsed = |s: &po::Shape| collapsed.contains(&s.key);
let nothing_left = match op {
PathfinderOp::Intersect => shapes.iter().any(is_collapsed),
PathfinderOp::MinusFront => shapes.first().is_some_and(is_collapsed),
PathfinderOp::MinusBack => shapes.last().is_some_and(is_collapsed),
_ => false,
};
if nothing_left {
return Some(vec![]);
}
shapes.retain(|s| !s.path.is_empty() && s.path.subpaths.iter().all(|sp| sp.anchors.iter().all(|a| a.p.x.is_finite() && a.p.y.is_finite())));
if shapes.is_empty() {
return Some(vec![]);
+43
View File
@@ -196,6 +196,49 @@ fn shape_modes_follow_the_pathfinder_panel() {
assert_eq!(div.len(), 3);
}
/// An Intersect effect draws nothing when a group member's art has no area left at the effect's
/// precision, and so do Minus Back when that group is in front and Subtract when it is at the
/// back. Add, and Subtract with the group in front, leave such a member out.
#[test]
fn intersect_with_a_group_that_collapses_at_its_precision_draws_nothing() {
let thin_pair = |width: f64| {
let bar = |id: u64, x: f64| {
let fill = Appearance::basic(Paint::solid(Color::rgb(0.0, 0.0, 1.0)), Paint::None, 0.0);
Arc::new(Node::path(NodeId(id), shapes::rectangle(Rect::new(x, 10.0, x + width, 30.0)), fill))
};
Node::group(NodeId(5), vec![bar(3, 10.0), bar(4, 50.0)])
};
let with_precision = |front: Node, effect: &str| {
let mut g = square_and(Some(front), effect);
g.appearance.effects[0].params = serde_json::json!({"precision": 1.0});
g
};
let int = results(&with_precision(thin_pair(0.5), "pathfinder.intersect"));
assert!(int.is_empty(), "{int:#?}");
// Bars wider than the precision keep their area.
let int = results(&with_precision(thin_pair(2.0), "pathfinder.intersect"));
let total: f64 = int.iter().map(|n| area(n)).sum();
assert!((total - 80.0).abs() < 1.0, "{total}: {int:#?}");
// Add keeps the square alone.
let add = results(&with_precision(thin_pair(0.5), "pathfinder.add"));
assert_eq!(add.len(), 1);
assert!((area(&add[0]) - 10_000.0).abs() < 1.0, "{add:#?}");
// Minus Back cuts the square from the empty group in front, and Subtract cuts nothing from
// the square.
let minus_back = results(&with_precision(thin_pair(0.5), "pathfinder.minusBack"));
assert!(minus_back.is_empty(), "{minus_back:#?}");
let subtract = results(&with_precision(thin_pair(0.5), "pathfinder.subtract"));
assert_eq!(subtract.len(), 1);
assert!((area(&subtract[0]) - 10_000.0).abs() < 1.0, "{subtract:#?}");
// With the group at the back, Subtract cuts the square from it and leaves nothing.
let mut back = with_precision(thin_pair(0.5), "pathfinder.subtract");
if let NodeKind::Group { children, .. } = &mut back.kind {
children.reverse();
}
let subtract = results(&back);
assert!(subtract.is_empty(), "{subtract:#?}");
}
/// Divide and Outline draw no piece of a member without fill or stroke, the same as when such
/// pieces are left out.
#[test]