DesignCraft: An open-source page and layout design editor written by scratch in pure Rust
Effect blend modes: reject an unknown mode; IDML shadows without BlendMode stay Normal
paaspaas00c9e831e7321e
diff --git a/crates/engine/src/cmd/object.rs b/crates/engine/src/cmd/object.rsindex 29358fcac..72f2fa01f 100644--- a/crates/engine/src/cmd/object.rs+++ b/crates/engine/src/cmd/object.rs@@ -385,6 +385,7 @@ pub fn specs() -> Vec<CommandSpec> {"{on?: bool, distance?, angle?, globalLight?: bool, opacity?, size?, spread? (%), color? (swatch), mode?: blend mode (multiply…), ids?}",has_selection,|s, p| {+ let mode = blend_param(p, "object.dropShadow")?;let p2 = p.clone();set_effects(s, p, "object.dropShadow", move |fx| {let ds = &mut fx.drop_shadow;@@ -398,7 +399,7 @@ pub fn specs() -> Vec<CommandSpec> {if let Some(c) = str_param(&p2, "color") {ds.color = c.into();}- if let Some(m) = blend_param(&p2) {+ if let Some(m) = mode {ds.mode = m;}})@@ -412,6 +413,7 @@ pub fn specs() -> Vec<CommandSpec> {"{on?: bool, distance?, angle?, globalLight?: bool, opacity?, size?, choke? (%), color?, mode?: blend mode, ids?}",has_selection,|s, p| {+ let mode = blend_param(p, "object.innerShadow")?;let p2 = p.clone();set_effects(s, p, "object.innerShadow", move |fx| {let e = &mut fx.inner_shadow;@@ -425,7 +427,7 @@ pub fn specs() -> Vec<CommandSpec> {if let Some(c) = str_param(&p2, "color") {e.color = c.into();}- if let Some(m) = blend_param(&p2) {+ if let Some(m) = mode {e.mode = m;}})@@ -439,6 +441,7 @@ pub fn specs() -> Vec<CommandSpec> {"{on?: bool, opacity?, size?, spread? (%), color?, mode?: blend mode (screen…), ids?}",has_selection,|s, p| {+ let mode = blend_param(p, "object.outerGlow")?;let p2 = p.clone();set_effects(s, p, "object.outerGlow", move |fx| {let e = &mut fx.outer_glow;@@ -449,7 +452,7 @@ pub fn specs() -> Vec<CommandSpec> {if let Some(c) = str_param(&p2, "color") {e.color = c.into();}- if let Some(m) = blend_param(&p2) {+ if let Some(m) = mode {e.mode = m;}})@@ -3111,9 +3114,17 @@ fn align(s: &mut Session, p: &Value) -> Result<Value> {}/// An effect's `mode` parameter: a blend mode by name ("multiply", "screen", or as saved).-fn blend_param(p: &Value) -> Option<BlendMode> {- let v = p.get("mode")?;- v.as_str().and_then(BlendMode::parse).or_else(|| serde_json::from_value(v.clone()).ok())+/// An effect's `mode`: `None` when omitted or null, an error when it names no blend mode.+fn blend_param(p: &Value, cmd: &str) -> Result<Option<BlendMode>> {+ match p.get("mode") {+ None | Some(Value::Null) => Ok(None),+ Some(v) => v+ .as_str()+ .and_then(BlendMode::parse)+ .or_else(|| serde_json::from_value(v.clone()).ok())+ .map(Some)+ .ok_or_else(|| bad(cmd, format!("unknown blend mode {v}"))),+ }}fn distribute(s: &mut Session, p: &Value) -> Result<Value> {@@ -4083,6 +4094,16 @@ mod light_tests {assert_eq!(fx.drop_shadow.mode, BlendMode::Screen);assert_eq!(fx.inner_shadow.mode, BlendMode::Overlay);assert_eq!(fx.outer_glow.mode, BlendMode::Normal);+ let before = s.doc().unwrap().doc.clone();+ for cmd in ["object.dropShadow", "object.innerShadow", "object.outerGlow"] {+ for mode in [json!("multply"), json!(3)] {+ let e = s.execute(cmd, &json!({"on": true, "mode": mode, "ids": [id]})).unwrap_err();+ assert!(e.to_string().contains("unknown blend mode"), "{cmd}: {e}");+ }+ }+ assert!(std::sync::Arc::ptr_eq(&before, &s.doc().unwrap().doc), "nothing changed");+ s.execute("object.dropShadow", &json!({"mode": null, "ids": [id]})).unwrap();+ assert_eq!(s.doc().unwrap().doc.item(ItemId(id)).unwrap().effects.drop_shadow.mode, BlendMode::Screen, "null keeps the mode");}#[test]diff --git a/crates/idml/src/import.rs b/crates/idml/src/import.rsindex 30a5caa34..4de0e08cc 100644--- a/crates/idml/src/import.rs+++ b/crates/idml/src/import.rs@@ -2699,7 +2699,9 @@ impl<'r> Importer<'r> {size: ds.num("Size").unwrap_or(def.size),spread: ds.num("Spread").unwrap_or(def.spread),global_light: false,- mode: ds.get("BlendMode").map_or(def.mode, names::blend_in),+ // Without the attribute (earlier DesignCraft exports) the shadow stays Normal, as it+ // rendered then; a new shadow's default is Multiply.+ mode: ds.get("BlendMode").map_or(designcraft_color::BlendMode::Normal, names::blend_in),};}if let Some(f) = t.find("FeatherSetting")diff --git a/crates/idml/src/tests.rs b/crates/idml/src/tests.rsindex 035333da4..99ae66625 100644--- a/crates/idml/src/tests.rs+++ b/crates/idml/src/tests.rs@@ -3616,6 +3616,7 @@ fn object_style_effects_import_by_category_and_based_on() {let base = fx("Base").unwrap();assert!(base.drop_shadow.on && base.drop_shadow.size == 9.0 && (base.drop_shadow.distance - 5.0).abs() < 1e-9);assert_eq!(base.feather, 0.0, "EnableFeather=false");+ assert_eq!(base.drop_shadow.mode, designcraft_color::BlendMode::Normal, "no BlendMode: Normal, as earlier exports rendered");let child = fx("Child").unwrap();assert_eq!((child.drop_shadow.size, child.feather), (9.0, 6.0), "settings from the BasedOn style");assert!(fx("Off").is_none());