Swatches: a library pattern painted with another pattern is matched by both

Adding a library pattern swatch reused a document pattern with the same
tile options and art even when that art paints with a pattern of the same
name and a different definition, so the library's inner pattern never came
in. A pattern whose art uses other resources is now matched by importing it
into a copy of the document, which compares those resources too.
This commit is contained in:
dotCooCoo
2026-11-16 21:51:12 -08:00
parent ad2572734c
commit 49184fc486
3 changed files with 90 additions and 4 deletions
+45 -3
View File
@@ -12,7 +12,7 @@ use vectorcraft_geom::{PathData, Point, Rect, SubPath, shapes};
use super::Result;
use super::clipboard::Clipboard;
use super::clipboard::resources::SwatchChoices;
use super::clipboard::resources::{Imported, SwatchChoices};
use super::swatchlib::LibraryInfo;
/// A built-in pattern library: a stable id, its menu name and the function that adds its patterns.
@@ -89,10 +89,52 @@ pub fn swatch_source(w: &Swatch) -> Option<Arc<Document>> {
/// 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`.
pub(crate) fn import(d: &mut Document, lib: &Document, name: &str) -> Result<String> {
let imported = gather(lib, name).import_into(d, &SwatchChoices::default(), false)?;
Ok(imported_name(&imported, name))
}
/// Pattern `name` of `lib` with the resources its art uses, as an object painted with it is copied.
fn gather(lib: &Document, name: &str) -> Clipboard {
let user = Node::path(NodeId(0), shapes::rectangle(Rect::new(0.0, 0.0, 1.0, 1.0)), Appearance::basic(pattern_paint(name), Paint::None, 0.0));
let imported = Clipboard::gather(lib, vec![user]).import_into(d, &SwatchChoices::default(), false)?;
Clipboard::gather(lib, vec![user])
}
/// The name pattern `name` has in the document `imported` went into.
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());
Ok(renamed.unwrap_or(name).to_string())
renamed.unwrap_or(name).to_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.
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
&& gathered.symbols.is_empty()
&& gathered.swatches.is_empty()
&& gathered.graphic_styles.is_empty()
&& gathered.char_styles.is_empty()
&& gathered.para_styles.is_empty()
&& gathered.brushes.is_empty()
&& gathered.images.is_empty();
if only_pattern {
return existing_pattern(d, lib, name);
}
// Importing reuses only a pattern under that name or a numbered copy of it with the same tile
// options and as many art objects; without one, the document is not copied.
let def = lib.pattern(name)?;
let bare = |p: &PatternDef| PatternDef { name: String::new(), art: vec![], ..p.clone() };
let copy = |n: &str| {
n == name || n.strip_prefix(name).and_then(|r| r.strip_prefix(' ')).is_some_and(|r| !r.is_empty() && r.bytes().all(|b| b.is_ascii_digit()))
};
if !d.patterns.iter().any(|p| copy(&p.name) && p.art.len() == def.art.len() && bare(p) == bare(def)) {
return None;
}
let mut probe = d.clone();
let imported = gathered.import_into(&mut probe, &SwatchChoices::default(), false).ok()?;
(imported.added == 0).then(|| imported_name(&imported, name))
}
/// The pattern of `d` that is pattern `name` of library `lib`: the same tile options and art under