From 485a1e5ae83caee4bff4f9ebda2806844f5c4923 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Mon, 16 Nov 2026 22:30:20 -0800 Subject: [PATCH] Swatches: a color a pattern brings in is not added again in the same call When one Add to Swatches call added a pattern swatch and then a global or spot color its art links to, the pattern brought the color in and the color was added a second time as a numbered copy. The color the pattern brought now takes the library swatch's place and color group instead. --- crates/engine/src/cmd/patternlib.rs | 17 ++++++++++--- crates/engine/src/cmd/swatchlib.rs | 22 ++++++++++++++--- crates/engine/src/tests/swatchlib.rs | 36 ++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+), 6 deletions(-) diff --git a/crates/engine/src/cmd/patternlib.rs b/crates/engine/src/cmd/patternlib.rs index 3a13100200..68d371b5ab 100644 --- a/crates/engine/src/cmd/patternlib.rs +++ b/crates/engine/src/cmd/patternlib.rs @@ -87,10 +87,21 @@ pub fn swatch_source(w: &Swatch) -> Option> { /// Add pattern `name` of library `lib` to `d` with the resources its art uses, as pasting art /// painted with it does: the same pattern there already is used, and a name another pattern or -/// swatch has gets a number → the pattern's name in `d`. -pub(crate) fn import(d: &mut Document, lib: &Document, name: &str) -> Result { +/// swatch has gets a number → the pattern's name in `d`, and the swatches the import added for +/// the pattern's art: (the swatch's name in `lib`, its name in `d`), pattern swatches left out. +pub(crate) fn import(d: &mut Document, lib: &Document, name: &str) -> Result<(String, Vec<(String, String)>)> { + let had: std::collections::HashSet = d.swatches_iter().map(|w| w.name.clone()).collect(); let imported = gather(lib, name).import_into(d, &SwatchChoices::default(), false)?; - Ok(imported_name(&imported, name)) + let from = |to: &str| { + let renamed = imported.renamed.iter().find(|r| r["kind"] == "swatch" && r["to"] == to).and_then(|r| r["from"].as_str()); + renamed.unwrap_or(to).to_string() + }; + let added = d + .swatches_iter() + .filter(|w| !had.contains(&w.name) && !matches!(w.paint, Paint::Pattern { .. })) + .map(|w| (from(&w.name), w.name.clone())) + .collect(); + Ok((imported_name(&imported, name), added)) } /// Pattern `name` of `lib` with the resources its art uses, as an object painted with it is copied. diff --git a/crates/engine/src/cmd/swatchlib.rs b/crates/engine/src/cmd/swatchlib.rs index 37e015423c..f874fa819f 100644 --- a/crates/engine/src/cmd/swatchlib.rs +++ b/crates/engine/src/cmd/swatchlib.rs @@ -970,13 +970,17 @@ fn add(s: &mut Session, p: &Value) -> Result { let before = d.swatch_groups.len(); let mut placed: Vec<(&str, usize)> = vec![]; let mut taken = taken_names(d); + // The swatches the patterns imported in this call brought with their art: (library + // name, document name). + let mut brought: Vec<(String, String)> = vec![]; for (group, w) in fresh { let (name, sw) = if let (Paint::Pattern { pattern, .. }, Some(src)) = (&w.paint, &patterns) { // The pattern comes with its swatch; a pattern the document has without one // gets one. The swatch a pattern that comes in brings is named after the pattern: // it takes the library swatch's name and color group instead. let had = taken_names(d); - let target = patternlib::import(d, src, pattern)?; + let (target, colors) = patternlib::import(d, src, pattern)?; + brought.extend(colors); if let Some(name) = library_pattern_swatch(d, w, &target).filter(|n| had.contains(n)) { reused.push((w.name.clone(), name)); taken = taken_names(d); @@ -1000,8 +1004,20 @@ fn add(s: &mut Session, p: &Value) -> Result { }; (name.clone(), Swatch { name, paint, global: false, spot: false }) } else { - let name = free_in(&mut taken, &w.name); - (name.clone(), Swatch { name, ..(*w).clone() }) + // A color a pattern added earlier in this call brought with its art is that + // swatch: it moves to the library swatch's place. + let at = brought.iter().position(|(from, _)| *from == w.name); + let moved = at.map(|i| brought.swap_remove(i).1).and_then(|n| { + let i = d.swatches.iter().position(|x| x.name == n)?; + Some(d.swatches.remove(i)) + }); + match moved { + Some(sw) => (sw.name.clone(), sw), + None => { + let name = free_in(&mut taken, &w.name); + (name.clone(), Swatch { name, ..(*w).clone() }) + } + } }; match group { Some(g) => { diff --git a/crates/engine/src/tests/swatchlib.rs b/crates/engine/src/tests/swatchlib.rs index 1f5d4ef3fb..8f16cae027 100644 --- a/crates/engine/src/tests/swatchlib.rs +++ b/crates/engine/src/tests/swatchlib.rs @@ -413,6 +413,42 @@ fn a_pattern_painted_with_another_pattern_is_not_matched_by_the_inner_patterns_n assert_eq!((&again["added"], &again["existing"]), (&json!([]), &json!(["Tiles 2"])), "{again}"); } +/// A global color that a library pattern's art links to, added in the same call after the pattern +/// swatch, is the swatch the pattern brought with it: it is added once, not again as a numbered +/// copy, and goes into the library swatch's color group. +#[test] +fn a_color_a_pattern_brings_is_not_added_twice_in_one_call() { + use std::sync::Arc; + use vectorcraft_doc::pattern::pattern_paint; + let mut s = session(); + run(&mut s, "swatch.new", json!({"name": "Ink", "color": "#123456", "global": true})); + s.edit("Pattern", |d, _| { + let ink = Paint::Solid { color: Color::rgb8(0x12, 0x34, 0x56), swatch: Some("Ink".into()), tint: 1.0 }; + let square = vectorcraft_geom::shapes::rectangle(vectorcraft_geom::Rect::new(0.0, 0.0, 6.0, 6.0)); + let square = vectorcraft_doc::Node::path(d.alloc_id(), square, vectorcraft_doc::Appearance::basic(ink, Paint::None, 0.0)); + let tile = vectorcraft_geom::Rect::new(0.0, 0.0, 8.0, 8.0); + d.patterns.push(vectorcraft_doc::PatternDef { tile, ..vectorcraft_doc::PatternDef::new("Squares", vec![Arc::new(square)]) }); + d.swatches.push(vectorcraft_color::Swatch { name: "Tiles".into(), paint: pattern_paint("Squares"), global: false, spot: false }); + // `Ink` goes into a color group of its own. + let i = d.swatches.iter().position(|w| w.name == "Ink").unwrap(); + let ink = d.swatches.remove(i); + d.swatch_groups.push(vectorcraft_color::SwatchGroup { name: "Inks".into(), swatches: vec![ink] }); + Ok(()) + }) + .unwrap(); + 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": "Squares.vectorcraft"}))["library"].clone(); + run(&mut s, "file.new", json!({"width": 100, "height": 100})); + let r = run(&mut s, "swatch.library.add", json!({"library": id, "names": ["Tiles", "Inks"]})); + let d = doc(&s); + let inks: Vec<&str> = d.swatches_iter().filter(|w| w.name.starts_with("Ink")).map(|w| w.name.as_str()).collect(); + assert_eq!(inks, ["Ink"], "{r}"); + assert_eq!((&r["added"], &r["existing"]), (&json!(["Tiles", "Ink"]), &json!([])), "{r}"); + let group = d.swatch_groups.iter().find(|g| g.name == "Inks").map(|g| g.swatches.iter().map(|w| w.name.as_str()).collect::>()); + assert_eq!(group, Some(vec!["Ink"]), "{r}"); + assert!(d.swatches.iter().all(|w| w.name != "Ink"), "{r}"); +} + #[test] fn swatch_exchange_files_load_add_and_list_as_user_libraries() { let bytes = vectorcraft_testkit::ase::sample();