Files
photocraft/crates/engine/tests/it/gradient_overlay_strokes.rs
T
437cd7f0fe fix: brush hints keep a feathered mask's ring and the effects' reach (#9329) (#10104)
* fix: brush hints keep a feathered mask's ring and the effects' reach (#9329)

A command's damage hint (the bare dab) was intersected with the edit's
own damage, which `tile_damage` had grown by the mask's feather and the
layer's and enclosing groups' effects. The intersection cut that growth
off, so painting on a feathered layer mask left a stale soft ring around
each dab, and a dab's drop shadow was outside the reported damage.

`hint_damage` now grows the hint by the same rule first (`grow_hint`,
sharing `grow_in_layer` and `effects_reach` with `tile_damage` and the
group walk). The hint is still checked against the changed tiles as is.

The group-shadow test now cuts a hole at a tile's right edge, so its
shadow crosses into the next tile (it passed without the group's growth
before), and the clip-base test gains a dab at a tile edge.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* fix: brush hints skip the effects of layers that aren't drawn (#9329)

`grow_hint` grew a dab by the effects of every layer in the document,
hidden ones included, so a large drop shadow on a hidden layer widened
every `paint.stroke` dab elsewhere to all the tiles `step_damage`
reported, and each dab recomposited and uploaded that much.

It now skips layers the compositor skips, by the same rule as
`effect_layers`: a hidden layer, a clipped layer over a hidden base, and
everything inside a hidden group. A change there shows nowhere, and
every drawn layer still grows the hint as before.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* test: the group-shadow brush test goes through the hint (#9329)

`a_dab_inside_a_group_with_a_drop_shadow_damages_the_shadow` painted
with a plain `Session::edit`, so its damage came from `step_damage` and
never exercised `grow_hint`. It now paints with `paint.stroke`, and the
child carries a shadow of its own inside the shadowed group: the group's
shadow of the child's shadow lands past either shadow's reach alone, in
the next tile, so dropping the enclosing groups' reach from `grow_hint`
fails it.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Co-authored-by: FredApps <153173854+FredApps@users.noreply.github.com>
Co-authored-by: FredApps <FredApps@users.noreply.github.com>
2026-11-09 16:36:58 +01:00

93 lines
4.9 KiB
Rust

//! A Gradient Overlay spans one gradient over the layer's current bounds, so a stroke that grows
//! the layer re-lays the gradient out over all of it (#4053). The damage a stroke reports must
//! cover that, or a canvas that redraws only the damage keeps the old gradient around the earlier
//! strokes: one gradient per stroke instead of one over the whole layer.
use photocraft_engine::Session;
use serde_json::json;
/// What a canvas that redraws only each edit's damage shows: the cold render before the edit,
/// with the edit's damage (grown by the effects' reach, as the canvas grows it) rendered again.
fn redraw_damage(shown: &mut photocraft_compose::Buffer, s: &Session) {
let st = s.active().unwrap();
let reach = st.doc.layers.iter().filter(|l| photocraft_compose::effects::has_effects(l)).map(photocraft_compose::effects::margin).max().unwrap_or(0);
let damage = st.last_damage.expect("a stroke reports bounded damage").inflate(reach).intersect(&st.doc.bounds());
let (w, h) = (st.doc.size.width, st.doc.size.height);
let patch = photocraft_compose::render_reduced_damage(&st.doc, w, h, damage);
for y in patch.rect.y0..patch.rect.y1 {
for x in patch.rect.x0..patch.rect.x1 {
let i = (y * w as i32 + x) as usize;
shown.px[i] = patch.get(x, y);
}
}
}
fn session(effect: &str, params: serde_json::Value) -> Session {
let mut s = Session::new();
s.execute("file.new", json!({"width": 160, "height": 80, "background": "white"})).unwrap();
s.execute("layer.new.layer", json!({})).unwrap();
s.execute(effect, params).unwrap();
s
}
const GREY: &str = "#808080";
/// Each stroke redrawn over the last cold render equals a cold render after it.
fn strokes_redraw_like_a_cold_render(mut s: Session, strokes: &[serde_json::Value]) {
s.execute("paint.stroke", json!({"points": [[20, 20], [40, 30]], "size": 12, "hardness": 1.0, "color": GREY})).unwrap();
for (i, p) in strokes.iter().enumerate() {
let st = s.active().unwrap();
let mut shown = photocraft_compose::render(&st.doc, st.doc.bounds());
s.execute("paint.stroke", p.clone()).unwrap();
redraw_damage(&mut shown, &s);
let st = s.active().unwrap();
let cold = photocraft_compose::render(&st.doc, st.doc.bounds());
let worst = shown.px.iter().zip(&cold.px).map(|(a, b)| (0..4).map(|c| (a[c] - b[c]).abs()).fold(0.0f32, f32::max)).fold(0.0f32, f32::max);
assert!(worst < 1e-6, "after stroke {i} the redrawn canvas differs from a cold render by {worst}");
}
}
#[test]
fn a_gradient_overlay_is_laid_out_again_after_each_stroke() {
let s = session("layer.layerStyle.gradientOverlay", json!({"from": "#ff0000", "to": "#0000ff", "angle": 0}));
strokes_redraw_like_a_cold_render(
s,
&[
// Far from the first: the layer grows to the right and down.
json!({"points": [[120, 50], [140, 64]], "size": 12, "hardness": 1.0, "color": GREY}),
// Up and to the left of both.
json!({"points": [[6, 6], [10, 8]], "size": 6, "hardness": 1.0, "color": GREY}),
// Erasing the far stroke shrinks the layer again.
json!({"points": [[110, 40], [150, 74]], "size": 40, "hardness": 1.0, "erase": true}),
],
);
}
#[test]
fn a_stroke_inside_the_layer_keeps_its_own_damage() {
let mut s = session("layer.layerStyle.gradientOverlay", json!({"from": "#ff0000", "to": "#0000ff", "angle": 0}));
s.execute("paint.stroke", json!({"points": [[10, 10], [150, 70]], "size": 12, "hardness": 1.0, "color": GREY})).unwrap();
// Within the layer's bounds: the gradient's frame stays, so only the stroke repaints (grown
// by the effects' margin, as any change to the layer's pixels is).
let r = s.execute("paint.stroke", json!({"points": [[60, 40], [70, 40]], "size": 6, "hardness": 1.0, "color": GREY})).unwrap();
let d = &r["damage"];
let reported = photocraft_geom::Rect::new(
d[0].as_i64().unwrap() as i32,
d[1].as_i64().unwrap() as i32,
(d[0].as_i64().unwrap() + d[2].as_i64().unwrap()) as i32,
(d[1].as_i64().unwrap() + d[3].as_i64().unwrap()) as i32,
);
let st = s.active().unwrap();
let margin = photocraft_compose::effects::margin(st.doc.layer(st.active_layer.unwrap()).unwrap());
assert_eq!(st.last_damage, Some(reported.inflate(margin)));
}
#[test]
fn effects_not_laid_out_over_the_layer_keep_the_stroke_damage() {
let mut s = session("layer.layerStyle.colorOverlay", json!({"color": "#00ff00"}));
s.execute("paint.stroke", json!({"points": [[20, 20], [40, 30]], "size": 12, "hardness": 1.0, "color": GREY})).unwrap();
s.execute("paint.stroke", json!({"points": [[120, 50], [140, 64]], "size": 12, "hardness": 1.0, "color": GREY})).unwrap();
let d = s.active().unwrap().last_damage.unwrap();
assert!(d.x0 >= 100, "a Color Overlay repaints only the stroke: {d:?}");
}