From c1f74eeeb18ecb73045e9b812889f4c5d6cc90b9 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sun, 22 Nov 2026 14:20:51 -0800 Subject: [PATCH] Plug-ins: returned effects check listed choices, blend modes, colors and two more ranges An effect record a painted live effect returns now has each parameter that `effect.list` documents as a list of choices checked against that list, so Roughen's `points` takes only "smooth" or "corner". A Drop Shadow's, Inner Glow's or Outer Glow's `mode` must name a blend mode. An unknown name was drawn as Normal before. A `color` is taken as [r, g, b] or a color object as well as "#rrggbb", as `effect.apply` takes it; the check of its default's type refused those forms before. Pucker & Bloat's `amount` and Temperature/Tint's `temperature` and `tint` are documented as `lo..hi` ranges, so a returned value outside -200..200 or -100..100 is refused. The effects already clamp to these ranges. --- crates/effects/src/catalog.rs | 28 +++++++++++++++++++++++++-- crates/effects/src/params.rs | 18 +++++++++++++++-- crates/effects/src/raster.rs | 7 ++++++- crates/effects/src/tests/painted.rs | 30 +++++++++++++++++++++++++++++ docs/plugins.md | 6 ++++-- 5 files changed, 82 insertions(+), 7 deletions(-) diff --git a/crates/effects/src/catalog.rs b/crates/effects/src/catalog.rs index c79baa50e0..47599c02c4 100644 --- a/crates/effects/src/catalog.rs +++ b/crates/effects/src/catalog.rs @@ -85,6 +85,30 @@ pub(crate) fn any_documented_range(id: &str, key: &str) -> Option Option> { + let doc = catalog_index().get(id)?.params; + doc.match_indices(key).map(|(at, _)| at).filter(|at| starts_key(doc, *at)).find_map(|at| { + let rest = doc.get(at + key.len()..)?; + let mut rest = rest.strip_prefix(": ").or_else(|| rest.strip_prefix("?: "))?; + let mut choices = vec![]; + while let Some((choice, after)) = rest.strip_prefix('"').and_then(|r| r.split_once('"')) { + choices.push(choice); + let after = match after.strip_prefix(" (") { + Some(note) => note.get(note.find(')')? + 1..)?, + None => after, + }; + match after.strip_prefix('|') { + Some(next) => rest = next, + None => break, + } + } + (choices.len() >= 2).then_some(choices) + }) +} + /// The range parameter `key` of effect `id` is documented with in `unit` (`key: unit lo..hi`). fn documented_range(id: &str, key: &str, unit: &str) -> Option> { let doc = catalog_index().get(id)?.params; @@ -290,7 +314,7 @@ pub fn effect_catalog() -> Vec { "{corners: [[x,y]×4] (new positions of the bounding-box corners TL, TR, BR, BL in unit box coordinates; identity [[0,0],[1,0],[1,1],[0,1]]; placed gradients move and scale from the object's bounds to its distorted bounds)}", json!({"corners": [[0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [0.0, 1.0]]}), ), - g("distort.puckerBloat", "Pucker & Bloat…", DT, "{amount: % (-200 pucker … 200 bloat, default 0)}", json!({"amount": 0.0})), + g("distort.puckerBloat", "Pucker & Bloat…", DT, "{amount: % -200..200 (0; -200 puckers, 200 bloats)}", json!({"amount": 0.0})), g( "distort.roughen", "Roughen…", @@ -841,7 +865,7 @@ pub fn effect_catalog() -> Vec { "adjust.temperatureTint", "Temperature/Tint…", ADJUST, - "{temperature: -100 (cooler) .. 100 (warmer) (0), tint: -100 (greener) .. 100 (more magenta) (0)} recolours as Brightness/Contrast does", + "{temperature: -100..100 (0; -100 cooler, 100 warmer), tint: -100..100 (0; -100 greener, 100 more magenta)} recolours as Brightness/Contrast does", json!({"temperature": 0.0, "tint": 0.0}), ), ]); diff --git a/crates/effects/src/params.rs b/crates/effects/src/params.rs index 4ac8d3dfde..93c5e2a502 100644 --- a/crates/effects/src/params.rs +++ b/crates/effects/src/params.rs @@ -121,7 +121,8 @@ pub fn new_effect(id: &str, params: &Value) -> Option { /// fills or strokes (`row`) ([`vectorcraft_plugins::painted::CheckEffect`]): a built-in effect other /// than a 3D or Pathfinder effect, a raster effect only on a fill or stroke, and parameters that effect takes. A /// parameter is one its defaults or its parameter documentation name, of the type its default has, -/// within the range its documentation gives ([`crate::catalog::any_documented_range`]); a texture is one as `effect.list` +/// within the range its documentation gives ([`crate::catalog::any_documented_range`]) and one of the +/// choices it lists ([`crate::catalog::documented_choices`]); a texture is one as `effect.list` /// gives it, and an SVG filter's XML one drawable filter. A plug-in effect, the plug-in itself /// included, is refused, so the effects a plug-in returns never run a plug-in. pub fn check_returned_effect(e: &Effect, row: bool) -> Result<(), String> { @@ -152,8 +153,11 @@ pub fn check_returned_effect(e: &Effect, row: bool) -> Result<(), String> { return Err(format!("`{key}` is not a parameter of {:?}", e.id)); } let kind = |v: &Value| std::mem::discriminant(v); + // A `color` is checked as a color, in any form the effect draws ([`is_color`]). + let color = key == "color" && default.is_some_and(Value::is_string); if let Some(d) = default && !d.is_null() + && !color && kind(d) != kind(v) { return Err(format!("`{key}` must be of the type its default {d} has")); @@ -164,6 +168,11 @@ pub fn check_returned_effect(e: &Effect, row: bool) -> Result<(), String> { { return Err(format!("`{key}` must be from {} to {}", r.start(), r.end())); } + if let Some(choices) = crate::catalog::documented_choices(&e.id, key) + && !v.as_str().is_some_and(|v| choices.contains(&v)) + { + return Err(format!("`{key}` must be one of \"{}\"", choices.join("\", \""))); + } } if let Some(t) = given.get(crate::pixel::TEXTURE_IMAGE) && t.as_str().is_none_or(|t| crate::pixel::texture(t).is_none()) @@ -175,7 +184,8 @@ pub fn check_returned_effect(e: &Effect, row: bool) -> Result<(), String> { /// The values in `given`, the parameters of built-in effect `id` that a painted live-effect plug-in /// returned, that are checked further: an SVG filter's XML, `scale`, `origin`, `axes` and -/// `name`; a Free Distort's four `corners`; a Warp's `version`; a `color` the effect can draw; a Levels or Curves `channel`; a Radial +/// `name`; a Free Distort's four `corners`; a Warp's `version`; a `color` the effect can draw; a +/// shadow's or glow's blend `mode`; a Levels or Curves `channel`; a Radial /// Blur's center; and the type of each parameter without a default. fn check_returned_values(id: &str, given: &Map) -> Result<(), String> { let number = |key: &str| given.get(key).is_some_and(|v| !v.is_number()); @@ -211,6 +221,10 @@ fn check_returned_values(id: &str, given: &Map) -> Result<(), Str if takes_color && given.get("color").is_some_and(|v| !is_color(v)) { return Err("`color` must be a #rrggbb color".into()); } + let takes_blend_mode = catalog_index().get(id).is_some_and(|info| info.params.contains("mode: blend mode")); + if takes_blend_mode && given.get("mode").is_some_and(|v| v.as_str().and_then(crate::raster::named_blend_mode).is_none()) { + return Err("`mode` must be a blend mode, such as \"multiply\" or \"screen\"".into()); + } if matches!(id, "adjust.levels" | "adjust.curves") && given.get("channel").is_some_and(|v| !v.as_str().is_some_and(crate::is_channel)) { return Err(format!("`channel` must be one of {}", crate::CHANNELS.join(", "))); } diff --git a/crates/effects/src/raster.rs b/crates/effects/src/raster.rs index 6948d0f977..c72391e56d 100644 --- a/crates/effects/src/raster.rs +++ b/crates/effects/src/raster.rs @@ -365,8 +365,13 @@ impl Rasterize { /// Parse a blend mode name (`"multiply"`, `"Screen"`, `"color-burn"`…). pub fn blend_mode(s: &str) -> BlendMode { + named_blend_mode(s).unwrap_or(BlendMode::Normal) +} + +/// The blend mode `s` names as [`blend_mode`] reads it; `None` when it names none. +pub(crate) fn named_blend_mode(s: &str) -> Option { let key: String = s.chars().filter(|c| c.is_ascii_alphanumeric()).collect::().to_ascii_lowercase(); - BlendMode::ALL.iter().copied().find(|m| format!("{m:?}").to_ascii_lowercase() == key).unwrap_or(BlendMode::Normal) + BlendMode::ALL.iter().copied().find(|m| format!("{m:?}").to_ascii_lowercase() == key) } /// An effect's `color` parameter (`"#rrggbb"`, `[r, g, b]` 0..1 or a colour object). diff --git a/crates/effects/src/tests/painted.rs b/crates/effects/src/tests/painted.rs index a88d751e03..c1f0105f6b 100644 --- a/crates/effects/src/tests/painted.rs +++ b/crates/effects/src/tests/painted.rs @@ -284,6 +284,22 @@ fn returned_effects_are_built_in_effects_with_their_own_parameters() { assert_eq!(check(&e("distort.transform", json!({"copies": 1000})), false), Ok(())); // A parameter documented right after a comma has its range checked too. assert_eq!(check(&e("artistic.paintDaubs", json!({"sharpness": 25})), false), Ok(())); + // Each documented choice and each end of a documented range passes. + for (id, params) in [ + ("distort.roughen", json!({"points": "smooth"})), + ("distort.roughen", json!({"points": "corner"})), + ("distort.zigZag", json!({"points": "corner"})), + ("distort.puckerBloat", json!({"amount": -200})), + ("distort.puckerBloat", json!({"amount": 200})), + ("adjust.temperatureTint", json!({"temperature": -100, "tint": 100})), + ("stylize.dropShadow", json!({"mode": "color-burn"})), + ("stylize.outerGlow", json!({"mode": "Normal"})), + // A `color` is also taken as [r, g, b] 0..1 or a color object, as the effects draw it. + ("stylize.dropShadow", json!({"color": [1.0, 0.0, 0.0]})), + ("stylize.outerGlow", json!({"color": {"model": "gray", "k": 0.5}})), + ] { + assert_eq!(check(&e(id, params.clone()), false), Ok(()), "{id} {params}"); + } // Every effect's own defaults pass the check, so each documented range holds its default. for info in crate::effect_catalog().into_iter().filter(|i| !crate::is_three_d(i.id) && !crate::is_pathfinder(i.id)) { assert_eq!(check(&e(info.id, info.defaults.clone()), false), Ok(()), "{}", info.id); @@ -315,6 +331,9 @@ fn returned_effects_are_built_in_effects_with_their_own_parameters() { (crate::SVG_FILTER, json!({"name": "f"}), "name"), ("warp.arc", json!({"version": 99}), "version"), ("stylize.dropShadow", json!({"color": "#zz0000"}), "color"), + ("stylize.dropShadow", json!({"color": [1.0, "0", 0.0]}), "color"), + ("stylize.innerGlow", json!({"color": {"model": "rgb"}}), "color"), + ("stylize.outerGlow", json!({"color": true}), "color"), ("adjust.levels", json!({"channel": "cyan"}), "channel"), ("blur.radial", json!({"centerX": 1.5}), "centerX"), ("blur.gaussian", json!({"linear": 1}), "linear"), @@ -322,6 +341,17 @@ fn returned_effects_are_built_in_effects_with_their_own_parameters() { ("distort.twist", json!({"angle": 4000.0}), "angle"), ("distort.freeDistort", json!({"corners": []}), "corners"), ("distort.freeDistort", json!({"corners": [[0, 0], [1, 0], [1, 1], [0]]}), "corners"), + ("distort.roughen", json!({"points": "invalid"}), "points"), + ("distort.zigZag", json!({"points": "Corner"}), "points"), + ("path.offsetPath", json!({"joins": "square"}), "joins"), + (crate::RASTERIZE, json!({"colorModel": "cmyk"}), "colorModel"), + (crate::CROP_MARKS, json!({"style": 1}), "style"), + ("distort.puckerBloat", json!({"amount": -201}), "amount"), + ("distort.puckerBloat", json!({"amount": 201}), "amount"), + ("adjust.temperatureTint", json!({"temperature": 101}), "temperature"), + ("adjust.temperatureTint", json!({"tint": -101}), "tint"), + ("stylize.dropShadow", json!({"mode": "not-a-mode"}), "mode"), + ("stylize.innerGlow", json!({"mode": "???"}), "mode"), ] { let why = check(&e(id, params.clone()), false).expect_err(&format!("{id} {params}")); assert!(why.contains(key), "{id} {params}: {why}"); diff --git a/docs/plugins.md b/docs/plugins.md index ec9a71079d..5fb19c4cc4 100644 --- a/docs/plugins.md +++ b/docs/plugins.md @@ -875,9 +875,11 @@ fill and stroke the piece returns, three more keys: them may stay a bare paint. A stroke takes them beside its `paint`, `width`, `join`, `cap` and `miterLimit`. - An effect record names a built-in effect that `effect.list` lists, other than a 3D effect or a Pathfinder effect (`pathfinder.*`, which has nothing to combine on a piece's one path), with parameters that effect takes, each of the - type of its default and within the range `effect.list` gives for it, where it gives one. The values `effect.apply` + type of its default, within the range `effect.list` gives for it, where it gives one, and one of the choices it + lists for it, where it lists them (Roughen's `points` is `"smooth"` or `"corner"`). The values `effect.apply` checks for one effect are checked the same way: an SVG filter's `xml`, `scale`, `origin`, `axes` and `name`, a - Warp's `version`, a `color`, a Levels or Curves `channel` and a Radial Blur's center. Omitted parameters take the + Warp's `version`, a `color`, a Levels or Curves `channel` and a Radial Blur's center. A Drop Shadow's, Inner + Glow's or Outer Glow's `mode` must name a blend mode (`"multiply"`, `"color-burn"`). Omitted parameters take the effect's defaults. On a fill or stroke only raster effects are taken, and only from an effect in the object's stack: the fills or strokes that an effect on a fill or a stroke returns take `opacity` and `blend` but no `effects` (its pieces still take them). A plug-in effect (`plugin.`), this effect's own included, is refused, so the effects a