diff --git a/crates/doc/src/reach.rs b/crates/doc/src/reach.rs index 0403d26044..d08354f8d4 100644 --- a/crates/doc/src/reach.rs +++ b/crates/doc/src/reach.rs @@ -25,6 +25,16 @@ fn ends(segs: &[PathSeg]) -> Option<[(Point, Vec2); 2]> { Some([(first.start(), -tangent(first, 0.0)), (last.end(), tangent(last, 1.0))]) } +/// `out` grown to take in the corners of the projecting caps, reaching `side` past each end of the +/// open subpath `segs`. +fn square_corners(mut out: Rect, segs: &[PathSeg], side: f64) -> Rect { + for (at, dir) in ends(segs).into_iter().flatten() { + let n = Vec2::new(-dir.y, dir.x) * side; + out = out.union_pt(at + dir * side + n).union_pt(at + dir * side - n); + } + out +} + /// The joints between consecutive segments (and, `closed`, where the subpath closes): the point /// and the unit tangents in and out. fn joints(segs: &[PathSeg], closed: bool) -> impl Iterator + '_ { @@ -263,6 +273,9 @@ impl StrokeLayer { // box needed. let even_only = !subs.iter().any(|s| self.stops_at_corners(s.1)); let mut even = even_only.then_some(bounds); + // Tip-aligned arrowheads shorten the line, and its caps sit on the shortened ends. + let pieces = crate::arrow::stroke_pieces(bp, self); + let shortened = matches!(pieces.line, std::borrow::Cow::Owned(_)); for (r, closed) in subs { let side = self.side_reach(closed); let butt = self.cap == LineCap::Butt && !closed; @@ -309,14 +322,40 @@ impl StrokeLayer { } else if !even_only { even = vectorcraft_geom::union_opt(even, segs.iter().map(ParamCurveExtrema::bounding_box).reduce(|a, b| a.union(b))); } - if square { - for (at, dir) in ends(&segs).into_iter().flatten() { - let n = Vec2::new(-dir.y, dir.x) * side; - out = out.union_pt(at + dir * side + n).union_pt(at + dir * side - n); + if square && !shortened { + out = square_corners(out, &segs, side); + } + } + if shortened && self.cap == LineCap::Square { + let line = pieces.line.elements(); + // The renderer aligns the stroke along every subpath by whether the path ends closed. + let side = self.side_reach(matches!(bp.elements().last(), Some(vectorcraft_geom::PathEl::ClosePath))); + for (r, closed) in subpaths(&pieces.line) { + if side <= 0.0 { + continue; + } + if closed { + // A closed subpath with no length, a lone anchor included, paints the dot at the + // whole path's reach when the stroke draws dots. + if !self.draws_dots() || vectorcraft_geom::kurbo::segments(line[r.clone()].iter().copied()).any(|s| has_length(&s)) { + continue; + } + let lone = line[r.start].end_point().map(|p| Rect::from_points(p, p)); + let dot = segments(&line[r]).iter().map(ParamCurveExtrema::bounding_box).reduce(|a, b| a.union(b)).or(lone); + out = dot.map_or(out, |d| out.union(d.inflate(side, side))); + continue; + } + let segs = drawn(&line[r.clone()]); + if segs.is_empty() { + // A trim that leaves no length still paints the caps' dot. + let dot = segments(&line[r]).iter().map(ParamCurveExtrema::bounding_box).reduce(|a, b| a.union(b)); + out = dot.map_or(out, |d| out.union(d.inflate(side, side))); + } else { + out = square_corners(out, &segs, side); } } } - for head in crate::arrow::stroke_pieces(bp, self).heads { + for head in pieces.heads { if !head.outline.elements().is_empty() { out = out.union(head.outline.bounding_box()); } @@ -710,6 +749,58 @@ mod tests { assert_eq!(bounds.x1, b.x1, "the end has no head and a butt cap"); } + /// A Tip-aligned arrowhead shortens the line, and the projecting cap sits on the shortened end: + /// the box takes in that cap's corners, and not the corners a cap on the path's own end would have. + #[test] + fn projecting_caps_under_tip_aligned_arrowheads_sit_on_the_shortened_ends() { + let l = line(); + let b = l.bounding_box(); + let st = stroke(10.0, |s| { + s.cap = LineCap::Square; + s.join = LineJoin::Bevel; + s.end_arrow = Some(Arrowhead::Triangle); + s.arrow_align = ArrowAlign::Tip; + }); + let pieces = crate::arrow::stroke_pieces(&l, &st); + let segs = drawn(pieces.line.elements()); + let [_, (at, dir)] = ends(&segs).unwrap_or_else(|| panic!("the arrowhead covers the whole line")); + assert!(at.x < 95.0 && (dir.x - 1.0).abs() < 1e-9, "{at:?} {dir:?}"); + let bounds = st.bounds_on(&l, b); + let n = Vec2::new(-dir.y, dir.x) * 5.0; + for corner in [at + dir * 5.0 + n, at + dir * 5.0 - n] { + assert!(bounds.inflate(1e-9, 1e-9).contains(corner), "{corner:?} outside {bounds:?}"); + } + // Nothing is painted right of the head's tip at x = 100; a cap on the path's own end would + // reach to x = 105. + let head = pieces.heads.iter().map(|h| h.outline.bounding_box()).reduce(|a, h| a.union(h)); + assert_eq!(head.map(|h| h.x1), Some(100.0)); + assert_eq!(bounds.x1, 100.0, "{bounds:?}"); + // The start has no head, and its cap projects 5 in front of it. + assert_eq!(bounds.x0, -5.0, "{bounds:?}"); + } + + /// A closed lone anchor in a path that ends open is painted as a dot at the whole path's reach, + /// so the box takes it in when a Tip-aligned arrowhead moves the start cap off that anchor. + #[test] + fn closed_lone_anchors_keep_their_dot_under_tip_aligned_arrowheads() { + let mut p = BezPath::new(); + p.move_to((0.0, 0.0)); + p.line_to((100.0, 0.0)); + p.move_to((0.0, 0.0)); + p.close_path(); + p.move_to((110.0, 0.0)); + p.line_to((120.0, 0.0)); + let st = stroke(10.0, |s| { + s.cap = LineCap::Square; + s.join = LineJoin::Bevel; + s.align = StrokeAlign::Inside; + s.start_arrow = Some(Arrowhead::Triangle); + s.arrow_align = ArrowAlign::Tip; + }); + let bounds = st.bounds_on(&p, p.bounding_box()); + assert_eq!(bounds.x0, -5.0, "the lone anchor's square dot reaches 5 left of it: {bounds:?}"); + } + #[test] fn rectangles_keep_exact_bounds_and_sharp_corners_spike_up_to_the_limit() { let sq = square();