mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-26 08:26:04 +00:00
Swatch libraries: library tints and gradients link to their colors in any order
A library tint or gradient whose global or spot color comes in under a numbered name now links to that copy whichever order the swatches are picked in, when the color was added by an earlier call, and when a pattern's art brought the color or took the document's copy of it. Adding the same tints again finds the ones added before. - Each tint's or gradient's color is looked up for itself before anything is added, past a process swatch of the same paint and past a color group of that name. A numbered copy that the library's own swatch of that name is stays that swatch's, and a new copy skips the library's other names. - A tint links to a global or spot color of the same kind only, and to a color this call picks rather than to a copy a pattern took. - Links are set as soon as the color's name is known. A tint waiting for a color that a pattern's art brings learns its name from an import into a copy, so the pattern does not add a second tint. - A swatch a pattern brought takes a picked swatch's place only when its colors link where the picked one's do. - A stop linked to the Registration swatch counts as linked. - library.use, Add to Swatches and the reuse check share one import probe (patternlib::probe_import), and the probed copy is kept instead of importing a second time. Docs: docs/mcp.md, docs/target-app-parity.md.
This commit is contained in:
@@ -623,9 +623,8 @@ impl SwatchChoices {
|
||||
Some(v) => Ok(Self { all: one(v)?, by_name: BTreeMap::new() }),
|
||||
}
|
||||
}
|
||||
/// Add for every conflict, as a library pattern is added to a document: each global or spot
|
||||
/// color of its art whose name the document gives another color comes in under a numbered
|
||||
/// name, or as the document's numbered copy of that name with the same color.
|
||||
/// 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() }
|
||||
}
|
||||
|
||||
@@ -20,7 +20,6 @@ use serde_json::{Map, Value, json};
|
||||
use vectorcraft_color::{Color, GradientPaint, Paint};
|
||||
use vectorcraft_doc::{Appearance, Node, NodeId, TextStyleDef};
|
||||
|
||||
use super::clipboard::SwatchChoices;
|
||||
use super::{CommandSpec, EngineError, Result, Session, always, bad, cmd, color_value, has_doc, ids_param, index_opt, ok, point_param, str_param};
|
||||
use crate::Clipboard;
|
||||
|
||||
@@ -1353,32 +1352,13 @@ fn use_pattern(s: &mut Session, data: &Value, stroke: bool) -> Result<String> {
|
||||
if clip.nodes.is_empty() {
|
||||
return Err(damaged());
|
||||
}
|
||||
// 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"), as
|
||||
// Add to Swatches brings a library pattern's colors.
|
||||
let mut choices = SwatchChoices::add_all();
|
||||
// The pattern's name in the document, worked out on a copy: nothing to add is no edit.
|
||||
// The pattern's name in the document, worked out on a copy: nothing to add is no edit. A global
|
||||
// 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 mut probe = (*current).clone();
|
||||
let mut imported = clip.import_into(&mut probe, &choices, false)?;
|
||||
let pattern_of = |i: &super::clipboard::resources::Imported| {
|
||||
i.renamed.iter().find(|r| r["kind"] == "pattern" && r["from"] == name.as_str()).and_then(|r| r["to"].as_str()).unwrap_or(&name).to_string()
|
||||
};
|
||||
// A pattern used before a color of its art kept the library's color has its art linked to the
|
||||
// document's own color of that name: when merging those colors reuses a pattern of the document
|
||||
// that keeping them would add again, they merge.
|
||||
if current.pattern(&pattern_of(&imported)).is_none()
|
||||
&& imported.renamed.iter().any(|r| r["kind"] == "swatch")
|
||||
&& super::patternlib::has_candidate(¤t, &doc, &name)
|
||||
{
|
||||
let mut merged = (*current).clone();
|
||||
let found = clip.import_into(&mut merged, &SwatchChoices::default(), false)?;
|
||||
if current.pattern(&pattern_of(&found)).is_some() {
|
||||
(choices, probe, imported) = (SwatchChoices::default(), merged, found);
|
||||
}
|
||||
}
|
||||
let (choices, probe, imported) = super::patternlib::probe_import(¤t, &doc, &name, &clip)?;
|
||||
drop(current);
|
||||
let target = pattern_of(&imported);
|
||||
let target = super::patternlib::imported_name(&imported, &name);
|
||||
let paint = vectorcraft_doc::pattern::pattern_paint(&target);
|
||||
// A pattern the document holds without a swatch (an imported SVG's) gets one.
|
||||
let has_swatch =
|
||||
|
||||
@@ -85,22 +85,29 @@ pub fn swatch_source(w: &Swatch) -> Option<Arc<Document>> {
|
||||
library_doc(b.id).filter(|d| d.pattern(pattern).is_some())
|
||||
}
|
||||
|
||||
/// Swatches as (the swatch's name in a library, its name in a document).
|
||||
pub(crate) type SwatchNames = Vec<(String, String)>;
|
||||
|
||||
/// Add pattern `name` of library `lib` to `d` with the resources its art uses, as pasting art
|
||||
/// painted with it does: the same pattern there already is used, and a name another pattern or
|
||||
/// swatch has gets a number → the pattern's name in `d`, and the swatches the import added for
|
||||
/// the pattern's art: (the swatch's name in `lib`, its name in `d`), pattern swatches left out.
|
||||
/// the pattern's art: (the swatch's name in `lib`, its name in `d`), pattern swatches left out,
|
||||
/// and the swatches `d` had that the art took under another name than its own, in the same form.
|
||||
/// 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
|
||||
/// ([`merge_reuses`]).
|
||||
pub(crate) fn import(d: &mut Document, lib: &Document, name: &str) -> Result<(String, Vec<(String, String)>)> {
|
||||
/// ([`probe_import`]).
|
||||
pub(crate) fn import(d: &mut Document, lib: &Document, name: &str) -> Result<(String, SwatchNames, SwatchNames)> {
|
||||
let gathered = gather(lib, name);
|
||||
let merge = !gathered.swatches.is_empty()
|
||||
&& has_candidate(d, lib, name)
|
||||
&& probe(d, &gathered, &SwatchChoices::add_all()).and_then(|with_add| merge_reuses(d, &gathered, name, &with_add)).is_some();
|
||||
let choices = if merge { SwatchChoices::default() } else { SwatchChoices::add_all() };
|
||||
let had: std::collections::HashSet<String> = d.swatches_iter().map(|w| w.name.clone()).collect();
|
||||
let imported = gathered.import_into(d, &choices, false)?;
|
||||
// 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)?;
|
||||
*d = probed;
|
||||
imported
|
||||
} else {
|
||||
gathered.import_into(d, &SwatchChoices::add_all(), false)?
|
||||
};
|
||||
let from = |to: &str| {
|
||||
let renamed = imported.renamed.iter().find(|r| r["kind"] == "swatch" && r["to"] == to).and_then(|r| r["from"].as_str());
|
||||
renamed.unwrap_or(to).to_string()
|
||||
@@ -110,7 +117,14 @@ pub(crate) fn import(d: &mut Document, lib: &Document, name: &str) -> Result<(St
|
||||
.filter(|w| !had.contains(&w.name) && !matches!(w.paint, Paint::Pattern { .. }))
|
||||
.map(|w| (from(&w.name), w.name.clone()))
|
||||
.collect();
|
||||
Ok((imported_name(&imported, name), added))
|
||||
let reused = imported
|
||||
.renamed
|
||||
.iter()
|
||||
.filter(|r| r["kind"] == "swatch")
|
||||
.filter_map(|r| Some((r["from"].as_str()?.to_string(), r["to"].as_str()?.to_string())))
|
||||
.filter(|(_, to)| had.contains(to))
|
||||
.collect();
|
||||
Ok((imported_name(&imported, name), added, reused))
|
||||
}
|
||||
|
||||
/// Pattern `name` of `lib` with the resources its art uses, as an object painted with it is copied.
|
||||
@@ -119,8 +133,13 @@ fn gather(lib: &Document, name: &str) -> Clipboard {
|
||||
Clipboard::gather(lib, vec![user])
|
||||
}
|
||||
|
||||
/// The names in `lib` of the swatches that pattern `name` of `lib` brings with its art.
|
||||
pub(crate) fn brought_names(lib: &Document, name: &str) -> std::collections::HashSet<String> {
|
||||
gather(lib, name).swatches.into_iter().map(|w| w.name).collect()
|
||||
}
|
||||
|
||||
/// The name pattern `name` has in the document `imported` went into.
|
||||
fn imported_name(imported: &Imported, name: &str) -> String {
|
||||
pub(crate) fn imported_name(imported: &Imported, name: &str) -> String {
|
||||
let renamed = imported.renamed.iter().find(|r| r["kind"] == "pattern" && r["from"] == name).and_then(|r| r["to"].as_str());
|
||||
renamed.unwrap_or(name).to_string()
|
||||
}
|
||||
@@ -128,8 +147,8 @@ fn imported_name(imported: &Imported, name: &str) -> String {
|
||||
/// The pattern of `d` that [`import`] of pattern `name` of library `lib` would use without adding
|
||||
/// anything to `d` → its name in `d`. 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`, so that those resources are compared too, with the colors
|
||||
/// of its art merged when [`merge_reuses`] says so.
|
||||
/// 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> {
|
||||
let gathered = gather(lib, name);
|
||||
let only_pattern = gathered.patterns.len() == 1
|
||||
@@ -148,11 +167,8 @@ pub(crate) fn present_pattern(d: &Document, lib: &Document, name: &str) -> Optio
|
||||
if !has_candidate(d, lib, name) {
|
||||
return None;
|
||||
}
|
||||
let with_add = probe(d, &gathered, &SwatchChoices::add_all())?;
|
||||
if with_add.added == 0 {
|
||||
return Some(imported_name(&with_add, name));
|
||||
}
|
||||
merge_reuses(d, &gathered, name, &with_add).filter(|m| m.added == 0).map(|m| imported_name(&m, name))
|
||||
let (_, _, imported) = probe_import(d, lib, name, &gathered).ok()?;
|
||||
(imported.added == 0).then(|| imported_name(&imported, name))
|
||||
}
|
||||
|
||||
/// Whether `d` has a pattern that importing pattern `name` of `lib` can reuse: one under that name
|
||||
@@ -166,22 +182,25 @@ pub(crate) fn has_candidate(d: &Document, lib: &Document, name: &str) -> bool {
|
||||
d.patterns.iter().any(|p| copy(&p.name) && p.art.len() == def.art.len() && bare(p) == bare(def))
|
||||
}
|
||||
|
||||
/// What importing `gathered` into a copy of `d` with `choices` does.
|
||||
fn probe(d: &Document, gathered: &Clipboard, choices: &SwatchChoices) -> Option<Imported> {
|
||||
let mut probe = d.clone();
|
||||
gathered.import_into(&mut probe, choices, false).ok()
|
||||
}
|
||||
|
||||
/// When importing `gathered` (pattern `name`) into `d` with Add (`with_add`) adds the pattern again
|
||||
/// because a global or spot color of its art comes in numbered, while merging those colors reuses a
|
||||
/// pattern of `d`, what merging does. 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.
|
||||
fn merge_reuses(d: &Document, gathered: &Clipboard, name: &str, with_add: &Imported) -> Option<Imported> {
|
||||
/// Import `gathered`, which paints with pattern `name` of `lib`, into a copy of `d` → the swatch
|
||||
/// choices used, the copy and what the import did. A global or spot color whose name `d` gives
|
||||
/// 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)> {
|
||||
let reused = |i: &Imported| d.pattern(&imported_name(i, name)).is_some();
|
||||
if reused(with_add) || !with_add.renamed.iter().any(|r| r["kind"] == "swatch") {
|
||||
return None;
|
||||
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();
|
||||
if let Ok(found) = gathered.import_into(&mut merged, &SwatchChoices::default(), false)
|
||||
&& reused(&found)
|
||||
{
|
||||
return Ok((SwatchChoices::default(), merged, found));
|
||||
}
|
||||
}
|
||||
probe(d, gathered, &SwatchChoices::default()).filter(reused)
|
||||
Ok((SwatchChoices::add_all(), with_add, added))
|
||||
}
|
||||
|
||||
/// The pattern of `d` that is pattern `name` of library `lib`: the same tile options and art under
|
||||
|
||||
@@ -49,7 +49,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Add to Swatches",
|
||||
["Window", "Swatch Libraries"],
|
||||
None,
|
||||
"{library: id or name, names?: [swatch or colour group names in the library] (default: all of it; a group comes as a colour group, a single swatch ungrouped), apply?: \"fill\"|\"stroke\" (also apply the first one to the selection and the defaults, as paint.setFill / paint.setStroke), focus?: true (with apply: false keeps the active proxy)} copy library swatches into the document as one undo step. A swatch the document already has (same name and paint, 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 pattern swatch brings its pattern, as pasting art painted with it does: the same pattern the document has is used, also when its art links to the document's own color of a name, and a name another pattern or swatch has gets a number; a global or spot color its art links to whose name the document gives another color comes in numbered, and the pattern links to it → {library, added: [names in the document, the colors patterns 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, 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 links to its color under the name that color has in the document. A pattern swatch brings its pattern, as pasting art painted with it does: the same pattern the document has is used, also when its art links to the document's own color of a name, and a name another pattern or swatch has gets a number; a global or spot color its art links to whose name the document gives another color comes in numbered, and the pattern links to it → {library, added: [names in the document, the colors patterns brought included], existing: [names already there], applied?: name}",
|
||||
has_doc,
|
||||
add
|
||||
),
|
||||
@@ -1011,6 +1011,89 @@ fn free_in(taken: &mut HashSet<String>, base: &str) -> String {
|
||||
name
|
||||
}
|
||||
|
||||
/// [`free_in`] for a library swatch's copy: a numbered name also skips the names `reserved` says
|
||||
/// the library's other swatches have, so that a later add finds the copy as this swatch's.
|
||||
fn free_copy_in(taken: &mut HashSet<String>, base: &str, reserved: impl Fn(&str) -> bool) -> String {
|
||||
let name = if taken.contains(base) {
|
||||
(2..).map(|i| format!("{base} {i}")).find(|n| !taken.contains(n) && !reserved(n)).unwrap_or_else(|| base.to_string())
|
||||
} else {
|
||||
base.to_string()
|
||||
};
|
||||
taken.insert(name.clone());
|
||||
name
|
||||
}
|
||||
|
||||
/// Link the swatches of `d` that `pending` names (document name, library swatch) to their colors
|
||||
/// under the names `links` gives them, for each whose colors `links` names now, in one pass over
|
||||
/// the document's swatches.
|
||||
fn settle(d: &mut vectorcraft_doc::Document, pending: &mut Vec<(String, &Swatch)>, links: &std::collections::HashMap<String, String>) {
|
||||
let mut done: std::collections::HashMap<String, Paint> = std::collections::HashMap::new();
|
||||
pending.retain(|(name, w)| {
|
||||
let all = all_linked(&w.paint, links);
|
||||
if all {
|
||||
done.insert(name.clone(), relinked(&w.paint, links));
|
||||
}
|
||||
!all
|
||||
});
|
||||
if !done.is_empty() {
|
||||
for x in d.swatches_iter_mut() {
|
||||
if let Some(paint) = done.get(&x.name) {
|
||||
x.paint = paint.clone();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether paint `a` shows what paint `b` shows: the same color, or the same gradient with the
|
||||
/// colors its stops show, whichever swatches those colors link to.
|
||||
fn shows(a: &Paint, b: &Paint) -> bool {
|
||||
let unlinked = |p: &Paint| match p {
|
||||
Paint::Gradient(g) => {
|
||||
let mut g = g.clone();
|
||||
g.swatch = None;
|
||||
g.gradient.stops.iter_mut().for_each(|s| s.swatch = None);
|
||||
Paint::Gradient(g)
|
||||
}
|
||||
other => other.clone(),
|
||||
};
|
||||
match (a.color(), b.color()) {
|
||||
(Some(x), Some(y)) => x == y,
|
||||
_ => unlinked(a) == unlinked(b),
|
||||
}
|
||||
}
|
||||
|
||||
/// The names of the swatches the colors of paint `p` link to: a tint's base color, or the colors
|
||||
/// of a gradient's stops.
|
||||
fn linked_names(p: &Paint) -> Vec<&str> {
|
||||
match p {
|
||||
Paint::Solid { swatch: Some(b), .. } => vec![b.as_str()],
|
||||
Paint::Gradient(g) => g.gradient.stops.iter().filter_map(|s| s.swatch.as_deref()).collect(),
|
||||
_ => vec![],
|
||||
}
|
||||
}
|
||||
|
||||
/// Paint `p` with each link to a swatch that `links` names changed to the name it maps to.
|
||||
fn relinked(p: &Paint, links: &std::collections::HashMap<String, String>) -> Paint {
|
||||
let mut out = p.clone();
|
||||
let swap = |name: &mut String| {
|
||||
if let Some(to) = links.get(name.as_str()) {
|
||||
*name = to.clone();
|
||||
}
|
||||
};
|
||||
match &mut out {
|
||||
Paint::Solid { swatch: Some(b), .. } => swap(b),
|
||||
Paint::Gradient(g) => g.gradient.stops.iter_mut().filter_map(|s| s.swatch.as_mut()).for_each(swap),
|
||||
_ => {}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// Whether `links` names every swatch the colors of paint `p` link to, the built-in Registration
|
||||
/// swatch aside (every document has it under its own name).
|
||||
fn all_linked(p: &Paint, links: &std::collections::HashMap<String, String>) -> bool {
|
||||
linked_names(p).iter().all(|n| *n == REGISTRATION || links.contains_key(*n))
|
||||
}
|
||||
|
||||
fn add(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
const C: &str = "swatch.library.add";
|
||||
let (info, lib) = library_param(s, p, C)?;
|
||||
@@ -1042,42 +1125,69 @@ fn add(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
copies.entry(base).or_default().push(w);
|
||||
}
|
||||
}
|
||||
// A numbered copy whose name a picked library swatch has stands for that swatch alone.
|
||||
let picked_names: HashSet<&str> = picked.iter().map(|(_, w)| w.name.as_str()).collect();
|
||||
// `paint` is `w`'s paint with a tint's base color under the name the base has in the document.
|
||||
let there = |w: &Swatch, paint: &Paint| match (&w.paint, &patterns) {
|
||||
// The library's swatches by name (the first of a name wins), looked up once.
|
||||
let mut lib_by_name: std::collections::HashMap<&str, &Swatch> = std::collections::HashMap::new();
|
||||
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);
|
||||
}
|
||||
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
|
||||
// numbered copy whose name the library gives a swatch of the same paint stands for that swatch
|
||||
// alone, as adding that swatch finds it under its own name. Paints are compared with their
|
||||
// colors linked as `links` says ([`relinked`]).
|
||||
let same_kind = |x: &Swatch, w: &Swatch| x.global == w.global && x.spot == w.spot;
|
||||
let numbered = |w: &Swatch, paint: &Paint, links: &std::collections::HashMap<String, String>| {
|
||||
let c = copies.get(w.name.as_str())?;
|
||||
let own = |x: &Swatch| lib_by_name.get(x.name.as_str()).is_some_and(|l| relinked(&l.paint, links) == x.paint);
|
||||
let found = c.iter().find(|x| !own(x) && x.paint == *paint && same_kind(x, w));
|
||||
found.map(|x| x.name.clone())
|
||||
};
|
||||
let unlinked = std::collections::HashMap::new();
|
||||
// `paint` is `w`'s paint with the colors it links to as `links` has them in the document.
|
||||
let there = |w: &Swatch, paint: &Paint, links: &std::collections::HashMap<String, String>| match (&w.paint, &patterns) {
|
||||
(Paint::Pattern { pattern, .. }, Some(src)) => {
|
||||
patternlib::present_pattern(d, src, pattern).and_then(|target| library_pattern_swatch(d, w, &target))
|
||||
}
|
||||
_ => match by_name.get(w.name.as_str()) {
|
||||
Some(x) if x.paint == *paint => Some(w.name.clone()),
|
||||
// A color whose name the document gives another color is the numbered copy of it that
|
||||
// an earlier add or a pattern's art brought in, when that copy is the same color.
|
||||
Some(_) => copies
|
||||
.get(w.name.as_str())
|
||||
.and_then(|c| {
|
||||
c.iter().find(|x| !picked_names.contains(x.name.as_str()) && x.paint == *paint && x.global == w.global && x.spot == w.spot)
|
||||
})
|
||||
.map(|x| x.name.clone()),
|
||||
Some(_) => numbered(w, paint, links),
|
||||
None if group_names.contains(w.name.as_str()) => numbered(w, paint, links),
|
||||
None => (w.name == REGISTRATION && d.swatch(&w.name).is_some_and(|x| x.paint == *paint)).then(|| w.name.clone()),
|
||||
},
|
||||
};
|
||||
let picked_names: HashSet<&str> = picked.iter().map(|(_, w)| w.name.as_str()).collect();
|
||||
// The global or spot color of the document that library color `b` is: its own name, or the
|
||||
// numbered copy of it that adding `b` would find. While no color or color group has its name,
|
||||
// a numbered copy is looked for only when this call does not add `b` under its own name.
|
||||
let base_in_doc = |b: &Swatch| match by_name.get(b.name.as_str()) {
|
||||
Some(x) if x.paint == b.paint && same_kind(x, b) => Some(b.name.clone()),
|
||||
Some(_) => numbered(b, &b.paint, &unlinked),
|
||||
None if group_names.contains(b.name.as_str()) || !picked_names.contains(b.name.as_str()) => numbered(b, &b.paint, &unlinked),
|
||||
None => None,
|
||||
};
|
||||
// The library's global and spot colors that the picked tints and gradients link to, with the
|
||||
// name each has in the document: library name → document name. Each is looked up for itself,
|
||||
// whether or not this call picks it, once.
|
||||
let mut base_names: std::collections::HashMap<String, String> = std::collections::HashMap::new();
|
||||
let mut looked_up: HashSet<&str> = HashSet::new();
|
||||
for (_, w) in &picked {
|
||||
for base in linked_names(&w.paint) {
|
||||
if !looked_up.insert(base) {
|
||||
continue;
|
||||
}
|
||||
if let Some(n) = lib_by_name.get(base).filter(|b| b.global || b.spot).and_then(|b| base_in_doc(b)) {
|
||||
base_names.insert(base.to_string(), n);
|
||||
}
|
||||
}
|
||||
}
|
||||
// Library name → the name of the document's swatch.
|
||||
let mut existing: Vec<(String, String)> = vec![];
|
||||
let mut fresh = vec![];
|
||||
for &(group, w) in &picked {
|
||||
// A tint of a color the document has under a numbered name is compared linked to that name.
|
||||
let relinked = match &w.paint {
|
||||
Paint::Solid { swatch: Some(base), .. } => existing.iter().find(|(l, n)| l == base && n != base).map(|(_, n)| {
|
||||
let mut paint = w.paint.clone();
|
||||
if let Paint::Solid { swatch: Some(b), .. } = &mut paint {
|
||||
*b = n.clone();
|
||||
}
|
||||
paint
|
||||
}),
|
||||
_ => None,
|
||||
};
|
||||
match there(w, relinked.as_ref().unwrap_or(&w.paint)) {
|
||||
// A tint or gradient is compared with its colors linked to the names they have here.
|
||||
let paint = relinked(&w.paint, &base_names);
|
||||
match there(w, &paint, &base_names) {
|
||||
Some(name) => existing.push((w.name.clone(), name)),
|
||||
None => fresh.push((group, w)),
|
||||
}
|
||||
@@ -1087,8 +1197,14 @@ fn add(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
// library swatch of this call stands for are added too; they are listed after the others and
|
||||
// are never the one `apply` paints with.
|
||||
let mut brought_alone: Vec<(String, String)> = vec![];
|
||||
// Library name → the name of the document's swatch for it, for the tints added here.
|
||||
let mut links: std::collections::HashMap<String, String> = existing.iter().cloned().collect();
|
||||
// Library name → the name of the document's global or spot color for it, which the tints and
|
||||
// gradients added here link to: one that is global or spot as the library color is.
|
||||
let base_of = |l: &str, n: &str| {
|
||||
let (Some(w), Some(x)) = (lib_by_name.get(l), by_name.get(n)) else { return false };
|
||||
(x.global || x.spot) && same_kind(x, w)
|
||||
};
|
||||
let mut links: std::collections::HashMap<String, String> = existing.iter().filter(|(l, n)| base_of(l, n)).cloned().collect();
|
||||
links.extend(base_names);
|
||||
let added: Vec<(String, String)> = if fresh.is_empty() {
|
||||
vec![]
|
||||
} else {
|
||||
@@ -1104,13 +1220,40 @@ fn add(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
// The swatches the patterns imported in this call brought with their art: (library
|
||||
// name, document name).
|
||||
let mut brought: Vec<(String, String)> = vec![];
|
||||
// The swatches added here with a link `links` did not name yet: (document name, library
|
||||
// swatch).
|
||||
let mut pending: Vec<(String, &Swatch)> = vec![];
|
||||
for (group, w) in fresh {
|
||||
let (name, sw) = if let (Paint::Pattern { pattern, .. }, Some(src)) = (&w.paint, &patterns) {
|
||||
// The pattern comes with its swatch; a pattern the document has without one
|
||||
// gets one. The swatch a pattern that comes in brings is named after the pattern:
|
||||
// it takes the library swatch's name and color group instead.
|
||||
// The tints and gradients added so far link to their colors first, so that the
|
||||
// pattern's art finds them. A color they wait for that the pattern's art brings
|
||||
// is named by importing the pattern into a copy first.
|
||||
let brings = if pending.is_empty() { Default::default() } else { patternlib::brought_names(src, pattern) };
|
||||
let waited = |n: &&str| !links.contains_key(*n) && brings.contains(*n);
|
||||
if pending.iter().any(|(_, w)| linked_names(&w.paint).iter().any(waited)) {
|
||||
let mut probe = d.clone();
|
||||
let (_, colors, took) = patternlib::import(&mut probe, src, pattern)?;
|
||||
for (from, to) in colors.iter().chain(took.iter().filter(|(from, _)| !picked_names.contains(from.as_str()))) {
|
||||
if probe.swatch(to).is_some_and(|x| x.global || x.spot) {
|
||||
links.entry(from.clone()).or_insert_with(|| to.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
settle(d, &mut pending, &links);
|
||||
let had = taken_names(d);
|
||||
let (target, colors) = patternlib::import(d, src, pattern)?;
|
||||
let (target, colors, took) = patternlib::import(d, src, pattern)?;
|
||||
// A global or spot color the pattern brought, or took from the document under
|
||||
// a numbered name, is the one the library's tints of it link to, unless the
|
||||
// document already has one for it. A color this call picks is added under
|
||||
// its own name instead of the one the pattern took.
|
||||
for (from, to) in colors.iter().chain(took.iter().filter(|(from, _)| !picked_names.contains(from.as_str()))) {
|
||||
if d.swatch(to).is_some_and(|x| x.global || x.spot) {
|
||||
links.entry(from.clone()).or_insert_with(|| to.clone());
|
||||
}
|
||||
}
|
||||
brought.extend(colors);
|
||||
if let Some(name) = library_pattern_swatch(d, w, &target).filter(|n| had.contains(n)) {
|
||||
reused.push((w.name.clone(), name));
|
||||
@@ -1137,9 +1280,20 @@ fn add(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
} else {
|
||||
// A color a pattern added earlier in this call brought with its art is that
|
||||
// swatch when it shows the library swatch's color: it moves to the library
|
||||
// swatch's place.
|
||||
let same =
|
||||
|to: &str| d.swatch(to).is_some_and(|x| x.paint.color() == w.paint.color() && x.global == w.global && x.spot == w.spot);
|
||||
// swatch's place, when its colors link where the library swatch's do: to the
|
||||
// names `links` gives them, and not elsewhere for a color this call has yet to
|
||||
// add.
|
||||
let fits = |x: &Paint| {
|
||||
let (ours, theirs) = (linked_names(&w.paint), linked_names(x));
|
||||
ours.len() == theirs.len()
|
||||
&& ours.iter().zip(&theirs).all(|(l, t)| match links.get(*l) {
|
||||
Some(to) => to == t,
|
||||
None => !picked_names.contains(l),
|
||||
})
|
||||
};
|
||||
let same = |to: &str| {
|
||||
d.swatch(to).is_some_and(|x| shows(&x.paint, &w.paint) && x.global == w.global && x.spot == w.spot && fits(&x.paint))
|
||||
};
|
||||
let at = brought.iter().position(|(from, to)| *from == w.name && same(to));
|
||||
let moved = at.map(|i| brought.swap_remove(i).1).and_then(|n| {
|
||||
let i = d.swatches.iter().position(|x| x.name == n)?;
|
||||
@@ -1148,20 +1302,23 @@ fn add(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
match moved {
|
||||
Some(sw) => (sw.name.clone(), sw),
|
||||
None => {
|
||||
let name = free_in(&mut taken, &w.name);
|
||||
let mut sw = Swatch { name: name.clone(), ..(*w).clone() };
|
||||
// A tint links to its base color under the name the base has in the
|
||||
// document.
|
||||
if let Paint::Solid { swatch: Some(base), .. } = &mut sw.paint
|
||||
&& let Some(to) = links.get(base.as_str())
|
||||
{
|
||||
*base = to.clone();
|
||||
// A numbered copy skips the names of the library's other swatches,
|
||||
// which a later add looks up as theirs.
|
||||
let name = free_copy_in(&mut taken, &w.name, |n| lib_by_name.contains_key(n));
|
||||
// A tint or gradient links to its colors under the names they have in
|
||||
// the document; a color that comes in later in this call is linked
|
||||
// when it has come in.
|
||||
let paint = relinked(&w.paint, &links);
|
||||
if !all_linked(&w.paint, &links) {
|
||||
pending.push((name.clone(), w));
|
||||
}
|
||||
(name, sw)
|
||||
(name.clone(), Swatch { name, paint, ..(*w).clone() })
|
||||
}
|
||||
}
|
||||
};
|
||||
links.insert(w.name.clone(), name.clone());
|
||||
if sw.global || sw.spot {
|
||||
links.insert(w.name.clone(), name.clone());
|
||||
}
|
||||
match group {
|
||||
Some(g) => {
|
||||
let found = placed.iter().find(|(n, _)| *n == g).map(|&(_, i)| i);
|
||||
@@ -1182,6 +1339,14 @@ fn add(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
}
|
||||
out.push((w.name.clone(), name));
|
||||
}
|
||||
let later: std::collections::HashMap<String, Paint> = pending.into_iter().map(|(name, w)| (name, relinked(&w.paint, &links))).collect();
|
||||
if !later.is_empty() {
|
||||
for x in d.swatches_iter_mut() {
|
||||
if let Some(paint) = later.get(&x.name) {
|
||||
x.paint = paint.clone();
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok((out, reused, brought))
|
||||
})?;
|
||||
existing.extend(reused);
|
||||
|
||||
Reference in New Issue
Block a user