From 4afbfef2ae7303c5067d1bee6a355ecb2d553b9b Mon Sep 17 00:00:00 2001 From: jacabus Date: Wed, 28 Oct 2026 15:56:22 +1100 Subject: [PATCH] =?UTF-8?q?ARTCRAFT-PC-2026-001:=20gradient=20layer=20impo?= =?UTF-8?q?rt=20=E2=80=94=20one-stop=20guard=20and=20explicit=20defaults?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fix 2: gradient_color_stops now returns None when fewer than 2 color stops are present, preventing a partial broken gradient layer from being created from a malformed descriptor. Fix 3: gradient_layer_set_params now always writes explicit values for reverse (true/false), dither (true/false), scale (value or 100.0), offset ([h, v] always, defaulting to [0, 0]), and transparency stops (always included, even if all opacities are 100). Fix 1 (midpoints) not applied: gradient.fill.set does not accept midpoints as a third element on individual stops. It accepts them via a separate "midpoints" array parameter — that conversion is deferred. Co-Authored-By: Claude Sonnet 4.6 --- crates/engine/src/atn_cmds.rs | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/crates/engine/src/atn_cmds.rs b/crates/engine/src/atn_cmds.rs index 09282262479..006a89aedb6 100644 --- a/crates/engine/src/atn_cmds.rs +++ b/crates/engine/src/atn_cmds.rs @@ -1165,14 +1165,18 @@ const GRADIENT_STYLES: &[(&str, &str)] = &[ ]; /// Color stops from a `gradientLayer` Grad descriptor: `[[location_0_to_1, "#rrggbb"], …]`. +/// Returns `None` if the descriptor is malformed or has fewer than 2 stops. fn gradient_color_stops(grad: &Descriptor) -> Option> { let clrs = get_list(grad, "Clrs")?; - clrs.iter().map(|item| { + let stops: Option> = clrs.iter().map(|item| { let D::Descriptor(stop) = item else { return None }; let hex = color_hex(get_obj(stop, "Clr ")?)?; let loc = get_num(stop, "Lctn")? / 4096.0; Some(json!([loc, hex])) - }).collect() + }).collect(); + let stops = stops?; + if stops.len() < 2 { return None; } + Some(stops) } /// Transparency stops from a `gradientLayer` Grad descriptor: `[[location_0_to_1, opacity_0_to_100], …]`. @@ -1195,22 +1199,14 @@ fn gradient_layer_set_params(t: &Descriptor) -> Option { p.insert("style".into(), style.into()); p.insert("angle".into(), angle.into()); p.insert("stops".into(), Value::Array(stops)); - let trns = gradient_transparency_stops(grad); - if trns.iter().any(|s| s[1].as_f64() != Some(100.0)) { - p.insert("transparency".into(), Value::Array(trns)); - } - if get_bool(t, "Rvrs") == Some(true) { p.insert("reverse".into(), true.into()); } - if get_bool(t, "Dthr") == Some(true) { p.insert("dither".into(), true.into()); } - if let Some(s) = get_num(t, "Scl ").filter(|&s| (s - 100.0).abs() > 0.01) { - p.insert("scale".into(), s.into()); - } - if let Some(ofst) = get_obj(t, "Ofst") { - let h = get_num(ofst, "Hrzn").unwrap_or(0.0); - let v = get_num(ofst, "Vrtc").unwrap_or(0.0); - if h.abs() > 0.001 || v.abs() > 0.001 { - p.insert("offset".into(), json!([h, v])); - } - } + p.insert("transparency".into(), Value::Array(gradient_transparency_stops(grad))); + p.insert("reverse".into(), get_bool(t, "Rvrs").unwrap_or(false).into()); + p.insert("dither".into(), get_bool(t, "Dthr").unwrap_or(false).into()); + p.insert("scale".into(), get_num(t, "Scl ").unwrap_or(100.0).into()); + let (h, v) = get_obj(t, "Ofst") + .map(|ofst| (get_num(ofst, "Hrzn").unwrap_or(0.0), get_num(ofst, "Vrtc").unwrap_or(0.0))) + .unwrap_or((0.0, 0.0)); + p.insert("offset".into(), json!([h, v])); Some(Value::Object(p)) }