From 101ece647de43a5c41e541653ef6ef0144fb6d4f Mon Sep 17 00:00:00 2001 From: Terra854 Date: Fri, 27 Nov 2026 14:10:31 +0800 Subject: [PATCH] Brush: taper supersampled coverage at the cursor edge (#17161) --- crates/paint/src/brush_tests.rs | 15 +++++++++++++++ crates/paint/src/lib.rs | 7 +++++++ crates/paint/src/render.rs | 4 +++- 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/crates/paint/src/brush_tests.rs b/crates/paint/src/brush_tests.rs index a8ad8c6755..b986c87049 100644 --- a/crates/paint/src/brush_tests.rs +++ b/crates/paint/src/brush_tests.rs @@ -134,6 +134,21 @@ fn bounded_soft_tip_leaves_pixel_centres_outside_its_edge_untouched() { } } +#[test] +fn bounded_one_pixel_soft_tip_tapers_as_the_pixel_centre_reaches_the_edge() { + let brush = BrushSettings { hardness: 0.0, tip_boundary: Some(2.0), ..brush() }; + let coverage_at = |distance: f64| { + let dab = Dab::round(Point::new(50.5 - distance, 50.5), 0.5, 1.0); + at(&raster(&brush, &dab), 50, 50) + }; + + let [inside, near, edge, outside] = [0.75, 0.99, 1.0, 1.01].map(coverage_at); + assert!(inside > 0.05); + assert!(near > 0.0 && near < inside / 20.0, "coverage {near} close to the edge, {inside} further inside"); + assert_eq!(edge, 0.0); + assert_eq!(outside, 0.0); +} + #[test] fn sampled_tip_is_scaled_and_oriented() { // Tip: left half painted. diff --git a/crates/paint/src/lib.rs b/crates/paint/src/lib.rs index 967376c21a..a2c1a68aab 100644 --- a/crates/paint/src/lib.rs +++ b/crates/paint/src/lib.rs @@ -234,6 +234,13 @@ impl TipProfile { pub(crate) fn contains_pixel(self, distance_squared: f32) -> bool { !self.bounded || distance_squared < self.reach * self.reach } + + #[inline] + pub(crate) fn cap_supersampled_coverage(self, distance: f32, average: f32) -> f32 { + // Inward samples can still be inside the tip as the pixel centre reaches the boundary. + // The centre's falloff keeps their average from leaving a visible jump at the cutoff. + if self.bounded { average.min(self.falloff(distance)) } else { average } + } } /// `exp(-u)` as `(1 − u/64)⁶⁴` by six squarings: within 1/255 of `exp` where it matters, exact diff --git a/crates/paint/src/render.rs b/crates/paint/src/render.rs index 7354db4f83..327ee1684a 100644 --- a/crates/paint/src/render.rs +++ b/crates/paint/src/render.rs @@ -1650,7 +1650,9 @@ impl GeneralTip<'_> { sum += profile.falloff(sd); } } - (sum / (SUBPIXEL.len() * SUBPIXEL.len()) as f32, d2.sqrt() / rm) + let distance = d2.sqrt(); + let average = sum / (SUBPIXEL.len() * SUBPIXEL.len()) as f32; + (profile.cap_supersampled_coverage(distance, average), distance / rm) } None => { let d2 = (u * ro).powi(2) + v * v;