diff --git a/crates/render/src/stroke.rs b/crates/render/src/stroke.rs index 2e78d37ed2..7e286c5d45 100644 --- a/crates/render/src/stroke.rs +++ b/crates/render/src/stroke.rs @@ -397,9 +397,17 @@ fn params(c: &CubicBez, most: usize, chord: f64, pin_start: bool, pin_end: bool) } /// How far the curve `c` lies from the chord between parameters `a` and `b`, at its parameter -/// midway: the curve's point there minus the chord's middle. +/// midway: the part across the chord of the curve's point there minus the chord's middle. A chord +/// of no length keeps the whole difference. fn bulge(c: &CubicBez, a: f64, b: f64) -> Vec2 { - c.eval((a + b) / 2.0) - c.eval(a).midpoint(c.eval(b)) + let (p, q) = (c.eval(a), c.eval(b)); + let gap = c.eval((a + b) / 2.0) - p.midpoint(q); + let along = q - p; + let len2 = along.hypot2(); + if !(len2 > 0.0 && len2.is_finite()) { + return gap; + } + gap - along * (gap.dot(along) / len2) } /// A curve of a subpath drawn as lines: the curve, its line ends' parameters, whether its first and diff --git a/crates/render/src/tests/thincoverage.rs b/crates/render/src/tests/thincoverage.rs index 8770990034..967f3bf91c 100644 --- a/crates/render/src/tests/thincoverage.rs +++ b/crates/render/src/tests/thincoverage.rs @@ -91,6 +91,23 @@ fn thin_strokes_drawn_as_lines_keep_caps_joins_and_holes() { assert_eq!(o.winding(kurbo::Point::new(300.0, 300.0)), 0, "the hole"); } +/// A thin curve that runs out and back along almost the same line ends where the curve turns +/// back. The curve goes from (0, 0) to x = 75 and back; its outline reaches no further than half +/// the stroke's width past that. +#[test] +fn thin_strokes_drawn_as_lines_stop_where_a_curve_turns_back() { + let (w, tol, e) = (0.5, 0.25, 1e-9); + let mut path = BezPath::new(); + path.move_to((0.0, 0.0)); + path.curve_to((100.0, e / 3.0), (100.0, 2.0 * e / 3.0), (0.0, e)); + let st = StrokeLayer::new(Paint::solid(Color::BLACK), w); + let o = raster_stroke_outline(&path, &st, w, tol); + let inside = |x: f64| o.winding(kurbo::Point::new(x, 0.0)) != 0; + assert!(inside(10.0) && inside(70.0), "the band"); + let far = o.bounding_box().x1; + assert!(far <= 75.0 + w / 2.0 + 1e-6, "the outline reaches x = {far}"); +} + /// 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]