From 289fe5cc145693fa2e0bca8737710cb3eb272e25 Mon Sep 17 00:00:00 2001 From: David Mann Date: Tue, 1 Dec 2026 03:45:07 +0100 Subject: [PATCH] Batch fix-up: Symmetry Fill dilations are the same for any selection on the canvas (CodeRabbit on #18551) The range of dilation steps came from the painted rect, so a small selection dropped enlarged copies that still cover it (a translucent tile's centre pixel changed), and type 6 numbered its copies from the largest step, so the same copy drifted to another colour when the range changed. The range now comes from the canvas (with the area), and dilated copies take their own step as the number for the colour drift; the drawing order stays as it was. Co-Authored-By: Claude Opus 5.5 --- crates/compose/src/scripted_pattern.rs | 1 + .../compose/src/scripted_pattern/symmetry.rs | 46 ++++++++++++------- .../src/fill_script_symmetry_tests.rs | 38 +++++++++++++++ 3 files changed, 68 insertions(+), 17 deletions(-) diff --git a/crates/compose/src/scripted_pattern.rs b/crates/compose/src/scripted_pattern.rs index 179c84c85f..955ce4f5f2 100644 --- a/crates/compose/src/scripted_pattern.rs +++ b/crates/compose/src/scripted_pattern.rs @@ -508,6 +508,7 @@ pub fn render(script: &str, value: &dyn Fn(&str) -> f64, seed: u64, tile: &Tile, scale: value("patternScale"), shift: (value("translationAlongWidth"), value("translationAlongHeight")), centre, + bounds: canvas, jitter, }; Some(fill.render(tile, width, height, rect)) diff --git a/crates/compose/src/scripted_pattern/symmetry.rs b/crates/compose/src/scripted_pattern/symmetry.rs index 57d5a99721..975471e90b 100644 --- a/crates/compose/src/scripted_pattern/symmetry.rs +++ b/crates/compose/src/scripted_pattern/symmetry.rs @@ -74,6 +74,8 @@ pub struct SymmetryFill { pub shift: (f64, f64), /// The point the symmetry turns about, in document pixels. pub centre: (f64, f64), + /// The region the layout is made for (the canvas): the copies are the same for every painted area within it. + pub bounds: Rect, pub jitter: Jitter, } @@ -129,12 +131,17 @@ impl Map { /// Where the copies go. enum Layout { - /// A fixed list of copies, the last on top. - Copies(Vec), + /// A fixed list of copies, the last on top, each with its own number for the per-copy randomness. + Copies(Vec<(i64, Map)>), /// The copies `reps` repeated over the lattice of `e1` and `e2` (a band along `e1` without `e2`). Lattice { e1: (f64, f64), e2: Option<(f64, f64)>, reps: Vec }, } +/// The copies `maps` numbered in order. +fn numbered(maps: Vec) -> Layout { + Layout::Copies((0..).zip(maps).collect()) +} + fn turns(n: u32) -> Vec { (0..n).map(|i| Map::turn(f64::from(i) * 360.0 / f64::from(n))).collect() } @@ -143,11 +150,12 @@ fn mirrors(degrees: &[f64]) -> Vec { degrees.iter().map(|d| Map::mirror(*d)).collect() } -/// Copies scaled by `k` and turned by `deg` per step about the centre, for the steps `steps`, the largest first. +/// Copies scaled by `k` and turned by `deg` per step about the centre, for the steps `steps`, the largest first; each +/// copy is numbered by its step. fn dilations(k: f64, deg: f64, steps: std::ops::RangeInclusive) -> Layout { let (lo, hi) = (*steps.start(), *steps.end()); let lo = lo.max(hi - MAX_DILATION_STEPS + 1); - Layout::Copies((lo..=hi).rev().map(|n| Map::turn(n as f64 * deg).scaled(k.powf(n as f64))).collect()) + Layout::Copies((lo..=hi).rev().map(|n| (n, Map::turn(n as f64 * deg).scaled(k.powf(n as f64)))).collect()) } /// The layout of `kind` for lattice size `a`; `steps(k)` is the range of dilation steps that can show for ratio `k`. @@ -159,9 +167,9 @@ fn layout(kind: usize, a: f64, steps: impl Fn(f64) -> std::ops::RangeInclusive| Layout::Lattice { e1: (a, 0.0), e2: Some(e2), reps }; let (oblique, square, centred, hex) = ((0.25 * a, a), (0.0, a), (half, 0.75 * a), (half, a * 3f64.sqrt() / 2.0)); match kind { - 1 => Layout::Copies(vec![id, mx]), - 2 => Layout::Copies(vec![id, r180]), - 3 => Layout::Copies(turns(4)), + 1 => numbered(vec![id, mx]), + 2 => numbered(vec![id, r180]), + 3 => numbered(turns(4)), 4 => Layout::Lattice { e1: (0.0, a), e2: None, reps: vec![id] }, 5 => Layout::Lattice { e1: (0.0, a), e2: None, reps: vec![id, mx.shifted(0.0, half)] }, 6 => dilations(1.5, 0.0, steps(1.5)), @@ -195,11 +203,12 @@ fn layout(kind: usize, a: f64, steps: impl Fn(f64) -> std::ops::RangeInclusive = match &layout { - Layout::Copies(maps) => maps.iter().filter_map(|m| Some((m.inverse()?, m.apply(motif), radius * m.stretch()))).collect(), + let fixed: Vec<(i64, Map, (f64, f64), f64)> = match &layout { + Layout::Copies(maps) => maps.iter().filter_map(|(n, m)| Some((*n, m.inverse()?, m.apply(motif), radius * m.stretch()))).collect(), Layout::Lattice { .. } => Vec::new(), }; let reps: Vec<(Map, (f64, f64))> = match &layout { @@ -260,9 +271,9 @@ impl SymmetryFill { found.clear(); match &layout { Layout::Copies(_) => { - for (k, (back, c, r)) in fixed.iter().enumerate() { + for (k, (n, back, c, r)) in fixed.iter().enumerate() { if (p.0 - c.0).powi(2) + (p.1 - c.1).powi(2) <= r * r { - found.push(Candidate { order: (0, 0, k), back: *back, stretch: 1.0 / back.stretch() }); + found.push(Candidate { order: (0, 0, k), id: (0, *n), back: *back, stretch: 1.0 / back.stretch() }); } } } @@ -272,7 +283,8 @@ impl SymmetryFill { if found.len() < MAX_LAYERS { // Back from the shifted copy: first undo the lattice step, then the representative. let back = Map { t: back.apply((-off.0, -off.1)), ..*back }; - found.push(Candidate { order: (row, col, k), back, stretch: 1.0 }); + let id = (row, col.wrapping_mul(64).wrapping_add(k as i64)); + found.push(Candidate { order: (row, col, k), id, back, stretch: 1.0 }); } }); } @@ -317,7 +329,7 @@ impl SymmetryFill { continue; } let mut q = [q[0] / q[3], q[1] / q[3], q[2] / q[3], q[3] / (taps * taps) as f32]; - let d = self.jitter.offsets(c.order.0, c.order.1.wrapping_mul(64).wrapping_add(c.order.2 as i64)); + let d = self.jitter.offsets(c.id.0, c.id.1); // Without a drift a copy keeps its exact colour, also a float one past 1. for ch in 0..3 { if d[ch] != 0.0 { diff --git a/crates/engine-paint/src/fill_script_symmetry_tests.rs b/crates/engine-paint/src/fill_script_symmetry_tests.rs index 7b9b9f2c12..24c9a47fbe 100644 --- a/crates/engine-paint/src/fill_script_symmetry_tests.rs +++ b/crates/engine-paint/src/fill_script_symmetry_tests.rs @@ -206,3 +206,41 @@ fn colour_randomness_varies_the_copies_only_above_zero() { s.execute("edit.fill", symmetry(json!({"symmetryType": 1, "colorRandomness": 1, "seed": 3}))).unwrap(); assert_ne!(rgb8(pixel(&s, 315, 210)), rgb8(pixel(&s, 285, 210)), "each copy drifts on its own"); } + +#[test] +fn a_small_selection_keeps_the_dilations_that_cover_it() { + // A faint 10 % tile on the centre: every enlarged copy covers the centre and shows through the ones above it, so the + // centre pixel is the same whether the whole canvas or only a small square around it is filled. + let mut s = crate::session(); + s.execute("file.new", json!({"width": 40, "height": 30, "background": "transparent"})).unwrap(); + s.execute("edit.fill", json!({"contents": "color", "color": "#ff0000", "opacity": 10})).unwrap(); + s.execute("edit.definePattern", json!({"name": "probe", "rect": [0, 0, 40, 30]})).unwrap(); + s.execute("file.new", json!({"width": 600, "height": 400, "background": "white"})).unwrap(); + s.execute("layer.new.layer", json!({})).unwrap(); + let p = symmetry(json!({"symmetryType": 6, "translationAlongWidth": 0, "translationAlongHeight": 0})); + s.execute("edit.fill", p.clone()).unwrap(); + let whole = pixel(&s, 300, 200); + s.execute("edit.undo", json!({})).unwrap(); + s.execute("select.rect", json!({"x": 290, "y": 190, "width": 20, "height": 20})).unwrap(); + s.execute("edit.fill", p).unwrap(); + let small = pixel(&s, 300, 200); + assert!(whole[3] > 0.1, "several copies over the centre: {whole:?}"); + assert!((small[3] - whole[3]).abs() < 0.01, "{small:?} as {whole:?}"); +} + +#[test] +fn a_dilated_copy_keeps_its_colour_drift_on_a_larger_canvas() { + // The copies of 6: dilatation symmetry drift by their own step, not by how many larger copies the canvas needs: the + // same fill about the same centre on a larger canvas gives the copies near the centre the same colours. + let p = symmetry(json!({"symmetryType": 6, "colorRandomness": 1, "seed": 5, "translationAlongWidth": 0, "translationAlongHeight": 0})); + let mut s = session(); + s.execute("edit.fill", p.clone()).unwrap(); + let small: Vec<[u8; 3]> = (290..310).map(|x| rgb8(pixel(&s, x, 205))).collect(); + s.execute("file.new", json!({"width": 1000, "height": 800, "background": "white"})).unwrap(); + s.execute("layer.new.layer", json!({})).unwrap(); + s.execute("select.rect", json!({"x": 200, "y": 200, "width": 600, "height": 400})).unwrap(); + s.execute("edit.fill", p).unwrap(); + let large: Vec<[u8; 3]> = (490..510).map(|x| rgb8(pixel(&s, x, 405))).collect(); + assert!(small.iter().any(|c| *c != [0, 0, 0]), "{small:?}"); + assert_eq!(large, small); +}