mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-26 20:46:22 +01:00
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.
This commit is contained in:
1 parent
1cf0a2806a
commit
f6bbab4b6e
7 files changed
+981
-110
No files matched your search
@@ -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<String, SwatchChoice>,
|
||||
/// 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<BTreeMap<String, Swatch>>,
|
||||
}
|
||||
|
||||
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::<Result<_>>()? })
|
||||
}
|
||||
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::<Result<_>>()?,
|
||||
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<BTreeMap<String, Swatch>>) -> 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<String>,
|
||||
/// The global and spot colors that took the document's color (merged).
|
||||
merged_swatches: BTreeSet<String>,
|
||||
/// 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<String> =
|
||||
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<String, Swatch>,
|
||||
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<Placed>| 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)
|
||||
}
|
||||
@@ -1356,7 +1356,7 @@ fn use_pattern(s: &mut Session, data: &Value, stroke: bool) -> Result<String> {
|
||||
// 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);
|
||||
|
||||
@@ -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<BTreeMap<String, Swatch>>;
|
||||
|
||||
/// 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<String> = 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<String> {
|
||||
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<String> {
|
||||
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
|
||||
|
||||
@@ -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<CommandSpec> {
|
||||
"Add to Swatches",
|
||||
["Window", "Swatch Libraries"],
|
||||
None,
|
||||
"{library: id or name, names?: [swatch or colour group names in the library] (default: all of it; a group comes as a colour group, a single swatch ungrouped), apply?: \"fill\"|\"stroke\" (also apply the first one to the selection and the defaults, as paint.setFill / paint.setStroke), focus?: true (with apply: false keeps the active proxy)} copy library swatches into the document as one undo step. A swatch the document already has (same name and paint, and for a color the same process, global or spot kind, or a numbered copy of that name with the same color when the name has another color there) isn't added again; a name taken by another swatch gets a number (\"Clay 1 2\"); a tint or gradient whose global or spot color comes in under a numbered name, or is in the document as such a numbered copy, links to that copy; a global or spot color it links to that the document lacks comes in with it. A pattern swatch brings its pattern, as pasting art painted with it does: the same pattern the document has is used, also when its art links to the document's own color of a name, and a name another pattern or swatch has gets a number; a global or spot color its art links to whose name the document gives another color comes in numbered, 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<String>, 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<Swatch>, Vec<Swatch>) = (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<String>, taken: &mut HashSet<String>, 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<String> = 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<Value> {
|
||||
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<String> = 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<Value> {
|
||||
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<Value> {
|
||||
let mut learned = vec![];
|
||||
if pending.iter().any(|(_, w)| linked_names(&w.paint).iter().any(waited)) {
|
||||
let mut probe = d.clone();
|
||||
let (_, colors, took) = 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<Value> {
|
||||
}
|
||||
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<Value> {
|
||||
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 {
|
||||
|
||||
Reference in new issue
Block a user