diff --git a/crates/engine-paint/src/cmd/fill_cmds.rs b/crates/engine-paint/src/cmd/fill_cmds.rs index 158041242f..2de505d99c 100644 --- a/crates/engine-paint/src/cmd/fill_cmds.rs +++ b/crates/engine-paint/src/cmd/fill_cmds.rs @@ -143,6 +143,11 @@ 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)?; + if !crate::history_source::same_mode_and_depth(st, &source_doc) { + return Err(EngineError::Other( + "Fill with History needs a source of the document's colour mode and bit depth: set a matching source in the History panel".into(), + )); + } 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")?) diff --git a/crates/engine-paint/src/fill_history_mode_depth_tests.rs b/crates/engine-paint/src/fill_history_mode_depth_tests.rs new file mode 100644 index 0000000000..431ba73deb --- /dev/null +++ b/crates/engine-paint/src/fill_history_mode_depth_tests.rs @@ -0,0 +1,111 @@ +//! Fill › History refuses a source state of another colour mode or bit depth than the document, +//! adding no state and changing no pixel, and fills again once they match (#17463). Both +//! `edit.fill` with `contents: "history"` and ⌘⌥⌫ (`edit.fillHistory`) are checked. + +use serde_json::{Value, json}; + +use crate::Session; + +const FILLS: [(&str, &str); 2] = [("edit.fill", "contents"), ("edit.fillHistory", "")]; + +/// Rows Open (white), Fill (red), Fill (white), with the red row as the History Brush source. +fn session() -> Session { + let mut s = crate::session(); + s.execute("file.new", json!({"width": 24, "height": 16, "mode": "rgb", "depth": 8, "background": "white"})).unwrap(); + s.execute("edit.fill", json!({"color": "#ff0000"})).unwrap(); + s.execute("edit.fill", json!({"color": "#ffffff"})).unwrap(); + s.execute("history.setSource", json!({"state": 1})).unwrap(); + s +} + +fn rows(s: &Session) -> usize { + s.active().unwrap().history.entries().len() +} + +/// Only `edit.fill` takes `contents`; the key command sets it itself. +fn params(key: &str) -> Value { + if key.is_empty() { json!({}) } else { json!({ key: "history" }) } +} + +fn refused(s: &mut Session, case: &str) { + for (cmd, key) in FILLS { + let (before, n) = (s.active().unwrap().doc.clone(), s.active().unwrap().history.past_len()); + let e = s.execute(cmd, params(key)).expect_err(&format!("{case}: {cmd}")).to_string(); + assert!(e.contains("colour mode and bit depth"), "{case}: {cmd}: {e}"); + assert_eq!(s.active().unwrap().history.past_len(), n, "{case}: {cmd}: no state"); + assert!(s.active().unwrap().doc == before, "{case}: {cmd}: no pixel changed"); + } +} + +/// Both commands fill from the source: the pixels change, one state named "Fill" is added (each +/// fill is undone before the next command runs). +fn fills(s: &mut Session, case: &str) { + for (cmd, key) in FILLS { + let (before, n) = (s.active().unwrap().doc.clone(), rows(s)); + s.execute(cmd, params(key)).unwrap_or_else(|e| panic!("{case}: {cmd}: {e}")); + assert_eq!(rows(s), n + 1, "{case}: {cmd}: one state"); + assert_eq!(s.active().unwrap().history.entries().last().map(String::as_str), Some("Fill"), "{case}: {cmd}"); + assert!(s.active().unwrap().doc != before, "{case}: {cmd}: the red source filled"); + s.execute("history.undo", json!({})).unwrap(); + } +} + +#[test] +fn another_mode_or_depth_than_the_source_is_refused() { + let changes = ["image.mode.bits16", "image.mode.bits32", "image.mode.lab", "image.mode.grayscale", "image.mode.cmyk", "image.mode.indexedColor"]; + for change in changes { + let mut s = session(); + s.execute(change, json!({})).unwrap(); + refused(&mut s, change); + } +} + +#[test] +fn a_matching_mode_and_depth_fills_again() { + for (there, back) in [("image.mode.bits16", "image.mode.bits8"), ("image.mode.lab", "image.mode.rgb")] { + let mut s = session(); + s.execute(there, json!({})).unwrap(); + s.execute(back, json!({})).unwrap(); + fills(&mut s, &format!("{there} and back")); + } +} + +/// What counts is the source state: a source taken after the conversion fills. +#[test] +fn a_source_set_to_the_converted_state_fills() { + for change in ["image.mode.bits16", "image.mode.indexedColor"] { + let mut s = session(); + s.execute("edit.fill", json!({"color": "#ff0000"})).unwrap(); + s.execute(change, json!({})).unwrap(); + let converted = rows(&s) - 1; + s.execute("edit.fill", json!({"color": "#ffffff"})).unwrap(); + s.execute("history.setSource", json!({"state": converted})).unwrap(); + fills(&mut s, change); + } +} + +#[test] +fn a_16_bit_source_with_an_8_bit_current_state_is_refused() { + let mut s = session(); + s.execute("image.mode.bits16", json!({})).unwrap(); + let converted = rows(&s) - 1; + s.execute("history.setSource", json!({"state": converted})).unwrap(); + s.execute("history.selectState", json!({"state": converted - 1})).unwrap(); + refused(&mut s, "stepped back to 8 bit"); +} + +/// Changes that keep the colour mode, bit depth and canvas size are not part of the rule. +#[test] +fn changes_outside_the_rule_keep_filling() { + let changes = [ + ("image.imageSize", json!({"resolution": 144, "resample": "none"})), + ("edit.convertToProfile", json!({"profile": "display-p3", "intent": "relative"})), + ("image.imageRotation.180", json!({})), + ("image.imageRotation.flipCanvasHorizontal", json!({})), + ]; + for (change, p) in changes { + let mut s = session(); + s.execute(change, p).unwrap(); + fills(&mut s, change); + } +} diff --git a/crates/engine-paint/src/history_brush_format_mismatch_tests.rs b/crates/engine-paint/src/history_brush_format_mismatch_tests.rs index ed85086e49..6561dc9404 100644 --- a/crates/engine-paint/src/history_brush_format_mismatch_tests.rs +++ b/crates/engine-paint/src/history_brush_format_mismatch_tests.rs @@ -134,8 +134,8 @@ fn the_live_preview_is_refused_too() { } } -/// Only the History Brush refuses: the Eraser's Erase to History and Fill › History were not -/// measured and keep converting the source. +/// Only the History Brush refuses with these texts: the Eraser's Erase to History was not measured +/// and keeps converting the source (Fill › History has its own rule, #17463). #[test] fn other_history_painting_keeps_todays_behaviour() { for change in ["image.mode.grayscale", "image.mode.bits16"] { @@ -143,8 +143,7 @@ fn other_history_painting_keeps_todays_behaviour() { s.execute(change, json!({})).unwrap(); let n = steps(&s); s.execute("paint.historyBrush", params(json!({"eraseToHistory": true}))).unwrap(); - s.execute("edit.fill", json!({"contents": "history", "state": 1})).unwrap(); - assert_eq!(steps(&s), n + 2, "{change}"); + assert_eq!(steps(&s), n + 1, "{change}"); // Only the absence of this tool's texts: the Art History Brush gets its own colour mode // refusal in #12826. if let Err(e) = s.execute("paint.artHistoryBrush", params(json!({}))) { diff --git a/crates/engine-paint/src/history_source.rs b/crates/engine-paint/src/history_source.rs index da4ce71e5d..45c3542a76 100644 --- a/crates/engine-paint/src/history_source.rs +++ b/crates/engine-paint/src/history_source.rs @@ -51,6 +51,14 @@ pub fn checked_source(st: &DocState, p: &Value, cmd: &str) -> Result bool { + source.mode == st.doc.mode && source.depth == st.doc.depth +} + /// The History Brush's refusals after Image › Mode, with Photoshop's alert texts (#12842): only /// the History Brush's pairs were measured (RGB to Grayscale, a depth change, and any other mode /// in an Indexed Color or Bitmap document, #17841), so any other change, or both at once, @@ -74,6 +82,10 @@ pub fn refuse_other_format(st: &DocState, source: &Document) -> Result<()> { #[path = "history_brush_format_mismatch_tests.rs"] mod history_brush_format_mismatch_tests; +#[cfg(test)] +#[path = "fill_history_mode_depth_tests.rs"] +mod fill_history_mode_depth_tests; + #[cfg(test)] #[path = "history_brush_indexed_bitmap_mode_alert_tests.rs"] mod history_brush_indexed_bitmap_mode_alert_tests;