diff --git a/crates/engine/src/cmd/pattern_fill_cmds.rs b/crates/engine/src/cmd/pattern_fill_cmds.rs index 96810dd059c..c59f2e4b99d 100644 --- a/crates/engine/src/cmd/pattern_fill_cmds.rs +++ b/crates/engine/src/cmd/pattern_fill_cmds.rs @@ -229,6 +229,45 @@ mod tests { assert_eq!(s.active().unwrap().doc.layers.len(), count + 1); } + /// Not empty: a layer opaque only through its default pixel (no tiles stored, as KRA imports + /// it) and a blank video layer. Both get a fill above and keep what they had. + #[test] + fn a_default_pixel_or_video_layer_is_not_taken_for_an_empty_one() { + let mut s = Session::new(); + s.execute("file.new", json!({"width": 40, "height": 30})).unwrap(); + let opaque = LayerId(s.execute("layer.new.layer", json!({})).unwrap()["layer"].as_u64().unwrap()); + let doc = Arc::make_mut(&mut s.active_mut().unwrap().doc); + let l = doc.layer_mut(opaque).unwrap(); + let fmt = l.surface().unwrap().format(); + l.content = LayerContent::Raster(photocraft_raster::Surface::with_default(fmt, &[0.2, 0.4, 0.6, 1.0])); + let video = LayerId(s.execute("layer.videoLayers.newBlankVideoLayer", json!({})).unwrap()["layer"].as_u64().unwrap()); + for old in [opaque, video] { + s.execute("layer.select", json!({"layer": old.0})).unwrap(); + let before = s.active().unwrap().doc.layer(old).unwrap().clone(); + let count = s.active().unwrap().doc.layers.len(); + let id = LayerId(s.execute(CREATE, json!({"pattern": "Checkerboard"})).unwrap()["layer"].as_u64().unwrap()); + assert_ne!(id, old, "a fill above {old:?}"); + assert_eq!(s.active().unwrap().doc.layers.len(), count + 1); + assert_eq!(*s.active().unwrap().doc.layer(old).unwrap(), before, "{old:?} keeps its pixels and video"); + } + } + + /// An empty layer stays empty through a mode change (its default pixel stays transparent). + #[test] + fn an_empty_layer_is_still_the_fill_after_a_mode_change() { + for mode in ["image.mode.cmyk", "image.mode.lab"] { + let mut s = Session::new(); + s.execute("file.new", json!({"width": 40, "height": 30})).unwrap(); + let empty = s.execute("layer.new.layer", json!({})).unwrap()["layer"].as_u64().unwrap(); + s.execute(mode, json!({})).unwrap(); + s.execute("layer.select", json!({"layer": empty})).unwrap(); + let count = s.active().unwrap().doc.layers.len(); + let id = s.execute(CREATE, json!({"pattern": "Checkerboard"})).unwrap()["layer"].as_u64().unwrap(); + assert_eq!(id, empty, "{mode}"); + assert_eq!(s.active().unwrap().doc.layers.len(), count, "{mode}"); + } + } + #[test] fn preview_matches_what_the_command_commits() { let (mut s, id) = session(8); diff --git a/crates/engine/src/cmd/stamp_cmds.rs b/crates/engine/src/cmd/stamp_cmds.rs index 04f3b4ba2fa..1f96831f7ec 100644 --- a/crates/engine/src/cmd/stamp_cmds.rs +++ b/crates/engine/src/cmd/stamp_cmds.rs @@ -82,11 +82,16 @@ fn stamp_visible(s: &mut Session, p: &Value) -> Result { Ok(json!({ "layer": id.0 })) } -/// A plain, unlocked pixel layer with no pixels, mask or clipping: safe to stamp into. +/// A plain, unlocked pixel layer with no pixels, mask, clipping or video: safe to stamp into. +/// Pixels it has only through a visible default pixel (no tiles stored) count as pixels. pub(crate) fn is_empty_pixel_layer(doc: &Document, id: LayerId) -> bool { let Some(l) = doc.layer(id) else { return false }; let locks = doc.effective_locks(id); - matches!(&l.content, LayerContent::Raster(s) if s.content_bounds().is_empty()) + // Only the default pixel's alpha counts: a mode or profile conversion keeps it transparent but + // gives its colour channels other values (no ink in CMYK, a neutral a/b in Lab). + matches!(&l.content, LayerContent::Raster(s) + if s.content_bounds().is_empty() && s.format().alpha && s.default_pixel().last().is_some_and(|a| *a <= 0.0)) + && l.video.is_none() && l.visible && l.mask.is_none() && l.vector_mask.is_none() @@ -277,6 +282,45 @@ mod tests { assert_eq!(ids(&s).len(), 5); } + /// A layer opaque only through its default pixel (no tiles, as KRA imports it) and a blank + /// video layer are not empty: the stamp goes into a new layer above them. + #[test] + fn stamp_visible_keeps_default_pixel_and_video_layers() { + let (mut s, ..) = session(8); + s.execute("layer.new.layer", json!({"name": "Opaque"})).unwrap(); + let opaque = s.active().unwrap().active_layer.unwrap(); + let l = std::sync::Arc::make_mut(&mut s.active_mut().unwrap().doc).layer_mut(opaque).unwrap(); + let fmt = l.surface().unwrap().format(); + l.content = LayerContent::Raster(photocraft_raster::Surface::with_default(fmt, &[0.0, 1.0, 0.0, 1.0])); + let video = LayerId(s.execute("layer.videoLayers.newBlankVideoLayer", json!({})).unwrap()["layer"].as_u64().unwrap()); + for old in [opaque, video] { + s.execute("layer.select", json!({"layer": old.0})).unwrap(); + let before = s.active().unwrap().doc.layer(old).unwrap().clone(); + let n = ids(&s).len(); + let r = s.execute("layer.stampVisible", json!({})).unwrap(); + assert_ne!(r["layer"].as_u64(), Some(old.0), "a new layer above {old:?}"); + assert_eq!(ids(&s).len(), n + 1); + assert_eq!(*s.active().unwrap().doc.layer(old).unwrap(), before, "{old:?} keeps its pixels and video"); + } + } + + /// An empty layer stays empty through a mode change, though its default pixel's colour + /// bytes change: the stamp still goes into it. + #[test] + fn stamp_visible_fills_an_empty_layer_after_a_mode_change() { + for mode in ["image.mode.cmyk", "image.mode.lab"] { + let (mut s, ..) = session(8); + s.execute("layer.new.layer", json!({"name": "Empty"})).unwrap(); + let empty = s.active().unwrap().active_layer.unwrap(); + s.execute(mode, json!({})).unwrap(); + s.execute("layer.select", json!({"layer": empty.0})).unwrap(); + let n = ids(&s).len(); + let r = s.execute("layer.stampVisible", json!({})).unwrap(); + assert_eq!(r["layer"].as_u64(), Some(empty.0), "{mode}"); + assert_eq!(ids(&s).len(), n, "{mode}"); + } + } + #[test] fn stamp_visible_respects_opacity_blend_and_groups() { let (mut s, bg, _, blue) = session(8);