Swatch libraries: colors a pattern brings skip the library's other names

A global or spot color that a library pattern's art brings under a
numbered name no longer takes a name the library gives another swatch.
Before, adding a pattern whose art uses "Ink" to a document with another
"Ink" brought the color as "Ink 2" even when the library has its own
"Ink 2"; adding that library swatch next then came in as "Ink 2 2". The
brought color now takes the next free name the library does not use
("Ink 3"), and every paint linked to it follows the new name, so adding
the library's "Ink 2" next adds it under its own name and adding both
again adds nothing.

A color the pattern's art brings or takes is the color a library tint
or gradient links to only when it has the library color's paint and its
process, global or spot kind, as for the other lookups in the same add.

The test a_numbered_copy_named_like_another_library_color_is_found now
makes the blue "Ink 2" itself, as an earlier version's pattern add left
it, since a pattern add no longer gives that name.
This commit is contained in:
dotCooCoo
2026-11-21 15:38:19 -08:00
parent a71de2dd7a
commit ff26d5e4a3
4 changed files with 106 additions and 18 deletions
+36 -5
View File
@@ -49,7 +49,7 @@ pub fn specs() -> Vec<CommandSpec> {
"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, and for a color the same process, global or spot kind, or a numbered copy of that name with the same color when the name has another color there) isn't added again; a name taken by another swatch gets a number (\"Clay 1 2\"); a tint or gradient whose global or spot color comes in under a numbered name, or is in the document as such a numbered copy, links to that copy; a global or spot color it links to that the document lacks comes in with it. A pattern swatch brings its pattern, as pasting art painted with it does: the same pattern the document has is used, also when its art links to the document's own color of a name, and a name another pattern or swatch has gets a number; a global or spot color its art links to whose name the document gives another color comes in numbered, and the pattern links to it → {library, added: [names in the document, the colors patterns, tints and gradients brought included], 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 swatch the document already has (same name and paint, and for a color the same process, global or spot kind, or a numbered copy of that name with the same color when the name has another color there) isn't added again; a name taken by another swatch gets a number (\"Clay 1 2\"); a tint or gradient whose global or spot color comes in under a numbered name, or is in the document as such a numbered copy, links to that copy; a global or spot color it links to that the document lacks comes in with it. A pattern swatch brings its pattern, as pasting art painted with it does: the same pattern the document has is used, also when its art links to the document's own color of a name, and a name another pattern or swatch has gets a number; a global or spot color its art links to whose name the document gives another color comes in numbered, skipping the names of the library's other swatches, and the pattern links to it → {library, added: [names in the document, the colors patterns, tints and gradients brought included], existing: [names already there], applied?: name}",
has_doc,
add
),
@@ -1023,6 +1023,30 @@ fn free_copy_in(taken: &mut HashSet<String>, base: &str, reserved: impl Fn(&str)
name
}
/// Rename each swatch of `d` that `colors` lists (library name, document name) under a numbered
/// name that `reserved` says the library gives another swatch, to a free numbered name of its own
/// ([`free_copy_in`]), and link every paint linked to it to the new name. A later add then finds
/// the library's swatch of that name under its own name.
fn rename_brought(d: &mut vectorcraft_doc::Document, colors: &mut patternlib::SwatchNames, reserved: impl Fn(&str) -> bool) {
let mut taken = taken_names(d);
for (from, to) in colors.iter_mut() {
if from == to || !reserved(to) {
continue;
}
let new = free_copy_in(&mut taken, from, &reserved);
let Some(w) = d.swatches_iter_mut().find(|w| w.name == *to) else { continue };
w.name = new.clone();
map_swatch_links(d, &mut |_, link, _| {
let hit = link.as_deref() == Some(to.as_str());
if hit {
*link = Some(new.clone());
}
hit
});
*to = new;
}
}
/// Link the swatches of `d` that `pending` names (document name, library swatch) to their colors
/// under the names `links` gives them, for each whose colors `links` names now. Those leave
/// `pending` and are returned.
@@ -1363,11 +1387,14 @@ fn add(s: &mut Session, p: &Value) -> Result<Value> {
let waited = |n: &&str| !links.contains_key(*n) && brings.contains(*n);
// A global or spot color the pattern brought, or took from the document under
// a numbered name: library name → document name. A color this call picks is
// added under its own name instead of the one the pattern took.
// added under its own name instead of the one the pattern took. A library global
// or spot color is only a document color of its paint and kind, as for `base_of`.
let colors_of = |d: &vectorcraft_doc::Document, colors: &[(String, String)], took: &[(String, String)]| {
let mut out: HashMap<String, String> = HashMap::new();
for (from, to) in colors.iter().chain(took.iter().filter(|(from, _)| !picked_names.contains(from.as_str()))) {
if d.swatch(to).is_some_and(|x| x.global || x.spot) {
let library = lib_by_name.get(from.as_str()).filter(|b| b.global || b.spot);
let fits = |x: &Swatch| (x.global || x.spot) && library.is_none_or(|b| x.paint == b.paint && same_kind(x, b));
if d.swatch(to).is_some_and(fits) {
out.entry(from.clone()).or_insert_with(|| to.clone());
}
}
@@ -1377,7 +1404,8 @@ fn add(s: &mut Session, p: &Value) -> Result<Value> {
let mut learned = vec![];
if pending.iter().any(|(_, w)| linked_names(&w.paint).iter().any(waited)) {
let mut probe = d.clone();
let (_, colors, took) = patternlib::import(&mut probe, src, pattern)?;
let (_, mut colors, took) = patternlib::import(&mut probe, src, pattern)?;
rename_brought(&mut probe, &mut colors, |n| lib_by_name.contains_key(n));
for (from, to) in colors_of(&probe, &colors, &took) {
if let std::collections::hash_map::Entry::Vacant(e) = links.entry(from.clone()) {
e.insert(to);
@@ -1387,7 +1415,10 @@ fn add(s: &mut Session, p: &Value) -> Result<Value> {
}
let settled = settle(d, &mut pending, &links);
let had = taken_names(d);
let (target, colors, took) = patternlib::import(d, src, pattern)?;
let (target, mut colors, took) = patternlib::import(d, src, pattern)?;
// A color the art brought under a numbered name the library gives another
// swatch takes a free one instead.
rename_brought(d, &mut colors, |n| lib_by_name.contains_key(n));
// Those colors are the ones the library's tints of them link to, unless the
// document already has one for them. A name the copy gave that the import
// into the document did not give is replaced, and the tints linked to it are
+67 -11
View File
@@ -959,25 +959,18 @@ fn a_tint_linked_to_a_copy_the_library_names_is_not_another_colors_tint() {
}
/// A numbered copy of a color's name that the library also has as a swatch of another color is
/// still the copy of the color: adding the color after a pattern brought it under that name adds
/// no other copy.
/// still the copy of the color: adding the color to a document that has such a copy (as a pattern
/// added by an earlier version left it) adds no other copy.
#[test]
fn a_numbered_copy_named_like_another_library_color_is_found() {
let mut s = session();
run(&mut s, "swatch.new", json!({"name": "Ink", "color": "#0000ff", "global": true}));
run(&mut s, "swatch.new", json!({"name": "Ink 2", "color": "#00ff00", "global": true}));
let dot = run(&mut s, "shape.rectangle", json!({"x": 0, "y": 0, "width": 6, "height": 6}))["id"].clone();
run(&mut s, "paint.setFill", json!({"swatch": "Ink", "ids": [dot]}));
run(&mut s, "paint.setStroke", json!({"none": true, "ids": [dot]}));
run(&mut s, "select.set", json!({"ids": [dot]}));
run(&mut s, "object.pattern.make", json!({"name": "Tiles", "width": 8, "height": 8}));
run(&mut s, "object.pattern.done", json!({}));
let b64 = run(&mut s, "document.serialize", json!({}))["dataBase64"].as_str().unwrap().to_string();
let id = run(&mut s, "swatch.library.load", json!({"dataBase64": b64, "name": "Tiles.vectorcraft"}))["library"].clone();
let id = run(&mut s, "swatch.library.load", json!({"dataBase64": b64, "name": "Inks.vectorcraft"}))["library"].clone();
run(&mut s, "file.new", json!({"width": 100, "height": 100}));
run(&mut s, "swatch.new", json!({"name": "Ink", "color": "#ff0000", "global": true}));
run(&mut s, "swatch.library.add", json!({"library": id, "names": ["Tiles"]}));
assert_eq!(named_after(&s, "Ink"), ["Ink", "Ink 2"]);
run(&mut s, "swatch.new", json!({"name": "Ink 2", "color": "#0000ff", "global": true}));
let r = run(&mut s, "swatch.library.add", json!({"library": id, "names": ["Ink"]}));
assert_eq!((&r["added"], &r["existing"]), (&json!([]), &json!(["Ink 2"])), "{r}");
}
@@ -1183,6 +1176,69 @@ fn a_library_spot_tint_is_not_a_process_tint_a_pattern_brought() {
assert!(doc(&s).swatch(&base).is_some_and(|w| w.spot), "{r}");
}
/// A library tint of a spot color added with a pattern whose art the document holds linked to its
/// process color of the spot color's name and paint (as pasting with Merge leaves it) links to a
/// spot color, not to that process color, in either pick order.
#[test]
fn a_library_spot_tint_is_not_linked_to_the_process_color_a_merged_pattern_takes() {
let mut s = session();
run(&mut s, "swatch.new", json!({"name": "Ink", "color": "#0000ff", "spot": true}));
run(&mut s, "swatch.new", json!({"name": "Ink half", "swatch": "Ink", "tint": 50}));
let dot = run(&mut s, "shape.rectangle", json!({"x": 0, "y": 0, "width": 6, "height": 6}))["id"].clone();
run(&mut s, "paint.setFill", json!({"swatch": "Ink", "ids": [dot]}));
run(&mut s, "paint.setStroke", json!({"none": true, "ids": [dot]}));
run(&mut s, "select.set", json!({"ids": [dot]}));
run(&mut s, "object.pattern.make", json!({"name": "Tiles", "width": 8, "height": 8}));
run(&mut s, "object.pattern.done", json!({}));
let b64 = run(&mut s, "document.serialize", json!({}))["dataBase64"].as_str().unwrap().to_string();
let id = run(&mut s, "swatch.library.load", json!({"dataBase64": b64, "name": "Tiles.vectorcraft"}))["library"].clone();
let r = run(&mut s, "shape.rectangle", json!({"x": 0, "y": 0, "width": 40, "height": 40}))["id"].clone();
run(&mut s, "paint.setFill", json!({"swatch": "Tiles", "ids": [r]}));
run(&mut s, "select.set", json!({"ids": [r]}));
run(&mut s, "edit.copy", json!({}));
for names in [json!(["Ink half", "Tiles"]), json!(["Tiles", "Ink half"])] {
run(&mut s, "file.new", json!({"width": 100, "height": 100}));
run(&mut s, "swatch.new", json!({"name": "Ink", "color": "#0000ff", "global": true}));
run(&mut s, "edit.paste", json!({"swatchConflict": "merge"}));
// The paste merged the spot color into the document's process color of its name.
assert_eq!(named_after(&s, "Ink"), ["Ink"], "{names}");
assert!(doc(&s).swatch("Ink").is_some_and(|w| w.global && !w.spot), "{names}");
run(&mut s, "swatch.delete", json!({"names": ["Tiles"]}));
let r = run(&mut s, "swatch.library.add", json!({"library": id, "names": names}));
let base = tint_base(&s, "Ink half").unwrap_or_default();
assert!(doc(&s).swatch(&base).is_some_and(|w| w.spot), "{names}: {r}");
}
}
/// A color a library pattern's art brings under a numbered name does not take a name the library
/// gives another swatch: adding that swatch next adds it under its own name, and adding the two
/// again adds nothing.
#[test]
fn a_color_a_pattern_brings_skips_the_librarys_numbered_names() {
let mut s = session();
run(&mut s, "swatch.new", json!({"name": "Ink", "color": "#0000ff", "global": true}));
run(&mut s, "swatch.new", json!({"name": "Ink 2", "color": "#00ff00", "global": true}));
let dot = run(&mut s, "shape.rectangle", json!({"x": 0, "y": 0, "width": 6, "height": 6}))["id"].clone();
run(&mut s, "paint.setFill", json!({"swatch": "Ink", "ids": [dot]}));
run(&mut s, "paint.setStroke", json!({"none": true, "ids": [dot]}));
run(&mut s, "select.set", json!({"ids": [dot]}));
run(&mut s, "object.pattern.make", json!({"name": "Tiles", "width": 8, "height": 8}));
run(&mut s, "object.pattern.done", json!({}));
let b64 = run(&mut s, "document.serialize", json!({}))["dataBase64"].as_str().unwrap().to_string();
let id = run(&mut s, "swatch.library.load", json!({"dataBase64": b64, "name": "Tiles.vectorcraft"}))["library"].clone();
run(&mut s, "file.new", json!({"width": 100, "height": 100}));
run(&mut s, "swatch.new", json!({"name": "Ink", "color": "#ff0000", "global": true}));
let first = run(&mut s, "swatch.library.add", json!({"library": id, "names": ["Tiles"]}));
let brought =
first["added"].as_array().and_then(|a| a.iter().filter_map(|n| n.as_str()).find(|n| n.starts_with("Ink"))).unwrap_or_default().to_string();
assert!(brought != "Ink 2" && is_global(&s, &brought, "#0000ff"), "{first}");
let r = run(&mut s, "swatch.library.add", json!({"library": id, "names": ["Ink 2"]}));
assert_eq!(r["added"], json!(["Ink 2"]), "{r}");
assert!(is_global(&s, "Ink 2", "#00ff00"), "{r}");
let again = run(&mut s, "swatch.library.add", json!({"library": id, "names": ["Tiles", "Ink", "Ink 2"]}));
assert_eq!((&again["added"], &again["existing"]), (&json!([]), &json!(["Tiles", brought, "Ink 2"])), "{again}");
}
/// A library tint added without its global color to a document that has no swatch of the color's
/// name brings the color along under its own name.
#[test]