From 86c3e0cfea95c5de47d129c9cd2d35ebf2fdfd38 Mon Sep 17 00:00:00 2001 From: David Mann Date: Sun, 29 Nov 2026 23:56:28 +0100 Subject: [PATCH] Fill: History on a layer missing from the source clears to transparent (#17465) Edit > Fill with History read the source surface by the target layer's id and failed when the source state did not have that layer (a new or duplicated layer, every layer after Flatten Image). As in the reference application, such a layer now counts as transparent in the source: the selection is cleared, and the Background after Flatten Image takes white. A mask, alpha channel or Quick Mask the source lacks is still refused. Co-Authored-By: Claude Opus 5.5 --- crates/engine-paint/src/cmd/fill_cmds.rs | 20 ++- .../src/fill_history_missing_layer_tests.rs | 147 ++++++++++++++++++ 2 files changed, 164 insertions(+), 3 deletions(-) create mode 100644 crates/engine-paint/src/fill_history_missing_layer_tests.rs diff --git a/crates/engine-paint/src/cmd/fill_cmds.rs b/crates/engine-paint/src/cmd/fill_cmds.rs index 7968d1cc76..8e7bbba71f 100644 --- a/crates/engine-paint/src/cmd/fill_cmds.rs +++ b/crates/engine-paint/src/cmd/fill_cmds.rs @@ -119,9 +119,19 @@ pub fn fill(s: &mut Session, p: &Value) -> Result { "history" => { // A History panel row `state` or a `snapshot`, else the History Brush's source. let source_doc = crate::history_source::paint_source(st, p, CMD)?; - let src = crate::channel_cmds::read_target_surface(source_doc.as_ref(), id, p) - .map_err(|_| EngineError::Other("the target did not exist (as pixels, a mask or a channel) in that history state".into()))?; - Source::Pixels(area, read_rgba(src, area, "reading Fill's history source")?) + match crate::channel_cmds::read_target_surface(source_doc.as_ref(), id, p) { + Ok(src) => Source::Pixels(area, read_rgba(src, area, "reading Fill's history source")?), + // A pixel layer the source state lacks (a new, duplicated or flattened layer) counts as transparent + // there, as in the reference application; the Background (after Flatten Image) takes white, not the + // background colour (#17465). Masks and channels the source lacks stay an error. + Err(_) + if let Some(lid) = + id.filter(|&l| crate::channel_cmds::target_of(p) == crate::channel_cmds::Target::Pixels && source_doc.layer(l).is_none()) => + { + Source::Color(if st.doc.is_background(lid) { [1.0; 4] } else { [0.0; 4] }) + } + Err(_) => return Err(EngineError::Other("the target did not exist (as pixels, a mask or a channel) in that history state".into())), + } } "contentAware" => { let sel = sel.as_ref().ok_or_else(|| EngineError::Other("Content-Aware fill needs a selection".into()))?; @@ -175,3 +185,7 @@ mod mode_by_document_tests; #[cfg(test)] #[path = "fill_dissolve_opacity_rounding_tests.rs"] mod fill_dissolve_opacity_rounding_tests; + +#[cfg(test)] +#[path = "../fill_history_missing_layer_tests.rs"] +mod fill_history_missing_layer_tests; diff --git a/crates/engine-paint/src/fill_history_missing_layer_tests.rs b/crates/engine-paint/src/fill_history_missing_layer_tests.rs new file mode 100644 index 0000000000..b34f5551eb --- /dev/null +++ b/crates/engine-paint/src/fill_history_missing_layer_tests.rs @@ -0,0 +1,147 @@ +//! Edit › Fill with History on a pixel layer the source state does not have: the layer counts as +//! transparent there, so the selection is cleared (white on a layer without transparency, as after +//! Flatten Image), as one "Fill" state. Masks and channels the source lacks keep refusing (#17465). + +use serde_json::{Value, json}; + +use crate::Session; + +const TRANSPARENT: [f32; 4] = [0.0; 4]; +const GREEN: [f32; 4] = [0.0, 1.0, 0.0, 1.0]; +const RED: [f32; 4] = [1.0, 0.0, 0.0, 1.0]; +const WHITE: [f32; 4] = [1.0; 4]; + +/// The issue's document: `Paint` with red at 0,0-200,150 and 50 % blue at 200,0-400,150 on a +/// transparent (or white Background) canvas, no selection. Returns the session and the History +/// panel row of the finished setup, the source the fills read. +fn issue_document(white: bool) -> (Session, usize) { + let mut s = crate::session(); + s.execute("file.new", json!({"width": 400, "height": 300, "background": if white { "white" } else { "transparent" }})).unwrap(); + if white { + s.execute("layer.new.layer", json!({"name": "Paint"})).unwrap(); + } + rect(&mut s, [0, 0, 200, 150], "#ff0000", 100); + rect(&mut s, [200, 0, 200, 150], "#0000ff", 50); + s.execute("select.deselect", json!({})).unwrap(); + let row = steps(&s); + (s, row) +} + +fn rect(s: &mut Session, [x, y, width, height]: [i32; 4], color: &str, opacity: u32) { + s.execute("select.rect", json!({"x": x, "y": y, "width": width, "height": height})).unwrap(); + s.execute("edit.fill", json!({"contents": "color", "color": color, "opacity": opacity})).unwrap(); +} + +fn select_issue_rect(s: &mut Session) { + s.execute("select.rect", json!({"x": 100, "y": 50, "width": 200, "height": 200})).unwrap(); +} + +fn steps(s: &Session) -> usize { + s.active().unwrap().history.past_len() +} + +/// The active layer's pixel at (`x`, `y`). +fn px(s: &Session, x: i32, y: i32) -> [f32; 4] { + let st = s.active().unwrap(); + st.doc.layer(st.active_layer.unwrap()).unwrap().surface().unwrap().rgba(x, y) +} + +fn near(a: [f32; 4], b: [f32; 4]) -> bool { + a.iter().zip(b).all(|(x, y)| (x - y).abs() < 1.5 / 255.0) +} + +/// A History fill from `row` succeeds as one "Fill" state. +fn history_fill(s: &mut Session, row: usize, case: &str) { + let before = steps(s); + s.execute("edit.fill", json!({"contents": "history", "state": row})).unwrap_or_else(|e| panic!("{case}: {e}")); + assert_eq!(steps(s), before + 1, "{case}: one state"); + assert_eq!(s.active().unwrap().history.undo_label(), Some("Fill"), "{case}"); +} + +/// The selected area is cleared to `inside`, the rest keeps `outside`. +fn assert_cleared(s: &Session, inside: [f32; 4], outside: [f32; 4], case: &str) { + let (a, b) = (px(s, 150, 100), px(s, 50, 20)); + assert!(near(a, inside), "{case}: inside the selection {a:?}"); + assert!(near(b, outside), "{case}: outside the selection {b:?}"); +} + +#[test] +fn a_new_empty_layer_stays_transparent() { + let (mut s, row) = issue_document(false); + s.execute("layer.new.layer", json!({"name": "New"})).unwrap(); + select_issue_rect(&mut s); + history_fill(&mut s, row, "new layer"); + assert_cleared(&s, TRANSPARENT, TRANSPARENT, "new layer"); +} + +#[test] +fn a_duplicated_layer_is_cleared_inside_the_selection() { + let (mut s, row) = issue_document(false); + s.execute("layer.duplicate", json!({})).unwrap(); + s.execute("edit.fill", json!({"contents": "color", "color": "#00ff00"})).unwrap(); + select_issue_rect(&mut s); + history_fill(&mut s, row, "duplicate"); + assert_cleared(&s, TRANSPARENT, GREEN, "duplicate"); +} + +#[test] +fn a_layer_added_after_the_source_layer_was_deleted_is_cleared() { + let (mut s, row) = issue_document(false); + let paint = s.active().unwrap().active_layer.unwrap(); + s.execute("layer.new.layer", json!({"name": "New"})).unwrap(); + let new = s.active().unwrap().active_layer.unwrap(); + s.execute("edit.fill", json!({"contents": "color", "color": "#00ff00"})).unwrap(); + s.execute("layer.select", json!({"layer": paint.0})).unwrap(); + s.execute("layer.delete", json!({})).unwrap(); + s.execute("layer.select", json!({"layer": new.0})).unwrap(); + select_issue_rect(&mut s); + history_fill(&mut s, row, "Paint deleted"); + assert_cleared(&s, TRANSPARENT, GREEN, "Paint deleted"); +} + +/// Measured on both variants: after Flatten Image the selected area of the Background turns white, +/// whatever the background colour; the rest keeps the flattened pixels. +#[test] +fn after_flatten_image_the_background_takes_white() { + for white in [false, true] { + let (mut s, row) = issue_document(white); + s.execute("tools.setColors", json!({"background": "#0ac81e"})).unwrap(); + s.execute("layer.flattenImage", json!({})).unwrap(); + select_issue_rect(&mut s); + let case = format!("flatten, white background {white}"); + history_fill(&mut s, row, &case); + assert_cleared(&s, WHITE, RED, &case); + } +} + +/// A layer the source has keeps today's result: back to its pixels in that state. +#[test] +fn a_layer_the_source_has_is_restored() { + let (mut s, row) = issue_document(false); + s.execute("edit.fill", json!({"contents": "color", "color": "#00ff00"})).unwrap(); + select_issue_rect(&mut s); + history_fill(&mut s, row, "Paint"); + assert_cleared(&s, RED, GREEN, "Paint"); +} + +/// Not measured, so as before: a layer mask, an alpha channel or the Quick Mask the source lacks +/// is refused, adding no state and changing nothing. +#[test] +fn masks_channels_and_the_quick_mask_keep_refusing() { + let mut cases: Vec<(&str, Session, usize, Value)> = Vec::new(); + let (mut s, row) = issue_document(false); + s.execute("layer.layerMask.revealAll", json!({})).unwrap(); + cases.push(("layer mask", s, row, json!("mask"))); + let (mut s, row) = issue_document(false); + s.execute("channel.new", json!({"fill": "white"})).unwrap(); + cases.push(("alpha channel", s, row, json!({"channel": 0}))); + let (mut s, row) = issue_document(false); + s.execute("select.editInQuickMaskMode", json!({"on": true})).unwrap(); + cases.push(("Quick Mask", s, row, json!("quickMask"))); + for (what, mut s, row, target) in cases { + let (doc, before) = (s.active().unwrap().doc.clone(), steps(&s)); + assert!(s.execute("edit.fill", json!({"contents": "history", "state": row, "target": target})).is_err(), "{what}"); + assert_eq!(steps(&s), before, "{what}: no state"); + assert!(s.active().unwrap().doc == doc, "{what}: nothing changed"); + } +}