Fill Area: outline pieces on a clipping mask's edge count as shown

clip_to kept a piece of an unfilled outline only when its middle tested
inside the mask, and a point on the mask's own outline tests inside on
some sides and outside on others. A square clipped by the same square
kept two sides, so Fill Area found no area inside it.

A piece whose middle lies on the mask's outline now counts as shown when
the mask's filled area lies on one side of it. A piece just outside the
mask, and a piece on an outline of the mask that bounds no filled area,
stay hidden.
This commit is contained in:
dotCooCoo
2026-11-25 21:39:58 -08:00
parent 9375decc46
commit 013cf4dacb
3 changed files with 92 additions and 6 deletions
+23
View File
@@ -232,6 +232,29 @@ fn fill_area_takes_clipped_art_only_where_its_mask_shows_it() {
assert_eq!((undo_len(&s), stack(&s)), (undo, kids));
}
/// Parts of an outline that lie on the mask's edge show: a square without a fill, clipped by a mask
/// of the same square, keeps all four sides and bounds its whole area. A square just outside the
/// mask is hidden and bounds nothing.
#[test]
fn fill_area_keeps_outline_parts_on_the_mask_edge() {
let mut s = session();
let square = rect(&mut s, 0.0, 0.0, 100.0, 100.0);
s.execute("paint.setFill", &json!({"none": true})).unwrap();
clip_by(&mut s, &[square], 0.0, 0.0, 100.0, 100.0);
deselect(&mut s);
let r = s.execute("shapeBuilder.copyRegion", &json!({"point": [50, 50]})).unwrap();
assert!((area(&s, id_of(&r)) - 10000.0).abs() < 1.0, "{}", area(&s, id_of(&r)));
let mut s = session();
let square = rect(&mut s, -0.005, -0.005, 100.01, 100.01);
s.execute("paint.setFill", &json!({"none": true})).unwrap();
clip_by(&mut s, &[square], 0.0, 0.0, 100.0, 100.0);
deselect(&mut s);
let (undo, kids) = (undo_len(&s), stack(&s));
assert!(s.execute("shapeBuilder.copyRegion", &json!({"point": [50, 50]})).is_err());
assert_eq!((undo_len(&s), stack(&s)), (undo, kids));
}
/// In a clipping group inside another, clipped art shows only where both masks show it.
#[test]
fn fill_area_takes_art_in_nested_clipping_groups_only_where_every_mask_shows_it() {
+43 -6
View File
@@ -2,6 +2,7 @@
//! closed path of that area with the current fill and stroke (`shapeBuilder.copyRegion`). The
//! paths around the area stay as they are, and nothing needs to be selected.
use vectorcraft_geom::kurbo::ParamCurveDeriv;
use vectorcraft_geom::path::segment_bounds;
use vectorcraft_geom::{ParamCurve, PathSeg};
@@ -171,13 +172,39 @@ fn unmasked(doc: &Document, leaf: &Node, mut shape: po::Shape, masks: &mut Masks
Some(shape)
}
/// How far (pt) the middle of a piece of an outline may lie from a mask's outline and still count
/// as on the mask's edge, which [`clip_to`] keeps. It covers rounding only, so a piece just outside
/// the mask is still left out.
const ON_MASK: f64 = 1e-6;
/// How far (pt) to each side of a piece [`shows_through`] looks for the mask's filled area: past
/// [`ON_MASK`], and within a filled strip of the mask as narrow as this.
const MASK_SIDE: f64 = 1e-4;
/// Does the piece of an outline whose middle is `p`, running in direction `dir` there, show
/// through `mask` filled with `rule`? A piece off the mask's outline shows when the filled area
/// holds `p`. A piece on the outline ([`ON_MASK`]) shows when the filled area lies on one side of
/// it ([`MASK_SIDE`]), so every side of a square clipped by the same square shows, and an outline
/// of the mask that bounds no filled area (two copies of one outline under even-odd) hides it.
fn shows_through(mask: &PathData, rule: FillRule, p: Point, dir: vectorcraft_geom::Vec2) -> bool {
if !mask.nearest(p).is_some_and(|(.., d)| d <= ON_MASK) {
return path_contains(mask, rule, p);
}
let len = dir.hypot();
if !(len > 0.0 && len.is_finite()) {
return false;
}
let side = dir.turn_90() * (MASK_SIDE / len);
path_contains(mask, rule, p + side) || path_contains(mask, rule, p - side)
}
/// `shape` cut to `mask` (filled with `rule`). With `filled` (the path's fill draws), its closed
/// subpaths are cut to their overlap with the mask, whose edge then bounds the fill. Its other
/// subpaths, and every subpath of a path without a fill, are cut to their pieces inside the mask,
/// so a hidden part of an outline does not close an area. A subpath with more segments or
/// crossings than [`po::addons::pathtools::crossings_on`] takes is left out. None when nothing of
/// it is inside.
fn clip_to(shape: po::Shape, filled: bool, mask: &PathData, rule: FillRule) -> Option<po::Shape> {
/// subpaths, and every subpath of a path without a fill, are cut to their pieces inside the mask or
/// on its edge ([`shows_through`]), so a hidden part of an outline does not close an area. A
/// subpath with more segments or crossings than [`po::addons::pathtools::crossings_on`] takes is
/// left out. None when nothing of it is inside.
pub(super) fn clip_to(shape: po::Shape, filled: bool, mask: &PathData, rule: FillRule) -> Option<po::Shape> {
let (b, mb) = (shape.path.bounds()?, mask.bounds()?);
if !meet(&b, &mb) {
return None;
@@ -195,7 +222,17 @@ fn clip_to(shape: po::Shape, filled: bool, mask: &PathData, rule: FillRule) -> O
let (cut, at) = po::addons::pathtools::insert_at(sp, &places);
subs.extend(cut.cut_at(&at).into_iter().filter(|piece| {
let n = piece.segment_count();
n > 0 && path_contains(mask, rule, piece.segment(n / 2).eval(0.5))
n > 0 && {
let seg = piece.segment(n / 2);
// Where the tangent vanishes (a cusp at the middle), the curve runs along its second
// derivative there, and a straight segment with that degenerate form along its chord.
let d1 = seg.deriv();
let dir = [d1.eval(0.5).to_vec2(), d1.deriv().eval(0.5).to_vec2(), seg.p3 - seg.p0]
.into_iter()
.find(|v| v.hypot2() > 0.0)
.unwrap_or_default();
shows_through(mask, rule, seg.eval(0.5), dir)
}
}));
}
if subs.is_empty() {
+26
View File
@@ -3796,6 +3796,32 @@ fn fill_area_press_does_not_search_a_hover_position_it_passed() {
assert_eq!(super::fill_area::hover_searches_started() - started, 1, "only the search that was running");
}
/// An outline without a fill, cut to a clipping mask, keeps the pieces on the mask's edge: a square
/// clipped by the same square keeps all four sides. A square just outside the mask, and a square on
/// an outline of the mask that bounds no filled area (two copies of the square under even-odd),
/// keep nothing.
#[test]
fn fill_area_clip_keeps_outline_pieces_on_the_mask_edge() {
let unit = Rect::new(0.0, 0.0, 100.0, 100.0);
let clip =
|shape: PathData, mask: &PathData, rule: FillRule| super::fill_area::clip_to(po::Shape::new(shape, FillRule::NonZero, 1), false, mask, rule);
let kept = clip(shapes::rectangle(unit), &shapes::rectangle(unit), FillRule::NonZero).expect("the sides on the mask's edge");
assert_eq!(kept.path.subpaths.iter().map(|sp| sp.segment_count()).sum::<usize>(), 4, "{:?}", kept.path);
let outside = shapes::rectangle(Rect::new(-0.005, -0.005, 100.005, 100.005));
assert!(clip(outside, &shapes::rectangle(unit), FillRule::NonZero).is_none());
let mut twice = shapes::rectangle(unit);
let mut back = shapes::rectangle(unit).subpaths;
back[0].anchors.reverse();
twice.subpaths.extend(back);
assert!(clip(shapes::rectangle(unit), &twice, FillRule::EvenOdd).is_none());
// A top side drawn as a cubic whose tangent vanishes at its middle shows as well.
let a = |p: Point, h_in: Point, h_out: Point| vectorcraft_geom::Anchor { p, h_in, h_out, kind: vectorcraft_geom::AnchorKind::Corner };
let (o, x, xy, y) = (Point::new(0.0, 0.0), Point::new(100.0, 0.0), Point::new(100.0, 100.0), Point::new(0.0, 100.0));
let cusp = SubPath::new(vec![a(o, o, x), a(x, o, x), a(xy, xy, xy), a(y, y, y)], true);
let kept = clip(PathData::single(cusp), &shapes::rectangle(unit), FillRule::NonZero).expect("the sides on the mask's edge");
assert_eq!(kept.path.subpaths.iter().map(|sp| sp.segment_count()).sum::<usize>(), 4, "{:?}", kept.path);
}
#[test]
fn create_builds_the_three_tools() {
for id in ["shapeBuilder", "livePaintBucket", "livePaintSelection"] {