diff --git a/crates/geom/src/hit.rs b/crates/geom/src/hit.rs index 0358acac90..fea760b548 100644 --- a/crates/geom/src/hit.rs +++ b/crates/geom/src/hit.rs @@ -118,6 +118,14 @@ pub fn stroke_contains(path: &BezPath, width: f64, tol: f64, p: Point) -> bool { /// filled shape doesn't touch it, so a marquee drawn over a background picks only what is on it /// (#1103). pub fn intersects_rect(path: &PathData, r: Rect) -> bool { + // The control box holds every anchor and segment, and the path keeps it measured, so a path + // whose control box lies wholly apart from the rect misses it without measuring its segments. + // The test is strict: a control box that only touches the rect, or has no area, goes on to the + // exact checks below. + let (Some(c), n) = (path.control_bounds(), r.abs()) else { return false }; + if c.x1 < n.x0 || n.x1 < c.x0 || c.y1 < n.y0 || n.y1 < c.y0 { + return false; + } let Some(b) = path.bounds() else { return false }; let degenerate = |x: Rect| x.width() == 0.0 || x.height() == 0.0; if b.intersect(r).area() <= 0.0 && !degenerate(b) && !degenerate(r) { @@ -238,6 +246,30 @@ mod tests { // zero-height marquee missing it assert!(!intersects_rect(&p, Rect::new(20.0, 5.0, 30.0, 5.0))); } + + /// A marquee that lies apart from a path's control box misses it without measuring the path's + /// segments. One that reaches the control box but not the curve is still judged on the curve. + #[test] + fn marquee_apart_from_the_control_box_measures_no_segment() { + let p = shapes::ellipse(Rect::new(0.0, 0.0, 10.0, 10.0)); + let measured = crate::path::measured_segments; + let before = measured(); + assert!(!intersects_rect(&p, Rect::new(20.0, 20.0, 30.0, 30.0))); + assert!(!intersects_rect(&p, Rect::new(-30.0, 4.0, -20.0, 6.0))); + assert_eq!(measured(), before, "a marquee apart from the control box measures nothing"); + // A curve whose handle reaches into the marquee while the curve stays below it. + use crate::path::{Anchor, SubPath}; + let arch = PathData::new(vec![SubPath::new( + vec![ + Anchor::with_handles(Point::new(0.0, 0.0), Point::new(0.0, 0.0), Point::new(0.0, 40.0)), + Anchor::with_handles(Point::new(10.0, 0.0), Point::new(10.0, 40.0), Point::new(10.0, 0.0)), + ], + false, + )]); + assert!(!intersects_rect(&arch, Rect::new(-1.0, 35.0, 11.0, 39.0)), "the curve peaks at y 30"); + assert!(measured() > before, "a marquee inside the control box measures the curve"); + assert!(intersects_rect(&arch, Rect::new(4.0, 25.0, 6.0, 35.0)), "a marquee across the peak"); + } } #[cfg(test)]