From bcf271490c65d6c44f022f94e3034865ff9f9595 Mon Sep 17 00:00:00 2001 From: NaufalDzz Date: Tue, 27 Oct 2026 22:05:07 +0700 Subject: [PATCH 1/2] Convert to Layers keeps a knockout layer isolated in a group (#9753) A smart object isolates its contents, so a Shallow or Deep knockout in its single layer stops at the smart object's bottom. Convert to Layers folded that layer straight into the parent stack, where the knockout punched through unrelated layers to the Background. A knockout layer now keeps the Normal group, as Photoshop's Convert to Layers does. Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/cmd/smart_cmds/tests.rs | 32 ++++++++++++++++++++++ crates/engine/src/cmd/smart_cmds/unpack.rs | 6 ++-- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/crates/engine/src/cmd/smart_cmds/tests.rs b/crates/engine/src/cmd/smart_cmds/tests.rs index 6e8ce37f9cd..d836b0356d7 100644 --- a/crates/engine/src/cmd/smart_cmds/tests.rs +++ b/crates/engine/src/cmd/smart_cmds/tests.rs @@ -938,6 +938,38 @@ 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 +/// Normal group keeps the look, as Photoshop's Convert to Layers does. +#[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 ["shallow", "deep"] { + 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(); + convert(&mut s); + let before = flat(&s); + let r = convert_to_layers(&mut s); + assert_eq!(r["group"], true, "{knockout}: the contents stay isolated"); + let g = active_layer(&s); + assert_eq!(g.blend, BlendMode::Normal, "{knockout}"); + assert!(max_diff(&flat(&s), &before) <= 1.0 / 255.0 + 1e-6, "{knockout}: the look is kept"); + assert_eq!(g.children().unwrap()[0].advanced.knockout.name(), knockout, "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 e6ce545dc87..cd36d7c1757 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 photocraft_algo::transform::Interp; use photocraft_cms::Intent; 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 From f0b87c883c52110436a9947c7ff965de1d9fb045 Mon Sep 17 00:00:00 2001 From: NaufalDzz Date: Mon, 2 Nov 2026 14:56:38 +0700 Subject: [PATCH 2/2] test(engine): Convert to Layers keeps blend and opacity with and without knockout --- crates/engine/src/cmd/smart_cmds/tests.rs | 48 ++++++++++++++--------- 1 file changed, 30 insertions(+), 18 deletions(-) diff --git a/crates/engine/src/cmd/smart_cmds/tests.rs b/crates/engine/src/cmd/smart_cmds/tests.rs index d836b0356d7..3f88a032021 100644 --- a/crates/engine/src/cmd/smart_cmds/tests.rs +++ b/crates/engine/src/cmd/smart_cmds/tests.rs @@ -940,7 +940,8 @@ fn convert_to_layers_keeps_a_smart_layer_mask_on_a_group() { /// #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 -/// Normal group keeps the look, as Photoshop's Convert to Layers does. +/// 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]| { @@ -950,23 +951,34 @@ fn convert_to_layers_keeps_a_knockout_inside_a_group() { }) .unwrap(); }; - for knockout in ["shallow", "deep"] { - 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(); - convert(&mut s); - let before = flat(&s); - let r = convert_to_layers(&mut s); - assert_eq!(r["group"], true, "{knockout}: the contents stay isolated"); - let g = active_layer(&s); - assert_eq!(g.blend, BlendMode::Normal, "{knockout}"); - assert!(max_diff(&flat(&s), &before) <= 1.0 / 255.0 + 1e-6, "{knockout}: the look is kept"); - assert_eq!(g.children().unwrap()[0].advanced.knockout.name(), knockout, "the knockout setting survives"); + 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"); + } } }