From bcc2a5ed37224a0c580c2752c869b31b7b1e0546 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Tue, 17 Nov 2026 16:50:30 -0800 Subject: [PATCH] Pathfinder: warn when open subpaths cancel each other under the fill rule Make Compound Shape now also warns when a member's subpaths, all closed, enclose no area together: two open evenodd squares on top of each other, or an open evenodd square over the same closed square. Each subpath is still checked on its own, so a line beside a square still warns. A path with one subpath skips the combined check, and an area that can't be measured gives no warning. --- crates/engine/src/cmd/compoundshape.rs | 29 ++++++++++++++++------- crates/engine/src/tests/compound_shape.rs | 18 ++++++++++++-- 2 files changed, 36 insertions(+), 11 deletions(-) diff --git a/crates/engine/src/cmd/compoundshape.rs b/crates/engine/src/cmd/compoundshape.rs index 74257b528f..b1c610d8bd 100644 --- a/crates/engine/src/cmd/compoundshape.rs +++ b/crates/engine/src/cmd/compoundshape.rs @@ -8,7 +8,7 @@ use std::sync::Arc; use serde_json::{Value, json}; use vectorcraft_doc::{Document, Node, NodeId, NodeKind, ShapeMode}; -use vectorcraft_geom::{FillRule, PathData}; +use vectorcraft_geom::{FillRule, PathData, SubPath}; use vectorcraft_pathops as po; use vectorcraft_render::effects; @@ -195,9 +195,11 @@ fn make(s: &mut Session, p: &Value) -> Result { /// Member `n` holds an open path that adds no area to a compound shape: one of the paths it takes /// part as ([`effects::member_leaves`]), inside a group or a compound path too, has an open subpath -/// that encloses no area once closed (a straight line, or an evenodd contour traced twice). The area -/// is the one the path's fill rule gives, which also keeps a self-crossing path whose signed area -/// is zero (a bow tie) from the warning, as Divide measures it. +/// that encloses no area once closed (a straight line, or an evenodd contour traced twice), or it +/// has an open subpath and its subpaths, all closed, enclose no area together (open evenodd +/// subpaths that cancel one another or a closed subpath). The area is the one the path's fill rule +/// gives, which also keeps a self-crossing path whose signed area is zero (a bow tie) from the +/// warning, as Divide measures it. An area that can't be measured gives no warning. fn adds_no_area(d: &Document, n: &Node, hook: effects::OutlineHook) -> bool { effects::member_leaves(Some(d), n, Some(hook)).iter().any(|leaf| { let (paths, rule): (Vec<&PathData>, FillRule) = match &leaf.kind { @@ -205,13 +207,22 @@ fn adds_no_area(d: &Document, n: &Node, hook: effects::OutlineHook) -> bool { NodeKind::Path { path, rule, .. } => (vec![path], *rule), _ => return false, }; - paths.iter().flat_map(|p| &p.subpaths).filter(|sp| !sp.closed).any(|sp| { - let mut closed = sp.clone(); - closed.closed = true; + let subpaths: Vec<&SubPath> = paths.iter().flat_map(|p| &p.subpaths).collect(); + if subpaths.iter().all(|sp| sp.closed) { + return false; + } + let closed = |sp: &SubPath| { + let mut sp = sp.clone(); + sp.closed = true; + sp + }; + // An area that can't be measured counts as an area, so a failed measurement gives no warning. + let no_area = |p: PathData| po::try_area(&p, rule).is_ok_and(|a| a <= 0.0); + subpaths.iter().filter(|sp| !sp.closed).any(|sp| { // The signed-area test alone only holds for nonzero: evenodd can fill nothing where // the signed area is not zero (a contour traced twice). - !(rule == FillRule::NonZero && po::encloses_area(sp)) && po::area(&PathData::single(closed), rule) <= 0.0 - }) + !(rule == FillRule::NonZero && po::encloses_area(sp)) && no_area(PathData::single(closed(sp))) + }) || (subpaths.len() > 1 && no_area(PathData::new(subpaths.iter().map(|sp| closed(sp)).collect()))) }) } diff --git a/crates/engine/src/tests/compound_shape.rs b/crates/engine/src/tests/compound_shape.rs index 8127ea5bcb..0057af9253 100644 --- a/crates/engine/src/tests/compound_shape.rs +++ b/crates/engine/src/tests/compound_shape.rs @@ -702,7 +702,8 @@ fn a_locked_or_hidden_layer_keeps_the_isolated_compound_shape_from_clicks() { /// place in the stack, and its reply carries a warning that names Outline Stroke. The shape draws /// the area the other members make in that mode: the square alone in Add, or on top in Subtract, /// and nothing in Intersect or when the line is the backmost member in Subtract. A line in a group, -/// beside a closed path too, and an open evenodd square traced twice are the same. An open path that encloses an area once closed (a bow tie +/// beside a closed path too, an open evenodd square traced twice, and open evenodd subpaths that cancel one another or a closed +/// subpath of the same path are the same. An open path that encloses an area once closed (a bow tie /// whose lobes cancel in signed area included) and closed shapes alone give no warning (#7849). #[test] fn lines_that_add_no_area_are_reported() { @@ -724,6 +725,17 @@ fn lines_that_add_no_area_are_reported() { run(s, "path.setFillRule", json!({"rule": "evenOdd"})); id } + fn evenodd_of(s: &mut Session, d: &str) -> NodeId { + let id = NodeId(run(s, "path.create", json!({ "d": d }))["id"].as_u64().unwrap()); + run(s, "path.setFillRule", json!({"rule": "evenOdd"})); + id + } + fn two_open_squares_of(s: &mut Session) -> NodeId { + evenodd_of(s, "M20 20 L80 20 L80 80 L20 80 M20 20 L80 20 L80 80 L20 80") + } + fn open_over_closed_of(s: &mut Session) -> NodeId { + evenodd_of(s, "M20 20 L80 20 L80 80 L20 80 Z M20 20 L80 20 L80 80 L20 80") + } fn group_of(s: &mut Session, ids: &[NodeId]) -> NodeId { run(s, "select.set", json!({"ids": ids.iter().map(|i| i.0).collect::>()})); NodeId(run(s, "object.group", json!({}))["id"].as_u64().unwrap()) @@ -731,8 +743,10 @@ fn lines_that_add_no_area_are_reported() { type Build = fn(&mut Session) -> NodeId; // (case, mode, the other member, whether it goes behind the 100 pt square, warned, area drawn) type Case = (&'static str, &'static str, Build, bool, bool, Option); - let cases: [Case; 11] = [ + let cases: [Case; 13] = [ ("an open evenodd square traced twice", "add", twice_around_of, false, true, Some(10_000.0)), + ("two open evenodd squares on one another", "add", two_open_squares_of, false, true, Some(10_000.0)), + ("an open evenodd square on the same closed square", "add", open_over_closed_of, false, true, Some(10_000.0)), ("a line", "add", line_of, false, true, Some(10_000.0)), ("a line, subtracting", "subtract", line_of, false, true, Some(10_000.0)), ("a line, intersecting", "intersect", line_of, false, true, Some(0.0)),