Skip to content

VectorCraft: An open-source, vector art tool written by scratch in pure Rust

Gradients: a dithered gradient without stops draws nothing, as an undithered one does

Taha-Mahmoodi159e20053c29
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");
+}