Swatches: a library pattern keeps a global color whose name the document uses

When Add to Swatches brought a library pattern whose art links to a global
or spot color, and the document had that name for another color, the
pattern was relinked to the document's color. Adding the library's color
then made a numbered copy that the pattern did not use. The color now comes
in under a numbered name and the pattern links to it, as an added color
does, and the check for a pattern already in the document compares it the
same way.
This commit is contained in:
dotCooCoo committed 2026-11-17 06:20:11 -08:00
1 parent 485a1e5ae8
commit 9ef93eb563
5 files changed
+49 -4

No files matched your search

@@ -579,6 +579,11 @@ impl SwatchChoices {
Some(v) => Ok(Self { all: one(v)?, by_name: BTreeMap::new() }),
}
}
/// Add for every conflict: each pasted swatch whose name the document gives another color
/// comes in renamed.
pub(crate) fn add_all() -> Self {
Self { all: SwatchChoice::Add, by_name: BTreeMap::new() }
}
fn of(&self, name: &str) -> SwatchChoice {
self.by_name.get(name).copied().unwrap_or(self.all)
}
+4 -2
View File
@@ -89,9 +89,11 @@ pub fn swatch_source(w: &Swatch) -> Option<Arc<Document>> {
/// 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`, 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.
/// A global or spot color whose name `d` gives another color comes in under a free name, as Add
/// to Swatches adds such a color, so that the pattern keeps the library's colors.
pub(crate) fn import(d: &mut Document, lib: &Document, name: &str) -> Result<(String, Vec<(String, String)>)> {
let had: std::collections::HashSet<String> = d.swatches_iter().map(|w| w.name.clone()).collect();
let imported = gather(lib, name).import_into(d, &SwatchChoices::default(), false)?;
let imported = gather(lib, name).import_into(d, &SwatchChoices::add_all(), false)?;
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()
@@ -144,7 +146,7 @@ pub(crate) fn present_pattern(d: &Document, lib: &Document, name: &str) -> Optio
return None;
}
let mut probe = d.clone();
let imported = gathered.import_into(&mut probe, &SwatchChoices::default(), false).ok()?;
let imported = gathered.import_into(&mut probe, &SwatchChoices::add_all(), false).ok()?;
(imported.added == 0).then(|| imported_name(&imported, name))
}
+1 -1
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) 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 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; 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], existing: [names already there], applied?: name}",
has_doc,
add
),
+36
View File
@@ -449,6 +449,42 @@ fn a_color_a_pattern_brings_is_not_added_twice_in_one_call() {
assert!(d.swatches.iter().all(|w| w.name != "Ink"), "{r}");
}
/// A library pattern whose art links to a global color that the document has under the same name
/// in another color keeps the library's color: the color comes in under a free name ("Ink 2") and
/// the pattern links to it, as the color added in the same call does, and the document's own color
/// is left as it was. Adding the pattern again finds it.
#[test]
fn a_library_pattern_keeps_its_global_color_when_the_document_has_that_name_in_another_color() {
use std::sync::Arc;
use vectorcraft_doc::pattern::pattern_paint;
let mut s = session();
run(&mut s, "swatch.new", json!({"name": "Ink", "color": "#0000ff", "global": true}));
s.edit("Pattern", |d, _| {
let ink = Paint::Solid { color: Color::rgb8(0, 0, 0xff), 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 });
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}));
run(&mut s, "swatch.new", json!({"name": "Ink", "color": "#ff0000", "global": true}));
let r = run(&mut s, "swatch.library.add", json!({"library": id, "names": ["Tiles", "Ink"]}));
assert_eq!((&r["added"], &r["existing"]), (&json!(["Tiles", "Ink 2"]), &json!([])), "{r}");
let d = doc(&s);
let hex = |n: &str| d.swatch(n).and_then(|w| w.paint.color()).map(|c| c.to_hex());
assert_eq!((hex("Ink"), hex("Ink 2")), (Some("#ff0000".to_string()), Some("#0000ff".to_string())), "{r}");
let fill = d.pattern("Squares").unwrap().art[0].appearance.fill_paint();
let Paint::Solid { color, swatch, .. } = &fill else { panic!("the tile is filled with a color: {fill:?}") };
assert_eq!((swatch.as_deref(), color.to_hex()), (Some("Ink 2"), "#0000ff".to_string()), "{r}");
let again = run(&mut s, "swatch.library.add", json!({"library": id, "names": ["Tiles"]}));
assert_eq!((&again["added"], &again["existing"]), (&json!([]), &json!(["Tiles"])), "{again}");
}
#[test]
fn swatch_exchange_files_load_add_and_list_as_user_libraries() {
let bytes = vectorcraft_testkit::ase::sample();
+3 -1
View File
@@ -4931,7 +4931,9 @@ them as process colors; `.acb` files are read, never written. A file in the user
same extension and bytes as one there, loads as that User Defined library (category `user`).
From a document, such as a `.ai` or `.eps` swatch library, it reads the colors, tints, gradients and pattern swatches
in the document's order and color groups; `swatch.library.add` brings a pattern swatch's pattern, with its tile and
art, into the document as a pattern swatch under the library swatch's name and in its color group. A document with
art, into the document as a pattern swatch under the library swatch's name and in its color group. A global or spot
color the pattern's art links to keeps the library's color: when the document gives its name another color, it comes
in under a numbered name ("Ink 2") and the pattern links to that. A document with
no swatches, and a `.ai`, `.eps` or PostScript file whose swatches are only the default ones, are refused with
"`<file>` has no swatches of its own". The Swatch Libraries filter of Other Library… lists `.ai`, `.ait` and `.eps`
(`.epsf`) files as well as the library files.