diff --git a/crates/doc/src/hit.rs b/crates/doc/src/hit.rs index 1a6ca42642..6cd009ca54 100644 --- a/crates/doc/src/hit.rs +++ b/crates/doc/src/hit.rs @@ -1563,6 +1563,15 @@ fn near_path_body(path: &PathData, p: Point, radius: f64, decoration_reach: f64) let decoration_sq = decoration_reach * decoration_reach; let mut near_decoration = false; for sp in &path.subpaths { + // A closed lone anchor has no segment, and a cap can draw a dot on it: the full check + // decides a click near it. + if sp.closed + && sp.segment_count() == 0 + && let Some(a) = sp.anchors.first() + && (a.p - p).hypot2() <= radius_sq.max(decoration_sq) + { + near_decoration = true; + } for i in 0..sp.segment_count() { let c = sp.segment(i); if decoration_reach > 0.0 && !near_decoration && ([c.p0, c.p3].iter().any(|q| (*q - p).hypot2() <= decoration_sq)) { diff --git a/crates/doc/src/reach.rs b/crates/doc/src/reach.rs index 9509690a59..f83b0e9329 100644 --- a/crates/doc/src/reach.rs +++ b/crates/doc/src/reach.rs @@ -356,13 +356,18 @@ impl StrokeLayer { /// wider side of a farther one can paint past the nearest. pub fn hits(&self, bp: &BezPath, rule: FillRule, p: Point, tol: f64) -> bool { let reach = self.reach() + tol; - if self.width.is_nan() || self.width <= 0.0 || !bp.bounding_box().inflate(reach, reach).contains(p) { + if self.width.is_nan() || self.width <= 0.0 { return false; } + let els = bp.elements(); + // The box of the segments leaves out a closed subpath of one anchor, which has none: away + // from the segments only the dot a cap draws for such a subpath can paint. + if !bp.bounding_box().inflate(reach, reach).contains(p) { + return self.draws_dots() && subpaths(bp).into_iter().any(|(r, closed)| self.dot_contains(bp, rule, (&els[r], closed), p, tol)); + } if head_ends(self, bp).into_iter().any(|(at, out, end)| self.head_contains(at, out, end, p, tol)) { return true; } - let els = bp.elements(); // Aligned strokes paint on one side of the path only. let on_side = |region: Option, d: f64| region.is_none_or(|inside| d <= tol || fill_contains(bp, rule, p) == inside); // Every dash gets the caps, as wide as the stroke where the dash ends: round ones reach as @@ -374,7 +379,13 @@ impl StrokeLayer { LineCap::Square => Some(std::f64::consts::SQRT_2), }; for (r, closed) in subpaths(bp) { - let segs = drawn(&els[r]); + let segs = drawn(&els[r.clone()]); + if segs.is_empty() { + if self.dot_contains(bp, rule, (&els[r], closed), p, tol) { + return true; + } + continue; + } let (half, region) = self.body(closed); let full = half * self.profile_max() * dash_caps.unwrap_or(1.0); // Past a butt cap or a bevel the stroke stops at the segments' corners. A width profile @@ -445,6 +456,32 @@ impl StrokeLayer { false } + /// Does this stroke draw a dot for a subpath that never leaves its first point? A round or a + /// projecting cap draws one on a stroke without a dash pattern or a width profile. + fn draws_dots(&self) -> bool { + self.cap != LineCap::Butt && self.profile.is_none() && !self.dash.as_ref().is_some_and(Dash::is_dashed) + } + + /// Is `p` within `tol` of the dot this stroke draws for the subpath `els` (`closed`: a closed + /// one) of `bp`, filled with `rule`, when every point of the subpath is its first point + /// ([`Self::draws_dots`])? A round cap draws a disc there and a projecting cap a square along + /// the x axis, as wide as the stroke's body ([`StrokeLayer::body`]). Inside and outside + /// strokes of a closed subpath paint on their own side of `bp` only. + fn dot_contains(&self, bp: &BezPath, rule: FillRule, (els, closed): (&[vectorcraft_geom::PathEl], bool), p: Point, tol: f64) -> bool { + if !self.draws_dots() || vectorcraft_geom::kurbo::segments(els.iter().copied()).any(|s| has_length(&s)) { + return false; + } + let Some(at) = els.first().and_then(vectorcraft_geom::PathEl::end_point) else { return false }; + let (half, region) = self.body(closed); + let v = p - at; + let d = v.hypot(); + let on_dot = match self.cap { + LineCap::Square => v.x.abs().max(v.y.abs()) <= half + tol, + _ => d <= half + tol, + }; + on_dot && region.is_none_or(|inside| d <= tol || fill_contains(bp, rule, p) == inside) + } + /// Does this stroke, where it stops at the corners of its segments ([`Self::stops_at_corners`]), /// paint `p`, which is within `side` (and `tol`) of segment `i` of the subpath `segs` /// (`closed`: a closed one) and nearest to it at `t`? It does when `p` is not past the end of @@ -1001,4 +1038,40 @@ mod tests { assert!(hit::hit_test(&d, Point::new(108.0, 150.0), opt).is_none(), "an unfilled interior"); assert_eq!(d.node(id).unwrap().visual_bounds(), Some(Rect::new(90.0, 90.0, 210.0, 210.0))); } + + /// A round cap draws a disc and a projecting cap a square on a subpath that never leaves its + /// first point, a closed lone anchor or a line to the same point: a click on it hits the path, + /// and one past it misses. A butt cap draws nothing there. + #[test] + fn clicks_hit_the_dot_of_a_lone_anchor() { + let at = Point::new(150.0, 120.0); + let mut lone = BezPath::new(); + lone.move_to(at); + lone.close_path(); + let mut zero = BezPath::new(); + zero.move_to(at); + zero.line_to(at); + let (on_edge, corner, past) = (Point::new(154.5, 120.0), Point::new(154.0, 124.0), Point::new(156.0, 120.0)); + for (name, bp) in [("closed lone anchor", &lone), ("zero-length line", &zero)] { + let round = stroke(10.0, |s| s.cap = LineCap::Round); + let square = stroke(10.0, |s| s.cap = LineCap::Square); + assert!(round.hits(bp, FillRule::NonZero, at, 0.1) && round.hits(bp, FillRule::NonZero, on_edge, 0.1), "{name}: round dot"); + assert!(!round.hits(bp, FillRule::NonZero, corner, 0.1) && !round.hits(bp, FillRule::NonZero, past, 0.1), "{name}: past the disc"); + assert!(square.hits(bp, FillRule::NonZero, corner, 0.1), "{name}: the square's corner"); + assert!(!square.hits(bp, FillRule::NonZero, past, 0.1), "{name}: past the square"); + assert!(!stroke(10.0, |_| {}).hits(bp, FillRule::NonZero, on_edge, 0.1), "{name}: a butt cap draws no dot"); + } + // In a document: a click on the dot of a closed lone anchor hits the path's stroke, and a + // marquee over the anchor selects it. + let mut d = Document::new(300.0, 300.0); + let (l, id) = (d.layers[0].id, d.alloc_id()); + let mut ap = Appearance::basic(Paint::None, Paint::solid(Color::BLACK), 10.0); + ap.stroke_mut().unwrap().cap = LineCap::Round; + d.insert(Some(l), 0, Node::path(id, PathData::from_bezpath(&lone), ap)).unwrap(); + let opt = hit::HitOptions { tol: 0.5, ..Default::default() }; + let h = hit::hit_test(&d, Point::new(153.5, 121.0), opt).expect("a click on the dot"); + assert_eq!((h.leaf, h.kind), (id, hit::HitKind::Stroke)); + assert!(hit::hit_test(&d, Point::new(157.0, 120.0), opt).is_none(), "past the dot"); + assert_eq!(hit::marquee(&d, Rect::new(140.0, 110.0, 160.0, 130.0), None, false), vec![id]); + } } diff --git a/crates/effects/src/stroke/tests_reach.rs b/crates/effects/src/stroke/tests_reach.rs index 5c66c9fe9d..c222e53734 100644 --- a/crates/effects/src/stroke/tests_reach.rs +++ b/crates/effects/src/stroke/tests_reach.rs @@ -38,6 +38,12 @@ fn check(bp: &BezPath, st: &StrokeLayer, what: &str) { let painted = region.bounding_box(); let bounds = st.bounds_on(bp, bp.bounding_box()); assert!(bounds.inflate(0.05, 0.05).contains_rect(painted), "{what}: paints {painted:?} outside {bounds:?}"); + check_clicks(bp, st, ®ion, what); +} + +/// Check that points of `region`, what `st` paints along `bp`, hit as [`check`] says. +fn check_clicks(bp: &BezPath, st: &StrokeLayer, region: &BezPath, what: &str) { + let painted = region.bounding_box(); let tol = if st.profile.is_some() { 1.0 } else { 0.25 }; // Off the outline's own coordinates, so no sample sits level with a vertex. let (mut x, mut checked) = (painted.x0 + 0.371, 0); @@ -45,7 +51,7 @@ fn check(bp: &BezPath, st: &StrokeLayer, what: &str) { let mut y = painted.y0 + 0.293; while y <= painted.y1 { let p = Point::new(x, y); - if fill_contains(®ion, FillRule::EvenOdd, p) { + if fill_contains(region, FillRule::EvenOdd, p) { assert!(st.hits(bp, FillRule::NonZero, p, tol), "{what}: painted {p:?} doesn't hit"); checked += 1; } @@ -140,6 +146,58 @@ fn custom_heads() -> Vec { vec![head("Wedge", &[(0.0, 0.0), (3.0, 2.0), (3.0, -1.0)]), head("Spindle", &[(-10.0, 0.0), (0.0, 2.0), (10.0, 0.0), (0.0, -2.0)])] } +/// Paths with a subpath that never leaves its first point, which a round or projecting cap draws +/// as a dot at (140, 130): a closed lone anchor, a line to the same point, open and closed, and a +/// curve with every handle on the point, alone and after a closed triangle. +fn dot_paths() -> Vec { + let at = Point::new(140.0, 130.0); + let mut lone = BezPath::new(); + lone.move_to(at); + lone.close_path(); + let mut line = BezPath::new(); + line.move_to(at); + line.line_to(at); + let mut closed_line = line.clone(); + closed_line.close_path(); + let mut curve = BezPath::new(); + curve.move_to(at); + curve.curve_to(at, at, at); + curve.close_path(); + let mut beside = paths()[2].clone(); + beside.extend(lone.iter()); + vec![lone, line, closed_line, curve, beside] +} + +/// A click on the dot a round or projecting cap draws for a subpath that never leaves its first +/// point hits the path, and where no dot is drawn (butt caps, dashes, width profiles, an inside +/// stroke of a closed subpath) a click beside the point misses. +#[test] +fn clicks_hit_the_dot_a_cap_draws_for_a_subpath_with_no_length() { + let beside = Point::new(142.0, 130.0); + let mut painted = 0; + for (pi, bp) in dot_paths().iter().enumerate() { + for align in [StrokeAlign::Center, StrokeAlign::Inside, StrokeAlign::Outside] { + for cap in [LineCap::Butt, LineCap::Round, LineCap::Square] { + for profile in [None, Some(WidthProfile::lens())] { + for dash in [None, Some(Dash { pattern: vec![7.0, 4.0], offset: 0.0, align_corners: false })] { + let mut st = StrokeLayer::new(Paint::solid(Color::BLACK), 6.0); + (st.align, st.cap, st.profile, st.dash) = (align, cap, profile.clone(), dash.clone()); + let what = format!("path {pi} {align:?} {cap:?} profile {} dashed {}", profile.is_some(), dash.is_some()); + let region = outline_region(&PathData::from_bezpath(bp), FillRule::NonZero, &st).to_bezpath(); + if !fill_contains(®ion, FillRule::EvenOdd, beside) { + assert!(!st.hits(bp, FillRule::NonZero, beside, 0.25), "{what}: a click beside the point hits, where no dot is drawn"); + continue; + } + painted += 1; + check_clicks(bp, &st, ®ion, &what); + } + } + } + } + } + assert_eq!(painted, 22, "dots drawn"); +} + #[test] fn bounds_and_clicks_cover_every_arrowhead() { let bp = &paths()[0];