diff --git a/crates/engine/src/cmd/patternlib.rs b/crates/engine/src/cmd/patternlib.rs index 28ec9a80af..e8cffa24aa 100644 --- a/crates/engine/src/cmd/patternlib.rs +++ b/crates/engine/src/cmd/patternlib.rs @@ -194,9 +194,8 @@ pub(crate) fn probe_import(d: &Document, lib: &Document, name: &str, gathered: & 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) - { + let found = gathered.import_into(&mut merged, &SwatchChoices::default(), false)?; + if reused(&found) { return Ok((SwatchChoices::default(), merged, found)); } } diff --git a/crates/engine/src/cmd/swatchlib.rs b/crates/engine/src/cmd/swatchlib.rs index 40b20e5c3a..c120f45038 100644 --- a/crates/engine/src/cmd/swatchlib.rs +++ b/crates/engine/src/cmd/swatchlib.rs @@ -49,7 +49,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, 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}", + "{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 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 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 ), @@ -1024,22 +1024,36 @@ fn free_copy_in(taken: &mut HashSet, base: &str, reserved: impl Fn(&str) } /// 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) { - let mut done: std::collections::HashMap = std::collections::HashMap::new(); +/// under the names `links` gives them, for each whose colors `links` names now. Those leave +/// `pending` and are returned. +fn settle<'a>( + d: &mut vectorcraft_doc::Document, + pending: &mut Vec<(String, &'a Swatch)>, + links: &HashMap, +) -> Vec<(String, &'a Swatch)> { + let mut done = vec![]; pending.retain(|(name, w)| { let all = all_linked(&w.paint, links); if all { - done.insert(name.clone(), relinked(&w.paint, links)); + done.push((name.clone(), *w)); } !all }); - if !done.is_empty() { - for x in d.swatches_iter_mut() { - if let Some(paint) = done.get(&x.name) { - x.paint = paint.clone(); - } + relink_in(d, &done, links); + done +} + +/// Give the swatches of `d` that `list` names (document name, library swatch) the library swatch's +/// paint with its colors linked to the names `links` gives them, in one pass over the document's +/// swatches. +fn relink_in(d: &mut vectorcraft_doc::Document, list: &[(String, &Swatch)], links: &HashMap) { + if list.is_empty() { + return; + } + let paints: HashMap<&str, Paint> = list.iter().map(|(name, w)| (name.as_str(), relinked(&w.paint, links))).collect(); + for x in d.swatches_iter_mut() { + if let Some(paint) = paints.get(x.name.as_str()) { + x.paint = paint.clone(); } } } @@ -1073,7 +1087,7 @@ fn linked_names(p: &Paint) -> Vec<&str> { } /// 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) -> Paint { +fn relinked(p: &Paint, links: &HashMap) -> Paint { let mut out = p.clone(); let swap = |name: &mut String| { if let Some(to) = links.get(name.as_str()) { @@ -1090,7 +1104,7 @@ fn relinked(p: &Paint, links: &std::collections::HashMap) -> Pai /// 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) -> bool { +fn all_linked(p: &Paint, links: &HashMap) -> bool { linked_names(p).iter().all(|n| *n == REGISTRATION || links.contains_key(*n)) } @@ -1114,7 +1128,7 @@ fn add(s: &mut Session, p: &Value) -> Result { // up once so that a large palette takes linear time. let mut by_name: std::collections::HashMap<&str, &Swatch> = std::collections::HashMap::new(); // The document's numbered copies ("Ink 2") by the name they are numbered after, in order. - let mut copies: std::collections::HashMap<&str, Vec<&Swatch>> = std::collections::HashMap::new(); + let mut copies: HashMap<&str, Vec<&Swatch>> = HashMap::new(); for w in d.swatches_iter() { let std::collections::hash_map::Entry::Vacant(e) = by_name.entry(w.name.as_str()) else { continue }; e.insert(w); @@ -1126,7 +1140,7 @@ fn add(s: &mut Session, p: &Value) -> Result { } } // 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(); + let mut lib_by_name: HashMap<&str, &Swatch> = 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); } @@ -1137,15 +1151,15 @@ fn add(s: &mut Session, p: &Value) -> Result { // 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| { + let numbered = |w: &Swatch, paint: &Paint, links: &HashMap| { 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(); + let unlinked = 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| match (&w.paint, &patterns) { + 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)) } @@ -1169,7 +1183,7 @@ fn add(s: &mut Session, p: &Value) -> Result { // 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 = std::collections::HashMap::new(); + let mut base_names: HashMap = HashMap::new(); let mut looked_up: HashSet<&str> = HashSet::new(); for (_, w) in &picked { for base in linked_names(&w.paint) { @@ -1203,7 +1217,7 @@ fn add(s: &mut Session, p: &Value) -> Result { 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 = existing.iter().filter(|(l, n)| base_of(l, n)).cloned().collect(); + 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() { vec![] @@ -1233,27 +1247,56 @@ fn add(s: &mut Session, p: &Value) -> Result { // 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); + // A global or spot color the pattern brought, or took from the document under + // a numbered name: library name → document name. A color this call picks is + // added under its own name instead of the one the pattern took. + let colors_of = |d: &vectorcraft_doc::Document, colors: &[(String, String)], took: &[(String, String)]| { + let mut out: HashMap = HashMap::new(); + 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) { + out.entry(from.clone()).or_insert_with(|| to.clone()); + } + } + out + }; + // The names learned from the copy, which the import into the document confirms. + let mut learned = vec![]; 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()); + 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); + learned.push(from); } } } - settle(d, &mut pending, &links); + let settled = settle(d, &mut pending, &links); let had = taken_names(d); 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()); + // 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 + // linked again. + let mut found = colors_of(d, &colors, &took); + let mut changed = false; + for from in learned { + let real = found.remove(&from); + if real.as_ref() != links.get(&from) { + changed = true; + match real { + Some(to) => links.insert(from, to), + None => links.remove(&from), + }; } } + for (from, to) in found { + links.entry(from).or_insert(to); + } + if changed { + relink_in(d, &settled, &links); + pending.extend(settled.into_iter().filter(|(_, w)| !all_linked(&w.paint, &links))); + } brought.extend(colors); if let Some(name) = library_pattern_swatch(d, w, &target).filter(|n| had.contains(n)) { reused.push((w.name.clone(), name)); @@ -1339,14 +1382,7 @@ fn add(s: &mut Session, p: &Value) -> Result { } out.push((w.name.clone(), name)); } - let later: std::collections::HashMap = 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(); - } - } - } + relink_in(d, &pending, &links); Ok((out, reused, brought)) })?; existing.extend(reused); diff --git a/docs/mcp.md b/docs/mcp.md index af535e4ff7..6eda2bc435 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -5805,8 +5805,9 @@ color the pattern's art links to keeps the library's color: when the document gi in under a numbered name ("Ink 2") and the pattern links to that. 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 links to its colors under the names those colors have in the document, -whether a color is picked before or after it, was added earlier or came with a pattern's art. `added` lists the colors +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. `added` lists the colors the pattern's art 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 diff --git a/docs/target-app-parity.md b/docs/target-app-parity.md index 3965f265bf..388bc655d1 100644 --- a/docs/target-app-parity.md +++ b/docs/target-app-parity.md @@ -2237,7 +2237,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`). 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, and adding it again uses that numbered copy; a library tint links to 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_picked_before_its_color_follows_it_to_its_numbered_name`, `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, and adding it again uses that numbered copy; a library tint or gradient links to 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_picked_before_its_color_follows_it_to_its_numbered_name`, `a_library_gradient_is_not_the_one_a_merged_pattern_brought`, `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`).