From f6bbab4b6e482185696e7ebfc5da2d6bb98fa0ba Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sun, 22 Nov 2026 09:50:00 -0800 Subject: [PATCH] Swatch libraries: swatches a pattern brings skip the library's numbered names Add to Swatches now passes the clipboard importer the library's numbered swatch names that a picked pattern's swatches and patterns can come in under. A swatch or pattern that the import brings under a numbered name skips those names. This replaces the placeholder and copy swatches that held the names around the import. Paste and Use Library Item pass none and number swatches as before. - A tint or gradient of a numbered library name that the art brings, such as a tint "Ink 2" of "Ink", keeps that name. Before, in a document with another "Ink", the art's "Ink" came in as "Ink 2", the tint as "Ink 2 2", and adding the library's "Ink 2" next added it again. - A swatch of a numbered library name that the document does not use comes in under that name instead of as the document's identical copy ("Ink 2 2"). A copy of it that the import already brought is not reused for another library color of the same paint. - Of two library swatches with one numbered name, the first one in the library keeps the name when the other is a swatch the art brings or a pattern swatch. - A pattern that the art is painted with, and its swatch, skip the library's names when they come in numbered. A pattern of a numbered name whose library swatch places it otherwise keeps its name, and the swatch that comes with it gets another, so adding the library swatch next finds its name free. - A pattern swatch's numbered name skips the library's names, as a color's does, and a pattern swatch takes the library swatch's name when it is free, also when its pattern is reused. - A pattern pasted into the document is still found: when skipping the names would add the pattern again, or would reuse it only by merging colors, an import that does not skip them is used if it reuses the pasted pattern and gives none of those names to another swatch. - The paint and kind check on the colors a pattern's art brings or takes has a test: a library whose spot "Ink" shares its name with a global "Ink" in a color group, so that the art brings both. docs/mcp.md is rewrapped and says that a library color is numbered beside a document color of the same name and paint of another kind (process, global or spot), not only beside a process color. The params text of swatch.library.add, docs/mcp.md and the docs/target-app-parity.md line describe these rules and name their tests. --- crates/engine/src/cmd/clipboard/resources.rs | 134 +++- crates/engine/src/cmd/library.rs | 2 +- crates/engine/src/cmd/patternlib.rs | 102 ++- crates/engine/src/cmd/swatchlib.rs | 100 ++- crates/engine/src/tests/swatchlib.rs | 714 +++++++++++++++++++ docs/mcp.md | 37 +- docs/target-app-parity.md | 2 +- 7 files changed, 981 insertions(+), 110 deletions(-) diff --git a/crates/engine/src/cmd/clipboard/resources.rs b/crates/engine/src/cmd/clipboard/resources.rs index c20c763495..6303eababe 100644 --- a/crates/engine/src/cmd/clipboard/resources.rs +++ b/crates/engine/src/cmd/clipboard/resources.rs @@ -14,13 +14,14 @@ //! the document's Spot Colors option says. Pasting back into the document the objects came from //! uses its resources as they are now. -use std::cell::OnceCell; +use std::cell::{Cell, OnceCell}; use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet}; use std::sync::Arc; use serde_json::{Map, Value, json}; use vectorcraft_brush::Brush; use vectorcraft_color::{Color, Paint, Swatch}; +use vectorcraft_doc::pattern::pattern_paint; use vectorcraft_doc::text::{KINSOKU_NAME_MAX, KINSOKU_SETS_MAX}; use vectorcraft_doc::{ Appearance, AppearanceItem, Document, Effect, GraphDesign, GraphicStyle, Kinsoku, KinsokuSet, Node, NodeKind, PatternDef, Symbol, @@ -606,6 +607,9 @@ pub(crate) enum SwatchChoice { pub(crate) struct SwatchChoices { all: SwatchChoice, by_name: BTreeMap, + /// Names a swatch or pattern that comes in under a numbered name skips, each with the swatch + /// it is reserved for, which still comes in under it when the document does not use it. + reserved: Arc>, } impl SwatchChoices { @@ -617,16 +621,22 @@ impl SwatchChoices { }; match p.get("swatchConflict") { None | Some(Value::Null) => Ok(Self::default()), - Some(Value::Object(m)) => { - Ok(Self { all: SwatchChoice::Merge, by_name: m.iter().map(|(k, v)| Ok((k.clone(), one(v)?))).collect::>()? }) - } - Some(v) => Ok(Self { all: one(v)?, by_name: BTreeMap::new() }), + Some(Value::Object(m)) => Ok(Self { + all: SwatchChoice::Merge, + by_name: m.iter().map(|(k, v)| Ok((k.clone(), one(v)?))).collect::>()?, + reserved: Arc::default(), + }), + Some(v) => Ok(Self { all: one(v)?, ..Self::default() }), } } /// 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() } + Self { all: SwatchChoice::Add, ..Self::default() } + } + /// These choices, with numbered names skipping the names of `names` ([`SwatchChoices::reserved`]). + pub(crate) fn reserving(self, names: &Arc>) -> Self { + Self { reserved: Arc::clone(names), ..self } } fn of(&self, name: &str) -> SwatchChoice { self.by_name.get(name).copied().unwrap_or(self.all) @@ -1145,6 +1155,9 @@ pub(crate) struct Imported { pub added_designs: BTreeSet, /// The global and spot colors that took the document's color (merged). merged_swatches: BTreeSet, + /// Whether a name [`SwatchChoices::reserving`] reserves changed the name a swatch or pattern + /// came in under. + pub reserved_named: bool, } impl Imported { @@ -1411,9 +1424,27 @@ impl Clipboard { let bare = |p: &PatternDef| PatternDef { name: String::new(), art: vec![], ..p.clone() }; let same = |n: &str| !fresh && d.pattern(n).is_some_and(|x| bare(x) == bare(def) && same_art(&out.relink, &x.art, &def.art)); let p = existing(&def.name, d.patterns.iter().map(|p| p.name.as_str()), same_doc, same).unwrap_or_else(|| { - // Its swatch comes along under the same name. - let name = unique_name(&def.name, |n| d.pattern(n).is_some() || d.swatch_name_taken(n)); + // Its swatch comes along under the same name, which skips the reserved swatch + // names too, except its own name when that is reserved for a swatch of it. + let skipped = Cell::new(false); + let ours = |w: &Swatch| matches!(&w.paint, Paint::Pattern { pattern, .. } if *pattern == def.name); + let name = unique_name(&def.name, |n| { + let used = d.pattern(n).is_some() || d.swatch_name_taken(n); + let held = !used && choices.reserved.get(n).is_some_and(|w| n != def.name || !ours(w)); + skipped.set(skipped.get() || held); + used || held + }); + out.reserved_named |= skipped.get(); add_pattern(d, PatternDef { name: name.clone(), ..def.clone() }); + // The name is left to a swatch it is reserved for that places the pattern + // otherwise; the swatch the pattern comes with gets another. + let placed = choices.reserved.get(&name).is_some_and(|w| ours(w) && w.paint != pattern_paint(&def.name)); + if placed && d.swatches.last().is_some_and(|w| w.name == name) { + let other = unique_name(&name, |n| n == name || d.swatch_name_taken(n) || choices.reserved.contains_key(n)); + if let Some(w) = d.swatches.last_mut() { + w.name = other; + } + } Placed::added(&def.name, name) }); added_patterns.extend(p.new_name(&def.name)); @@ -1639,13 +1670,21 @@ impl Clipboard { let conflicts: BTreeSet = if same_doc { BTreeSet::new() } else { self.conflicts(d, used).into_iter().map(|(w, _)| w.name.clone()).collect() }; let bases: Vec<&Swatch> = self.swatches.iter().filter(|w| is_base(w) && used.has(Res::Swatch, &w.name)).collect(); + // The swatches this import brings, for the names reserved for them. + let brought: Vec<&Swatch> = if choices.reserved.is_empty() { + vec![] + } else { + self.swatches.iter().filter(|w| if is_base(w) { used.has(Res::Swatch, &w.name) } else { used.carries(w) }).collect() + }; for w in &bases { let p = if conflicts.contains(&w.name) && choices.of(&w.name) == SwatchChoice::Merge { out.merged += 1; out.merged_swatches.insert(w.name.clone()); Placed::Existing } else { - add_swatch(d, w, same_doc) + let (p, reserved_named) = add_swatch(d, w, w, same_doc, &choices.reserved, &brought, &out.relink); + out.reserved_named |= reserved_named; + p }; out.placed(Res::Swatch, &w.name, &p); } @@ -1660,7 +1699,8 @@ impl Clipboard { for def in self.swatches.iter().filter(|w| !is_base(w) && used.carries(w)) { let mut w = def.clone(); out.relink.paint(&mut w.paint); - let p = add_swatch(d, &w, same_doc); + let (p, reserved_named) = add_swatch(d, &w, def, same_doc, &choices.reserved, &brought, &out.relink); + out.reserved_named |= reserved_named; out.placed(Res::Swatch, &def.name, &p); } } @@ -1862,13 +1902,73 @@ pub(crate) fn import_graph_designs(src: &Document, d: &mut Document, names: &[St clip.import_used(d, &used, &SwatchChoices::default(), false) } +/// Whether `name` ends in a space and a number, as a numbered copy's name ("Ink 2") does. +pub(crate) fn numbered_name(name: &str) -> bool { + name.rsplit_once(' ').is_some_and(|(_, k)| !k.is_empty() && k.bytes().all(|b| b.is_ascii_digit())) +} + /// Swatch `w` in `d` ([`existing`]), else added under a free name (swatches and colour groups -/// share one namespace). -fn add_swatch(d: &mut Document, w: &Swatch, same_doc: bool) -> Placed { +/// share one namespace) → where it went, and whether `reserved` changed its name. `source` is the +/// pasted swatch `w` stands for, before its links were relinked. A numbered name skips the names +/// in `reserved`. A numbered name in `reserved` ("Ink 2") that `d` does not use is kept for the +/// swatch it is reserved for: that swatch comes in under it, not as a numbered copy of it that `d` +/// has, and another swatch of that name gets a number. A numbered copy in `d` whose name is +/// reserved for another swatch that the import brings (`brought`), and that is that swatch with +/// its links as `relink` gives them, is left to it. +fn add_swatch( + d: &mut Document, + w: &Swatch, + source: &Swatch, + same_doc: bool, + reserved: &BTreeMap, + brought: &[&Swatch], + relink: &Relink, +) -> (Placed, bool) { let same = |n: &str| d.swatch(n).is_some_and(|t| *t == Swatch { name: n.to_string(), ..w.clone() }); - existing(&w.name, d.swatches_iter().map(|t| t.name.as_str()), same_doc, same).unwrap_or_else(|| { - let name = d.free_swatch_name(&w.name); - d.swatches.push(Swatch { name: name.clone(), ..w.clone() }); - Placed::added(&w.name, name) - }) + let kept = (!same_doc && numbered_name(&w.name) && !d.swatch_name_taken(&w.name)).then(|| reserved.get(&w.name)).flatten(); + let (owns, held) = (kept.is_some_and(|o| o == source), kept.is_some_and(|o| o != source)); + let is_owner = |t: &Swatch, o: &Swatch| { + let mut o = o.clone(); + relink.paint(&mut o.paint); + *t == o + }; + let theirs = |n: &str| { + n != w.name + && copy_of(&w.name, n) + && reserved.get(n).is_some_and(|o| o != source && brought.contains(&o) && d.swatch(n).is_some_and(|t| is_owner(t, o))) + }; + let names = || d.swatches_iter().map(|t| t.name.as_str()); + let excluded = Cell::new(false); + let found = existing( + &w.name, + names().filter(|n| { + let t = theirs(n); + excluded.set(excluded.get() || t); + !t + }), + same_doc, + same, + ); + let reused = |p: &Option| match p { + Some(Placed::Reused(n)) => Some(n.clone()), + _ => None, + }; + let passed_over = excluded.get() && reused(&found) != reused(&existing(&w.name, names(), same_doc, same)); + let over_copy = match found { + Some(p) if !owns => return (p, passed_over), + found => found.is_some(), + }; + let mut skipped = held; + let name = if d.swatch_name_taken(&w.name) || held { + let mut free = |n: &String| { + let taken = d.swatch_name_taken(n); + skipped |= !taken && reserved.contains_key(n); + !taken && !reserved.contains_key(n) + }; + (2..).map(|i| format!("{} {i}", w.name)).find(&mut free).unwrap_or_else(|| d.free_swatch_name(&w.name)) + } else { + w.name.clone() + }; + d.swatches.push(Swatch { name: name.clone(), ..w.clone() }); + (Placed::added(&w.name, name), over_copy || skipped || passed_over) } diff --git a/crates/engine/src/cmd/library.rs b/crates/engine/src/cmd/library.rs index ecbfa760bb..d04b038a7d 100644 --- a/crates/engine/src/cmd/library.rs +++ b/crates/engine/src/cmd/library.rs @@ -1356,7 +1356,7 @@ fn use_pattern(s: &mut Session, data: &Value, stroke: bool) -> Result { // or spot color the pattern's art links to keeps the library's color, as Add to Swatches brings // a library pattern's colors. let current = s.doc()?.doc.clone(); - let (choices, probe, imported) = super::patternlib::probe_import(¤t, &doc, &name, &clip)?; + let (choices, probe, imported) = super::patternlib::probe_import(¤t, &doc, &name, &clip, &Default::default())?; drop(current); let target = super::patternlib::imported_name(&imported, &name); let paint = vectorcraft_doc::pattern::pattern_paint(&target); diff --git a/crates/engine/src/cmd/patternlib.rs b/crates/engine/src/cmd/patternlib.rs index 64342b21dd..53fc83b7c9 100644 --- a/crates/engine/src/cmd/patternlib.rs +++ b/crates/engine/src/cmd/patternlib.rs @@ -3,6 +3,7 @@ //! document holding its patterns, drawn here from plain shapes ([`PATTERN_LIBRARIES`]). //! `swatch.library.add` copies a pattern into the document with its swatch. +use std::collections::BTreeMap; use std::sync::{Arc, LazyLock}; use vectorcraft_color::{Color, Paint, Swatch, SwatchLibrary}; @@ -15,6 +16,10 @@ use super::clipboard::Clipboard; use super::clipboard::resources::{Imported, SwatchChoices}; use super::swatchlib::LibraryInfo; +/// Names that swatches and patterns coming in under a numbered name skip, each with the library +/// swatch it is reserved for ([`SwatchChoices::reserving`]). +pub(crate) type Reserved = Arc>; + /// A built-in pattern library: a stable id, its menu name and the function that adds its patterns. pub struct BuiltinPatternLibrary { pub id: &'static str, @@ -96,17 +101,18 @@ pub(crate) type SwatchNames = Vec<(String, String)>; /// 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. Those colors /// merge instead when that reuses a pattern of `d` that keeping them would add again -/// ([`probe_import`]). -pub(crate) fn import(d: &mut Document, lib: &Document, name: &str) -> Result<(String, SwatchNames, SwatchNames)> { +/// ([`probe_import`]). A swatch that comes in under a numbered name skips the names in +/// `reserved`. +pub(crate) fn import(d: &mut Document, lib: &Document, name: &str, reserved: &Reserved) -> Result<(String, SwatchNames, SwatchNames)> { let gathered = gather(lib, name); let had: std::collections::HashSet = d.swatches_iter().map(|w| w.name.clone()).collect(); // Without a color to merge or a pattern to reuse, the document is not copied. let imported = if !gathered.swatches.is_empty() && has_candidate(d, lib, name) { - let (_, probed, imported) = probe_import(d, lib, name, &gathered)?; + let (_, probed, imported) = probe_import(d, lib, name, &gathered, reserved)?; *d = probed; imported } else { - gathered.import_into(d, &SwatchChoices::add_all(), false)? + gathered.import_into(d, &SwatchChoices::add_all().reserving(reserved), 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()); @@ -138,9 +144,11 @@ pub(crate) fn brought_names(lib: &Document, name: &str) -> std::collections::Has gather(lib, name).swatches.into_iter().map(|w| w.name).collect() } -/// The names in `lib` of pattern `name` and of the patterns its art brings. -pub(crate) fn brought_patterns(lib: &Document, name: &str) -> std::collections::HashSet { - gather(lib, name).patterns.into_iter().map(|p| p.name).collect() +/// The names in `lib` of the swatches and patterns that pattern `name` of `lib` brings, itself +/// included. +pub(crate) fn brought_resource_names(lib: &Document, name: &str) -> std::collections::HashSet { + let gathered = gather(lib, name); + gathered.swatches.into_iter().map(|w| w.name).chain(gathered.patterns.into_iter().map(|p| p.name)).collect() } /// The name pattern `name` has in the document `imported` went into. @@ -154,8 +162,8 @@ pub(crate) fn imported_name(imported: &Imported, name: &str) -> String { /// name than its own, as [`import`] lists them. A pattern whose art uses no other resource is /// compared as [`existing_pattern`] does. One whose art uses other resources, such as a pattern it /// is painted with, is imported into a copy of `d` as [`probe_import`] does, so that those -/// resources are compared too. -pub(crate) fn present_pattern(d: &Document, lib: &Document, name: &str) -> Option<(String, SwatchNames)> { +/// resources are compared too, with numbered names skipping `reserved`. +pub(crate) fn present_pattern(d: &Document, lib: &Document, name: &str, reserved: &Reserved) -> Option<(String, SwatchNames)> { let gathered = gather(lib, name); let only_pattern = gathered.patterns.len() == 1 && gathered.symbols.is_empty() @@ -173,7 +181,7 @@ pub(crate) fn present_pattern(d: &Document, lib: &Document, name: &str) -> Optio if !has_candidate(d, lib, name) { return None; } - let (_, _, imported) = probe_import(d, lib, name, &gathered).ok()?; + let (_, _, imported) = probe_import(d, lib, name, &gathered, reserved).ok()?; if imported.added != 0 { return None; } @@ -203,19 +211,73 @@ pub(crate) fn has_candidate(d: &Document, lib: &Document, name: &str) -> bool { /// another color comes in under a free name (Add). Those colors merge instead when Add would add /// the pattern again while merging reuses a pattern of `d`: such a pattern has its art linked to /// the document's own colors of those names, as pasting with Merge, or adding it from a library -/// before, leaves it. -pub(crate) fn probe_import(d: &Document, lib: &Document, name: &str, gathered: &Clipboard) -> Result<(SwatchChoices, Document, Imported)> { +/// before, leaves it. A swatch or pattern that comes in under a numbered name skips the names in +/// `reserved` ([`SwatchChoices::reserving`]). When that changed a name and the pattern would come +/// in again, or would be reused only by merging colors, an import that does not skip them is used +/// if it reuses a pattern of `d` (one pasted there), without merging in the second case, and uses +/// none of those names that `d` does not use, other than for the swatch a name is reserved for. +pub(crate) fn probe_import( + d: &Document, + lib: &Document, + name: &str, + gathered: &Clipboard, + reserved: &Reserved, +) -> Result<(SwatchChoices, Document, Imported)> { let reused = |i: &Imported| d.pattern(&imported_name(i, name)).is_some(); - let mut with_add = d.clone(); - let added = gathered.import_into(&mut with_add, &SwatchChoices::add_all(), false)?; - if !reused(&added) && added.renamed.iter().any(|r| r["kind"] == "swatch") && has_candidate(d, lib, name) { - let mut merged = d.clone(); - let found = gathered.import_into(&mut merged, &SwatchChoices::default(), false)?; - if reused(&found) { - return Ok((SwatchChoices::default(), merged, found)); + let found = probe_skipping(d, lib, name, gathered, reserved)?; + // A reuse that merges colors gives the pattern the document's colors, so one that keeps the + // library's is tried too. + let merged = |i: &Imported| reused(i) && i.merged != 0; + if found.2.reserved_named && (!reused(&found.2) || merged(&found.2)) && has_candidate(d, lib, name) { + let pasted = probe_skipping(d, lib, name, gathered, &Reserved::default())?; + let better = !reused(&found.2) || !merged(&pasted.2); + if reused(&pasted.2) && better && !takes_reserved(d, &pasted.1, &pasted.2, gathered, reserved) { + return Ok(pasted); } } - Ok((SwatchChoices::add_all(), with_add, added)) + Ok(found) +} + +/// Whether `probed`, the import of `gathered` into a copy of `d` that `imported` describes, gives a +/// name in `reserved` that `d` does not use to a pattern, or to a swatch other than the one the name +/// is reserved for coming in under its own name. +fn takes_reserved(d: &Document, probed: &Document, imported: &Imported, gathered: &Clipboard, reserved: &Reserved) -> bool { + let had: std::collections::HashSet<&str> = + d.swatches_iter().map(|w| w.name.as_str()).chain(d.swatch_groups.iter().map(|g| g.name.as_str())).collect(); + let renamed_to = |n: &str| imported.renamed.iter().any(|r| r["kind"] == "swatch" && r["to"] == n && r["from"] != n); + let owner_only = |n: &str, owner: &Swatch| gathered.swatches.iter().filter(|w| w.name == n).all(|w| w == owner); + let swatch = probed + .swatches_iter() + .map(|w| w.name.as_str()) + .filter(|n| !had.contains(n)) + .any(|n| reserved.get(n).is_some_and(|owner| !owner_only(n, owner) || renamed_to(n))); + let patterns: std::collections::HashSet<&str> = d.patterns.iter().map(|p| p.name.as_str()).collect(); + swatch || probed.patterns.iter().any(|p| !patterns.contains(p.name.as_str()) && reserved.contains_key(&p.name)) +} + +/// [`probe_import`] with numbered names skipping `reserved`. +fn probe_skipping( + d: &Document, + lib: &Document, + name: &str, + gathered: &Clipboard, + reserved: &Reserved, +) -> Result<(SwatchChoices, Document, Imported)> { + let reused = |i: &Imported| d.pattern(&imported_name(i, name)).is_some(); + let (add, merge) = (SwatchChoices::add_all().reserving(reserved), SwatchChoices::default().reserving(reserved)); + let mut with_add = d.clone(); + let mut added = gathered.import_into(&mut with_add, &add, false)?; + if !reused(&added) && added.renamed.iter().any(|r| r["kind"] == "swatch") && has_candidate(d, lib, name) { + let mut merged = d.clone(); + let mut found = gathered.import_into(&mut merged, &merge, false)?; + // Whether a reservation changed a name counts for either import. + found.reserved_named |= added.reserved_named; + if reused(&found) { + return Ok((merge, merged, found)); + } + added.reserved_named |= found.reserved_named; + } + Ok((add, with_add, added)) } /// The pattern of `d` that is pattern `name` of library `lib`: the same tile options and art under diff --git a/crates/engine/src/cmd/swatchlib.rs b/crates/engine/src/cmd/swatchlib.rs index 341a26a4b1..1176bc586c 100644 --- a/crates/engine/src/cmd/swatchlib.rs +++ b/crates/engine/src/cmd/swatchlib.rs @@ -17,6 +17,7 @@ use vectorcraft_color::swatch::REGISTRATION; use vectorcraft_color::{Paint, Swatch, SwatchGroup, SwatchLibrary, default_swatches}; use vectorcraft_doc::pattern::pattern_paint; +use super::clipboard::resources::numbered_name; use super::fileio::{create_dir, extension, read_file, read_file_head, write_file, write_new_file}; use super::menucmds::squash; use super::package::free_name; @@ -49,7 +50,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, 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}", + "{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; a swatch of a numbered library name that the art brings, such as a tint \"Ink 2\", keeps that name when the document does not use it, and a pattern swatch or a pattern the art is painted with that comes in numbered skips the library's other names too → {library, added: [names in the document, the colors patterns, tints and gradients brought included], existing: [names already there], applied?: name}", has_doc, add ), @@ -1023,55 +1024,27 @@ fn free_copy_in(taken: &mut HashSet, base: &str, reserved: impl Fn(&str) name } -/// [`patternlib::import`] of pattern `pattern` of `src` into `d`, keeping the library's numbered -/// swatch names (the keys of `names`, such as "Ink 2") that `d` does not use for the library's own -/// swatches of those names, so that a color the art brings under a numbered name does not take one -/// of them. While the import runs, a placeholder swatch holds each such name the art does not -/// bring, and a copy of the library's color holds each one the art brings, which the art then -/// takes. The placeholders are removed before it returns, and so is a copy the art did not take; -/// a copy the art took is listed with the colors the import brought. Names of the patterns the -/// import brings are not held. -fn import_holding( - d: &mut vectorcraft_doc::Document, - src: &vectorcraft_doc::Document, - pattern: &str, - names: &HashMap<&str, &Swatch>, -) -> Result<(String, patternlib::SwatchNames, patternlib::SwatchNames)> { - let numbered = |n: &str| n.rsplit_once(' ').is_some_and(|(_, k)| !k.is_empty() && k.bytes().all(|b| b.is_ascii_digit())); - let taken = taken_names(d); - let mut free: Vec<&str> = names.keys().copied().filter(|n| numbered(n) && !taken.contains(*n)).collect(); - free.sort_unstable(); - let (mut held, mut copies): (Vec, Vec) = (vec![], vec![]); - if !free.is_empty() { - let brings = patternlib::brought_names(src, pattern); - let patterns = patternlib::brought_patterns(src, pattern); - for n in free.into_iter().filter(|n| !patterns.contains(*n)) { - if !brings.contains(n) { - held.push(Swatch { name: n.to_string(), paint: Paint::None, global: false, spot: false }); - } else if let Some(w) = names.get(n).filter(|w| w.paint.color().is_some() && linked_names(&w.paint).is_empty()) { - copies.push((*w).clone()); - } +/// The name of the document's swatch for library pattern swatch `w`, whose pattern came in as +/// `target`, the import having made swatch `made` for it (removed again). A numbered name skips +/// the names of the library's other swatches (`lib_by_name`), as for a color; so does a swatch +/// named after its pattern when the pattern's name is one of them. The swatch takes the library +/// swatch's name when it is free and the library has no other swatch of that name, and leaves a +/// numbered name the library gives an earlier swatch to that one. +fn pattern_swatch_name(w: &Swatch, target: &str, made: Option, taken: &mut HashSet, lib_by_name: &HashMap<&str, &Swatch>) -> String { + let reserved = |n: &str| lib_by_name.contains_key(n); + let owns = lib_by_name.get(w.name.as_str()).is_some_and(|o| *o == w); + let named_after = matches!(&w.paint, Paint::Pattern { pattern, .. } if *pattern == w.name); + match made { + _ if named_after && owns && !taken.contains(&w.name) => w.name.clone(), + _ if !owns && numbered_name(&w.name) && !taken.contains(&w.name) => { + let mut held = taken.clone(); + held.insert(w.name.clone()); + free_copy_in(&mut held, &w.name, reserved) } + Some(made) if named_after => made, + None if named_after && (target == w.name || !reserved(target)) => free_copy_in(taken, target, reserved), + _ => free_copy_in(taken, &w.name, reserved), } - let start = d.swatches.len(); - d.swatches.extend(held.iter().chain(&copies).cloned()); - let imported = patternlib::import(d, src, pattern); - let used = |n: &str| { - imported - .as_ref() - .is_ok_and(|(_, colors, took)| !colors.iter().any(|(from, _)| from == n) && !took.iter().any(|(from, to)| from == n && to != n)) - }; - let used: HashSet = copies.iter().map(|w| w.name.clone()).filter(|n| used(n)).collect(); - let mut i = 0; - d.swatches.retain(|w| { - i += 1; - let ours = held.iter().chain(&copies).any(|h| h.name == w.name && h.paint == w.paint); - !(i > start && ours && !used.contains(&w.name)) - }); - let (target, mut colors, mut took) = imported?; - took.retain(|(from, _)| !used.contains(from)); - colors.extend(copies.into_iter().map(|w| w.name).filter(|n| used.contains(n)).map(|n| (n.clone(), n))); - Ok((target, colors, took)) } /// Link the swatches of `d` that `pending` names (document name, library swatch) to their colors @@ -1195,6 +1168,25 @@ fn add(s: &mut Session, p: &Value) -> Result { for w in lib.swatches.iter().chain(lib.groups.iter().flat_map(|g| &g.swatches)) { lib_by_name.entry(w.name.as_str()).or_insert(w); } + // A swatch a pattern's art brings under a numbered name skips the library's numbered names, + // which a later add looks up as those swatches'. Only the names a picked pattern's swatches and + // patterns can come in under, their own or one numbered after them, are needed. + let mut brings_names: HashSet = HashSet::new(); + if let Some(src) = patterns.as_ref().filter(|_| lib_by_name.keys().any(|n| numbered_name(n))) { + let picked_patterns: BTreeSet<&str> = picked + .iter() + .filter_map(|(_, w)| match &w.paint { + Paint::Pattern { pattern, .. } => Some(pattern.as_str()), + _ => None, + }) + .collect(); + for pattern in picked_patterns { + brings_names.extend(patternlib::brought_resource_names(src, pattern)); + } + } + let can_take = |n: &str| brings_names.contains(n) || n.rsplit_once(' ').is_some_and(|(base, _)| brings_names.contains(base)); + let lib_names: patternlib::Reserved = + Arc::new(lib_by_name.iter().filter(|(n, _)| numbered_name(n) && can_take(n)).map(|(n, w)| (n.to_string(), (*w).clone())).collect()); let group_names: HashSet<&str> = d.swatch_groups.iter().map(|g| g.name.as_str()).collect(); // A color whose name the document gives another color, or a color group, is the numbered copy // of it that an earlier add or a pattern's art brought in, when that copy is the same color. A @@ -1226,7 +1218,7 @@ fn add(s: &mut Session, p: &Value) -> Result { if let Some(src) = &patterns { for (_, w) in &picked { let Paint::Pattern { pattern, .. } = &w.paint else { continue }; - present.entry(pattern.as_str()).or_insert_with(|| patternlib::present_pattern(d, src, pattern)); + present.entry(pattern.as_str()).or_insert_with(|| patternlib::present_pattern(d, src, pattern, &lib_names)); } } // Whether the document's swatch `x` of the name of library swatch `w` is `w` with its colors @@ -1431,7 +1423,7 @@ fn add(s: &mut Session, p: &Value) -> Result { let mut learned = vec![]; if pending.iter().any(|(_, w)| linked_names(&w.paint).iter().any(waited)) { let mut probe = d.clone(); - let (_, colors, took) = import_holding(&mut probe, src, pattern, &lib_by_name)?; + let (_, colors, took) = patternlib::import(&mut probe, src, pattern, &lib_names)?; 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); @@ -1441,7 +1433,7 @@ fn add(s: &mut Session, p: &Value) -> Result { } let settled = settle(d, &mut pending, &links); let had = taken_names(d); - let (target, colors, took) = import_holding(d, src, pattern, &lib_by_name)?; + let (target, colors, took) = patternlib::import(d, src, pattern, &lib_names)?; // 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 @@ -1476,11 +1468,7 @@ fn add(s: &mut Session, p: &Value) -> Result { d.swatches.retain(|x| x.name != *made); } taken = taken_names(d); - let name = match made { - Some(made) if w.name == *pattern => made, - None if w.name == *pattern => d.free_swatch_name(&target), - _ => free_in(&mut taken, &w.name), - }; + let name = pattern_swatch_name(w, &target, made, &mut taken, &lib_by_name); taken.insert(name.clone()); // The library swatch's placement of its pattern is kept. let paint = match &w.paint { diff --git a/crates/engine/src/tests/swatchlib.rs b/crates/engine/src/tests/swatchlib.rs index 5d94eecc69..6e9c8ea8a8 100644 --- a/crates/engine/src/tests/swatchlib.rs +++ b/crates/engine/src/tests/swatchlib.rs @@ -1313,6 +1313,720 @@ fn a_color_a_pattern_brings_leaves_links_to_a_deleted_color_alone() { assert!(doc(&s).swatch("Ink 2").is_none(), "{r}"); } +/// A tint or gradient of a numbered library name ("Ink 2") that a library pattern's art brings +/// keeps that name when the document gives the art's color "Ink" another color, so adding the +/// library's "Ink 2" next finds it. +#[test] +fn a_numbered_tint_or_gradient_a_pattern_brings_keeps_the_librarys_name() { + let tint = json!({"name": "Ink 2", "swatch": "Ink", "tint": 50}); + let gradient = json!({"name": "Ink 2", "gradient": {"stops": [{"offset": 0, "swatch": "Ink"}, {"offset": 1, "color": "#ffffff"}]}}); + for numbered in [tint, gradient] { + let mut s = session(); + run(&mut s, "swatch.new", json!({"name": "Ink", "color": "#0000ff", "global": true})); + run(&mut s, "swatch.new", numbered.clone()); + 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 2", "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"]})); + assert!(doc(&s).swatch("Ink 2").is_some_and(|w| !w.global && !w.spot), "{numbered}: {first}"); + let r = run(&mut s, "swatch.library.add", json!({"library": id, "names": ["Ink 2"]})); + assert_eq!((&r["added"], &r["existing"]), (&json!([]), &json!(["Ink 2"])), "{numbered}: {first} {r}"); + } +} + +/// A library pattern swatch that comes in under a numbered name skips the names the library gives +/// its other swatches: adding the library's "Dots 2" next adds it under its own name. +#[test] +fn a_library_pattern_swatchs_copy_skips_the_librarys_other_names() { + let mut s = session(); + run(&mut s, "swatch.new", json!({"name": "Ink", "color": "#0000ff", "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": "Squares", "width": 8, "height": 8})); + run(&mut s, "object.pattern.done", json!({})); + s.edit("Dots", |d, _| { + let paint = vectorcraft_doc::pattern::pattern_paint("Squares"); + d.swatches.push(vectorcraft_color::Swatch { name: "Dots".into(), paint, global: false, spot: false }); + Ok(()) + }) + .unwrap(); + run(&mut s, "swatch.new", json!({"name": "Dots 2", "color": "#00ff00", "global": true})); + 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": "Dots", "color": "#ff0000"})); + let first = run(&mut s, "swatch.library.add", json!({"library": id, "names": ["Dots"]})); + let r = run(&mut s, "swatch.library.add", json!({"library": id, "names": ["Dots 2"]})); + assert_eq!(r["added"], json!(["Dots 2"]), "{first} {r}"); + assert!(is_global(&s, "Dots 2", "#00ff00"), "{first} {r}"); +} + +/// A library whose spot color "Ink" shares its name with a global color in a color group: a +/// pattern's art painted with both brings both. A library tint of the spot color, picked before +/// the pattern, links to the document's copy of the spot color the art takes, not to the global +/// color the art brings under a numbered name, whether that color has another paint or the same. +#[test] +fn a_library_tint_is_not_linked_to_a_namesake_of_another_kind_a_pattern_brings() { + for global_color in ["#ff0000", "#0000ff"] { + a_library_tint_skips_a_namesake_the_pattern_brings(global_color); + } +} + +fn a_library_tint_skips_a_namesake_the_pattern_brings(global_color: &str) { + let mut s = session(); + run(&mut s, "swatch.new", json!({"name": "Ink", "color": "#0000ff", "spot": true})); + run(&mut s, "swatch.new", json!({"name": "Ink 2", "color": "#0000ff", "spot": true})); + run(&mut s, "swatch.new", json!({"name": "Ink half", "swatch": "Ink", "tint": 50})); + run(&mut s, "swatch.new", json!({"name": "Crimson", "color": global_color, "global": true})); + let blue = run(&mut s, "shape.rectangle", json!({"x": 0, "y": 0, "width": 3, "height": 6}))["id"].clone(); + let red = run(&mut s, "shape.rectangle", json!({"x": 3, "y": 0, "width": 3, "height": 6}))["id"].clone(); + run(&mut s, "paint.setFill", json!({"swatch": "Ink", "ids": [blue]})); + run(&mut s, "paint.setFill", json!({"swatch": "Crimson", "ids": [red]})); + run(&mut s, "paint.setStroke", json!({"none": true, "ids": [blue, red]})); + run(&mut s, "select.set", json!({"ids": [blue, red]})); + run(&mut s, "object.pattern.make", json!({"name": "Tiles", "width": 8, "height": 8})); + run(&mut s, "object.pattern.done", json!({})); + // The global color becomes "Ink" in a color group, and the pattern's art links to it by that name. + s.edit("Rename", |d, _| { + let i = d.swatches.iter().position(|w| w.name == "Crimson").unwrap(); + let mut global = d.swatches.remove(i); + global.name = "Ink".into(); + d.swatch_groups.push(vectorcraft_color::SwatchGroup { name: "Reds".into(), swatches: vec![global] }); + let def = d.pattern_mut("Tiles").unwrap(); + for node in &mut def.art { + for it in &mut std::sync::Arc::make_mut(node).appearance.items { + it.paint_mut().map_links(&mut |_, link, _| { + let red = link.as_deref() == Some("Crimson"); + if red { + *link = Some("Ink".into()); + } + red + }); + } + } + Ok(()) + }) + .unwrap(); + let art_links: Vec> = doc(&s).pattern("Tiles").unwrap().art.iter().map(|n| tint_link(&n.appearance.fill_paint())).collect(); + assert_eq!(art_links, [Some("Ink".to_string()), Some("Ink".to_string())]); + 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": "#0000ff"})); + run(&mut s, "swatch.new", json!({"name": "Ink 2", "color": "#0000ff", "spot": true})); + let r = run(&mut s, "swatch.library.add", json!({"library": id, "names": ["Ink half", "Tiles"]})); + assert_eq!(tint_base(&s, "Ink half").as_deref(), Some("Ink 2"), "{global_color}: {r}"); + assert!(doc(&s).swatch("Ink 2").is_some_and(|w| w.spot), "{global_color}: {r}"); +} + +/// A color of a numbered library name ("Ink 2") that a pattern's art brings comes in under that +/// name when the document does not use it, not as the document's copy of it ("Ink 2 2"), so +/// adding the library's "Ink 2" next finds it. +#[test] +fn a_numbered_color_a_pattern_brings_keeps_its_name_beside_a_copy_of_it() { + let mut s = session(); + 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 2", "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 2 2", "color": "#00ff00", "global": true})); + let first = run(&mut s, "swatch.library.add", json!({"library": id, "names": ["Tiles"]})); + assert!(is_global(&s, "Ink 2", "#00ff00"), "{first}"); + let r = run(&mut s, "swatch.library.add", json!({"library": id, "names": ["Ink 2"]})); + assert_eq!((&r["added"], &r["existing"]), (&json!([]), &json!(["Ink 2"])), "{first} {r}"); +} + +/// A pattern that a library pattern's art is painted with ("Inner"), brought under a numbered name +/// because the document has a swatch "Inner", skips the name "Inner 2" that the library gives a +/// color, as does the swatch that comes with it. +#[test] +fn a_nested_pattern_a_library_pattern_brings_skips_the_librarys_names() { + let mut s = session(); + let dot = run(&mut s, "shape.rectangle", json!({"x": 0, "y": 0, "width": 4, "height": 4}))["id"].clone(); + run(&mut s, "select.set", json!({"ids": [dot]})); + run(&mut s, "object.pattern.make", json!({"name": "Inner", "width": 6, "height": 6})); + run(&mut s, "object.pattern.done", json!({})); + let tile = run(&mut s, "shape.rectangle", json!({"x": 0, "y": 0, "width": 10, "height": 10}))["id"].clone(); + run(&mut s, "paint.setFill", json!({"swatch": "Inner", "ids": [tile]})); + run(&mut s, "select.set", json!({"ids": [tile]})); + run(&mut s, "object.pattern.make", json!({"name": "Outer", "width": 12, "height": 12})); + run(&mut s, "object.pattern.done", json!({})); + run(&mut s, "swatch.new", json!({"name": "Inner 2", "color": "#00ff00", "global": true})); + 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": "Outer.vectorcraft"}))["library"].clone(); + run(&mut s, "file.new", json!({"width": 100, "height": 100})); + run(&mut s, "swatch.new", json!({"name": "Inner", "color": "#ff0000"})); + let first = run(&mut s, "swatch.library.add", json!({"library": id, "names": ["Outer"]})); + assert!(doc(&s).pattern("Inner 2").is_none(), "{first}"); + let r = run(&mut s, "swatch.library.add", json!({"library": id, "names": ["Inner 2"]})); + assert_eq!(r["added"], json!(["Inner 2"]), "{first} {r}"); + assert!(is_global(&s, "Inner 2", "#00ff00"), "{first} {r}"); +} + +/// A library pattern that was pasted into the document with its art linked to the document's +/// copy of a numbered library color ("Ink 2 2" for "Ink 2") is found when it is added from the +/// library, not added again. +#[test] +fn a_pasted_pattern_linked_to_a_copy_of_a_numbered_color_is_found() { + let mut s = session(); + 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 2", "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!({})); + run(&mut s, "file.new", json!({"width": 100, "height": 100})); + run(&mut s, "swatch.new", json!({"name": "Ink 2 2", "color": "#00ff00", "global": true})); + run(&mut s, "edit.paste", json!({})); + assert!(doc(&s).swatch("Ink 2").is_none(), "the paste reused the copy"); + run(&mut s, "swatch.delete", json!({"names": ["Tiles"]})); + let r = run(&mut s, "swatch.library.add", json!({"library": id, "names": ["Tiles"]})); + assert_eq!(doc(&s).patterns.len(), 1, "{r}"); +} + +/// A library pattern swatch named after its pattern ("Tiles 2") that comes in with a new pattern +/// under a numbered name ("Tiles 2 2"), because the document has another pattern of that name +/// without a swatch, takes the library swatch's name. +#[test] +fn a_new_patterns_swatch_takes_the_library_swatchs_free_name() { + let mut s = session(); + let dot = run(&mut s, "shape.rectangle", json!({"x": 0, "y": 0, "width": 6, "height": 6}))["id"].clone(); + run(&mut s, "select.set", json!({"ids": [dot]})); + run(&mut s, "object.pattern.make", json!({"name": "Tiles 2", "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})); + let own = run(&mut s, "shape.rectangle", json!({"x": 0, "y": 0, "width": 9, "height": 9}))["id"].clone(); + run(&mut s, "select.set", json!({"ids": [own]})); + run(&mut s, "object.pattern.make", json!({"name": "Tiles 2", "width": 12, "height": 12})); + run(&mut s, "object.pattern.done", json!({})); + run(&mut s, "swatch.delete", json!({"names": ["Tiles 2"]})); + assert!(doc(&s).pattern("Tiles 2").is_some(), "the document keeps its pattern"); + let r = run(&mut s, "swatch.library.add", json!({"library": id, "names": ["Tiles 2"]})); + assert_eq!(r["added"], json!(["Tiles 2"]), "{r}"); + let paint = doc(&s).swatch("Tiles 2").map(|w| w.paint.clone()); + assert!(matches!(&paint, Some(Paint::Pattern { pattern, .. }) if pattern != "Tiles 2"), "{r} {paint:?}"); + // Added again after its swatch is deleted, it reuses the pattern under the same swatch name. + run(&mut s, "swatch.delete", json!({"names": ["Tiles 2"]})); + let again = run(&mut s, "swatch.library.add", json!({"library": id, "names": ["Tiles 2"]})); + assert_eq!(again["added"], json!(["Tiles 2"]), "{r} {again}"); + assert_eq!(doc(&s).swatch("Tiles 2").map(|w| w.paint.clone()), paint, "{r} {again}"); +} + +/// A library whose process color "Ink 2" shares its name with a global color in a color group +/// that a pattern's art is painted with: the global color comes in numbered, so adding the +/// library's "Ink 2" next adds the process color under its own name. +#[test] +fn a_numbered_name_is_kept_for_the_library_swatch_it_names() { + let mut s = session(); + run(&mut s, "swatch.new", json!({"name": "Ink 2", "color": "#00ff00"})); + run(&mut s, "swatch.new", json!({"name": "Crimson", "color": "#0000ff", "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": "Crimson", "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!({})); + group_as(&mut s, "Crimson", "Ink 2"); + 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})); + let first = run(&mut s, "swatch.library.add", json!({"library": id, "names": ["Tiles"]})); + let r = run(&mut s, "swatch.library.add", json!({"library": id, "names": ["Ink 2"]})); + assert_eq!(r["added"], json!(["Ink 2"]), "{first} {r}"); + assert!(doc(&s).swatch("Ink 2").is_some_and(|w| !w.global && !w.spot), "{first} {r}"); +} + +/// A library pattern pasted with Add, whose color came in as "Ink 2" and was then deleted without +/// unlinking the art, is not reused through a new "Ink 2" when the library gives that name another +/// color: adding that color next adds it under its own name. This holds too when the document has +/// a pattern "Ink 2" without a swatch. +#[test] +fn a_pasted_pattern_is_not_reused_by_taking_a_librarys_name() { + for bare in [false, true] { + 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 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!({})); + run(&mut s, "file.new", json!({"width": 100, "height": 100})); + if bare { + let own = run(&mut s, "shape.rectangle", json!({"x": 0, "y": 0, "width": 9, "height": 9}))["id"].clone(); + run(&mut s, "select.set", json!({"ids": [own]})); + run(&mut s, "object.pattern.make", json!({"name": "Ink 2", "width": 12, "height": 12})); + run(&mut s, "object.pattern.done", json!({})); + run(&mut s, "swatch.delete", json!({"names": ["Ink 2"]})); + assert!(doc(&s).pattern("Ink 2").is_some(), "the document keeps its pattern"); + } + run(&mut s, "swatch.new", json!({"name": "Ink", "color": "#ff0000", "global": true})); + run(&mut s, "edit.paste", json!({"swatchConflict": "add"})); + assert!(is_global(&s, "Ink 2", "#0000ff"), "the paste brings the blue color as Ink 2 ({bare})"); + run(&mut s, "swatch.delete", json!({"names": ["Ink 2"], "unlink": false})); + run(&mut s, "swatch.delete", json!({"names": ["Tiles"]})); + let first = run(&mut s, "swatch.library.add", json!({"library": id, "names": ["Tiles"]})); + let r = run(&mut s, "swatch.library.add", json!({"library": id, "names": ["Ink 2"]})); + assert_eq!(r["added"], json!(["Ink 2"]), "{bare} {first} {r}"); + assert!(is_global(&s, "Ink 2", "#00ff00"), "{bare} {first} {r}"); + } +} + +/// A library pattern painted with two global colors of one paint, "Ink" and "Ink 2", added beside +/// the document's red "Ink" brings them as "Ink 3" and "Ink 2". Adding the pattern again finds it, +/// in either order of the library's colors: "Ink" does not reuse the library's "Ink 2". +#[test] +fn a_color_a_pattern_brings_again_does_not_reuse_a_copy_the_library_names() { + for ink_first in [true, false] { + let mut s = session(); + let names = if ink_first { ["Ink", "Ink 2"] } else { ["Ink 2", "Ink"] }; + for n in names { + run(&mut s, "swatch.new", json!({"name": n, "color": "#0000ff", "global": true})); + } + let mut dots = vec![]; + for (i, n) in ["Ink", "Ink 2"].iter().enumerate() { + let dot = run(&mut s, "shape.rectangle", json!({"x": i * 5, "y": 0, "width": 4, "height": 4}))["id"].clone(); + run(&mut s, "paint.setFill", json!({"swatch": n, "ids": [dot]})); + run(&mut s, "paint.setStroke", json!({"none": true, "ids": [dot]})); + dots.push(dot); + } + run(&mut s, "select.set", json!({"ids": dots})); + run(&mut s, "object.pattern.make", json!({"name": "Tiles", "width": 12, "height": 6})); + 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"]})); + assert!(is_global(&s, "Ink 2", "#0000ff") && is_global(&s, "Ink 3", "#0000ff"), "{ink_first} {first}"); + run(&mut s, "swatch.delete", json!({"names": ["Tiles"]})); + let r = run(&mut s, "swatch.library.add", json!({"library": id, "names": ["Tiles"]})); + assert_eq!(doc(&s).patterns.len(), 1, "{ink_first} {first} {r}"); + } +} + +/// A library pattern painted with a blue global "Ink" and a green global "Ink 2", added to a +/// document with a red "Ink" and a blue global "Ink 2": the art's "Ink" is the document's "Ink 2", +/// since that is not the library's "Ink 2", and no third copy comes in. +#[test] +fn a_color_a_pattern_brings_reuses_a_copy_that_is_not_the_librarys_namesake() { + 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 mut dots = vec![]; + for (i, n) in ["Ink", "Ink 2"].iter().enumerate() { + let dot = run(&mut s, "shape.rectangle", json!({"x": i * 5, "y": 0, "width": 4, "height": 4}))["id"].clone(); + run(&mut s, "paint.setFill", json!({"swatch": n, "ids": [dot]})); + run(&mut s, "paint.setStroke", json!({"none": true, "ids": [dot]})); + dots.push(dot); + } + run(&mut s, "select.set", json!({"ids": dots})); + run(&mut s, "object.pattern.make", json!({"name": "Tiles", "width": 12, "height": 6})); + 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})); + 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": ["Tiles"]})); + assert!(doc(&s).swatch("Ink 3").is_none(), "{r}"); + assert!(is_global(&s, "Ink 2", "#0000ff") && is_global(&s, "Ink 2 2", "#00ff00"), "{r}"); +} + +/// A library pattern painted with a blue global "Ink", pasted beside the document's red "Ink" and +/// then left without its color "Ink 2" (deleted without unlinking), is not reused by bringing the +/// library's "Ink" back as "Ink 2" when the library has its own blue "Ink 2": the pattern links to +/// another copy. +#[test] +fn a_pasted_pattern_is_not_reused_by_giving_a_librarys_name_to_another_color() { + 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": "#0000ff", "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 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!({})); + 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, "edit.paste", json!({"swatchConflict": "add"})); + assert!(is_global(&s, "Ink 2", "#0000ff"), "the paste brings the blue color as Ink 2"); + run(&mut s, "swatch.delete", json!({"names": ["Ink 2"], "unlink": false})); + run(&mut s, "swatch.delete", json!({"names": ["Tiles"]})); + let r = run(&mut s, "swatch.library.add", json!({"library": id, "names": ["Tiles"]})); + let pattern = match doc(&s).swatch("Tiles").map(|w| w.paint.clone()) { + Some(Paint::Pattern { pattern, .. }) => pattern, + p => panic!("{r} {p:?}"), + }; + let links: Vec> = doc(&s).pattern(&pattern).unwrap().art.iter().map(|n| tint_link(&n.appearance.fill_paint())).collect(); + assert!(!links.contains(&Some("Ink 2".to_string())), "{r} {links:?}"); +} + +/// A library pattern painted with two alike gradients of its blue global "Ink", "Ramp 2" and then +/// "Ramp", added beside the document's red "Ink": the color comes in as "Ink 2", and both +/// gradients come in under their own names, linked to it, instead of "Ramp" taking "Ramp 2". +#[test] +fn alike_gradients_a_pattern_brings_keep_their_names_when_their_color_is_renamed() { + let mut s = session(); + run(&mut s, "swatch.new", json!({"name": "Ink", "color": "#0000ff", "global": true})); + let ramp = json!({"stops": [{"offset": 0, "swatch": "Ink"}, {"offset": 1, "color": "#ffffff"}]}); + run(&mut s, "swatch.new", json!({"name": "Ramp 2", "gradient": ramp})); + run(&mut s, "swatch.new", json!({"name": "Ramp", "gradient": ramp})); + let mut dots = vec![]; + for (i, n) in ["Ramp 2", "Ramp"].iter().enumerate() { + let dot = run(&mut s, "shape.rectangle", json!({"x": i * 5, "y": 0, "width": 4, "height": 4}))["id"].clone(); + run(&mut s, "paint.setFill", json!({"swatch": n, "ids": [dot]})); + run(&mut s, "paint.setStroke", json!({"none": true, "ids": [dot]})); + dots.push(dot); + } + run(&mut s, "select.set", json!({"ids": dots})); + run(&mut s, "object.pattern.make", json!({"name": "Tiles", "width": 12, "height": 6})); + 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 r = run(&mut s, "swatch.library.add", json!({"library": id, "names": ["Tiles"]})); + assert!(is_global(&s, "Ink 2", "#0000ff"), "{r}"); + assert!(doc(&s).swatch("Ramp").is_some() && doc(&s).swatch("Ramp 2").is_some(), "{r}"); +} + +/// A library pattern painted with blue globals "Ink" and "Ink 2", pasted twice beside the +/// document's red "Ink" and blue "Ink 2" (with Add, both fills on "Ink 2"; with Merge, one on the +/// red "Ink"), is found again as the copy that keeps its blue, not the merged one. +#[test] +fn a_pattern_pasted_with_add_and_with_merge_is_found_as_the_copy_with_its_colors() { + 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": "#0000ff", "global": true})); + let mut dots = vec![]; + for (i, n) in ["Ink", "Ink 2"].iter().enumerate() { + let dot = run(&mut s, "shape.rectangle", json!({"x": i * 5, "y": 0, "width": 4, "height": 4}))["id"].clone(); + run(&mut s, "paint.setFill", json!({"swatch": n, "ids": [dot]})); + run(&mut s, "paint.setStroke", json!({"none": true, "ids": [dot]})); + dots.push(dot); + } + run(&mut s, "select.set", json!({"ids": dots})); + run(&mut s, "object.pattern.make", json!({"name": "Tiles", "width": 12, "height": 6})); + 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!({})); + 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.new", json!({"name": "Ink 2", "color": "#0000ff", "global": true})); + run(&mut s, "edit.paste", json!({"swatchConflict": "add"})); + run(&mut s, "edit.paste", json!({"swatchConflict": "merge"})); + let links = |s: &Session, p: &str| -> Vec> { + doc(s).pattern(p).unwrap().art.iter().map(|n| tint_link(&n.appearance.fill_paint())).collect() + }; + let kept: Vec = doc(&s).patterns.iter().map(|p| p.name.clone()).filter(|p| !links(&s, p).contains(&Some("Ink".into()))).collect(); + assert_eq!((doc(&s).patterns.len(), kept.len()), (2, 1), "one pasted copy keeps the blue color"); + for w in doc(&s).swatches.clone().iter().filter(|w| matches!(w.paint, Paint::Pattern { .. })) { + run(&mut s, "swatch.delete", json!({"names": [w.name]})); + } + let r = run(&mut s, "swatch.library.add", json!({"library": id, "names": ["Tiles"]})); + let pattern = match doc(&s).swatch("Tiles").map(|w| w.paint.clone()) { + Some(Paint::Pattern { pattern, .. }) => pattern, + p => panic!("{r} {p:?}"), + }; + assert_eq!((doc(&s).patterns.len(), &pattern), (2, &kept[0]), "{r}"); +} + +/// A library whose color "Tiles 2" shares its name with a pattern swatch in a color group: adding +/// the group gives the pattern swatch another name, so adding the color next adds it under its own. +#[test] +fn a_grouped_pattern_swatch_leaves_a_numbered_name_to_the_librarys_color() { + let mut s = session(); + let dot = run(&mut s, "shape.rectangle", json!({"x": 0, "y": 0, "width": 4, "height": 4}))["id"].clone(); + run(&mut s, "select.set", json!({"ids": [dot]})); + run(&mut s, "object.pattern.make", json!({"name": "Dots", "width": 6, "height": 6})); + run(&mut s, "object.pattern.done", json!({})); + run(&mut s, "swatch.new", json!({"name": "Tiles 2", "color": "#00ff00", "global": true})); + s.edit("Group", |d, _| { + let i = d.swatches.iter().position(|w| w.name == "Dots").unwrap(); + let mut w = d.swatches.remove(i); + w.name = "Tiles 2".into(); + d.swatch_groups.push(vectorcraft_color::SwatchGroup { name: "Group".into(), swatches: vec![w] }); + 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": "Dots.vectorcraft"}))["library"].clone(); + run(&mut s, "file.new", json!({"width": 100, "height": 100})); + let first = run(&mut s, "swatch.library.add", json!({"library": id, "names": ["Group"]})); + let r = run(&mut s, "swatch.library.add", json!({"library": id, "names": ["Tiles 2"]})); + assert_eq!(r["added"], json!(["Tiles 2"]), "{first} {r}"); + assert!(is_global(&s, "Tiles 2", "#00ff00"), "{first} {r}"); +} + +/// As above, with the grouped swatch named after its pattern "Tiles 2", which the document holds +/// (pasted there) without a swatch: the reused pattern's swatch leaves the name to the color too. +#[test] +fn a_grouped_swatch_of_a_reused_pattern_leaves_a_numbered_name_to_the_librarys_color() { + let mut s = session(); + let dot = run(&mut s, "shape.rectangle", json!({"x": 0, "y": 0, "width": 4, "height": 4}))["id"].clone(); + run(&mut s, "select.set", json!({"ids": [dot]})); + run(&mut s, "object.pattern.make", json!({"name": "Tiles 2", "width": 6, "height": 6})); + run(&mut s, "object.pattern.done", json!({})); + 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 2", "ids": [r]})); + run(&mut s, "select.set", json!({"ids": [r]})); + run(&mut s, "edit.copy", json!({})); + run(&mut s, "swatch.new", json!({"name": "Bright", "color": "#00ff00", "global": true})); + s.edit("Group", |d, _| { + let i = d.swatches.iter().position(|w| w.name == "Tiles 2").unwrap(); + let w = d.swatches.remove(i); + d.swatch_groups.push(vectorcraft_color::SwatchGroup { name: "Group".into(), swatches: vec![w] }); + d.swatches.iter_mut().find(|w| w.name == "Bright").unwrap().name = "Tiles 2".into(); + 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": "Tiles.vectorcraft"}))["library"].clone(); + run(&mut s, "file.new", json!({"width": 100, "height": 100})); + run(&mut s, "edit.paste", json!({})); + run(&mut s, "swatch.delete", json!({"names": ["Tiles 2"]})); + assert!(doc(&s).pattern("Tiles 2").is_some() && doc(&s).swatch("Tiles 2").is_none(), "the pasted pattern stays"); + let first = run(&mut s, "swatch.library.add", json!({"library": id, "names": ["Group"]})); + let r = run(&mut s, "swatch.library.add", json!({"library": id, "names": ["Tiles 2"]})); + assert_eq!(r["added"], json!(["Tiles 2"]), "{first} {r}"); + assert!(is_global(&s, "Tiles 2", "#00ff00"), "{first} {r}"); +} + +/// A pattern "Inner 2" that a library pattern's art is painted with, whose library swatch "Inner 2" +/// places it with an offset, leaves the name to that swatch: adding the library's "Inner 2" next +/// adds it under its own name with its placement. Picked first, "Inner 2" comes in under its own +/// name too. +#[test] +fn a_nested_pattern_leaves_its_name_to_a_library_swatch_that_places_it_otherwise() { + for outer_first in [true, false] { + let mut s = session(); + let dot = run(&mut s, "shape.rectangle", json!({"x": 0, "y": 0, "width": 4, "height": 4}))["id"].clone(); + run(&mut s, "select.set", json!({"ids": [dot]})); + run(&mut s, "object.pattern.make", json!({"name": "Inner 2", "width": 6, "height": 6})); + run(&mut s, "object.pattern.done", json!({})); + let tile = run(&mut s, "shape.rectangle", json!({"x": 0, "y": 0, "width": 10, "height": 10}))["id"].clone(); + run(&mut s, "paint.setFill", json!({"swatch": "Inner 2", "ids": [tile]})); + run(&mut s, "select.set", json!({"ids": [tile]})); + run(&mut s, "object.pattern.make", json!({"name": "Outer", "width": 12, "height": 12})); + run(&mut s, "object.pattern.done", json!({})); + let offset = Paint::Pattern { pattern: "Inner 2".into(), xf: kurbo::Affine::translate((3.0, 0.0)) }; + let lib_paint = offset.clone(); + s.edit("Offset", move |d, _| { + d.swatches.iter_mut().find(|w| w.name == "Inner 2").unwrap().paint = lib_paint; + 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": "Outer.vectorcraft"}))["library"].clone(); + run(&mut s, "file.new", json!({"width": 100, "height": 100})); + let picks = if outer_first { ["Outer", "Inner 2"] } else { ["Inner 2", "Outer"] }; + let first = run(&mut s, "swatch.library.add", json!({"library": id, "names": [picks[0]]})); + let r = run(&mut s, "swatch.library.add", json!({"library": id, "names": [picks[1]]})); + assert_eq!(doc(&s).swatch("Inner 2").map(|w| w.paint.clone()), Some(offset), "{outer_first} {first} {r}"); + assert_eq!(doc(&s).patterns.len(), 2, "{outer_first} {first} {r}"); + } +} + +/// A library with a pattern swatch "Tiles 2" and, in a color group, a swatch of the same name that +/// places the same pattern with an offset: adding the group first leaves the name to the +/// ungrouped swatch, which comes in under it next. +#[test] +fn a_grouped_namesake_of_a_pattern_swatch_leaves_it_the_name() { + let mut s = session(); + let dot = run(&mut s, "shape.rectangle", json!({"x": 0, "y": 0, "width": 4, "height": 4}))["id"].clone(); + run(&mut s, "select.set", json!({"ids": [dot]})); + run(&mut s, "object.pattern.make", json!({"name": "Tiles 2", "width": 6, "height": 6})); + run(&mut s, "object.pattern.done", json!({})); + s.edit("Group", |d, _| { + let offset = Paint::Pattern { pattern: "Tiles 2".into(), xf: kurbo::Affine::translate((3.0, 0.0)) }; + let w = vectorcraft_color::Swatch { name: "Tiles 2".into(), paint: offset, global: false, spot: false }; + d.swatch_groups.push(vectorcraft_color::SwatchGroup { name: "Group".into(), swatches: vec![w] }); + 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": "Tiles.vectorcraft"}))["library"].clone(); + run(&mut s, "file.new", json!({"width": 100, "height": 100})); + let first = run(&mut s, "swatch.library.add", json!({"library": id, "names": ["Group"]})); + let r = run(&mut s, "swatch.library.add", json!({"library": id, "names": ["Tiles 2"]})); + let paint = doc(&s).swatch("Tiles 2").map(|w| w.paint.clone()); + assert!(matches!(&paint, Some(Paint::Pattern { xf, .. }) if *xf == kurbo::Affine::IDENTITY), "{first} {r} {paint:?}"); +} + +/// A pattern "Inner 2" that a library pattern's art is painted with, whose swatch the library +/// deleted before giving the name "Inner 2" to a color, comes in under another name, so adding +/// that color next adds it under its own name. +#[test] +fn a_nested_pattern_does_not_take_a_name_the_library_gives_a_color() { + let mut s = session(); + let dot = run(&mut s, "shape.rectangle", json!({"x": 0, "y": 0, "width": 4, "height": 4}))["id"].clone(); + run(&mut s, "select.set", json!({"ids": [dot]})); + run(&mut s, "object.pattern.make", json!({"name": "Inner 2", "width": 6, "height": 6})); + run(&mut s, "object.pattern.done", json!({})); + let tile = run(&mut s, "shape.rectangle", json!({"x": 0, "y": 0, "width": 10, "height": 10}))["id"].clone(); + run(&mut s, "paint.setFill", json!({"swatch": "Inner 2", "ids": [tile]})); + run(&mut s, "select.set", json!({"ids": [tile]})); + run(&mut s, "object.pattern.make", json!({"name": "Outer", "width": 12, "height": 12})); + run(&mut s, "object.pattern.done", json!({})); + run(&mut s, "swatch.delete", json!({"names": ["Inner 2"]})); + assert!(doc(&s).pattern("Inner 2").is_some(), "the library keeps the pattern"); + run(&mut s, "swatch.new", json!({"name": "Inner 2", "color": "#00ff00", "global": true})); + 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": "Outer.vectorcraft"}))["library"].clone(); + run(&mut s, "file.new", json!({"width": 100, "height": 100})); + let first = run(&mut s, "swatch.library.add", json!({"library": id, "names": ["Outer"]})); + let r = run(&mut s, "swatch.library.add", json!({"library": id, "names": ["Inner 2"]})); + assert_eq!(r["added"], json!(["Inner 2"]), "{first} {r}"); + assert!(is_global(&s, "Inner 2", "#00ff00"), "{first} {r}"); +} + +/// A library pattern pasted into the document, whose global color "Ink 2" (in a color group) was +/// then deleted without unlinking the art, is not reused by bringing that color back as "Ink 2" +/// when the library also has a process color "Ink 2": adding that color next adds it under its +/// own name. +#[test] +fn a_pasted_pattern_is_not_reused_by_bringing_back_a_librarys_name() { + let mut s = session(); + run(&mut s, "swatch.new", json!({"name": "Ink 2", "color": "#00ff00"})); + run(&mut s, "swatch.new", json!({"name": "Crimson", "color": "#0000ff", "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": "Crimson", "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!({})); + group_as(&mut s, "Crimson", "Ink 2"); + 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!({})); + run(&mut s, "file.new", json!({"width": 100, "height": 100})); + run(&mut s, "edit.paste", json!({})); + assert!(is_global(&s, "Ink 2", "#0000ff"), "the paste brings the global color as Ink 2"); + run(&mut s, "swatch.delete", json!({"names": ["Ink 2"], "unlink": false})); + run(&mut s, "swatch.delete", json!({"names": ["Tiles"]})); + let first = run(&mut s, "swatch.library.add", json!({"library": id, "names": ["Tiles"]})); + let r = run(&mut s, "swatch.library.add", json!({"library": id, "names": ["Ink 2"]})); + assert_eq!(r["added"], json!(["Ink 2"]), "{first} {r}"); + assert!(doc(&s).swatch("Ink 2").is_some_and(|w| !w.global && !w.spot), "{first} {r}"); +} + +/// Move swatch `name` of `s`'s document into a new color group under the name `to`, and link the +/// art of every pattern linked to it to `to`. +fn group_as(s: &mut Session, name: &str, to: &str) { + s.edit("Group", |d, _| { + let i = d.swatches.iter().position(|w| w.name == name).unwrap(); + let mut w = d.swatches.remove(i); + w.name = to.into(); + d.swatch_groups.push(vectorcraft_color::SwatchGroup { name: "Group".into(), swatches: vec![w] }); + for def in &mut d.patterns { + for node in &mut def.art { + for it in &mut std::sync::Arc::make_mut(node).appearance.items { + it.paint_mut().map_links(&mut |_, link, _| { + let hit = link.as_deref() == Some(name); + if hit { + *link = Some(to.into()); + } + hit + }); + } + } + } + Ok(()) + }) + .unwrap(); +} + +/// The swatch paint `p` links to. +fn tint_link(p: &Paint) -> Option { + match p { + Paint::Solid { swatch, .. } => swatch.clone(), + _ => None, + } +} + +/// A library pattern "Tiles" added beside the document's own swatch "Tiles" does not take the name +/// "Tiles 2" that the library gives a color, neither for the pattern nor for its swatch; nor does +/// its swatch when the document holds the pattern as "Tiles 2" (pasted there) without a swatch. +#[test] +fn a_pattern_swatch_named_after_its_numbered_pattern_skips_the_librarys_names() { + for pasted in [false, true] { + let mut s = session(); + let dot = run(&mut s, "shape.rectangle", json!({"x": 0, "y": 0, "width": 6, "height": 6}))["id"].clone(); + 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!({})); + run(&mut s, "swatch.new", json!({"name": "Tiles 2", "color": "#00ff00", "global": true})); + 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!({})); + run(&mut s, "file.new", json!({"width": 100, "height": 100})); + run(&mut s, "swatch.new", json!({"name": "Tiles", "color": "#ff0000"})); + if pasted { + run(&mut s, "edit.paste", json!({})); + assert!(doc(&s).pattern("Tiles 2").is_some(), "the paste numbers the pattern"); + let names: Vec = doc(&s) + .swatches_iter() + .filter(|w| matches!(&w.paint, Paint::Pattern { pattern, .. } if pattern == "Tiles 2")) + .map(|w| w.name.clone()) + .collect(); + run(&mut s, "swatch.delete", json!({"names": names})); + } + let first = run(&mut s, "swatch.library.add", json!({"library": id, "names": ["Tiles"]})); + let r = run(&mut s, "swatch.library.add", json!({"library": id, "names": ["Tiles 2"]})); + assert_eq!(r["added"], json!(["Tiles 2"]), "{pasted}: {first} {r}"); + assert!(is_global(&s, "Tiles 2", "#00ff00"), "{pasted}: {first} {r}"); + } +} + /// 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] diff --git a/docs/mcp.md b/docs/mcp.md index 1253778638..5fe5561778 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -5833,21 +5833,28 @@ From a document, such as a `.ai` or `.eps` swatch library, it reads the colors, 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 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. That numbered name skips the names the library -gives its other swatches. A library color whose name the document gives -another color is the document's numbered copy of the same color when it has one, so adding the pattern and its color -again adds nothing. A pattern the document already holds with its art linked to the document's own color of that name -is used as it is. A library 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, whether the color is picked before or after it, was added -earlier or came with a pattern's art. A library global color is not the document's process color of the same name and -paint: it comes in numbered. A global or spot color that a library tint or gradient links to and the document lacks -comes in with the tint or gradient, under a numbered name when the document gives its name another swatch. The -document's tint or gradient of the library one's name is that swatch when it links to the copy of the color that the -art of a pattern picked with it takes; a numbered copy that the library also names stands for that library swatch -otherwise. When the document's tint or gradient links to a color the document no longer has (deleted without -unlinking), that color comes back under its own name. A pattern is found again when it was pasted with Add or Merge for all of its colors; one pasted -with Merge for some colors and Add for others is added again as a copy. `added` lists the colors the pattern's art, a -tint or a gradient brings too. A document with +in under a numbered name ("Ink 2") and the pattern links to that. That numbered name, and the numbered names of a +pattern swatch and of a pattern the art is painted with, skip the names the library gives its other swatches. A swatch +of a numbered library name, such as a color or tint "Ink 2", that the pattern's art brings keeps that name when the +document does not use it. Of two library swatches with one numbered name, the first one in the library keeps the name +when the other is a swatch the pattern's art brings or a pattern swatch. A pattern the art is painted with keeps a +numbered name; when the library's swatch of that name places the pattern otherwise, the swatch that comes with the +pattern gets another name. A library color whose name the document gives another +color is the document's numbered copy +of the same color when it has one, so adding the pattern and its color again adds nothing. A pattern the document +already holds with its art linked to the document's own color of that name, or to a numbered copy of the color as +pasting leaves it, is used as it is, unless using it would give another swatch a name the library uses. A library +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, whether the color is picked before or after it, was added earlier or came with a pattern's +art. A library global or spot color is not the document's color of the same name and paint when that color is of +another kind (process, global or spot): it comes in numbered. A global or spot color that a library tint or gradient +links to and the document lacks comes in with the tint or gradient, under a numbered name when the document gives its +name another swatch. The document's tint or gradient of the library one's name is that swatch when it links to the +copy of the color that the art of a pattern picked with it takes; a numbered copy that the library also names stands +for that library swatch otherwise. When the document's tint or gradient links to a color the document no longer has +(deleted without unlinking), that color comes back under its own name. A pattern is found again when it was pasted +with Add or Merge for all of its colors; one pasted with Merge for some colors and Add for others is added again as a +copy. `added` lists the colors the pattern's art, a tint or a gradient brings too. A document with no swatches, and a `.ai`, `.eps` or PostScript file whose swatches are only the default ones, are refused with "`` has no swatches of its own", unless the document has graph designs: it then loads with no swatches. The Swatch Libraries filter of Other Library… lists `.ai`, `.ait` and `.eps` (`.epsf`) files as well as the library files. diff --git a/docs/target-app-parity.md b/docs/target-app-parity.md index 16b1fc7ab3..748b978927 100644 --- a/docs/target-app-parity.md +++ b/docs/target-app-parity.md @@ -2247,7 +2247,7 @@ Moved from the ROADMAP's "Shipped so far" on 2026-10-10. Update it when a featur A symbol instance written as its art, and a blend, mesh, repeat, placed document, intertwine or envelope written as a group of its art, likewise still wraps area type by its bounds when the file is opened, as it did before saving, not by the members of that art: the group starts with the same unpainted rectangle, which takes the object's lock and visibility, and the group keeps the object's name and transparency without options of its own. Area type in that art is above the rectangle, so the rectangle does not wrap it. The rectangle takes its place in the object limit before the art; without room for it, the options are left out with the limit's warning. An object without finite bounds keeps the options on its group. This is VectorCraft's own round-trip rule (`crates/eps/src/ai_editing/tests.rs` `live_art_written_as_a_group_keeps_wrapping_by_its_bounds`, `wrap_bounds_of_live_art_fit_the_object_limit_and_leave_area_type_in_the_art_unwrapped`, `live_art_without_bounds_keeps_its_text_wrap_on_the_group`). In a `.ai` or EPS opened from its editing data, type has its text object's opacity, blend mode and knockout shape flag, whether it is made from the file's text document or taken from the page, and type made from the text document takes its text object's lock. Where such type has transparency and comes in with other art (the rest of its look, or page type and strokes that go with it), a group of them has it. Where the page draws its type, or the strokes around it, with transparency that is neither its text object's nor its text object's combined with that of the objects around it, the page's type and strokes keep their own instead (`crates/eps/src/import/tests_native.rs`). Where the page draws type of no text object over text objects made from the text document, with their words in any case, that type is theirs as the page draws it and is left out; the page's other type of no text object goes only to text objects whose type the text document can't make, or else is left out with a warning. `.ai` editing-data import restores the document's listed graphic styles in panel order and links objects that use a listed style; unsupported appearances keep their drawn look (`crates/eps/src/import/ai/tests.rs`). - Swatches › Open Swatch Library › Other Library… lists `.ai` and `.eps` files in its Swatch Libraries filter and loads another document's swatches ([importing swatches from another document](https://help.adobe.com/archive/en_US/illustrator/cs5/illustrator_cs5_help.pdf#page=134)): its colors, tints, gradients and pattern swatches, in its order and color groups. Adding a pattern swatch from such a library brings the pattern with its tile and art as a pattern swatch, under the library swatch's name and in its color group. A document with no swatches, and a `.ai` or EPS whose swatches are only the default ones, are refused with a message (`cmd::fileio::tests_aiimport::a_swatch_library_in_a_ai_file_lists_and_adds_its_pattern_swatches`, `a_pattern_from_a_ai_library_keeps_a_color_the_library_lacks`, `swatch_library_documents_without_swatches_or_damaged_are_refused`, `cmd::fileio::tests::the_swatch_library_filter_lists_ai_and_eps_files`). As VectorCraft's own rule, a global or spot color whose name the document gives another color, added from a library on its own, with a tint or with a pattern's art, comes in under a numbered name that skips the names of the library's other swatches, and adding it again uses that numbered copy; a library tint or gradient links to it, and brings its color when the document lacks it. Use Library Item adds a library pattern's colors the same way (`tests::swatchlib::adding_a_library_pattern_and_its_color_again_reuses_the_numbered_color`, `a_library_tint_follows_its_color_to_its_numbered_name`, `a_library_tint_added_alone_brings_its_color_beside_a_namesake`, `a_library_tint_picked_before_its_color_follows_it_to_its_numbered_name`, `a_library_gradient_is_not_the_one_a_merged_pattern_brought`, `a_color_a_pattern_brings_skips_the_librarys_numbered_names`, `tests::library::a_pattern_item_keeps_its_global_color_when_the_document_has_that_name_in_another_color`). + Swatches › Open Swatch Library › Other Library… lists `.ai` and `.eps` files in its Swatch Libraries filter and loads another document's swatches ([importing swatches from another document](https://help.adobe.com/archive/en_US/illustrator/cs5/illustrator_cs5_help.pdf#page=134)): its colors, tints, gradients and pattern swatches, in its order and color groups. Adding a pattern swatch from such a library brings the pattern with its tile and art as a pattern swatch, under the library swatch's name and in its color group. A document with no swatches, and a `.ai` or EPS whose swatches are only the default ones, are refused with a message (`cmd::fileio::tests_aiimport::a_swatch_library_in_a_ai_file_lists_and_adds_its_pattern_swatches`, `a_pattern_from_a_ai_library_keeps_a_color_the_library_lacks`, `swatch_library_documents_without_swatches_or_damaged_are_refused`, `cmd::fileio::tests::the_swatch_library_filter_lists_ai_and_eps_files`). As VectorCraft's own rule, a global or spot color whose name the document gives another color, added from a library on its own, with a tint or with a pattern's art, comes in under a numbered name that skips the names of the library's other swatches, and adding it again uses that numbered copy; a library tint or gradient links to it, and brings its color when the document lacks it, or when the document deleted it without unlinking the tint. A swatch of a numbered library name that a pattern's art brings, a tint or gradient included, keeps that name, and a pattern swatch or a pattern the art is painted with that comes in numbered skips the library's other names too. Use Library Item adds a library pattern's colors the same way (`tests::swatchlib::adding_a_library_pattern_and_its_color_again_reuses_the_numbered_color`, `a_library_tint_follows_its_color_to_its_numbered_name`, `a_library_tint_added_alone_brings_its_color_beside_a_namesake`, `a_library_tint_picked_before_its_color_follows_it_to_its_numbered_name`, `a_library_gradient_is_not_the_one_a_merged_pattern_brought`, `a_library_tint_whose_color_the_document_deleted_brings_the_color_back`, `a_color_a_pattern_brings_skips_the_librarys_numbered_names`, `a_pattern_bringing_a_color_and_its_numbered_namesake_keeps_the_librarys_name`, `a_numbered_tint_or_gradient_a_pattern_brings_keeps_the_librarys_name`, `a_numbered_color_a_pattern_brings_keeps_its_name_beside_a_copy_of_it`, `a_numbered_library_pattern_keeps_its_name`, `a_library_pattern_swatchs_copy_skips_the_librarys_other_names`, `a_pattern_swatch_named_after_its_numbered_pattern_skips_the_librarys_names`, `a_nested_pattern_a_library_pattern_brings_skips_the_librarys_names`, `a_nested_pattern_does_not_take_a_name_the_library_gives_a_color`, `a_nested_pattern_leaves_its_name_to_a_library_swatch_that_places_it_otherwise`, `a_pasted_pattern_linked_to_a_copy_of_a_numbered_color_is_found`, `a_new_patterns_swatch_takes_the_library_swatchs_free_name`, `a_numbered_name_is_kept_for_the_library_swatch_it_names`, `a_color_a_pattern_brings_again_does_not_reuse_a_copy_the_library_names`, `a_color_a_pattern_brings_reuses_a_copy_that_is_not_the_librarys_namesake`, `alike_gradients_a_pattern_brings_keep_their_names_when_their_color_is_renamed`, `a_pasted_pattern_is_not_reused_by_giving_a_librarys_name_to_another_color`, `a_pattern_pasted_with_add_and_with_merge_is_found_as_the_copy_with_its_colors`, `a_grouped_pattern_swatch_leaves_a_numbered_name_to_the_librarys_color`, `a_grouped_swatch_of_a_reused_pattern_leaves_a_numbered_name_to_the_librarys_color`, `a_grouped_namesake_of_a_pattern_swatch_leaves_it_the_name`, `a_pasted_pattern_is_not_reused_by_taking_a_librarys_name`, `a_pasted_pattern_is_not_reused_by_bringing_back_a_librarys_name`, `a_library_tint_is_not_linked_to_a_namesake_of_another_kind_a_pattern_brings`, `a_color_a_pattern_brings_leaves_links_to_a_deleted_color_alone`, `tests::library::a_pattern_item_keeps_its_global_color_when_the_document_has_that_name_in_another_color`). A `.ai` or EPS graphic style library, whose editing data holds the styles and their list but no layers, opens with those styles in list order. Other Library… loads another document's graphic styles ([Apply graphic styles](https://helpx.adobe.com/illustrator/desktop/special-effects-styles/apply-graphic-styles/apply-graphic-styles.html)) and lists such a library's styles in a library panel. A `.ai` or EPS read as its PDF part or page because its layers can't be used also gets the styles of its list in place of the default ones (`crates/eps/src/import/tests_native.rs`, `cmd::fileio::tests_aiimport::a_graphic_style_library_in_a_ai_file_without_layers_loads_its_styles`). A `.ai` or EPS symbol library, whose editing data holds symbol definitions and their list but no layers, opens with those symbols in list order, each with its registration point. Other Library… in the Symbols panel's Symbol Libraries Menu imports the symbols of the file you pick ([Create or import symbol libraries](https://helpx.adobe.com/illustrator/desktop/manage-objects/traces-mockups-symbols/create-or-import-symbol-libraries.html)) and lists such a library's symbols in a library panel. A `.ai` or EPS read as its PDF part or page because its layers can't be used also gets the symbols defined before its layers (`crates/eps/src/import/tests_native.rs`, `crates/eps/src/import/ai/tests.rs`, `cmd::fileio::tests_aiimport::a_symbol_library_in_a_ai_file_without_layers_loads_its_symbols`). In a large canvas `.ai` or EPS ([Large canvas FAQ](https://helpx.adobe.com/illustrator/desktop/add-and-import-files/start-a-new-file/large-canvas-faq.html)), or a `.ai` whose page is in larger units, opened from its editing data, VectorCraft reads the symbol definitions in the same units as the layers. When the file opens at its full size, each symbol's art, strokes and effects are scaled with the layers, and each instance keeps its own scale and rotation and draws where and as large as before. A new instance comes in at the symbol's full size, the size of an instance at 100% in the file. A symbol library from such a file comes in at the same size, and opacity masks on the layers that don't move with their object are scaled with the layers too. Editing data that VectorCraft wrote keeps its symbols and those masks at their own size, as its `.ai` save writes them (`tests_native::a_large_canvas_eps_keeps_its_symbols_at_the_pages_size`, `a_large_canvas_ai_keeps_its_symbols_at_the_pages_size`, `a_large_canvas_ai_vectorcraft_wrote_keeps_its_symbols_at_their_own_size`, `a_symbol_library_in_larger_units_comes_in_at_the_pages_size`, `build::candidate_tests::scaling_the_symbols_keeps_their_instances_in_place`, `scaling_a_read_document_scales_unlinked_opacity_masks`).