diff --git a/crates/engine/src/cmd/swatchlib.rs b/crates/engine/src/cmd/swatchlib.rs index ed712d8509..d6eed98378 100644 --- a/crates/engine/src/cmd/swatchlib.rs +++ b/crates/engine/src/cmd/swatchlib.rs @@ -49,7 +49,7 @@ pub fn specs() -> Vec { "Add to Swatches", ["Window", "Swatch Libraries"], None, - "{library: id or name, names?: [swatch or colour group names in the library] (default: all of it; a group comes as a colour group, a single swatch ungrouped), apply?: \"fill\"|\"stroke\" (also apply the first one to the selection and the defaults, as paint.setFill / paint.setStroke), focus?: true (with apply: false keeps the active proxy)} copy library swatches into the document as one undo step. A swatch the document already has (same name and paint) isn't added again; a name taken by another swatch gets a number (\"Clay 1 2\"). A pattern swatch brings its pattern, as pasting art painted with it does: the same pattern the document has is used, and a name another pattern or swatch has gets a number → {library, added: [names in the document], existing: [names already there], applied?: name}", + "{library: id or name, names?: [swatch or colour group names in the library] (default: all of it; a group comes as a colour group, a single swatch ungrouped), apply?: \"fill\"|\"stroke\" (also apply the first one to the selection and the defaults, as paint.setFill / paint.setStroke), focus?: true (with apply: false keeps the active proxy)} copy library swatches into the document as one undo step. A gradient or pattern swatch in a library color group is refused (color groups hold solid colors only), and the document is left unchanged. A swatch the document already has (same name and paint) isn't added again; a name taken by another swatch gets a number (\"Clay 1 2\"). A pattern swatch brings its pattern, as pasting art painted with it does: the same pattern the document has is used, and a name another pattern or swatch has gets a number → {library, added: [names in the document], existing: [names already there], applied?: name}", has_doc, add ), @@ -839,6 +839,10 @@ fn add(s: &mut Session, p: &Value) -> Result { }; let focus = switch_or(p, "focus", true, C)?; let picked = picked(&lib, &names_param(p, C)?, C)?; + // Color groups hold solid colors only, as swatch.new, swatch.newGroup and swatch.move keep them. + if let Some((_, w)) = picked.iter().find(|(g, w)| g.is_some() && w.paint.color().is_none()) { + return Err(bad(C, format!("`{}`: colour groups hold solid colours only", w.name))); + } // Swatches the document already has (same name and paint; for a pattern swatch, a swatch of the // same pattern) are reused, not added again. let patterns = patternlib::library_doc(&info.id); diff --git a/crates/engine/src/tests/swatchlib.rs b/crates/engine/src/tests/swatchlib.rs index dbf32eba7c..fb088508c4 100644 --- a/crates/engine/src/tests/swatchlib.rs +++ b/crates/engine/src/tests/swatchlib.rs @@ -160,6 +160,38 @@ fn save_and_load_round_trip_keeps_cmyk_spot_and_groups() { assert!(s.execute("swatch.library.save", &json!({"user": true})).is_err(), "no user folder in a headless session"); } +/// A library color group that holds a gradient is not copied into the document: color groups +/// hold solid colors only. The document keeps its swatches and no undo step is recorded. +#[test] +fn add_refuses_a_library_color_group_holding_a_gradient() { + let mut s = session(); + run(&mut s, "swatch.newGroup", json!({"name": "Brand", "colors": ["#123456", "#abcdef"]})); + let r = run(&mut s, "swatch.library.save", json!({"names": ["Brand", "Sunset"], "name": "Mixed"})); + // Move the Sunset gradient into the library's Brand group, as a hand-edited file can. + let mut lib: Value = serde_json::from_str(r["data"].as_str().unwrap()).unwrap(); + let swatches = lib["swatches"].as_array_mut().unwrap(); + let at = swatches.iter().position(|w| w["name"] == "Sunset").unwrap(); + let sunset = swatches.remove(at); + lib["groups"][0]["swatches"].as_array_mut().unwrap().insert(1, sunset); + let r = run(&mut s, "swatch.library.load", json!({"data": lib.to_string(), "name": "mixed.vcswatches"})); + let id = r["library"].as_str().unwrap().to_string(); + let (_, loaded) = cmd::swatchlib::library(&s, &id).unwrap(); + assert!(matches!(loaded.groups[0].swatches[1].paint, Paint::Gradient(_)), "the reader keeps the gradient in the group"); + run(&mut s, "swatch.delete", json!({"names": ["Brand", "Sunset"]})); + let (before, undo) = (doc(&s).swatch_groups.clone(), undo_len(&s)); + for p in [json!({"library": id}), json!({"library": id, "names": ["Brand"]})] { + let e = s.execute("swatch.library.add", &p).unwrap_err().to_string(); + assert!(e.contains("`Sunset`: colour groups hold solid colours only"), "{p}: {e}"); + assert_eq!(doc(&s).swatch_groups, before, "{p}"); + assert_eq!(undo_len(&s), undo, "{p}"); + } + // The gradient on its own comes in ungrouped. + let r = run(&mut s, "swatch.library.add", json!({"library": id, "names": ["Sunset"]})); + assert_eq!(r["added"], json!(["Sunset"]), "{r}"); + assert!(doc(&s).swatches.iter().any(|w| w.name == "Sunset" && matches!(w.paint, Paint::Gradient(_)))); + assert_eq!(doc(&s).swatch_groups, before); +} + /// The swatch library commands, and the graphic style library save and load that share their file /// handling, refuse a parameter of the wrong form, such as `user: "true"` or `names: "Grays"`, with /// an error that names it; the document keeps its swatches and no undo step is recorded. A null diff --git a/docs/mcp.md b/docs/mcp.md index d0df04dbf5..2c374000c3 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -4739,7 +4739,8 @@ them (`id`, `name`, `category`, `count`) and `swatch.library.get {library}` retu in the `swatch.list` shape (`library` is an id or a name). `swatch.library.add {library, names?}` copies swatches into the document as one undo step: a colour group's name brings the whole group, a swatch's name the swatch alone, no names the whole library; swatches the document already has (same name and paint) are reported under `existing` -and not added again. `apply: "fill"|"stroke"` also applies the first one, in the same undo step (what clicking a +and not added again. Color groups hold solid colors only: an add that would bring a gradient or pattern swatch +into a color group is refused, and the document is left unchanged. `apply: "fill"|"stroke"` also applies the first one, in the same undo step (what clicking a swatch in the library panel does). The pattern libraries Geometric Patterns (`geometric-patterns`), Grit Textures (`grit-textures`: seamless black grain, grit, sand, speckle, stipple, scratch and fleck tiles) and Optical Effects (`optical-effects`: VectorCraft's own black wave, ring, warped-check, pinched-stripe, chevron and lens tiles),