diff --git a/crates/engine/src/cmd/smart_cmds/tests.rs b/crates/engine/src/cmd/smart_cmds/tests.rs index 2b1af572165..041e1113140 100644 --- a/crates/engine/src/cmd/smart_cmds/tests.rs +++ b/crates/engine/src/cmd/smart_cmds/tests.rs @@ -1126,6 +1126,50 @@ fn convert_to_layers_keeps_a_smart_layer_mask_on_a_group() { assert_eq!(max_diff(&flat(&s), &before), 0.0); } +/// #9753: a smart object isolates its contents, so a knockout inside it stops at its bottom. +/// Folded into the parent stack, it would punch through the layers beneath to the Background; a +/// group keeps the look, as Photoshop's Convert to Layers does. The group takes the smart object's +/// blend mode and opacity, and without a knockout the one layer still folds as before. +#[test] +fn convert_to_layers_keeps_a_knockout_inside_a_group() { + let fill = |s: &mut Session, r: Rect, rgba: [f32; 4]| { + s.edit("paint", |doc, active| { + doc.layer_mut(active.unwrap()).unwrap().surface_mut().unwrap().fill_rect(r, &rgba); + Ok(()) + }) + .unwrap(); + }; + for knockout in ["none", "shallow", "deep"] { + for (blend, mode, opacity) in [("normal", BlendMode::Normal, 1.0), ("multiply", BlendMode::Multiply, 0.6)] { + let case = format!("{knockout}, {blend} at {opacity}"); + let mut s = Session::new(); + s.execute("file.new", json!({"width": W, "height": H})).unwrap(); + fill(&mut s, Rect::new(0, 0, W, H), [0.0, 0.0, 1.0, 1.0]); + s.execute("layer.new.layer", json!({})).unwrap(); + fill(&mut s, Rect::new(0, 0, W, H), [1.0, 1.0, 0.0, 1.0]); + s.execute("layer.new.layer", json!({})).unwrap(); + fill(&mut s, Rect::new(10, 10, 30, 30), [1.0, 0.0, 0.0, 1.0]); + s.execute("layer.layerStyle.blendingOptions", json!({"knockout": knockout, "fillOpacity": 50})).unwrap(); + let id = convert(&mut s); + s.execute("layer.setProps", json!({"layer": id, "blend": blend, "opacity": opacity})).unwrap(); + let before = flat(&s); + let r = convert_to_layers(&mut s); + let g = active_layer(&s); + assert!(max_diff(&flat(&s), &before) <= 1.0 / 255.0 + 1e-6, "{case}: the look is kept"); + assert_eq!(g.blend, mode, "{case}"); + if knockout == "none" { + // Fill opacity on the one layer folds into it unchanged, as before #9753. + assert_eq!(r["group"], false, "{case}: folds into one layer"); + assert!((g.opacity - opacity).abs() < 1e-6, "{case}: opacity {}", g.opacity); + continue; + } + assert_eq!(r["group"], true, "{case}: the contents stay isolated"); + assert!((g.opacity - opacity).abs() < 1e-6, "{case}: the group's opacity {}", g.opacity); + assert_eq!(g.children().unwrap()[0].advanced.knockout.name(), knockout, "{case}: the knockout setting survives"); + } + } +} + #[test] fn convert_to_layers_applies_the_placement() { let mut s = session(8); diff --git a/crates/engine/src/cmd/smart_cmds/unpack.rs b/crates/engine/src/cmd/smart_cmds/unpack.rs index a77941e19cd..a7d1eb9030d 100644 --- a/crates/engine/src/cmd/smart_cmds/unpack.rs +++ b/crates/engine/src/cmd/smart_cmds/unpack.rs @@ -9,7 +9,7 @@ use std::sync::Arc; use craft_cms::Intent; use photocraft_algo::transform::Interp; use photocraft_color::BlendMode; -use photocraft_doc::{Document, Group, Layer, LayerContent, LayerId, Metadata, SmartObject, SmartSource}; +use photocraft_doc::{Document, Group, Knockout, Layer, LayerContent, LayerId, Metadata, SmartObject, SmartSource}; use serde_json::{Value, json}; use super::{authorize_source, authorized_source_bytes, decode_source, detach_psd, other, placement, shift_layer, smart}; @@ -131,7 +131,8 @@ fn released(l: &mut Layer, f: impl FnOnce(&mut Layer) -> Result<()>) -> Result<( /// Whether the smart layer's own properties fold into its one unpacked layer `c` without /// changing the look: nothing on the smart layer acts on `c`'s composite (masks, style, fill /// opacity, Blend If, channel exclusions), and `c` blends normally and doesn't look at what's -/// beneath it (alone in the smart object, it lands on transparency). +/// beneath it (alone in the smart object, it lands on transparency; a knockout there stops at +/// the smart object's bottom instead of punching through to the Background, #9753). fn foldable(so: &Layer, c: &Layer) -> bool { so.mask.is_none() && so.vector_mask.is_none() @@ -143,6 +144,7 @@ fn foldable(so: &Layer, c: &Layer) -> bool { && matches!(c.blend, BlendMode::Normal | BlendMode::PassThrough) && c.blend_if.is_default() && c.excluded_channels == 0 + && c.advanced.knockout == Knockout::None } /// What replaces smart layer `so`: its one unpacked layer with `so`'s properties folded in, or a