From 86694a1b30bda9a67e1f8046bc1b3d0ca25e225b Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sun, 15 Nov 2026 23:28:21 -0800 Subject: [PATCH] Symbols: check dialog fields and the sprayed style before any change The Symbolism Tools Options dialog refuses a tool that is not a Symbolism tool and a number field that does not read as a number before it gives the selected symbol sets a new density, so a refused OK leaves the document as it was. A length with its unit is still a diameter. symbol.spray with Style: User Defined refuses a style the document does not have. The Symbol Sprayer leaves out a style that has been deleted, so the default style applies. --- crates/engine/src/cmd/brushsym.rs | 2 +- crates/engine/src/cmd/brushsym/symbols.rs | 4 +++ crates/engine/src/tests/symbolism_options.rs | 5 ++++ crates/tools/src/symbolism.rs | 3 +- .../ui-egui/src/dialogs/symbolism_options.rs | 29 ++++++++++++++++++- docs/mcp.md | 3 +- 6 files changed, 42 insertions(+), 4 deletions(-) diff --git a/crates/engine/src/cmd/brushsym.rs b/crates/engine/src/cmd/brushsym.rs index f338dd34c7..0956bf5017 100644 --- a/crates/engine/src/cmd/brushsym.rs +++ b/crates/engine/src/cmd/brushsym.rs @@ -350,7 +350,7 @@ pub fn specs() -> Vec { "Symbol Sprayer", [], None, - "{points: [[x,y]…] (a third number is not used; one that isn't a number is an error), name?, radius?: 40, density?: 1..10 (5), scale?: 1, alt?: bool (remove instances), attributes?: {scrunch?, size?, spin?, screen?, stain?, style?: average|userDefined (average)}, color? (#rrggbbaa and #rgba are refused), keepModel?: bool (as paint.setFill), style?: name} spray instances into a symbol set → {id, count}. Each of `attributes` says what a new instance takes: average, the mean of the selected set's instances within `radius` of the stroke point (their spacing along the stroke, size, rotation, opacity, and the stain or style of the nearest one that has one at the mean amount), and with none there the symbol's size, no rotation, full opacity and no stain or style; or userDefined: spacing from the symbol's size and `density`, the symbol's size times `scale`, turned the way the stroke runs there (not turned for one point), full opacity, stained in full with `color` (default: the fill color), and the fill of graphic style `style` (default: the first style after the default one) in full. The stain and the style are both the instance's fill, and the style replaces the stain. Another key or value in `attributes` is an error", + "{points: [[x,y]…] (a third number is not used; one that isn't a number is an error), name?, radius?: 40, density?: 1..10 (5), scale?: 1, alt?: bool (remove instances), attributes?: {scrunch?, size?, spin?, screen?, stain?, style?: average|userDefined (average)}, color? (#rrggbbaa and #rgba are refused), keepModel?: bool (as paint.setFill), style?: name} spray instances into a symbol set → {id, count}. Each of `attributes` says what a new instance takes: average, the mean of the selected set's instances within `radius` of the stroke point (their spacing along the stroke, size, rotation, opacity, and the stain or style of the nearest one that has one at the mean amount), and with none there the symbol's size, no rotation, full opacity and no stain or style; or userDefined: spacing from the symbol's size and `density`, the symbol's size times `scale`, turned the way the stroke runs there (not turned for one point), full opacity, stained in full with `color` (default: the fill color), and the fill of graphic style `style` (default: the first style after the default one) in full; a `style` the document does not have is then an error. The stain and the style are both the instance's fill, and the style replaces the stain. Another key or value in `attributes` is an error", has_doc, symbol_spray ), diff --git a/crates/engine/src/cmd/brushsym/symbols.rs b/crates/engine/src/cmd/brushsym/symbols.rs index f832e71e22..25c83152ff 100644 --- a/crates/engine/src/cmd/brushsym/symbols.rs +++ b/crates/engine/src/cmd/brushsym/symbols.rs @@ -942,6 +942,10 @@ pub(super) fn symbol_spray(s: &mut Session, p: &Value) -> Result { None => s.paint.fill.color().unwrap_or(Color::BLACK), }; let style = style_paint(&st.doc, p); + // Style: User Defined uses the style `style` names, so one the document does not have is an error. + if let Some(n) = str_param(p, "style").filter(|_| !modes.style && style.is_none()) { + return Err(bad(C, format!("unknown graphic style `{n}`"))); + } let size = natural_size(&st.doc, &name); let set = st.selection.objects.first().copied().filter(|id| st.selection.len() == 1 && st.doc.node(*id).is_some_and(is_symbol_set)); // Only a new Symbol Set needs a layer that takes new art. diff --git a/crates/engine/src/tests/symbolism_options.rs b/crates/engine/src/tests/symbolism_options.rs index bb52cf67c4..badd38a3ba 100644 --- a/crates/engine/src/tests/symbolism_options.rs +++ b/crates/engine/src/tests/symbolism_options.rs @@ -488,6 +488,11 @@ fn bad_sprayer_attributes_are_refused() { } refused(&mut s, "symbol.spray", json!({"name": "Dot", "points": [[200, 150]], "keepModel": "yes"}), "`keepModel` must be true or false"); refused(&mut s, "symbol.spray", json!({"name": "Dot", "points": [[200, 150, "x"]]}), "a point's pressure must be a number, not \"x\""); + // Style: User Defined with a style the document does not have; Average does not use it. + let user = json!({"name": "Dot", "points": [[200, 150]], "attributes": {"style": "userDefined"}, "style": "Nope"}); + refused(&mut s, "symbol.spray", user, "unknown graphic style `Nope`"); + let average = json!({"name": "Dot", "points": [[200, 150]], "style": "Nope"}); + assert_eq!(s.execute("symbol.spray", &average).unwrap()["count"], 1); let all = json!({"scrunch": "average", "size": "userDefined", "spin": null, "screen": "average", "stain": "average", "style": "average"}); assert_eq!(s.execute("symbol.spray", &json!({"name": "Dot", "points": [[200, 150]], "attributes": all})).unwrap()["count"], 1); } diff --git a/crates/tools/src/symbolism.rs b/crates/tools/src/symbolism.rs index ce2396c45c..2c51af2365 100644 --- a/crates/tools/src/symbolism.rs +++ b/crates/tools/src/symbolism.rs @@ -166,7 +166,8 @@ impl SymbolismTool { v["color"] = serde_json::to_value(c).unwrap_or(Value::Null); v["keepModel"] = json!(true); } - if let Some(s) = &self.style { + // A style the document no longer has (a deleted one) is left out: the default style applies. + if let Some(s) = self.style.as_ref().filter(|s| cx.doc.graphic_styles.iter().any(|g| g.name == **s)) { v["style"] = json!(s); } return ("symbol.spray".into(), v); diff --git a/crates/ui-egui/src/dialogs/symbolism_options.rs b/crates/ui-egui/src/dialogs/symbolism_options.rs index 677aa63fec..969a625ee0 100644 --- a/crates/ui-egui/src/dialogs/symbolism_options.rs +++ b/crates/ui-egui/src/dialogs/symbolism_options.rs @@ -124,7 +124,20 @@ fn confirm(app: &mut VectorcraftApp, d: &Dialog) -> DialogResult { // The checkboxes and choices as the fields hold them, so a value the tools do not have is refused. values.extend(SWITCHES.iter().chain(&SPRAY).chain(&["method"]).filter_map(|k| Some((k.to_string(), d.fields.get(*k)?.clone())))); // An option `tool.setOption` refuses leaves the symbol sets as they are too. - vectorcraft_tools::symbolism::check_options(&d.str("tool"), &values)?; + let tool = d.str("tool"); + if !SYMBOLISM.contains(&tool.as_str()) { + return Err(format!("unknown tool `{tool}`")); + } + // A number, or text that reads as one (a length with its unit for the diameter). + let readable = |v: &Value| match v { + Value::Number(_) => true, + Value::String(s) => vectorcraft_doc::Unit::Points.parse(s).is_some(), + _ => false, + }; + if let Some(k) = NUMBERS.iter().chain(&[DENSITY_WAS]).find(|k| d.fields.get(**k).is_some_and(|v| !readable(v))) { + return Err(format!("`{k}` must be a number")); + } + vectorcraft_tools::symbolism::check_options(&tool, &values)?; let (from, to) = (d.f64(DENSITY_WAS, 0.0).clamp(1.0, 10.0), d.f64("density", 0.0).clamp(1.0, 10.0)); let sets = selected_sets(app); if d.fields.contains_key(DENSITY_WAS) && from != to && !sets.is_empty() { @@ -281,6 +294,20 @@ mod tests { d.fields.insert("usePressure".into(), json!(1)); assert!(super::super::confirm(&mut app).is_err()); assert_eq!((width(&app), undo(&app)), (was, steps)); + // A tool that is not a Symbolism tool, or a density that is not a number. + for (k, v) in [("tool", json!("pen")), ("density", json!("three")), ("densityWas", json!(null))] { + app.run("tool.options", json!({"tool": "symbolSprayer"})).unwrap(); + let d = app.ui.dialog.as_mut().unwrap(); + d.fields.insert("density".into(), json!(3)); + d.fields.insert(k.into(), v); + assert!(super::super::confirm(&mut app).is_err(), "{k}"); + assert_eq!((width(&app), undo(&app)), (was, steps), "{k}"); + } + // A length with its unit is a diameter. + app.run("tool.options", json!({"tool": "symbolSprayer"})).unwrap(); + app.ui.dialog.as_mut().unwrap().fields.insert("diameter".into(), json!("12 pt")); + super::super::confirm(&mut app).unwrap(); + assert_eq!(app.session.tool_options_of("symbolSizer")["diameter"].as_f64(), Some(12.0)); assert_eq!(app.session.tool_options_of("symbolSizer")["density"].as_f64(), Some(9.0)); app.ui.dialog = None; // Without a set selected only the option changes. diff --git a/docs/mcp.md b/docs/mcp.md index 30d85c2056..7528fd44db 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -3419,7 +3419,8 @@ already walks Image Trace. Short agent entry points: `points` as `[x, y, pressure]` (each point's pen pressure, 0..1, is the intensity there), and for `size` `proportional` (true) and `affectsDensity` (false). `symbol.spray` takes `attributes` `{scrunch?, size?, spin?, screen?, stain?, style?}`, each `average` or `userDefined`, with `color`, `keepModel` and - `style` for the User Defined stain and style. `symbol.setDensity {from, to, ids?|id?}` gives the selected symbol + `style` for the User Defined stain and style (with `style: userDefined`, a `style` the document does not have is an + error). `symbol.setDensity {from, to, ids?|id?}` gives the selected symbol sets a new Symbol Set Density. VectorCraft's own rules: - `random` gives each instance its own share of the tool's change for the whole stroke, from none of it to twice it (the Spinner: a turn either way), the same each time the stroke is applied.