Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Group effects follow their children's effects, not just their pixels (#3274) (#3326)

alexanderalber980cf05be492
diff --git a/crates/compose/src/lib.rs b/crates/compose/src/lib.rs
index 712033e54..37c6167e9 100644
--- a/crates/compose/src/lib.rs
+++ b/crates/compose/src/lib.rs
@@ -628,6 +628,27 @@ pub fn layer_bounds(layer: &Layer, canvas: Rect) -> Rect {
}
}
+/// Where a layer's effects take their shape from: [`layer_bounds`], except that a group's
+/// children count with their own effects (a child's outside stroke shapes the group's drop
+/// shadow and must not be cut off at the child's pixels, #3274).
+pub fn effect_source_bounds(layer: &Layer, canvas: Rect) -> Rect {
+ match &layer.content {
+ LayerContent::Group(g) if g.artboard.is_none() && !advanced::shapeless(layer) => g.children.iter().filter(|c| c.visible).fold(Rect::EMPTY, |acc, c| {
+ let m = if effects::has_effects(c) { effects::margin(c) } else { 0 };
+ let b = effect_source_bounds(c, canvas);
+ let b = if b.is_empty() { b } else { b.inflate(m) };
+ if b.is_empty() {
+ acc
+ } else if acc.is_empty() {
+ b
+ } else {
+ acc.union(&b)
+ }
+ }),
+ _ => layer_bounds(layer, canvas),
+ }
+}
+
/// The path a shape layer's effects are shaped by: its fill path. Photoshop builds a filled
/// shape's layer effects from its vector outline, not from its pixels: a stroke runs along the
/// whole path even where a gradient fill fades out (psd-tools stroke-effects). `None` for other
@@ -851,9 +872,10 @@ fn render_fill(f: &Fill, rect: Rect, canvas: Rect, patterns: &pattern::PreparedP
fn empty_in(layer: &Layer, rect: Rect) -> bool {
if effects::has_effects(layer) {
// Effects reach at most `margin` beyond the layer's pixels (when it is transparent
- // outside them): render tiles away from a small text layer skip it entirely.
+ // outside them; a group's from its children with their effects): render tiles away from a
+ // small text layer skip it entirely.
let canvas = Rect::new(i32::MIN / 4, i32::MIN / 4, i32::MAX / 4, i32::MAX / 4);
- let bounds = shapeless_stroke_bounds(layer).unwrap_or_else(|| layer_bounds(layer, canvas));
+ let bounds = shapeless_stroke_bounds(layer).unwrap_or_else(|| effect_source_bounds(layer, canvas));
return transparent_outside(layer) && bounds.inflate(effects::margin(layer)).intersect(&rect).is_empty();
}
match &layer.content {
@@ -1453,7 +1475,7 @@ fn fx_cache() -> &'static std::sync::Mutex<FxCache> {
/// (or anything else rendering children over a wider area) gets the whole region.
fn effect_region(layer: &Layer, rect: Rect, cx: &Ctx) -> (Rect, bool) {
let m = effects::margin(layer);
- let region = layer_bounds(layer, cx.canvas).inflate(m).intersect(&cx.canvas.inflate(m));
+ let region = effect_source_bounds(layer, cx.canvas).inflate(m).intersect(&cx.canvas.inflate(m));
let clip = *cx.fx_clip.lock().unwrap_or_else(|e| e.into_inner());
match clip {
Some(c) if c.contains_rect(&rect) && advanced::shapeless(layer) && effects::maps_are_local(layer) => {
diff --git a/crates/compose/src/tests.rs b/crates/compose/src/tests.rs
index 1bb4afc16..853767ae8 100644
--- a/crates/compose/src/tests.rs
+++ b/crates/compose/src/tests.rs
@@ -652,6 +652,22 @@ fn drop_shadow_falls_away_from_light() {
assert!(close4(px(&d, 32, 20), [1.0; 4]));
}
+#[test]
+fn group_effects_reach_past_children_effects() {
+ // A child's outside stroke reaches further than its group's drop shadow: the group's shadow
+ // follows the stroke, not the child's pixels alone (#3274).
+ let mut d = doc_white(48, 48);
+ let mut sq = solid_layer("sq", Rect::new(14, 14, 34, 34), [1.0, 0.0, 0.0, 1.0]);
+ sq.effects.items = vec![stroke(6.0, StrokePosition::Outside)];
+ let mut grp = Layer::group("g", vec![sq]);
+ grp.effects.items = vec![Effect::DropShadow(shadow(2.0, 90.0))];
+ d.layers.push(grp);
+ assert!(close4(px(&d, 9, 24), [0.0, 0.0, 1.0, 1.0]), "stroke left of the pixels: {:?}", px(&d, 9, 24));
+ assert!(close4(px(&d, 24, 38), [0.0, 0.0, 1.0, 1.0]), "stroke below the pixels: {:?}", px(&d, 24, 38));
+ assert!(close4(px(&d, 24, 41), [0.0, 0.0, 0.0, 1.0]), "shadow below the stroke: {:?}", px(&d, 24, 41));
+ assert!(close4(px(&d, 24, 43), [1.0; 4]), "nothing past the shadow: {:?}", px(&d, 24, 43));
+}
+
#[test]
fn drop_shadow_uses_global_light_and_knockout() {
let mut d = fx_doc(vec![Effect::DropShadow(Shadow { use_global_light: true, ..shadow(5.0, 0.0) })]);
diff --git a/crates/gpu/src/bounds.rs b/crates/gpu/src/bounds.rs
index 2a833846d..0dbdf4aa3 100644
--- a/crates/gpu/src/bounds.rs
+++ b/crates/gpu/src/bounds.rs
@@ -14,14 +14,15 @@ pub fn layer_bounds(layer: &Layer, canvas: Rect) -> Rect {
photocraft_compose::layer_bounds(layer, canvas)
}
-/// The region a layer's effect maps cover: its bounds grown by the effect reach, within the
+/// The region a layer's effect maps cover: its effect source bounds (for a group, its children
+/// with their effects: `photocraft_compose::effect_source_bounds`) grown by the effect reach, within the
/// canvas grown likewise (`photocraft_compose::effect_maps`) when the layer reaches far beyond
/// it. A layer about the canvas's size keeps its whole region even where it overhangs the edge:
/// then the region keeps its size while the layer moves (a Move tool drag), so its maps move with
/// it instead of being rebuilt every frame (#761). The maps are exact either way.
pub fn effect_region(layer: &Layer, canvas: Rect) -> Rect {
let m = photocraft_compose::effects::margin(layer);
- let (whole, window) = (layer_bounds(layer, canvas).inflate(m), canvas.inflate(m));
+ let (whole, window) = (photocraft_compose::effect_source_bounds(layer, canvas).inflate(m), canvas.inflate(m));
let area = |r: Rect| u64::from(r.width()) * u64::from(r.height());
if area(whole) <= area(window).saturating_add(area(window) / 4) { whole } else { whole.intersect(&window) }
}
diff --git a/crates/gpu/tests/parity.rs b/crates/gpu/tests/parity.rs
index 9681a0756..fca32a02c 100644
--- a/crates/gpu/tests/parity.rs
+++ b/crates/gpu/tests/parity.rs
@@ -1466,6 +1466,14 @@ fn layer_effects_on_groups_clipping_and_fills() {
d.layers.push(grp);
fx_check(&mut g, &d, &format!("group {blend:?}"));
}
+ // A child's stroke reaching past the group's own effect reach (#3274).
+ let mut d = fx_doc(100, 80, SampleType::U8);
+ let mut a = blob("a", d.pixel_format(), 45.0, 40.0, 16.0, [0.6, 0.6, 0.6]);
+ a.effects.items = vec![Effect::Stroke(stroke(8.0, StrokePosition::Outside, FxPaint::Color(Color::rgb(0.0, 0.0, 0.0))))];
+ let mut grp = Layer::group("g", vec![a]);
+ grp.effects.items = vec![Effect::DropShadow(shadow(BlendMode::Multiply, 0.8, 120.0, 3.0, 2.0, 0.0))];
+ d.layers.push(grp);
+ fx_check(&mut g, &d, "group effects around a child's stroke");
// An effect layer as a clipping base, and a clipped layer with effects.
let mut d = fx_doc(100, 80, SampleType::U8);
let mut base = blob("base", d.pixel_format(), 45.0, 40.0, 24.0, [0.3, 0.8, 0.4]);