diff --git a/crates/compose/src/effects.rs b/crates/compose/src/effects.rs index 6966254547f..5acdd793144 100644 --- a/crates/compose/src/effects.rs +++ b/crates/compose/src/effects.rs @@ -13,7 +13,8 @@ //! Interior effects are painted relative to the layer's shape and then take the shape's alpha //! (an overlay recolours a half-transparent edge without adding coverage). The outside parts //! of strokes blend onto the backdrop with their own modes, an upper stroke covering the -//! lower ones. Drop shadows are knocked out only through see-through fill. +//! lower ones. Drop shadows are knocked out through see-through fill, and beneath the whole shape +//! when the layer's mode lets what's under it show through (see [`shadow_see_through`]). //! //! Shapes come from the layer's alpha (after its mask). A stroke's outer edge, and precise glows / //! chiselled bevels, use an exact Euclidean transform: a 5 × 5 chamfer's level sets are polygons, @@ -1850,6 +1851,16 @@ fn knock_out_exterior(map: &mut Map, shape: &Map, fill_opacity: f32) { } } +/// How far the layer lets a knocked-out drop shadow show beneath its own shape (Layer Knocks Out +/// Drop Shadow), 0 = covered, 1 = knocked out wholly. In a normal mode an opaque layer covers the +/// shadow anyway, so only see-through fill knocks it out (and anti-aliased edges are not +/// attenuated twice). In any other mode the layer blends with what's beneath it, shadow and all: +/// an Exclusion or Difference layer would invert it, Multiply darken by it, Screen show it as +/// grey. Photoshop keeps it out from under the shape, so here it's knocked out wholly. +pub fn shadow_see_through(layer: &Layer) -> f32 { + if matches!(layer.blend, BlendMode::Normal | BlendMode::Dissolve | BlendMode::PassThrough) { 1.0 - layer.fill_opacity.clamp(0.0, 1.0) } else { 1.0 } +} + /// Blend Interior Effects as Group off (Photoshop's default): the layer's mode applies to its /// own pixels only, and the interior effects paint over the blended result. The layer is merged /// into the backdrop before they paint, so an Overlay colour overlay on a black Color-mode layer @@ -1916,10 +1927,10 @@ pub(crate) fn composite_with_effects_prepared( if let Effect::DropShadow(s) = e { let mut m = fx(i, 0); if s.knocks_out { - // The layer hides the shadow beneath it only where its fill is see-through: at - // 100 % fill the layer covers it anyway (and anti-aliased edges are not - // attenuated twice), at 0 % the shape shows the bare backdrop. - knock_out_exterior(&mut m, &shape, layer.fill_opacity); + // The layer hides the shadow beneath it where its fill is see-through (at 100 % + // fill in a normal mode it covers it anyway; at 0 % the shape shows the bare + // backdrop), and wholly in a mode that blends with it ([`shadow_see_through`]). + knock_out_exterior(&mut m, &shape, 1.0 - shadow_see_through(layer)); } paint_color(&mut work, &m, rgb(&s.color), s.common.blend, s.common.opacity); } diff --git a/crates/compose/src/tests.rs b/crates/compose/src/tests.rs index f9e49ac04ed..778d9cf29a0 100644 --- a/crates/compose/src/tests.rs +++ b/crates/compose/src/tests.rs @@ -1473,6 +1473,36 @@ fn shadow_knockout_needs_see_through_fill() { assert!(close4(on, px(&base, 10, 20)), "{on:?}"); } +#[test] +fn shadow_knockout_beneath_a_layer_in_a_blending_mode() { + // Layer Knocks Out Drop Shadow keeps the shadow out from under the layer's shape. In Normal + // mode an opaque layer covers it anyway; in a mode that blends with what's beneath, the shadow + // would show through: under a white Exclusion square on white, it read as white (white over + // the black shadow) instead of black (white over white), so a logo's Exclusion bar with a + // drop shadow came out blank. A red Multiply square came out black instead of red. + for (blend, colour, inside, depth) in [ + (photocraft_color::BlendMode::Exclusion, [1.0, 1.0, 1.0, 1.0], [0.0, 0.0, 0.0, 1.0]), + (photocraft_color::BlendMode::Multiply, [1.0, 0.0, 0.0, 1.0], [1.0, 0.0, 0.0, 1.0]), + ] + .into_iter() + .flat_map(|c| SampleType::ALL.map(|depth| (c, depth))) + .map(|((b, c, i), depth)| (b, c, i, depth)) + { + let mut d = Document::new("t", Size::new(40, 40), ColorMode::Rgb, depth); + d.layers.push(solid_layer("bg", Rect::new(0, 0, 40, 40), [1.0; 4])); + let mut l = solid_layer("sq", Rect::new(10, 10, 30, 30), colour); + l.blend = blend; + l.effects.items = vec![Effect::DropShadow(shadow(0.0, 90.0))]; + d.layers.push(l); + assert!(close4(px(&d, 20, 20), inside), "{blend:?} {depth:?}: {:?}", px(&d, 20, 20)); + // Not knocked out, the shadow shows through the blend, as in Photoshop with the box unticked. + if let Effect::DropShadow(s) = &mut d.layers[1].effects.items[0] { + s.knocks_out = false; + } + assert!(!close4(px(&d, 20, 20), inside), "{blend:?} {depth:?} without knockout: {:?}", px(&d, 20, 20)); + } +} + #[test] fn linked_pattern_overlay_anchors_at_the_effects_reference_point() { use photocraft_doc::pattern::Pattern; diff --git a/crates/gpu/src/plan.rs b/crates/gpu/src/plan.rs index b8c083c7af4..48abbc09300 100644 --- a/crates/gpu/src/plan.rs +++ b/crates/gpu/src/plan.rs @@ -1097,9 +1097,10 @@ impl<'a> Planner<'a> { }; for &(i, e) in &rev { if let Effect::DropShadow(s) = e { - // The layer hides the shadow beneath it where its fill is see-through. + // The layer hides the shadow beneath it where its fill is see-through, and wholly + // in a mode that blends with it (as the CPU renderer: `shadow_see_through`). let flags = if s.knocks_out { F_KNOCKOUT } else { 0 }; - let see_through = 1.0 - layer.fill_opacity.clamp(0.0, 1.0); + let see_through = photocraft_compose::effects::shadow_see_through(layer); w = self.paint_k( w, content,