mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-05 21:42:59 -05:00
Strokes: projecting caps under Tip-aligned arrowheads are measured at the shortened ends
With Tip alignment the arrowhead shortens the line and the projecting cap is drawn at the shortened end, but reach_along put the cap's corners at the path's own end, so the bounds reached half a stroke width past the head's tip. The caps are now measured along the shortened line, aligned as the renderer aligns them, and a trim that leaves no length still counts the cap's dot. Follows #10994.
This commit is contained in:
1 parent
fd470699e0
commit
028454cd57
1 file changed
+63
-5
+63
-5
@@ -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<Item = (Point, Vec2, Vec2)> + '_ {
|
||||
@@ -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,29 @@ 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 closed || side <= 0.0 {
|
||||
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 +738,36 @@ 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:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rectangles_keep_exact_bounds_and_sharp_corners_spike_up_to_the_limit() {
|
||||
let sq = square();
|
||||
|
||||
Reference in new issue
Block a user