diff --git a/crates/engine/src/cmd/swatchlib.rs b/crates/engine/src/cmd/swatchlib.rs index a2fa4bb0bb..48413d99a8 100644 --- a/crates/engine/src/cmd/swatchlib.rs +++ b/crates/engine/src/cmd/swatchlib.rs @@ -1168,20 +1168,64 @@ fn add(s: &mut Session, p: &Value) -> Result { .into_iter() .any(|n| !links.contains_key(n) && by_name.contains_key(n) && lib_by_name.get(n).is_some_and(|b| b.global || b.spot)) }; - // `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: &HashMap| match (&w.paint, &patterns) { - (Paint::Pattern { pattern, .. }, Some(src)) => { - patternlib::present_pattern(d, src, pattern).and_then(|target| library_pattern_swatch(d, w, &target)) + let picked_names: HashSet<&str> = picked.iter().map(|(_, w)| w.name.as_str()).collect(); + // Whether the document's swatch `x` of the name of library swatch `w` is `w` with its colors + // linked to swatches of the document → the library's global and spot colors to bring back. + // `x` shows `w`'s paint with each color linked to: the name `links` gives it; its own name when + // that is the document's color of the library color's paint and kind; or, for a library color + // the document has under no name, that color's own name, which then comes in under it. + // Registration and colors the library does not have keep their names. + let fit = |x: &Swatch, w: &Swatch, links: &HashMap| -> Option> { + let (ours, theirs) = (linked_names(&w.paint), linked_names(&x.paint)); + if ours.len() != theirs.len() { + return None; } - _ if misplaced(&w.paint, links) => None, - _ => match by_name.get(w.name.as_str()) { - Some(x) if x.paint == *paint && same_color_kind(x, w) => Some(w.name.clone()), + let map: HashMap = ours.iter().zip(&theirs).map(|(l, t)| (l.to_string(), t.to_string())).collect(); + if relinked(&w.paint, &map) != x.paint { + return None; + } + let mut restore = vec![]; + for (&l, &t) in ours.iter().zip(&theirs) { + let ok = match (links.get(l), lib_by_name.get(l).filter(|b| b.global || b.spot)) { + (Some(to), _) => to == t, + (None, None) => l == t, + (None, Some(b)) => match by_name.get(t) { + Some(y) => l == t && y.paint == b.paint && same_kind(y, b), + None if l == t && !group_names.contains(t) => { + if !picked_names.contains(l) { + restore.push(l.to_string()); + } + true + } + None => false, + }, + }; + if !ok { + return None; + } + } + Some(restore) + }; + // `paint` is `w`'s paint with the colors it links to as `links` has them in the document → the + // name of the document's swatch, and the library colors to bring back for it ([`fit`]). + let there = |w: &Swatch, paint: &Paint, links: &HashMap| -> Option<(String, Vec)> { + if let (Paint::Pattern { pattern, .. }, Some(src)) = (&w.paint, &patterns) { + let target = patternlib::present_pattern(d, src, pattern)?; + return library_pattern_swatch(d, w, &target).map(|n| (n, vec![])); + } + if let Some(restore) = by_name.get(w.name.as_str()).filter(|x| same_color_kind(x, w)).and_then(|x| fit(x, w, links)) { + return Some((w.name.clone(), restore)); + } + if misplaced(&w.paint, links) { + return None; + } + let found = match by_name.get(w.name.as_str()) { 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()), - }, + }; + found.map(|n| (n, vec![])) }; - 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. @@ -1206,14 +1250,47 @@ fn add(s: &mut Session, p: &Value) -> Result { } } } + // A color that the art of a picked pattern the document holds takes under another name (a + // numbered copy that the library also names) is the one the picked tints and gradients link + // to, as when the pattern came in, unless this call adds the color itself. The colors the art + // takes are those of an import into a copy of the document, which adds nothing. + if let Some(src) = &patterns + && looked_up.iter().any(|n| !base_names.contains_key(*n)) + { + for (_, w) in &picked { + let Paint::Pattern { pattern, .. } = &w.paint else { continue }; + if patternlib::present_pattern(d, src, pattern).is_none() { + continue; + } + let mut probe: vectorcraft_doc::Document = (**d).clone(); + let Ok((_, _, took)) = patternlib::import(&mut probe, src, pattern) else { continue }; + for (from, to) in took { + let fits = |b: &&Swatch| by_name.get(to.as_str()).is_some_and(|x| (x.global || x.spot) && x.paint == b.paint && same_kind(x, b)); + if looked_up.contains(from.as_str()) && !picked_names.contains(from.as_str()) && lib_by_name.get(from.as_str()).is_some_and(fits) { + base_names.entry(from).or_insert(to); + } + } + } + } // Library name → the name of the document's swatch. let mut existing: Vec<(String, String)> = vec![]; let mut fresh = vec![]; + // The library colors that the document's tints and gradients found here link to by name while + // the document has no swatch of that name (one deleted without unlinking them): they come back + // under their own names. + let mut restore: Vec = vec![]; for &(group, w) in &picked { // 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)), + Some((name, back)) => { + existing.push((w.name.clone(), name)); + for n in back { + if !restore.contains(&n) { + restore.push(n); + } + } + } None => fresh.push((group, w)), } } @@ -1230,7 +1307,7 @@ fn add(s: &mut Session, p: &Value) -> Result { }; let mut links: HashMap = existing.iter().filter(|(l, n)| base_of(l, n)).cloned().collect(); links.extend(base_names); - let added: Vec<(String, String)> = if fresh.is_empty() { + let added: Vec<(String, String)> = if fresh.is_empty() && restore.is_empty() { vec![] } else { let (added, reused, brought) = s.edit("Add to Swatches", |d, _| { @@ -1245,6 +1322,15 @@ fn add(s: &mut Session, p: &Value) -> Result { // The swatches the patterns imported in this call brought with their art: (library // name, document name). let mut brought: Vec<(String, String)> = vec![]; + // The colors to bring back come first, under their own names, which no swatch or color + // group of the document has; the tints and gradients added here link to them too. + for n in &restore { + let Some(b) = lib_by_name.get(n.as_str()) else { continue }; + let name = free_in(&mut taken, &b.name); + d.swatches.push(Swatch { name: name.clone(), ..(*b).clone() }); + links.insert(n.clone(), name.clone()); + brought.push((n.clone(), name)); + } // The swatches added here with a link `links` did not name yet: (document name, library // swatch). let mut pending: Vec<(String, &Swatch)> = vec![]; diff --git a/crates/engine/src/tests/swatchlib.rs b/crates/engine/src/tests/swatchlib.rs index 9315eadb06..8e24403d60 100644 --- a/crates/engine/src/tests/swatchlib.rs +++ b/crates/engine/src/tests/swatchlib.rs @@ -831,6 +831,80 @@ fn a_numbered_copy_the_library_also_names_is_not_shared() { assert_eq!((&r["added"], &r["existing"]), (&json!(["Ink 3"]), &json!(["Ink 2"])), "{r}"); } +/// A tint of the document whose global color was deleted without unlinking it is the library's +/// tint only with that color: adding the tint brings the color back under its name, listed as +/// added, and the tint links to it again. +#[test] +fn a_library_tint_whose_color_the_document_deleted_brings_the_color_back() { + let mut s = session(); + let id = tinted_ink_library_and_a_red_ink(&mut s); + run(&mut s, "file.new", json!({"width": 100, "height": 100})); + run(&mut s, "swatch.new", json!({"name": "Ink", "color": "#0000ff", "global": true})); + run(&mut s, "swatch.new", json!({"name": "Ink half", "swatch": "Ink", "tint": 50})); + run(&mut s, "swatch.delete", json!({"names": ["Ink"], "unlink": false})); + assert!(doc(&s).swatch("Ink").is_none()); + let r = run(&mut s, "swatch.library.add", json!({"library": id, "names": ["Ink half"]})); + assert_eq!((&r["added"], &r["existing"]), (&json!(["Ink"]), &json!(["Ink half"])), "{r}"); + assert!(is_global(&s, "Ink", "#0000ff"), "{r}"); + assert_eq!(tint_base(&s, "Ink half").as_deref(), Some("Ink"), "{r}"); + assert_eq!(named_after(&s, "Ink half"), ["Ink half"], "{r}"); + let again = run(&mut s, "swatch.library.add", json!({"library": id, "names": ["Ink half"]})); + assert_eq!((&again["added"], &again["existing"]), (&json!([]), &json!(["Ink half"])), "{again}"); +} + +/// A library tint added with a pattern whose art took a numbered copy that the library also names +/// is found again on the next add: the tint links to the copy the art took, and adding the pattern +/// and the tint again adds nothing. +#[test] +fn a_library_tint_linked_to_the_copy_a_pattern_took_is_found_again() { + 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})); + run(&mut s, "swatch.new", json!({"name": "Ink half", "swatch": "Ink", "tint": 50})); + 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(); + 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", "Ink half"]})); + let base = tint_base(&s, "Ink half").unwrap_or_default(); + assert!(is_global(&s, &base, "#0000ff"), "{r}"); + let swatches = doc(&s).swatches_iter().count(); + let again = run(&mut s, "swatch.library.add", json!({"library": id, "names": ["Tiles", "Ink half"]})); + assert_eq!(again["added"], json!([]), "{again}"); + assert_eq!(doc(&s).swatches_iter().count(), swatches, "{again}"); + assert_eq!(named_after(&s, "Ink half"), ["Ink half"], "{again}"); + assert_eq!(tint_base(&s, "Ink half"), Some(base), "{again}"); +} + +/// Without a pattern whose art takes it, a numbered copy that the library also names stands for +/// that library swatch alone: the document's tint linked to it is not the library's tint of the +/// color the copy is numbered after. +#[test] +fn a_tint_linked_to_a_copy_the_library_names_is_not_another_colors_tint() { + 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})); + run(&mut s, "swatch.new", json!({"name": "Ink half", "swatch": "Ink", "tint": 50})); + 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": "Inks.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})); + run(&mut s, "swatch.new", json!({"name": "Ink half", "swatch": "Ink 2", "tint": 50})); + let r = run(&mut s, "swatch.library.add", json!({"library": id, "names": ["Ink half"]})); + assert_eq!(r["existing"], json!([]), "{r}"); + let added = r["added"][0].as_str().unwrap_or_default().to_string(); + let base = tint_base(&s, &added).unwrap_or_default(); + assert!(base != "Ink 2" && is_global(&s, &base, "#0000ff"), "{r}"); +} + /// A numbered copy of a color's name that the library also has as a swatch of another color is /// still the copy of the color: adding the color after a pattern brought it under that name adds /// no other copy. diff --git a/docs/mcp.md b/docs/mcp.md index 0a31452419..0de5ad1692 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -5809,8 +5809,13 @@ is used as it is. A library tint or gradient whose global or spot color comes in 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. `added` -lists the colors the pattern's art, a tint or a gradient brings too. A document with +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.