mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
fix(paint): reject malformed pressure provenance before painting
This commit is contained in:
1 parent
a725d9bb77
commit
dbc4d242b9
3 files changed
+56
-4
No files matched your search
@@ -60,16 +60,16 @@ pub(crate) const MAX_COORD: f64 = 1_000_000.0;
|
||||
|
||||
/// Parse `points`: arrays `[x, y, pressure?, tiltX?, tiltY?, rotation?, timeMs?, wheel?, pressurePresent?]` or
|
||||
/// objects `{"x":…, "y":…, "pressure":…, "tiltX":…, …, "time":…}`.
|
||||
/// The optional presence marker is 0 for absent pressure or 1 for supplied pressure.
|
||||
/// The optional presence marker is 0/1 in arrays or `pressurePresent: false/true` in objects.
|
||||
pub fn parse_points(p: &Value, cmd: &str) -> Result<Vec<StrokePoint>> {
|
||||
let arr = p.get("points").and_then(Value::as_array).ok_or_else(|| bad(cmd, "missing `points`"))?;
|
||||
// An explicit invalid pressure must not silently become a mouse sample or be discarded.
|
||||
// Invalid pressure or provenance must not silently become a mouse sample or be discarded.
|
||||
if arr.iter().any(|v| match v {
|
||||
Value::Array(a) => a.get(2).is_some_and(|v| v.as_f64().is_none()) || a.get(8).is_some_and(|v| !matches!(v.as_f64(), Some(0.0 | 1.0))),
|
||||
Value::Object(o) => o.get("pressure").is_some_and(|v| v.as_f64().is_none()),
|
||||
Value::Object(o) => o.get("pressure").is_some_and(|v| v.as_f64().is_none()) || o.get("pressurePresent").is_some_and(|v| v.as_bool().is_none()),
|
||||
_ => false,
|
||||
}) {
|
||||
return Err(bad(cmd, "point pressure must be numeric and its presence marker must be 0 or 1"));
|
||||
return Err(bad(cmd, "point pressure must be numeric and its presence marker must be 0 or 1 in arrays or boolean in objects"));
|
||||
}
|
||||
let pts: Vec<StrokePoint> = arr
|
||||
.iter()
|
||||
|
||||
@@ -294,6 +294,34 @@ fn mixer_long_segments_are_interpolated_and_replay_matches_live() {
|
||||
assert_eq!(target(&s).rgba(x, 48), expected.rgba(x, 48));
|
||||
assert!(target(&s).rgba(x, 48)[3] > 0.0);
|
||||
}
|
||||
|
||||
// A supplied-to-mouse transition crosses several internal interpolation batches. Resolve
|
||||
// the absent endpoint before interpolating its pressure with the supplied starting point.
|
||||
let mut s = fixture(PixelFormat::RGBA8);
|
||||
let points = [StrokePoint::new(20.0, 48.0, 0.25), StrokePoint::without_pressure(5020.0, 48.0)];
|
||||
let p = json!({
|
||||
"points": points, "seed": 71, "size": 8, "sampleAllLayers": false, "wet": 0, "mix": 0,
|
||||
"load": 100, "color": "#ff0000", "brush": {
|
||||
"pressureSize": false, "smoothing": {"catchUpOnEnd": true},
|
||||
"shapeDynamics": {"enabled": true, "size": {"control": "penPressure"}},
|
||||
"pose": {"enabled": true, "pressure": 0.5, "overridePressure": false}
|
||||
}
|
||||
});
|
||||
let live = LiveMixer::begin(&s, &p).unwrap();
|
||||
let id = s.active().unwrap().active_layer.unwrap();
|
||||
let expected = live.doc.layer(id).unwrap().surface().unwrap().clone();
|
||||
let mut resolved = p.clone();
|
||||
resolved["points"] = json!([points[0], StrokePoint::new(5020.0, 48.0, 0.5)]);
|
||||
let control = LiveMixer::begin(&s, &resolved).unwrap();
|
||||
assert_eq!(expected, *control.doc.layer(id).unwrap().surface().unwrap(), "missing pressure resolves before long-segment interpolation");
|
||||
resolved["points"] = json!([points[0], StrokePoint::new(5020.0, 48.0, 1.0)]);
|
||||
let full = LiveMixer::begin(&s, &resolved).unwrap();
|
||||
assert_ne!(expected, *full.doc.layer(id).unwrap().surface().unwrap(), "supplied full pressure must not take the mouse fallback");
|
||||
s.execute("paint.mixerBrush", p).unwrap();
|
||||
assert_eq!(target(&s), expected, "the no-pen Pose branch replays every long-segment pixel exactly");
|
||||
for x in [20, 1020, 2068, 4090, 5019] {
|
||||
assert!(target(&s).rgba(x, 48)[3] > 0.0);
|
||||
}
|
||||
}
|
||||
|
||||
/// Authorization, validation, unknown commands and missing targets all discard the single-use offer.
|
||||
|
||||
@@ -1731,3 +1731,27 @@ fn explicit_null_pressure_is_rejected_even_beside_valid_command_points() {
|
||||
assert_eq!(rgba(&s, 10, 20)[3], 0.0, "an invalid pressure must not partially paint");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn invalid_object_pressure_presence_rejects_the_whole_stroke_without_document_changes() {
|
||||
for cmd in ["paint.stroke", "paint.pencil", "paint.mixerBrush"] {
|
||||
let mut s = session(80, 40);
|
||||
s.execute("paint.stroke", json!({"points": [[5, 5], [70, 5]], "size": 4, "color": "#0000ff"})).unwrap();
|
||||
assert!(rgba(&s, 20, 5)[3] > 0.0, "the document already contains paint");
|
||||
let st = s.active().unwrap();
|
||||
let (doc, revision, history, layer) = (st.doc.clone(), st.revision, st.history.past_len(), st.active_layer);
|
||||
let journal = s.journal.len();
|
||||
for marker in [json!("false"), json!(0), json!(null), json!([]), json!({})] {
|
||||
let points = json!([
|
||||
{"x": 10, "y": 20, "pressure": 0.5, "pressurePresent": true},
|
||||
{"x": 40, "y": 20, "pressure": 0.5, "pressurePresent": marker},
|
||||
{"x": 70, "y": 20, "pressurePresent": false}
|
||||
]);
|
||||
assert!(s.execute(cmd, json!({"points": points, "size": 8})).is_err(), "{cmd}: {marker}");
|
||||
let st = s.active().unwrap();
|
||||
assert!(std::sync::Arc::ptr_eq(&doc, &st.doc), "{cmd}: no part of the document may change");
|
||||
assert_eq!((st.revision, st.history.past_len(), st.active_layer), (revision, history, layer));
|
||||
assert_eq!(s.journal.len(), journal, "rejected metadata must not enter replay");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user