PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Merge origin/main into fix/fill-layer-angle-finite
David Mann4bf11b129370
diff --cc crates/engine/src/commands.rsindex c257a1bc9,cd7f5d606..5fab50cd5--- a/crates/engine/src/commands.rs+++ b/crates/engine/src/commands.rs@@@ -229,11 -232,52 +232,49 @@@ pub(crate) fn u32_id_param(cmd: &str, pp.get(key).map(|v| u32_id(cmd, key, v)).transpose()}- pub(crate) fn color_param(p: &Value, key: &str, default: [f32; 4]) -> [f32; 4] {- match p.get(key) {-fn f32_or(p: &Value, key: &str, default: f32) -> f32 {- p.get(key).and_then(Value::as_f64).map(|v| v as f32).unwrap_or(default)-}+ /// JSON numbers may be finite f64 values but overflow when stored in a document's f32 field.+ pub(crate) fn finite_f32(cmd: &str, key: &str, value: f64) -> Result<f32> {+ let narrowed = value as f32;+ if narrowed.is_finite() { Ok(narrowed) } else { Err(bad(cmd, format!("`{key}` must fit a finite 32-bit number"))) }+ }++ /// Effect/type parameters contain nested colour arrays and snapshots as well as scalars.+ /// Validate before parsing or editing, including numbers hidden in an effect snapshot.+ pub(crate) fn check_f32_values(cmd: &str, value: &Value) -> Result<()> {+ // Bound the call stack, without allocating a work list proportional to an input array.+ fn check(cmd: &str, value: &Value, depth: usize) -> Result<()> {+ if depth > 64 {+ return Err(bad(cmd, "parameters nested deeper than 64 levels"));+ }+ match value {+ Value::Number(n) => {+ finite_f32(cmd, "number", n.as_f64().ok_or_else(|| bad(cmd, "invalid number"))?)?;+ }+ Value::Array(a) => {+ for v in a {+ check(cmd, v, depth + 1)?;+ }+ }+ Value::Object(o) => {+ for v in o.values() {+ check(cmd, v, depth + 1)?;+ }+ }+ _ => {}+ }+ Ok(())+ }+ check(cmd, value, 0)+ }++ pub(crate) fn color_param(cmd: &str, p: &Value, key: &str, default: [f32; 4]) -> Result<[f32; 4]> {+ Ok(match p.get(key) {Some(Value::Array(a)) if a.len() >= 3 => {- let v: Vec<f32> = a.iter().map(|x| x.as_f64().unwrap_or(0.0) as f32).collect();- [v[0], v[1], v[2], v.get(3).copied().unwrap_or(1.0)]+ let mut out = [0.0, 0.0, 0.0, 1.0];+ for (slot, value) in out.iter_mut().zip(a) {+ *slot = finite_f32(cmd, key, value.as_f64().unwrap_or(0.0))?;+ }+ out}Some(Value::String(s)) => parse_hex(s).unwrap_or(default),_ => default,@@@ -985,9 -1052,9 +1049,9 @@@ fn build() -> Vec<CommandSpec>r##"{"from":"#rrggbb","to":"#rrggbb","angle":deg=90,"style":"linear|radial|angle|reflected|diamond","reverse":bool,"path":"work"|saved path name|{…path}? (the active path: becomes the vector mask; else the selection is the layer mask)}"##,has_doc,|s, p| {- let a = color_param(p, "from", s.tools.foreground);- let b = color_param(p, "to", s.tools.background);+ let a = color_param("layer.newFillLayer.gradient", p, "from", s.tools.foreground)?;+ let b = color_param("layer.newFillLayer.gradient", p, "to", s.tools.background)?;- let angle = f32_or(p, "angle", 90.0);+ let angle = crate::gradient_fill_cmds::angle_param("layer.newFillLayer.gradient", p, 90.0)?;let style = crate::layer_style::gradient_style(p.get("style").and_then(Value::as_str).unwrap_or("linear"));let reverse = p.get("reverse").and_then(Value::as_bool).unwrap_or(false);let id = s.edit("New Gradient Fill Layer", |doc, active| {