diff --git a/crates/render/src/paint.rs b/crates/render/src/paint.rs index 74d111236..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}; @@ -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_flipped(ctx, kind, *focus, &ramp, bounds, tile, g.flip.unwrap_or(GradientFlip::None), 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 aef17fa81..f6800402f 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}; @@ -62,6 +63,12 @@ pub(crate) fn fit_raster(edges: usize, pw: u32, ph: u32) -> (u32, u32) { /// 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) +} + +/// [`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), bounds.y0 + bounds.height() * focus[1].clamp(0.0, 1.0), @@ -107,7 +114,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) } } } @@ -287,15 +294,74 @@ 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); +} + +/// [`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, + kind: &str, + focus: [f64; 4], + stops: &[(f64, Rgba)], + bounds: Rect, + tile: Rect, + path: &BezPath, + m: Affine, + alpha: f64, +) { + 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( + 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 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 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, pw, ph) = if kind == "shape" { + 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() * sx).ceil().clamp(1.0, limit) as u32, (sample.height() * sy).ceil().clamp(1.0, limit) as u32); + let (mut outline, pw, ph) = if kind == "shape" { let max_edges = edge_budget(pw, ph); let pixel = (bounds.width() / f64::from(pw)).min(bounds.height() / f64::from(ph)); let o = simplify(polygons(path), pixel, max_edges); @@ -309,13 +375,21 @@ pub fn fill(ctx: &mut RenderContext, kind: &str, focus: [f64; 4], stops: &[(f64, if kind == "shape" && outline.is_empty() { return; } + 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 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); let mul = |v: u8| (v as f64 * ca / 255.0).round().clamp(0.0, 255.0) as u8; @@ -324,12 +398,24 @@ pub fn fill(ctx: &mut RenderContext, kind: &str, focus: [f64; 4], stops: &[(f64, } 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 = |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(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() + } + }; + 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(); @@ -455,6 +541,48 @@ 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 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 + // 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))]; diff --git a/crates/render/src/tests_wild.rs b/crates/render/src/tests_wild.rs index 2da29cb37..3df3096c6 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 _; @@ -397,6 +397,92 @@ 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)); + assert!(img.pixel(450, 200)[0] > 80, "next tile {:?}", img.pixel(450, 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)); + 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] +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_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)));