From 0aaadb2914905205eaa486a761f2f7471248c31b Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Thu, 5 Nov 2026 07:01:23 -0800 Subject: [PATCH 1/7] Apply gradient tile rectangles to path fills --- crates/render/src/paint.rs | 10 ++++++---- crates/render/src/pathgrad.rs | 35 ++++++++++++++++++++++++++++++--- crates/render/src/tests_wild.rs | 25 +++++++++++++++++++++++ 3 files changed, 63 insertions(+), 7 deletions(-) diff --git a/crates/render/src/paint.rs b/crates/render/src/paint.rs index 74d111236..a0a4c2161 100644 --- a/crates/render/src/paint.rs +++ b/crates/render/src/paint.rs @@ -51,16 +51,18 @@ pub fn fill_path( // its edges); a radial one ends on the circle around it, from a focus placed in the // shape's own box (PowerPoint's "from corner" presets centre the tile on the corner). let tile = tile_box(bounds, g.tile); - let stops: Vec<(f64, Rgba)> = g.stops.iter().map(|s| (s.pos, shade(rctx.color(&s.color, ph), mode))).collect(); - ctx.set_paint(gradient(&g.shape, stops, bounds, alpha)); - let mut stops: Vec<(f64, Rgba)> = g.stops.iter().map(|s| (s.pos.clamp(0.0, 1.0), shade(rctx.color(&s.color, ph), mode))).collect(); stops.sort_by(|a, b| a.0.total_cmp(&b.0)); // Rectangle and outline paths: rasterised (see `pathgrad`). if let GradientShape::Path { path: kind, focus } = &g.shape && matches!(kind.as_str(), "rect" | "shape") { - crate::pathgrad::fill(ctx, kind, *focus, &crate::ramp::expand(&stops), bounds, path, m, alpha); + let ramp = crate::ramp::expand(&stops); + if g.tile.iter().all(|v| *v == 0.0) { + crate::pathgrad::fill(ctx, kind, *focus, &ramp, bounds, path, m, alpha); + } else { + crate::pathgrad::fill_tiled(ctx, kind, *focus, &ramp, bounds, tile, path, m, alpha); + } return; } // PowerPoint's ramp between stops (linear light, S-shaped), see `ramp`. diff --git a/crates/render/src/pathgrad.rs b/crates/render/src/pathgrad.rs index 168d9e0a3..27cce00c5 100644 --- a/crates/render/src/pathgrad.rs +++ b/crates/render/src/pathgrad.rs @@ -29,6 +29,10 @@ const MIN_POLY_EDGES: usize = 8; /// focus rect `focus` (fractions inset from left, top, right, bottom), `outline` being the /// shape's flattened polygon(s) for `shape`. pub fn position(kind: &str, bounds: Rect, focus: [f64; 4], outline: &[Vec], p: Point) -> f64 { + position_in_tile(kind, bounds, bounds, focus, outline, p) +} + +fn position_in_tile(kind: &str, bounds: Rect, tile: Rect, focus: [f64; 4], outline: &[Vec], p: Point) -> f64 { let f = Rect::new( bounds.x0 + bounds.width() * focus[0].clamp(0.0, 1.0), bounds.y0 + bounds.height() * focus[1].clamp(0.0, 1.0), @@ -74,7 +78,7 @@ pub fn position(kind: &str, bounds: Rect, focus: [f64; 4], outline: &[Vec 0.0 } }; - axis(p.x, f.x0, f.x1, bounds.x0, bounds.x1).max(axis(p.y, f.y0, f.y1, bounds.y0, bounds.y1)).clamp(0.0, 1.0) + axis(p.x, f.x0, f.x1, tile.x0, tile.x1).max(axis(p.y, f.y0, f.y1, tile.y0, tile.y1)).clamp(0.0, 1.0) } } } @@ -224,15 +228,32 @@ pub fn ramp(stops: &[(f64, Rgba)]) -> Vec { /// `ramp::expand`) over `bounds`, faded by `alpha`; `m` maps the path to the output. #[allow(clippy::too_many_arguments)] pub fn fill(ctx: &mut RenderContext, kind: &str, focus: [f64; 4], stops: &[(f64, Rgba)], bounds: Rect, path: &BezPath, m: Affine, alpha: f64) { + fill_tiled(ctx, kind, focus, stops, bounds, bounds, path, m, alpha); +} + +#[allow(clippy::too_many_arguments)] +pub fn fill_tiled( + ctx: &mut RenderContext, + kind: &str, + focus: [f64; 4], + stops: &[(f64, Rgba)], + bounds: Rect, + tile: Rect, + path: &BezPath, + m: Affine, + alpha: f64, +) { if bounds.width() <= 0.0 || bounds.height() <= 0.0 || !bounds.is_finite() || stops.is_empty() { return; } + let tile = if tile.is_finite() && tile.width() > 0.0 && tile.height() > 0.0 { tile } else { bounds }; + let untiled = tile == bounds; let k = crate::scale_of(m); let k = if k.is_finite() && k > 0.0 { k } else { 1.0 }; let longest = bounds.width().max(bounds.height()) * k; let s = (MAX_SIDE / longest).min(1.0) * k; let (pw, ph) = (((bounds.width() * s).ceil() as u32).clamp(1, 4096), ((bounds.height() * s).ceil() as u32).clamp(1, 4096)); - let outline = if kind == "shape" { + let mut outline = if kind == "shape" { let pixels = u64::from(pw).saturating_mul(u64::from(ph)).max(1); let max_edges = usize::try_from(MAX_EDGE_WORK / pixels).unwrap_or(usize::MAX).max(MIN_POLY_EDGES); let pixel = (bounds.width() / f64::from(pw)).min(bounds.height() / f64::from(ph)); @@ -240,13 +261,21 @@ pub fn fill(ctx: &mut RenderContext, kind: &str, focus: [f64; 4], stops: &[(f64, } else { Vec::new() }; + if !untiled { + for poly in &mut outline { + for p in poly { + p.x = tile.x0 + (p.x - bounds.x0) * tile.width() / bounds.width(); + p.y = tile.y0 + (p.y - bounds.y0) * tile.height() / bounds.height(); + } + } + } let lut = ramp(stops); let a = alpha.clamp(0.0, 1.0); let mut data = Vec::with_capacity((pw * ph) as usize); for j in 0..ph { for i in 0..pw { let p = Point::new(bounds.x0 + (i as f64 + 0.5) / pw as f64 * bounds.width(), bounds.y0 + (j as f64 + 0.5) / ph as f64 * bounds.height()); - let t = position(kind, bounds, focus, &outline, p); + let t = if untiled { position(kind, bounds, focus, &outline, p) } else { position_in_tile(kind, bounds, tile, focus, &outline, p) }; let c = lut.get((t * 255.0).round().clamp(0.0, 255.0) as usize).copied().unwrap_or(Rgba::BLACK); let ca = (c.a as f64 * a).round().clamp(0.0, 255.0); let mul = |v: u8| (v as f64 * ca / 255.0).round().clamp(0.0, 255.0) as u8; diff --git a/crates/render/src/tests_wild.rs b/crates/render/src/tests_wild.rs index 2da29cb37..03587c5ec 100644 --- a/crates/render/src/tests_wild.rs +++ b/crates/render/src/tests_wild.rs @@ -397,6 +397,31 @@ fn a_linear_gradient_spans_its_tile_rect() { assert!(img.pixel(305, 150)[0] < 15 && img.pixel(495, 150)[0] < 15, "{:?}", img.pixel(305, 150)); } +#[test] +fn a_rectangular_gradient_spans_its_tile_rect() { + let mut sh = radial([0.5; 4]); + if let Some(Fill::Gradient(g)) = &mut sh.fill { + g.shape = deckcraft_model::style::GradientShape::Path { path: "rect".into(), focus: [0.5; 4] }; + g.tile = [0.25, 0.0, 0.25, 0.0]; + } + let img = render(vec![sh]); + assert!(img.pixel(300, 200)[0] > 240, "center {:?}", img.pixel(300, 200)); + assert!(img.pixel(395, 200)[0] < 80, "tile edge {:?}", img.pixel(395, 200)); +} + +#[test] +fn a_shape_gradient_spans_its_tile_rect() { + let mut sh = radial([0.5; 4]); + sh.geom = Geom::Preset { name: "ellipse".into(), adj: vec![] }; + if let Some(Fill::Gradient(g)) = &mut sh.fill { + g.shape = deckcraft_model::style::GradientShape::Path { path: "shape".into(), focus: [0.5; 4] }; + g.tile = [0.25, 0.0, 0.25, 0.0]; + } + let img = render(vec![sh]); + assert!(img.pixel(300, 200)[0] > 240, "center {:?}", img.pixel(300, 200)); + assert!(img.pixel(395, 200)[0] < 80, "tile edge {:?}", img.pixel(395, 200)); +} + #[test] fn a_3d_pictures_border_stays_centred_on_its_edge() { let mut p = picture_deck(Some(Line::solid(ColorRef::rgb(RED), 8.0))); From 926b7a863357a572150719463bba1c3d925998a9 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Thu, 5 Nov 2026 07:44:21 -0800 Subject: [PATCH 2/7] Repeat tiled path gradients --- crates/render/src/paint.rs | 4 +-- crates/render/src/pathgrad.rs | 43 +++++++++++++++++++++++++++------ crates/render/src/tests_wild.rs | 33 ++++++++++++++++++++++++- 3 files changed, 69 insertions(+), 11 deletions(-) diff --git a/crates/render/src/paint.rs b/crates/render/src/paint.rs index a0a4c2161..e494f1f75 100644 --- a/crates/render/src/paint.rs +++ b/crates/render/src/paint.rs @@ -3,7 +3,7 @@ use deckcraft_color::Rgba; use deckcraft_geom::preset::FillMode; use deckcraft_model::resolve::Ctx; -use deckcraft_model::style::{ColorRef, Fill, GradientShape, PictureFill, PictureMode}; +use deckcraft_model::style::{ColorRef, Fill, GradientFlip, GradientShape, PictureFill, PictureMode}; use deckcraft_model::{PhType, Presentation}; use kurbo::{Affine, BezPath, Point, Rect, Shape as _}; use vello_cpu::{RenderContext, peniko}; @@ -61,7 +61,7 @@ pub fn fill_path( if g.tile.iter().all(|v| *v == 0.0) { crate::pathgrad::fill(ctx, kind, *focus, &ramp, bounds, path, m, alpha); } else { - crate::pathgrad::fill_tiled(ctx, kind, *focus, &ramp, bounds, tile, path, m, alpha); + crate::pathgrad::fill_tiled_flipped(ctx, kind, *focus, &ramp, bounds, tile, g.flip.unwrap_or(GradientFlip::None), path, m, alpha); } return; } diff --git a/crates/render/src/pathgrad.rs b/crates/render/src/pathgrad.rs index 27cce00c5..476fa0837 100644 --- a/crates/render/src/pathgrad.rs +++ b/crates/render/src/pathgrad.rs @@ -12,6 +12,7 @@ use std::sync::Arc; use deckcraft_color::Rgba; +use deckcraft_model::style::GradientFlip; use kurbo::{Affine, BezPath, PathEl, Point, Rect}; use vello_cpu::{Pixmap, RenderContext, peniko}; @@ -242,17 +243,35 @@ pub fn fill_tiled( path: &BezPath, m: Affine, alpha: f64, +) { + fill_tiled_flipped(ctx, kind, focus, stops, bounds, tile, GradientFlip::None, path, m, alpha); +} + +/// Fill `path` with repeated path-gradient tiles, reflecting alternate tiles along `flip` axes. +#[allow(clippy::too_many_arguments)] +pub(crate) fn fill_tiled_flipped( + ctx: &mut RenderContext, + kind: &str, + focus: [f64; 4], + stops: &[(f64, Rgba)], + bounds: Rect, + tile: Rect, + flip: GradientFlip, + path: &BezPath, + m: Affine, + alpha: f64, ) { if bounds.width() <= 0.0 || bounds.height() <= 0.0 || !bounds.is_finite() || stops.is_empty() { return; } let tile = if tile.is_finite() && tile.width() > 0.0 && tile.height() > 0.0 { tile } else { bounds }; let untiled = tile == bounds; + let sample = if untiled { bounds } else { tile }; let k = crate::scale_of(m); let k = if k.is_finite() && k > 0.0 { k } else { 1.0 }; - let longest = bounds.width().max(bounds.height()) * k; + let longest = sample.width().max(sample.height()) * k; let s = (MAX_SIDE / longest).min(1.0) * k; - let (pw, ph) = (((bounds.width() * s).ceil() as u32).clamp(1, 4096), ((bounds.height() * s).ceil() as u32).clamp(1, 4096)); + let (pw, ph) = (((sample.width() * s).ceil() as u32).clamp(1, 4096), ((sample.height() * s).ceil() as u32).clamp(1, 4096)); let mut outline = if kind == "shape" { let pixels = u64::from(pw).saturating_mul(u64::from(ph)).max(1); let max_edges = usize::try_from(MAX_EDGE_WORK / pixels).unwrap_or(usize::MAX).max(MIN_POLY_EDGES); @@ -274,7 +293,7 @@ pub fn fill_tiled( let mut data = Vec::with_capacity((pw * ph) as usize); for j in 0..ph { for i in 0..pw { - let p = Point::new(bounds.x0 + (i as f64 + 0.5) / pw as f64 * bounds.width(), bounds.y0 + (j as f64 + 0.5) / ph as f64 * bounds.height()); + let p = Point::new(sample.x0 + (i as f64 + 0.5) / pw as f64 * sample.width(), sample.y0 + (j as f64 + 0.5) / ph as f64 * sample.height()); let t = if untiled { position(kind, bounds, focus, &outline, p) } else { position_in_tile(kind, bounds, tile, focus, &outline, p) }; let c = lut.get((t * 255.0).round().clamp(0.0, 255.0) as usize).copied().unwrap_or(Rgba::BLACK); let ca = (c.a as f64 * a).round().clamp(0.0, 255.0); @@ -284,12 +303,20 @@ pub fn fill_tiled( } let pm = Arc::new(Pixmap::from_parts(data, pw as u16, ph as u16)); ctx.set_transform(m); - ctx.set_paint(vello_cpu::Image { - image: vello_cpu::ImageSource::Pixmap(pm), - sampler: peniko::ImageSampler { quality: peniko::ImageQuality::Medium, ..Default::default() }, - }); + let extend = |reflected: bool| if reflected { peniko::Extend::Reflect } else { peniko::Extend::Repeat }; + let sampler = if untiled { + peniko::ImageSampler { quality: peniko::ImageQuality::Medium, ..Default::default() } + } else { + peniko::ImageSampler { + x_extend: extend(matches!(flip, GradientFlip::X | GradientFlip::Xy)), + y_extend: extend(matches!(flip, GradientFlip::Y | GradientFlip::Xy)), + quality: peniko::ImageQuality::Medium, + ..Default::default() + } + }; + ctx.set_paint(vello_cpu::Image { image: vello_cpu::ImageSource::Pixmap(pm), sampler }); ctx.set_paint_transform( - Affine::translate(bounds.origin().to_vec2()) * Affine::scale_non_uniform(bounds.width() / pw as f64, bounds.height() / ph as f64), + Affine::translate(sample.origin().to_vec2()) * Affine::scale_non_uniform(sample.width() / pw as f64, sample.height() / ph as f64), ); ctx.fill_path(path); ctx.reset_paint_transform(); diff --git a/crates/render/src/tests_wild.rs b/crates/render/src/tests_wild.rs index 03587c5ec..d79ba3653 100644 --- a/crates/render/src/tests_wild.rs +++ b/crates/render/src/tests_wild.rs @@ -6,7 +6,7 @@ use super::{ render_slide, trim_ends, }; use deckcraft_color::Rgba; -use deckcraft_model::style::ColorRef; +use deckcraft_model::style::{ColorRef, GradientFlip}; use deckcraft_model::{CustomPath, Geom, Presentation, Shape, ShapeId, Xfrm}; use kurbo::Shape as _; @@ -407,6 +407,7 @@ fn a_rectangular_gradient_spans_its_tile_rect() { let img = render(vec![sh]); assert!(img.pixel(300, 200)[0] > 240, "center {:?}", img.pixel(300, 200)); assert!(img.pixel(395, 200)[0] < 80, "tile edge {:?}", img.pixel(395, 200)); + assert!(img.pixel(450, 200)[0] > 80, "next tile {:?}", img.pixel(450, 200)); } #[test] @@ -420,6 +421,36 @@ fn a_shape_gradient_spans_its_tile_rect() { let img = render(vec![sh]); assert!(img.pixel(300, 200)[0] > 240, "center {:?}", img.pixel(300, 200)); assert!(img.pixel(395, 200)[0] < 80, "tile edge {:?}", img.pixel(395, 200)); + assert!(img.pixel(450, 200)[0] > 80, "next tile {:?}", img.pixel(450, 200)); +} + +#[test] +fn a_rectangular_gradient_reflects_alternate_tiles() { + let mut sh = radial([0.5; 4]); + if let Some(Fill::Gradient(g)) = &mut sh.fill { + g.shape = deckcraft_model::style::GradientShape::Path { path: "rect".into(), focus: [0.25, 0.5, 0.75, 0.5] }; + g.tile = [0.25, 0.0, 0.25, 0.0]; + } + let repeated = render(vec![sh.clone()]).pixel(450, 200)[0]; + if let Some(Fill::Gradient(g)) = &mut sh.fill { + g.flip = Some(GradientFlip::X); + } + let reflected = render(vec![sh]).pixel(450, 200)[0]; + assert!(repeated > reflected + 60, "repeat {repeated}, reflected {reflected}"); +} + +#[test] +fn small_rectangular_gradient_tiles_remain_distinct_in_a_large_shape() { + let mut sh = radial([0.5; 4]); + sh.xfrm = Some(Xfrm::new(100.0, 100.0, 4000.0, 200.0)); + if let Some(Fill::Gradient(g)) = &mut sh.fill { + g.shape = deckcraft_model::style::GradientShape::Path { path: "rect".into(), focus: [0.5; 4] }; + g.tile = [0.4995, 0.0, 0.4995, 0.0]; + } + let img = render(vec![sh]); + let edge = img.pixel(150, 200)[0]; + let center = img.pixel(152, 200)[0]; + assert!(i32::from(center) > i32::from(edge) + 100, "edge {edge}, center {center}"); } #[test] From b78f16a1cc99147f2cd6d0d3f3a2f5c2f97ea07b Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Thu, 5 Nov 2026 07:59:08 -0800 Subject: [PATCH 3/7] Preserve detail in narrow gradient tiles --- crates/render/src/pathgrad.rs | 6 +++--- crates/render/src/tests_wild.rs | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/crates/render/src/pathgrad.rs b/crates/render/src/pathgrad.rs index 476fa0837..0175185d2 100644 --- a/crates/render/src/pathgrad.rs +++ b/crates/render/src/pathgrad.rs @@ -269,9 +269,9 @@ pub(crate) fn fill_tiled_flipped( let sample = if untiled { bounds } else { tile }; let k = crate::scale_of(m); let k = if k.is_finite() && k > 0.0 { k } else { 1.0 }; - let longest = sample.width().max(sample.height()) * k; - let s = (MAX_SIDE / longest).min(1.0) * k; - let (pw, ph) = (((sample.width() * s).ceil() as u32).clamp(1, 4096), ((sample.height() * s).ceil() as u32).clamp(1, 4096)); + let s = if untiled { (MAX_SIDE / (sample.width().max(sample.height()) * k)).min(1.0) * k } else { k }; + let limit = if untiled { 4096.0 } else { MAX_SIDE }; + let (pw, ph) = ((sample.width() * s).ceil().clamp(1.0, limit) as u32, (sample.height() * s).ceil().clamp(1.0, limit) as u32); let mut outline = if kind == "shape" { let pixels = u64::from(pw).saturating_mul(u64::from(ph)).max(1); let max_edges = usize::try_from(MAX_EDGE_WORK / pixels).unwrap_or(usize::MAX).max(MIN_POLY_EDGES); diff --git a/crates/render/src/tests_wild.rs b/crates/render/src/tests_wild.rs index d79ba3653..94e1bac88 100644 --- a/crates/render/src/tests_wild.rs +++ b/crates/render/src/tests_wild.rs @@ -453,6 +453,20 @@ fn small_rectangular_gradient_tiles_remain_distinct_in_a_large_shape() { assert!(i32::from(center) > i32::from(edge) + 100, "edge {edge}, center {center}"); } +#[test] +fn narrow_rectangular_gradient_tiles_keep_their_horizontal_ramp() { + let mut sh = radial([0.5; 4]); + sh.xfrm = Some(Xfrm::new(100.0, 100.0, 400.0, 4000.0)); + if let Some(Fill::Gradient(g)) = &mut sh.fill { + g.shape = deckcraft_model::style::GradientShape::Path { path: "rect".into(), focus: [0.5, 0.025, 0.5, 0.975] }; + g.tile = [0.495, 0.0, 0.495, 0.0]; + } + let img = render(vec![sh]); + let edge = img.pixel(150, 200)[0]; + let center = img.pixel(152, 200)[0]; + assert!(i32::from(center) > i32::from(edge) + 100, "edge {edge}, center {center}"); +} + #[test] fn a_3d_pictures_border_stays_centred_on_its_edge() { let mut p = picture_deck(Some(Line::solid(ColorRef::rgb(RED), 8.0))); From 0c79f0dd5588c80e329087653a61af04eb10698b Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Thu, 5 Nov 2026 08:36:47 -0800 Subject: [PATCH 4/7] Preserve tiled gradient detail under nonuniform transforms --- crates/render/src/pathgrad.rs | 9 ++++++++- crates/render/src/tests_wild.rs | 16 ++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/crates/render/src/pathgrad.rs b/crates/render/src/pathgrad.rs index 0175185d2..647daa5c5 100644 --- a/crates/render/src/pathgrad.rs +++ b/crates/render/src/pathgrad.rs @@ -270,8 +270,15 @@ pub(crate) fn fill_tiled_flipped( let k = crate::scale_of(m); let k = if k.is_finite() && k > 0.0 { k } else { 1.0 }; let s = if untiled { (MAX_SIDE / (sample.width().max(sample.height()) * k)).min(1.0) * k } else { k }; + let (sx, sy) = if untiled { + (s, s) + } else { + let [a, b, c, d, _, _] = m.as_coeffs(); + let valid_scale = |v: f64| if v.is_finite() && v > 0.0 { v } else { 1.0 }; + (valid_scale(a.hypot(b)), valid_scale(c.hypot(d))) + }; let limit = if untiled { 4096.0 } else { MAX_SIDE }; - let (pw, ph) = ((sample.width() * s).ceil().clamp(1.0, limit) as u32, (sample.height() * s).ceil().clamp(1.0, limit) as u32); + let (pw, ph) = ((sample.width() * sx).ceil().clamp(1.0, limit) as u32, (sample.height() * sy).ceil().clamp(1.0, limit) as u32); let mut outline = if kind == "shape" { let pixels = u64::from(pw).saturating_mul(u64::from(ph)).max(1); let max_edges = usize::try_from(MAX_EDGE_WORK / pixels).unwrap_or(usize::MAX).max(MIN_POLY_EDGES); diff --git a/crates/render/src/tests_wild.rs b/crates/render/src/tests_wild.rs index 94e1bac88..3df3096c6 100644 --- a/crates/render/src/tests_wild.rs +++ b/crates/render/src/tests_wild.rs @@ -467,6 +467,22 @@ fn narrow_rectangular_gradient_tiles_keep_their_horizontal_ramp() { assert!(i32::from(center) > i32::from(edge) + 100, "edge {edge}, center {center}"); } +#[test] +fn a_scaled_group_keeps_detail_across_a_narrow_gradient_tile() { + let img = in_a_scaled_group(8.0, 0.125, Presentation::default(), |w, h| { + let mut sh = radial([0.5, 0.025, 0.5, 0.975]); + sh.xfrm = Some(Xfrm::new(0.0, 0.0, w, h)); + if let Some(Fill::Gradient(g)) = &mut sh.fill { + g.shape = deckcraft_model::style::GradientShape::Path { path: "rect".into(), focus: [0.5, 0.025, 0.5, 0.975] }; + g.tile = [0.491_666_666_666_666_64, 0.0, 0.491_666_666_666_666_64, 0.0]; + } + sh + }); + let edge = img.pixel(110, 130)[0]; + let center = img.pixel(112, 130)[0]; + assert!(i32::from(center) > i32::from(edge) + 60, "edge {edge}, center {center}"); +} + #[test] fn a_3d_pictures_border_stays_centred_on_its_edge() { let mut p = picture_deck(Some(Line::solid(ColorRef::rgb(RED), 8.0))); From e187f1d229fe0da7f18f2b4ab3c4b9fdefc1d28a Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Thu, 12 Nov 2026 08:41:55 -0800 Subject: [PATCH 5/7] Sample only the shape where a gradient tile spans it A tile rectangle larger than the shape on an axis (the from-corner presets) no longer stretches the raster over the whole tile. On that axis the raster covers the shape and its path, and nothing repeats. --- crates/render/src/pathgrad.rs | 56 ++++++++++++++++++++++++++++++++--- 1 file changed, 52 insertions(+), 4 deletions(-) diff --git a/crates/render/src/pathgrad.rs b/crates/render/src/pathgrad.rs index 7ea82dc15..8bb580f50 100644 --- a/crates/render/src/pathgrad.rs +++ b/crates/render/src/pathgrad.rs @@ -310,6 +310,18 @@ pub fn fill_tiled( fill_tiled_flipped(ctx, kind, focus, stops, bounds, tile, GradientFlip::None, path, m, alpha); } +/// The part of `tile` a tiled gradient raster covers, with whether it repeats across and down. On +/// an axis where `tile` spans all of `cover` (the shape's box and path), the raster covers only +/// `cover` and nothing repeats on that axis; on any other axis it covers the whole tile, which +/// repeats. +pub(crate) fn tiled_sample(cover: Rect, tile: Rect) -> (Rect, bool, bool) { + let spans_x = tile.x0 <= cover.x0 && tile.x1 >= cover.x1; + let spans_y = tile.y0 <= cover.y0 && tile.y1 >= cover.y1; + let (x0, x1) = if spans_x { (cover.x0, cover.x1) } else { (tile.x0, tile.x1) }; + let (y0, y1) = if spans_y { (cover.y0, cover.y1) } else { (tile.y0, tile.y1) }; + (Rect::new(x0, y0, x1, y1), !spans_x, !spans_y) +} + /// Fill `path` with repeated path-gradient tiles, reflecting alternate tiles along `flip` axes. #[allow(clippy::too_many_arguments)] pub(crate) fn fill_tiled_flipped( @@ -329,7 +341,9 @@ pub(crate) fn fill_tiled_flipped( } let tile = if tile.is_finite() && tile.width() > 0.0 && tile.height() > 0.0 { tile } else { bounds }; let untiled = tile == bounds; - let sample = if untiled { bounds } else { tile }; + let cover = kurbo::Shape::bounding_box(path); + let cover = if cover.is_finite() { cover.union(bounds) } else { bounds }; + let (sample, repeat_x, repeat_y) = if untiled { (bounds, false, false) } else { tiled_sample(cover, tile) }; let k = crate::scale_of(m); let k = if k.is_finite() && k > 0.0 { k } else { 1.0 }; let s = if untiled { (MAX_SIDE / (sample.width().max(sample.height()) * k)).min(1.0) * k } else { k }; @@ -379,13 +393,17 @@ pub(crate) fn fill_tiled_flipped( } let pm = Arc::new(Pixmap::from_parts(data, pw as u16, ph as u16)); ctx.set_transform(m); - let extend = |reflected: bool| if reflected { peniko::Extend::Reflect } else { peniko::Extend::Repeat }; + let extend = |repeats: bool, reflected: bool| match (repeats, reflected) { + (false, _) => peniko::Extend::Pad, + (true, true) => peniko::Extend::Reflect, + (true, false) => peniko::Extend::Repeat, + }; let sampler = if untiled { peniko::ImageSampler { quality: peniko::ImageQuality::Medium, ..Default::default() } } else { peniko::ImageSampler { - x_extend: extend(matches!(flip, GradientFlip::X | GradientFlip::Xy)), - y_extend: extend(matches!(flip, GradientFlip::Y | GradientFlip::Xy)), + x_extend: extend(repeat_x, matches!(flip, GradientFlip::X | GradientFlip::Xy)), + y_extend: extend(repeat_y, matches!(flip, GradientFlip::Y | GradientFlip::Xy)), quality: peniko::ImageQuality::Medium, ..Default::default() } @@ -518,6 +536,36 @@ mod tests { assert!(px.chunks(4).all(|p| p[3] == 0), "nothing was painted"); } + #[test] + fn a_tile_reaching_past_the_shape_samples_only_the_shape() { + let b = Rect::new(0.0, 0.0, 400.0, 200.0); + // A corner preset's tile reaches a box up and left: nothing repeats, the shape is sampled. + assert_eq!(tiled_sample(b, Rect::new(-400.0, -200.0, 400.0, 200.0)), (b, false, false)); + // A tile inside the shape repeats on both axes. + let inner = Rect::new(100.0, 50.0, 200.0, 150.0); + assert_eq!(tiled_sample(b, inner), (inner, true, true)); + // A tile as tall as the shape and narrower repeats across only. + assert_eq!(tiled_sample(b, Rect::new(100.0, -10.0, 200.0, 210.0)), (Rect::new(100.0, 0.0, 200.0, 200.0), true, false)); + } + + #[test] + fn a_tile_reaching_past_the_shape_keeps_its_detail() { + // The tile is four boxes wide; the hard stop half way sits 200 pt in. The raster covers the + // shape only, so the edge stays within about a point. + let b = Rect::new(0.0, 0.0, 400.0, 200.0); + let tile = Rect::new(-1200.0, -200.0, 400.0, 200.0); + let (w, k) = (800u16, 2.0); + let stops = [(0.0, Rgba::WHITE), (0.5, Rgba::WHITE), (0.5, Rgba::BLACK), (1.0, Rgba::BLACK)]; + let mut ctx = RenderContext::new(w, 400); + fill_tiled(&mut ctx, "rect", [0.0, 0.0, 1.0, 1.0], &stops, b, tile, &b.to_path(0.1), Affine::scale(k), 1.0); + ctx.flush(); + let mut px = vec![0u8; usize::from(w) * 400 * 4]; + ctx.render(vello_cpu::PixmapMut::new(w, 400, &mut px).unwrap(), &mut vello_cpu::Resources::new()); + let red = |x: usize, y: usize| px[(y * usize::from(w) + x) * 4]; + assert!(red(397, 20) > 240, "before the edge {}", red(397, 20)); + assert!(red(403, 20) < 15, "after the edge {}", red(403, 20)); + } + #[test] fn the_ramp_runs_through_the_stops() { let stops = [(0.0, Rgba::rgb(0, 0, 0)), (0.5, Rgba::rgb(128, 128, 128)), (1.0, Rgba::rgb(255, 255, 255))]; From b4294d8f25a4a3282cf7e3a701276fe947732dcf Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Wed, 25 Nov 2026 21:13:43 -0800 Subject: [PATCH 6/7] Render: document position_in_tile and fill_tiled --- crates/render/src/pathgrad.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/crates/render/src/pathgrad.rs b/crates/render/src/pathgrad.rs index 8bb580f50..4fecec004 100644 --- a/crates/render/src/pathgrad.rs +++ b/crates/render/src/pathgrad.rs @@ -66,6 +66,8 @@ pub fn position(kind: &str, bounds: Rect, focus: [f64; 4], outline: &[Vec position_in_tile(kind, bounds, bounds, focus, outline, p) } +/// [`position`] with the `rect` ramp ending on the edges of `tile` instead of `bounds`; the focus +/// rect is still measured in `bounds`. fn position_in_tile(kind: &str, bounds: Rect, tile: Rect, focus: [f64; 4], outline: &[Vec], p: Point) -> f64 { let f = Rect::new( bounds.x0 + bounds.width() * focus[0].clamp(0.0, 1.0), @@ -295,6 +297,9 @@ pub fn fill(ctx: &mut RenderContext, kind: &str, focus: [f64; 4], stops: &[(f64, fill_tiled(ctx, kind, focus, stops, bounds, bounds, path, m, alpha); } +/// [`fill`] with the gradient laid out in `tile` (the `a:tileRect` box in path units) and +/// repeated across the shape where `tile` does not cover it; a `tile` equal to `bounds` draws +/// as [`fill`] does. #[allow(clippy::too_many_arguments)] pub fn fill_tiled( ctx: &mut RenderContext, From 5af8de0ae62ae5e91796b5c8236937cb7976248f Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sat, 28 Nov 2026 23:45:23 -0800 Subject: [PATCH 7/7] Render: test the focus with an off-centre gradient tile A rect path gradient whose tile is the right half of the shape keeps its focus rect in the shape box and ends its ramp on the tile's edges. --- crates/render/src/pathgrad.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/crates/render/src/pathgrad.rs b/crates/render/src/pathgrad.rs index 4fecec004..f6800402f 100644 --- a/crates/render/src/pathgrad.rs +++ b/crates/render/src/pathgrad.rs @@ -553,6 +553,18 @@ mod tests { assert_eq!(tiled_sample(b, Rect::new(100.0, -10.0, 200.0, 210.0)), (Rect::new(100.0, 0.0, 200.0, 200.0), true, false)); } + #[test] + fn an_off_centre_tile_keeps_the_focus_in_the_shape_box() { + // The tile is the right half of the shape. A centred focus rect sits at the shape's centre, + // on the tile's left edge, and the ramp ends on the tile's right edge. + let b = Rect::new(0.0, 0.0, 400.0, 200.0); + let tile = Rect::new(200.0, 0.0, 400.0, 200.0); + let at = |x: f64| position_in_tile("rect", b, tile, CENTRE, &[], Point::new(x, 100.0)); + assert_eq!(at(200.0), 0.0, "the focus is at the shape's centre"); + assert!((at(300.0) - 0.5).abs() < 1e-9, "half way to the tile's right edge: {}", at(300.0)); + assert_eq!(at(400.0), 1.0, "the ramp ends on the tile's right edge"); + } + #[test] fn a_tile_reaching_past_the_shape_keeps_its_detail() { // The tile is four boxes wide; the hard stop half way sits 200 pt in. The raster covers the