mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-18 06:39:10 +00:00
Swatches: a color a pattern brings in is not added again in the same call
When one Add to Swatches call added a pattern swatch and then a global or spot color its art links to, the pattern brought the color in and the color was added a second time as a numbered copy. The color the pattern brought now takes the library swatch's place and color group instead.
This commit is contained in:
@@ -87,10 +87,21 @@ pub fn swatch_source(w: &Swatch) -> Option<Arc<Document>> {
|
||||
|
||||
/// 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`.
|
||||
pub(crate) fn import(d: &mut Document, lib: &Document, name: &str) -> Result<String> {
|
||||
/// 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.
|
||||
pub(crate) fn import(d: &mut Document, lib: &Document, name: &str) -> Result<(String, Vec<(String, String)>)> {
|
||||
let had: std::collections::HashSet<String> = d.swatches_iter().map(|w| w.name.clone()).collect();
|
||||
let imported = gather(lib, name).import_into(d, &SwatchChoices::default(), false)?;
|
||||
Ok(imported_name(&imported, name))
|
||||
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()
|
||||
};
|
||||
let added = d
|
||||
.swatches_iter()
|
||||
.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))
|
||||
}
|
||||
|
||||
/// Pattern `name` of `lib` with the resources its art uses, as an object painted with it is copied.
|
||||
|
||||
@@ -970,13 +970,17 @@ fn add(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let before = d.swatch_groups.len();
|
||||
let mut placed: Vec<(&str, usize)> = vec![];
|
||||
let mut taken = taken_names(d);
|
||||
// The swatches the patterns imported in this call brought with their art: (library
|
||||
// name, document name).
|
||||
let mut brought: Vec<(String, String)> = 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.
|
||||
let had = taken_names(d);
|
||||
let target = patternlib::import(d, src, pattern)?;
|
||||
let (target, colors) = patternlib::import(d, src, pattern)?;
|
||||
brought.extend(colors);
|
||||
if let Some(name) = library_pattern_swatch(d, w, &target).filter(|n| had.contains(n)) {
|
||||
reused.push((w.name.clone(), name));
|
||||
taken = taken_names(d);
|
||||
@@ -1000,8 +1004,20 @@ fn add(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
};
|
||||
(name.clone(), Swatch { name, paint, global: false, spot: false })
|
||||
} else {
|
||||
let name = free_in(&mut taken, &w.name);
|
||||
(name.clone(), Swatch { name, ..(*w).clone() })
|
||||
// A color a pattern added earlier in this call brought with its art is that
|
||||
// swatch: it moves to the library swatch's place.
|
||||
let at = brought.iter().position(|(from, _)| *from == w.name);
|
||||
let moved = at.map(|i| brought.swap_remove(i).1).and_then(|n| {
|
||||
let i = d.swatches.iter().position(|x| x.name == n)?;
|
||||
Some(d.swatches.remove(i))
|
||||
});
|
||||
match moved {
|
||||
Some(sw) => (sw.name.clone(), sw),
|
||||
None => {
|
||||
let name = free_in(&mut taken, &w.name);
|
||||
(name.clone(), Swatch { name, ..(*w).clone() })
|
||||
}
|
||||
}
|
||||
};
|
||||
match group {
|
||||
Some(g) => {
|
||||
|
||||
@@ -413,6 +413,42 @@ fn a_pattern_painted_with_another_pattern_is_not_matched_by_the_inner_patterns_n
|
||||
assert_eq!((&again["added"], &again["existing"]), (&json!([]), &json!(["Tiles 2"])), "{again}");
|
||||
}
|
||||
|
||||
/// A global color that a library pattern's art links to, added in the same call after the pattern
|
||||
/// swatch, is the swatch the pattern brought with it: it is added once, not again as a numbered
|
||||
/// copy, and goes into the library swatch's color group.
|
||||
#[test]
|
||||
fn a_color_a_pattern_brings_is_not_added_twice_in_one_call() {
|
||||
use std::sync::Arc;
|
||||
use vectorcraft_doc::pattern::pattern_paint;
|
||||
let mut s = session();
|
||||
run(&mut s, "swatch.new", json!({"name": "Ink", "color": "#123456", "global": true}));
|
||||
s.edit("Pattern", |d, _| {
|
||||
let ink = Paint::Solid { color: Color::rgb8(0x12, 0x34, 0x56), swatch: Some("Ink".into()), tint: 1.0 };
|
||||
let square = vectorcraft_geom::shapes::rectangle(vectorcraft_geom::Rect::new(0.0, 0.0, 6.0, 6.0));
|
||||
let square = vectorcraft_doc::Node::path(d.alloc_id(), square, vectorcraft_doc::Appearance::basic(ink, Paint::None, 0.0));
|
||||
let tile = vectorcraft_geom::Rect::new(0.0, 0.0, 8.0, 8.0);
|
||||
d.patterns.push(vectorcraft_doc::PatternDef { tile, ..vectorcraft_doc::PatternDef::new("Squares", vec![Arc::new(square)]) });
|
||||
d.swatches.push(vectorcraft_color::Swatch { name: "Tiles".into(), paint: pattern_paint("Squares"), global: false, spot: false });
|
||||
// `Ink` goes into a color group of its own.
|
||||
let i = d.swatches.iter().position(|w| w.name == "Ink").unwrap();
|
||||
let ink = d.swatches.remove(i);
|
||||
d.swatch_groups.push(vectorcraft_color::SwatchGroup { name: "Inks".into(), swatches: vec![ink] });
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
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": "Squares.vectorcraft"}))["library"].clone();
|
||||
run(&mut s, "file.new", json!({"width": 100, "height": 100}));
|
||||
let r = run(&mut s, "swatch.library.add", json!({"library": id, "names": ["Tiles", "Inks"]}));
|
||||
let d = doc(&s);
|
||||
let inks: Vec<&str> = d.swatches_iter().filter(|w| w.name.starts_with("Ink")).map(|w| w.name.as_str()).collect();
|
||||
assert_eq!(inks, ["Ink"], "{r}");
|
||||
assert_eq!((&r["added"], &r["existing"]), (&json!(["Tiles", "Ink"]), &json!([])), "{r}");
|
||||
let group = d.swatch_groups.iter().find(|g| g.name == "Inks").map(|g| g.swatches.iter().map(|w| w.name.as_str()).collect::<Vec<_>>());
|
||||
assert_eq!(group, Some(vec!["Ink"]), "{r}");
|
||||
assert!(d.swatches.iter().all(|w| w.name != "Ink"), "{r}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn swatch_exchange_files_load_add_and_list_as_user_libraries() {
|
||||
let bytes = vectorcraft_testkit::ase::sample();
|
||||
|
||||
Reference in New Issue
Block a user