From f1ef1a4e39359ec3489df9bfbbf77ace3f42703a Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Mon, 30 Nov 2026 23:02:15 -0800 Subject: [PATCH] Render: a thin stroke takes the corners of its own subpath A corner in one subpath gave the stroke's join to the lines of a curve in another subpath that met at the same point, so a miter could be drawn there instead of a round join. The corners are now kept per subpath and each subpath of the outline reads only its own. Test: a curve's outline is the same with and without a second subpath that has a corner at the curve's start. --- crates/render/src/stroke.rs | 32 +++++++++++++++---------- crates/render/src/tests/thincoverage.rs | 21 ++++++++++++++++ 2 files changed, 40 insertions(+), 13 deletions(-) diff --git a/crates/render/src/stroke.rs b/crates/render/src/stroke.rs index eaf22f0809..93fe88a1ed 100644 --- a/crates/render/src/stroke.rs +++ b/crates/render/src/stroke.rs @@ -50,10 +50,10 @@ fn thin_line(line: &BezPath, st: &StrokeLayer, width: f64) -> Option { } /// The points where `line` has a corner: where two of its segments meet with tangents that are not -/// in line ([`SMOOTH`]), or where a segment has no direction there. Sorted by [`point_order`]. -/// [`draw_sub`] leaves these points where they are, so the lines a curve is drawn with have each -/// corner at the same point. -fn corners(line: &BezPath) -> Vec { +/// in line ([`SMOOTH`]), or where a segment has no direction there. One list for each subpath, in +/// order, each sorted by [`point_order`]. [`draw_sub`] leaves these points where they are, so the +/// lines a curve is drawn with have each corner at the same point, and draws one subpath for each. +fn corners(line: &BezPath) -> Vec> { let tangents = |seg: PathSeg| { let c = match seg { PathSeg::Line(l) => CubicBez::new(l.p0, l.p0, l.p1, l.p1), @@ -64,6 +64,7 @@ fn corners(line: &BezPath) -> Vec { let mut out = Vec::new(); // A span of `f64::INFINITY` gives every segment no steps: only the segments are wanted. for sub in subpaths(line.elements(), f64::INFINITY, 1.0).unwrap_or_default() { + let mut here = Vec::new(); let n = sub.segs.len(); let first = if sub.closed { 0 } else { 1 }; for i in first..n { @@ -74,11 +75,12 @@ fn corners(line: &BezPath) -> Vec { _ => false, }; if !smooth { - out.push(b.start()); + here.push(b.start()); } } + here.sort_by(point_order); + out.push(here); } - out.sort_by(point_order); out } @@ -90,19 +92,22 @@ fn point_order(a: &Point, b: &Point) -> std::cmp::Ordering { /// The outline of stroke `st` at `width` along `lines`, a path of lines only ([`thin_line`]), to fill /// with the non-zero rule. Each side of each line is offset by half the width. Where two lines meet /// at a small angle ([`THIN_SMOOTH`]) both sides go to where the offsets cross. Elsewhere the -/// stroke's join is drawn at the path's `corners` ([`corners`]) and a round join where the lines a +/// stroke's join is drawn at the corners of the same subpath of the path (`corners`, one list for +/// each subpath of `lines`, [`corners`]) and a round join where the lines a /// curve is drawn with meet. An open subpath ends in the stroke's caps, with round parts drawn as /// lines within `fine` of their arcs. `None` when a subpath has fewer than two distinct points or a /// point that isn't finite, or is closed and no wider or taller than the stroke: those take /// [`effects::stroke::line_outline`]. -fn thin_band(lines: &BezPath, corners: &[Point], st: &StrokeLayer, width: f64, fine: f64) -> Option { +fn thin_band(lines: &BezPath, corners: &[Vec], st: &StrokeLayer, width: f64, fine: f64) -> Option { let h = width / 2.0; // The angle between two points of an arc of radius `h` drawn as lines within `fine` of it. let step = 2.0 * (1.0 - (fine / h).min(1.0)).acos().max(1e-3); let mut out = BezPath::new(); let mut pts: Vec = Vec::new(); let mut closed = false; - let flush = |pts: &mut Vec, closed: bool, out: &mut BezPath| -> Option<()> { + // The subpath being read; `None` before the first move. + let mut sub: Option = None; + let flush = |pts: &mut Vec, closed: bool, sub: Option, out: &mut BezPath| -> Option<()> { if pts.is_empty() { return Some(()); } @@ -119,14 +124,15 @@ fn thin_band(lines: &BezPath, corners: &[Point], st: &StrokeLayer, width: f64, f return None; } } - band_sub(out, pts, corners, closed, st, h, step); + band_sub(out, pts, corners.get(sub?)?, closed, st, h, step); pts.clear(); Some(()) }; for el in lines.elements() { match *el { PathEl::MoveTo(p) => { - flush(&mut pts, closed, &mut out)?; + flush(&mut pts, closed, sub, &mut out)?; + sub = Some(sub.map_or(0, |s| s + 1)); closed = false; pts.push(p); } @@ -139,7 +145,7 @@ fn thin_band(lines: &BezPath, corners: &[Point], st: &StrokeLayer, width: f64, f PathEl::QuadTo(..) | PathEl::CurveTo(..) => return None, } } - flush(&mut pts, closed, &mut out)?; + flush(&mut pts, closed, sub, &mut out)?; Some(out) } @@ -147,7 +153,7 @@ fn thin_band(lines: &BezPath, corners: &[Point], st: &StrokeLayer, width: f64, f /// repeating the first when `closed`) to `out`: for an open subpath one contour, its left side, /// the end cap, its right side backward and the start cap; for a closed one its left side and its /// right side backward, as two contours. `h` is half the width and `step` the angle between the -/// points of a round join or cap. A sharp turn takes the stroke's join at one of the path's +/// points of a round join or cap. A sharp turn takes the stroke's join at one of this subpath's /// `corners` (sorted by [`point_order`]) and a round join elsewhere. fn band_sub(out: &mut BezPath, pts: &[Point], corners: &[Point], closed: bool, st: &StrokeLayer, h: f64, step: f64) { use vectorcraft_doc::{LineCap, LineJoin}; diff --git a/crates/render/src/tests/thincoverage.rs b/crates/render/src/tests/thincoverage.rs index 62197d4ec0..ab31619111 100644 --- a/crates/render/src/tests/thincoverage.rs +++ b/crates/render/src/tests/thincoverage.rs @@ -254,6 +254,27 @@ fn thin_strokes_along_long_handles_cover_the_curve_outline() { } } +/// A corner of one subpath does not change the join where the lines of another subpath's curve +/// meet at the same point: the first subpath's outline is the same with or without the second. +#[test] +fn thin_strokes_take_corners_from_their_own_subpath() { + let w = 0.5; + let mut st = StrokeLayer::new(Paint::solid(Color::BLACK), w); + st.join = LineJoin::Miter; + st.miter_limit = 10.0; + let mut alone = BezPath::new(); + alone.move_to((-1.0, 0.0)); + alone.line_to((0.0, 0.0)); + alone.curve_to((0.000001, 0.0), (-1.0, 0.5), (-2.0, 1.0)); + let mut both = alone.clone(); + both.move_to((-10.0, -10.0)); + both.line_to((0.0, 0.0)); + both.line_to((-10.0, 10.0)); + let one = raster_stroke_outline(&alone, &st, w, 0.25); + let two = raster_stroke_outline(&both, &st, w, 0.25); + assert_eq!(two.elements().get(..one.elements().len()), Some(one.elements()), "the second subpath changed the first one's outline"); +} + /// Independently integrate coverage in radial/angle bins of an original notched ring. Checking /// only the outline geometry misses raster flattening that cancels its two nearby contours. #[test]