From 159e20053c297b2cef6432799f6f1dcec7400c01 Mon Sep 17 00:00:00 2001 From: Taha-Mahmoodi <85902429+Taha-Mahmoodi@users.noreply.github.com> Date: Thu, 22 Oct 2026 20:43:46 +0430 Subject: [PATCH] Gradients: a dithered gradient without stops draws nothing, as an undithered one does Review follow-up (CodeRabbit): a dithered fill skipped set_paint's guard for a gradient without stops and sampled a transparent grid to draw nothing. It now takes the same no-fill path. --- crates/render/src/paint.rs | 6 +++--- crates/render/src/tests_dither.rs | 12 ++++++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/crates/render/src/paint.rs b/crates/render/src/paint.rs index eb339a973..4d0250acf 100644 --- a/crates/render/src/paint.rs +++ b/crates/render/src/paint.rs @@ -52,11 +52,11 @@ pub(crate) fn set_paint(ctx: &mut RenderContext, p: &Paint, bounds: Rect, f: &Fr } } -/// [`set_paint`] for a fill: a dithered gradient (dither applies to fills only) is drawn as -/// pixels with fine noise ([`crate::dither`]). +/// [`set_paint`] for a fill: a dithered gradient (dither applies to fills only) with stops is +/// drawn as pixels with fine noise ([`crate::dither`]). pub(crate) fn set_fill_paint(ctx: &mut RenderContext, p: &Paint, bounds: Rect, f: &Frame) -> bool { match p { - Paint::Gradient(g) if g.gradient.dither && g.gradient.kind != GradientKind::Freeform => { + Paint::Gradient(g) if g.gradient.dither && g.gradient.kind != GradientKind::Freeform && !g.gradient.stops.is_empty() => { crate::dither::set_dithered_paint(ctx, g, bounds, f.ink) } _ => set_paint(ctx, p, bounds, f), diff --git a/crates/render/src/tests_dither.rs b/crates/render/src/tests_dither.rs index a7290c531..8d6938039 100644 --- a/crates/render/src/tests_dither.rs +++ b/crates/render/src/tests_dither.rs @@ -121,3 +121,15 @@ fn a_large_dithered_box_mostly_off_screen_samples_only_what_shows() { // ...from the cells around it only, not the whole grid. assert!(bytes > 0 && bytes <= 512 * 512 * 4, "{bytes} bytes of grid for a 64-pixel view"); } + +#[test] +fn a_dithered_gradient_without_stops_draws_nothing_and_samples_nothing() { + let mut g = Gradient::new(GradientKind::Linear, Vec::new()); + g.dither = true; + let d = doc(Paint::Gradient(Box::new(GradientPaint::new(g))), Paint::None); + let mut r = None; + let bytes = dither::grid_bytes_after(|| r = Some(render(&d, Affine::IDENTITY))); + assert_eq!(bytes, 0, "no grid is sampled"); + let r = r.unwrap(); + assert!((0..H).all(|y| (0..W).all(|x| r.pixel(x, y) == [255, 255, 255, 255])), "nothing is drawn"); +}