mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Merge pull request #12809 from rmsff/fix/art-history-noop-state-12636
Art History Brush: a click held back by the tolerance adds its state
This commit is contained in:
2 files changed
+162
-6
No files matched your search
@@ -215,8 +215,8 @@ pub fn run(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
return Err(err("stroke too large"));
|
||||
}
|
||||
|
||||
// Nothing written (every dab under the tolerance, outside the selection or on locked
|
||||
// transparency): a no-op stroke, with no history step.
|
||||
// A stroke that writes nothing even at tolerance 0 (wholly outside the selection, locked
|
||||
// transparency) is a quiet no-op; one the tolerance alone holds back adds an unchanged state (#12636).
|
||||
let mut nothing_written = false;
|
||||
let dmg = run_stroke(s, "Art History Brush", id, p, |_, surf, sel, lock| {
|
||||
let fmt = surf.format();
|
||||
@@ -232,6 +232,10 @@ pub fn run(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let sel_mask = selection_mask(sel, outer);
|
||||
let n = stamp_region(&mut dest, &src_reg, &dabs, hardness, opacity, flow, tolerance, sel_mask.as_deref(), lock, alpha_index(&fmt))?;
|
||||
if n == 0 {
|
||||
// `dest` is unchanged, so the same stroke at tolerance 0 tells whether it would paint.
|
||||
if tolerance > 0.0 && stamp_region(&mut dest, &src_reg, &dabs, hardness, opacity, flow, 0.0, sel_mask.as_deref(), lock, alpha_index(&fmt))? > 0 {
|
||||
return Ok(Rect::EMPTY);
|
||||
}
|
||||
nothing_written = true;
|
||||
return Err(err("the stroke changed no pixel"));
|
||||
}
|
||||
@@ -242,7 +246,7 @@ pub fn run(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
Err(_) if nothing_written => return Ok(json!({ "damage": Value::Null })),
|
||||
r => r?,
|
||||
};
|
||||
Ok(json!({ "damage": damage_json(dmg) }))
|
||||
Ok(json!({ "damage": if dmg.is_empty() { Value::Null } else { damage_json(dmg) } }))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -331,14 +335,15 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stroke_under_the_tolerance_records_no_step() {
|
||||
fn stroke_under_the_tolerance_records_an_unchanged_step() {
|
||||
let mut s = session(48, 32, 8, "rgb");
|
||||
paint_layer(&mut s, |_, _| [0.5, 0.5, 0.5, 1.0]);
|
||||
paint_layer(&mut s, |_, _| [0.6, 0.6, 0.6, 1.0]);
|
||||
let steps = s.active().unwrap().history.past_len();
|
||||
let r = stroke(&mut s, json!({"state": 1, "tolerance": 50})).unwrap();
|
||||
assert!(r["damage"].is_null(), "{r}");
|
||||
assert_eq!(s.active().unwrap().history.past_len(), steps);
|
||||
assert_eq!(s.active().unwrap().history.past_len(), steps + 1);
|
||||
assert_eq!(s.active().unwrap().history.undo_label(), Some("Art History Brush"));
|
||||
assert!((rgba(&s, 24, 16)[0] - 0.6).abs() < 0.01);
|
||||
}
|
||||
|
||||
@@ -388,7 +393,8 @@ mod tests {
|
||||
}
|
||||
|
||||
/// A stroke that writes nothing (wholly outside the selection, or on transparent pixels
|
||||
/// with Lock Transparency) does nothing, as with the tolerance: no error, no History step.
|
||||
/// with Lock Transparency) does nothing, unlike one the tolerance holds back: no error, no
|
||||
/// History step.
|
||||
#[test]
|
||||
fn a_stroke_that_writes_nothing_is_a_quiet_no_op() {
|
||||
let mut s = session(48, 32, 8, "rgb");
|
||||
@@ -525,3 +531,7 @@ mod tests {
|
||||
assert!(s.is_enabled("paint.artHistoryBrush"));
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "art_history_noop_state_tests.rs"]
|
||||
mod art_history_noop_state_tests;
|
||||
@@ -0,0 +1,146 @@
|
||||
//! An Art History Brush click the tolerance holds back still adds one unchanged "Art History
|
||||
//! Brush" state, as in Photoshop; refusals and strokes that could write nothing add none (#12636).
|
||||
|
||||
use super::*;
|
||||
|
||||
fn session(depth: u64) -> Session {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", json!({"width": 48, "height": 32, "depth": depth, "mode": "rgb"})).unwrap();
|
||||
s
|
||||
}
|
||||
|
||||
fn fill_gray(s: &mut Session, v: f32) {
|
||||
s.edit("setup", |doc, active| {
|
||||
let b = doc.bounds();
|
||||
let surf = doc.layer_mut(active.unwrap()).unwrap().surface_mut().unwrap();
|
||||
let fmt = surf.format();
|
||||
let px = photocraft_raster::from_rgba(&fmt, [v, v, v, 1.0]);
|
||||
let data: Vec<f32> = (0..b.width() * b.height()).flat_map(|_| px.iter().copied()).collect();
|
||||
surf.write_region(b, &data);
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
/// History row 1 is grey 0.5, the layer now grey 0.6: a small change every high tolerance holds back.
|
||||
fn held_back_session(depth: u64) -> Session {
|
||||
let mut s = session(depth);
|
||||
fill_gray(&mut s, 0.5);
|
||||
fill_gray(&mut s, 0.6);
|
||||
s
|
||||
}
|
||||
|
||||
fn pixels(s: &Session) -> Vec<f32> {
|
||||
let d = s.active().unwrap();
|
||||
d.doc.layer(d.active_layer.unwrap()).unwrap().surface().unwrap().read_region(d.doc.bounds())
|
||||
}
|
||||
|
||||
fn steps(s: &Session) -> usize {
|
||||
s.active().unwrap().history.past_len()
|
||||
}
|
||||
|
||||
fn stroke(s: &mut Session, extra: Value) -> Result<Value> {
|
||||
let mut p = json!({"points": [[8, 16], [40, 16]], "size": 12, "hardness": 100, "area": 50, "style": "tightMedium", "state": 1, "tolerance": 90});
|
||||
if let Some(map) = extra.as_object() {
|
||||
for (k, v) in map {
|
||||
p[k] = v.clone();
|
||||
}
|
||||
}
|
||||
s.execute(CMD, p)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_click_held_back_by_the_tolerance_adds_one_unchanged_state() {
|
||||
for depth in [8u64, 16, 32] {
|
||||
for tolerance in [90, 95, 99, 100] {
|
||||
for points in [json!([[24, 16]]), json!([[8, 16], [40, 16]])] {
|
||||
let mut s = held_back_session(depth);
|
||||
let (before, n) = (pixels(&s), steps(&s));
|
||||
let r = stroke(&mut s, json!({"tolerance": tolerance, "points": points})).unwrap();
|
||||
let case = format!("depth {depth} tolerance {tolerance} points {points}");
|
||||
assert!(r["damage"].is_null(), "{case}: {r}");
|
||||
assert_eq!(steps(&s), n + 1, "{case}: one state");
|
||||
assert_eq!(s.active().unwrap().history.undo_label(), Some("Art History Brush"), "{case}");
|
||||
assert_eq!(pixels(&s), before, "{case}: no pixel changed");
|
||||
assert!(s.undo(), "{case}");
|
||||
assert_eq!(steps(&s), n, "{case}: undo removes the state");
|
||||
assert_eq!(pixels(&s), before, "{case}: undo leaves the layer as it was");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn refusals_under_the_tolerance_add_no_state() {
|
||||
let refused = |name: &str, s: &mut Session, extra: Value| {
|
||||
let n = steps(s);
|
||||
assert!(stroke(s, extra).is_err(), "{name} is refused");
|
||||
assert_eq!(steps(s), n, "{name} adds no state");
|
||||
};
|
||||
|
||||
let mut s = held_back_session(8);
|
||||
s.execute("layer.setProps", json!({"visible": false})).unwrap();
|
||||
refused("a hidden layer", &mut s, json!({}));
|
||||
|
||||
let mut s = held_back_session(8);
|
||||
s.execute("layer.setProps", json!({"locks": {"pixels": true}})).unwrap();
|
||||
refused("a locked layer", &mut s, json!({}));
|
||||
|
||||
let mut s = held_back_session(8);
|
||||
s.execute("layer.new.group", json!({})).unwrap();
|
||||
refused("a group", &mut s, json!({}));
|
||||
|
||||
let mut s = held_back_session(8);
|
||||
refused("a missing state", &mut s, json!({"state": 99}));
|
||||
|
||||
let mut s = held_back_session(8);
|
||||
refused("a missing snapshot", &mut s, json!({"state": null, "snapshot": 99}));
|
||||
|
||||
let mut s = held_back_session(8);
|
||||
let snapshot = s.execute("history.newSnapshot", json!({})).unwrap()["snapshot"].clone();
|
||||
s.execute("history.setSource", json!({"snapshot": snapshot})).unwrap();
|
||||
s.execute("history.deleteSnapshot", json!({"snapshot": snapshot})).unwrap();
|
||||
refused("a deleted History panel source", &mut s, json!({"state": null}));
|
||||
|
||||
let mut s = held_back_session(8);
|
||||
s.execute("image.canvasSize", json!({"width": 60, "height": 40, "anchor": "center"})).unwrap();
|
||||
refused("a source of another canvas size", &mut s, json!({}));
|
||||
|
||||
let mut s = held_back_session(8);
|
||||
refused("another blend mode", &mut s, json!({"mode": "multiply"}));
|
||||
}
|
||||
|
||||
/// Not measured in Photoshop, so these keep the behaviour they had: no state, no error.
|
||||
#[test]
|
||||
fn strokes_that_can_write_nothing_stay_quiet() {
|
||||
let mut s = held_back_session(8);
|
||||
s.execute("select.rect", json!({"x": 0, "y": 0, "width": 4, "height": 4})).unwrap();
|
||||
let (before, n) = (pixels(&s), steps(&s));
|
||||
let r = stroke(&mut s, json!({})).unwrap();
|
||||
assert!(r["damage"].is_null(), "{r}");
|
||||
assert_eq!(steps(&s), n, "wholly outside the selection");
|
||||
assert_eq!(pixels(&s), before);
|
||||
|
||||
let mut s = session(8);
|
||||
s.execute("layer.new.layer", json!({})).unwrap();
|
||||
s.execute("layer.setProps", json!({"locks": {"transparency": true}})).unwrap();
|
||||
let n = steps(&s);
|
||||
// Row 1 is the new transparent layer: at tolerance 0 the stroke would write here, but Lock Transparency skips every pixel.
|
||||
let r = stroke(&mut s, json!({})).unwrap();
|
||||
assert!(r["damage"].is_null(), "{r}");
|
||||
assert_eq!(steps(&s), n, "locked transparent pixels");
|
||||
}
|
||||
|
||||
/// A selection across part of the stroke: at tolerance 0 the stroke would paint inside it, so the
|
||||
/// click the tolerance holds back adds its unchanged state.
|
||||
#[test]
|
||||
fn a_held_back_click_across_part_of_the_selection_adds_one_unchanged_state() {
|
||||
let mut s = held_back_session(8);
|
||||
s.execute("select.rect", json!({"x": 0, "y": 0, "width": 24, "height": 32})).unwrap();
|
||||
let (before, n) = (pixels(&s), steps(&s));
|
||||
let r = stroke(&mut s, json!({})).unwrap();
|
||||
assert!(r["damage"].is_null(), "{r}");
|
||||
assert_eq!(steps(&s), n + 1);
|
||||
assert_eq!(s.active().unwrap().history.undo_label(), Some("Art History Brush"));
|
||||
assert_eq!(pixels(&s), before);
|
||||
}
|
||||
Reference in new issue
Block a user