diff --git a/crates/engine/src/cmd/recolor.rs b/crates/engine/src/cmd/recolor.rs index db0c0ac367..90bbf9d2e6 100644 --- a/crates/engine/src/cmd/recolor.rs +++ b/crates/engine/src/cmd/recolor.rs @@ -1721,7 +1721,7 @@ fn apply(s: &mut Session, p: &Value) -> Result { let mut out = json!({ "changed": changed, "swatches": added }); not_changed.report(&mut out); if let Some(e) = edit { - e.defaults(s); + e.defaults(s)?; out["relinked"] = json!(e.relinked()); } if converted { diff --git a/crates/engine/src/cmd/swatch.rs b/crates/engine/src/cmd/swatch.rs index d0713a243a..3b98cbd9a6 100644 --- a/crates/engine/src/cmd/swatch.rs +++ b/crates/engine/src/cmd/swatch.rs @@ -322,7 +322,7 @@ fn swatch_delete(s: &mut Session, p: &Value) -> Result { super::paint::check_colorized_kept(colorized_ok, d).map_err(|e| bad(C, e))?; Ok(unlinked) })?; - map_default_paints(s, &mut unlink); + map_default_paints(s, &mut unlink)?; Ok(json!({"deleted": names, "unlinked": unlinked})) } @@ -660,20 +660,41 @@ impl Relink { renamed || recolored } - fn defaults(&self, s: &mut Session) { - map_default_paints(s, &mut |c, l, t| self.apply(c, l, t)); + fn defaults(&self, s: &mut Session) -> Result<()> { + map_default_paints(s, &mut |c, l, t| self.apply(c, l, t)) } } -/// Apply a swatch-link rewrite to the default fill and stroke for new art, except during a live -/// preview (Cancel rolls the document back, not the session). -pub(super) fn map_default_paints(s: &mut Session, f: &mut LinkFn) { - if s.in_interaction() { - return; +/// Apply a swatch-link rewrite to the fill and stroke new art takes and to the paints of its +/// template appearance, except during a live preview (Cancel rolls the document back, not the +/// session). A `command.batch` step rewrites them as the command run on its own does: a failed +/// batch, and a canceled batch preview, put back the paints the batch started with. A change is +/// recorded with the step just made, so Undo and Redo put these paints back with the document. +pub(super) fn map_default_paints(s: &mut Session, f: &mut LinkFn) -> Result<()> { + if s.in_interaction() && s.batch_stash.is_none() { + return Ok(()); } - for p in [&mut s.paint.fill, &mut s.paint.stroke] { - p.map_links(f); + let start = super::colormgmt::new_art_colors_before(s)?; + let pd = &mut s.paint; + let template = pd.appearance.iter_mut().flat_map(|a| a.items.iter_mut()).map(|it| match it { + vectorcraft_doc::AppearanceItem::Fill(f) => &mut f.paint, + vectorcraft_doc::AppearanceItem::Stroke(s) => &mut s.paint, + }); + let mut changed = false; + for p in [&mut pd.fill, &mut pd.stroke].into_iter().chain(template) { + changed |= p.map_links(f); } + if changed { + super::colormgmt::record_new_art_colors(s, start)?; + // The step takes the paints the command finally leaves, after the selection's appearance + // is inherited. + let st = s.doc()?; + if st.interaction.is_none() { + let uid = st.uid; + s.batch_paint_docs.push(uid); + } + } + Ok(()) } /// Apply a swatch-link rewrite to the document ([`Document::map_solid_paints`]) and to the art of @@ -888,7 +909,7 @@ fn swatch_edit(s: &mut Session, p: &Value) -> Result { super::paint::check_colorized_kept(colorized_ok, d).map_err(|e| bad(C, e))?; Ok(relinked) })?; - relink.defaults(s); + relink.defaults(s)?; Ok(json!({"name": to, "relinked": relinked})) } @@ -1084,7 +1105,7 @@ fn swatch_merge(s: &mut Session, p: &Value) -> Result { super::paint::check_colorized_kept(colorized_ok, d).map_err(|e| bad(C, e))?; Ok(relinked) })?; - map_default_paints(s, &mut relink); + map_default_paints(s, &mut relink)?; Ok(json!({"name": keep, "merged": merged, "relinked": relinked})) } @@ -1141,7 +1162,7 @@ fn swatch_spot_options(s: &mut Session, p: &Value) -> Result { let relinked = map_swatch_links(d, &mut follow_bases(&bases)); Ok((relinked, bases)) })?; - map_default_paints(s, &mut follow_bases(&bases)); + map_default_paints(s, &mut follow_bases(&bases))?; Ok(json!({"useLab": use_lab, "relinked": relinked})) } @@ -1165,8 +1186,11 @@ pub(super) struct GroupEdit { impl GroupEdit { /// Apply the link changes to the default fill and stroke for new art. - pub(super) fn defaults(&self, s: &mut Session) { - self.relinks.iter().for_each(|r| r.defaults(s)); + pub(super) fn defaults(&self, s: &mut Session) -> Result<()> { + if self.relinks.is_empty() { + return Ok(()); + } + map_default_paints(s, &mut |c, l, t| self.relinks.iter().any(|r| r.apply(c, l, t))) } /// How many paints linked to the group's swatches the rewrite changed. @@ -1242,6 +1266,6 @@ fn swatch_edit_group(s: &mut Session, p: &Value) -> Result { colors.into_iter().enumerate().map(|(i, c)| if old.get(i) == Some(&Some(c)) { c } else { super::paint::new_color(s, p, c) }).collect(); let rename = str_param(p, "rename"); let e = s.edit("Edit Color Group", |d, _| rewrite_group(d, group, &colors, rename, C))?; - e.defaults(s); + e.defaults(s)?; Ok(json!({"name": e.name, "swatches": e.swatches, "relinked": e.relinked})) } diff --git a/crates/engine/src/cmd/swatchlib.rs b/crates/engine/src/cmd/swatchlib.rs index 8101cfff97..ed712d8509 100644 --- a/crates/engine/src/cmd/swatchlib.rs +++ b/crates/engine/src/cmd/swatchlib.rs @@ -980,7 +980,7 @@ fn reset_defaults(s: &mut Session, p: &Value) -> Result { Ok(added) })?; if replace { - map_default_paints(s, &mut |_, l, _| unlink_dead(&live, l)); + map_default_paints(s, &mut |_, l, _| unlink_dead(&live, l))?; } Ok(json!({ "added": added })) } diff --git a/crates/engine/src/tests/swatches.rs b/crates/engine/src/tests/swatches.rs index a80769dd98..8abaec348c 100644 --- a/crates/engine/src/tests/swatches.rs +++ b/crates/engine/src/tests/swatches.rs @@ -1075,6 +1075,146 @@ fn deleting_a_global_swatch_unlinks_art_that_keeps_its_colour() { assert_eq!(fill_of(&s, a), linked("#2a6fb0", "Brand")); } +/// A session whose default fill and stroke for new art are linked to the global swatch `Brand` +/// (`#2a6fb0`), in color group `G`, beside the global swatch `Other` (`#ff8800`). +fn brand_defaults_session() -> Session { + let mut s = session(); + run(&mut s, "swatch.new", json!({"name": "Brand", "color": "#2a6fb0", "global": true})); + run(&mut s, "swatch.newGroup", json!({"name": "G", "swatches": ["Brand"]})); + run(&mut s, "swatch.new", json!({"name": "Other", "color": "#ff8800", "global": true})); + run(&mut s, "select.set", json!({"ids": []})); + run(&mut s, "paint.setFill", json!({"swatch": "Brand"})); + run(&mut s, "paint.setStroke", json!({"swatch": "Brand"})); + assert_eq!(s.paint.fill, linked("#2a6fb0", "Brand")); + s +} + +fn fill_and_stroke(s: &Session, id: NodeId) -> (Paint, Paint) { + let a = &doc(s).node(id).unwrap().appearance; + (a.fill_paint(), a.stroke_paint()) +} + +/// Run `steps` and then draw a rectangle, once one command at a time and once as one +/// `command.batch`, and check that both leave the same default paints and give the rectangle drawn +/// in the batch, and one drawn after it, the same fill and stroke. Returns the batch's rectangle's +/// fill and stroke. +fn batch_matches_steps(steps: &[(&str, Value)]) -> (Paint, Paint) { + let shape = ("shape.rectangle", json!({"x": 0, "y": 0, "width": 10, "height": 10})); + let mut one_by_one = brand_defaults_session(); + for (id, p) in steps { + run(&mut one_by_one, id, p.clone()); + } + let single = rect(&mut one_by_one); + let mut batched = brand_defaults_session(); + let commands: Vec = steps.iter().chain([&shape]).map(|(id, p)| json!({"command": id, "params": p})).collect(); + let r = run(&mut batched, "command.batch", json!({ "commands": commands })); + let in_batch = NodeId(r["results"][steps.len()]["id"].as_u64().unwrap()); + assert_eq!(batched.paint.fill, one_by_one.paint.fill, "the default fill after {steps:?}"); + assert_eq!(batched.paint.stroke, one_by_one.paint.stroke, "the default stroke after {steps:?}"); + let expected = fill_and_stroke(&one_by_one, single); + assert_eq!(fill_and_stroke(&batched, in_batch), expected, "art drawn in the batch after {steps:?}"); + let later = rect(&mut batched); + assert_eq!(fill_and_stroke(&batched, later), expected, "art drawn after the batch after {steps:?}"); + expected +} + +#[test] +fn swatch_steps_in_a_batch_change_the_new_art_paints_as_commands_do() { + let blue = Paint::solid(Color::from_hex("#2a6fb0").unwrap()); + assert_eq!(batch_matches_steps(&[("swatch.delete", json!({"name": "Brand"}))]), (blue.clone(), blue)); + let link = linked("#2a6fb0", "Brand"); + assert_eq!(batch_matches_steps(&[("swatch.delete", json!({"name": "Brand", "unlink": false}))]), (link.clone(), link)); + let signal = linked("#00ff00", "Signal"); + let edit = ("swatch.edit", json!({"name": "Brand", "newName": "Signal", "color": "#00ff00"})); + assert_eq!(batch_matches_steps(&[edit]), (signal.clone(), signal)); + let other = linked("#ff8800", "Other"); + assert_eq!(batch_matches_steps(&[("swatch.merge", json!({"names": ["Other", "Brand"]}))]), (other.clone(), other)); + let gray = linked("#ababab", "Brand"); + assert_eq!(batch_matches_steps(&[("swatch.editGroup", json!({"group": "G", "colors": ["#ababab"]}))]), (gray.clone(), gray)); +} + +#[test] +fn undo_and_redo_of_a_swatch_edit_put_back_the_new_art_paints() { + let edit = json!({"name": "Brand", "newName": "Signal", "color": "#00ff00"}); + let delete = json!({"name": "Brand"}); + for (id, p, fill) in + [("swatch.edit", &edit, linked("#00ff00", "Signal")), ("swatch.delete", &delete, Paint::solid(Color::from_hex("#2a6fb0").unwrap()))] + { + for batch in [false, true] { + let mut s = brand_defaults_session(); + let before = s.paint.clone(); + if batch { + run(&mut s, "command.batch", json!({"commands": [{"command": id, "params": p}]})); + } else { + run(&mut s, id, p.clone()); + } + assert_eq!(s.paint.fill, fill, "{id}, batch: {batch}"); + let after = s.paint.clone(); + run(&mut s, "edit.undo", json!({})); + assert_eq!(s.paint, before, "Undo of {id}, batch: {batch}"); + run(&mut s, "edit.redo", json!({})); + assert_eq!(s.paint, after, "Redo of {id}, batch: {batch}"); + } + } +} + +#[test] +fn swatch_edits_reach_the_paints_of_the_new_art_template() { + let mut s = brand_defaults_session(); + s.paint.appearance = Some(vectorcraft_doc::Appearance::basic(linked("#2a6fb0", "Brand"), linked("#2a6fb0", "Brand"), 1.0)); + run(&mut s, "swatch.edit", json!({"name": "Brand", "newName": "Signal", "color": "#00ff00"})); + let template = |s: &Session| -> Vec { + s.paint + .appearance + .iter() + .flat_map(|a| a.items.iter()) + .map(|it| match it { + vectorcraft_doc::AppearanceItem::Fill(f) => f.paint.clone(), + vectorcraft_doc::AppearanceItem::Stroke(k) => k.paint.clone(), + }) + .collect() + }; + assert_eq!(template(&s), [linked("#00ff00", "Signal"), linked("#00ff00", "Signal")]); + run(&mut s, "swatch.delete", json!({"name": "Signal"})); + let green = Paint::solid(Color::from_hex("#00ff00").unwrap()); + assert_eq!(template(&s), [green.clone(), green]); +} + +#[test] +fn a_failed_batch_puts_back_the_new_art_paints_a_swatch_step_changed() { + let mut s = brand_defaults_session(); + let before = s.paint.clone(); + let steps = json!([ + {"command": "swatch.edit", "params": {"name": "Brand", "newName": "Signal", "color": "#00ff00"}}, + {"command": "no.such.command", "params": {}}, + ]); + assert!(s.execute("command.batch", &json!({ "commands": steps })).is_err()); + assert_eq!(s.paint, before); + assert_eq!(doc(&s).swatch("Brand").unwrap().paint.color(), Some(Color::from_hex("#2a6fb0").unwrap())); +} + +#[test] +fn canceling_a_swatch_options_preview_keeps_the_new_art_paints() { + let mut s = brand_defaults_session(); + let before = s.paint.clone(); + s.begin_interaction("Swatch Options").unwrap(); + s.preview("swatch.edit", &json!({"name": "Brand", "newName": "Signal", "color": "#00ff00"})).unwrap(); + s.cancel_interaction().unwrap(); + assert_eq!(s.paint, before); + assert!(doc(&s).swatch("Signal").is_none()); + // A previewed batch that is canceled puts them back too, and one that is accepted keeps them. + let steps = json!([{"command": "swatch.edit", "params": {"name": "Brand", "newName": "Signal", "color": "#00ff00"}}]); + s.begin_interaction("Batch Preview").unwrap(); + s.preview("command.batch", &json!({ "commands": steps })).unwrap(); + s.cancel_interaction().unwrap(); + assert_eq!(s.paint, before); + s.begin_interaction("Batch Preview").unwrap(); + s.preview("command.batch", &json!({ "commands": steps })).unwrap(); + s.commit_interaction().unwrap(); + assert_eq!(s.paint.fill, linked("#00ff00", "Signal")); + assert_eq!(s.paint.stroke, linked("#00ff00", "Signal")); +} + #[test] fn deleting_several_swatches_and_groups() { let mut s = session(); diff --git a/docs/target-app-parity.md b/docs/target-app-parity.md index f3707717e6..621fcf06bf 100644 --- a/docs/target-app-parity.md +++ b/docs/target-app-parity.md @@ -1453,6 +1453,7 @@ Moved from the ROADMAP's "Shipped so far" on 2026-10-10. Update it when a featur - **CSS swatch libraries:** fractional linear and radial gradient stop positions are preserved, so nearby stops such as 12.5% and 12.890625% remain distinct instead of collapsing to 13%. Reference: [CSS Images 3 color stops](https://www.w3.org/TR/css-images-3/#color-stop-syntax). The original synthetic regression is `palette_io::tests::css_preserves_fractional_gradient_stop_positions` in `crates/color/src/palette_io.rs`. Scores and hours are unchanged. - **Swatches:** names follow the colour model and are unique; Swatch Options (process/spot, Global, Gray/RGB/HSB/CMYK/Web, live preview); editing a global or spot swatch recolours every linked fill, stroke and text run in one undo step; multi-select and delete with confirmation; New Swatch and New Color Group dialogs (a group from the selected artwork's colours); swatches in colour groups convert, separate and count like any other. The Swatches panel selects ranges and whole groups, has a find field, and drags swatches to reorder, regroup or paint art; Add Used Colors, Select All Unused, Merge, Ungroup and Sort by Kind; a CMYK document keeps applied colours in CMYK. Swatch libraries: nine generated colour libraries, seven gradient libraries (among them Organic Fades, soft perceptually mixed multi-stop fades that thin out to transparent, and Holographic, seven opaque linear foils of narrow pastel and pearl bands and seven radial glows that fade out to a clear edge) and three pattern libraries (Geometric Patterns; Grit Textures with eight seamless grain, speckle, stipple, scratch and fleck tiles; and Optical Effects with eight of VectorCraft's own seamless wave, ring, warped-check, stripe, chevron and lens tiles; their swatches bring their patterns) in a library panel, saved as `.vcswatches`, `.gpl`, swatch exchange (`.ase`) or CSS, and loaded from `.vcswatches`, `.gpl` and `.ase` files (`.ase` both ways: RGB, CMYK, Lab and Gray; global, spot and process; groups) and from color books (`.acb`, read only: the RGB, CMYK or Lab colors of a book such as a Pantone book a user owns, named with its prefix and suffix, spot unless it says process, #831); `swatch.library.copyToUser` copies a loaded library into the User Defined folder without replacing a file, and a file with the same extension and bytes as a User Defined library opens as that library. Tints of global and spot colours ("Name 40%") stay linked and separate as a percentage of their plate. Spot colours can be defined in Lab (Spot Colors options). A built-in [Registration] swatch prints on every plate. Editing, renaming, deleting or merging a global swatch also changes the colors linked to it in graph designs, the art of brushes and the tile that pattern editing's Cancel puts back, in the same undo step as the rest of the art; Default Swatches with replace unlinks them there too. + A swatch step in `command.batch` (Delete, Swatch Options, Merge, Edit Color Group) changes the fill and stroke for new art as the command run on its own does, so art drawn later in the batch or after it takes the same paint; a batch that fails, and a canceled batch preview, put back the paints it started with, and a canceled Swatch Options preview leaves them as they were. These swatch commands also change the fills and strokes of the appearance new art takes, and Undo and Redo of the command or the batch put the paints for new art back with the swatches. Evidence: `swatch_steps_in_a_batch_change_the_new_art_paints_as_commands_do`, `undo_and_redo_of_a_swatch_edit_put_back_the_new_art_paints`, `swatch_edits_reach_the_paints_of_the_new_art_template`, `a_failed_batch_puts_back_the_new_art_paints_a_swatch_step_changed` and `canceling_a_swatch_options_preview_keeps_the_new_art_paints` in `crates/engine/src/tests/swatches.rs`. Add Used Colors (`swatch.addUsedColors`) and Select All Unused (`swatch.unused`) look colors, swatch names, links, tints and gradients up in hash sets instead of searching every swatch and color for each color or linked paint (New Color Group from artwork too), and New Swatch with several colors (`swatch.new`), New Color Group (`swatch.newGroup`) and Edit Color Group (`swatch.editGroup`) name their new swatches from one set of the names already taken. The new swatches keep their names, order and links, and Select All Unused lists the same swatches. Evidence: `add_used_colors_adds_each_new_color_once_with_free_names`, `add_used_colors_global_links_every_paint_of_a_new_color`, `unused_matches_links_tints_colors_gradients_and_patterns_exactly`, `new_swatches_from_colors_are_numbered_after_names_already_taken`, `new_color_group_numbers_its_colors_after_the_group_and_its_moved_swatches`, `new_color_group_from_artwork_lists_each_color_once_with_free_names` and `edit_color_group_numbers_extra_colors_after_the_names_it_keeps` in `crates/engine/src/tests/swatchcmds.rs`; `free_swatch_names_match_naming_each_swatch_as_it_is_added` and `free_swatch_names_match_new_swatch_names_as_swatches_are_added` in `crates/doc/src/swatches.rs`. - **Colorized images (#1466):** a fill color applied to a selected grayscale or 1-bit image (`paint.setFill`) colorizes it with black, white, a process color or a spot color, and refuses a process color when the image has an alpha channel ([Convert color modes](https://web.archive.org/web/20251220223850/https://helpx.adobe.com/illustrator/desktop/manage-colors/select-and-adjust-colors/convert-color-modes.html), "Colorize Grayscale or 1-bit images"); black and white are accepted there too. The canvas draws each pixel in the color as far as the pixel is dark, with white pixels transparent; PDF export writes the image in the color (a spot color as its ink, Registration in every ink) with the image as its soft mask; Separations Preview and printed separations show it on its color's plates; native files keep the color. SVG writes the fill color through a coverage mask (or a colorized PNG in Tiny 1.2); EPS writes binary coverage as an image mask in the color's ink, and a grayscale image as ink samples scaled by coverage, with white pixels left out at Level 3 and gray midtones written over white. The prompt for deleting several swatches and groups uses the interface language's plural form for the count. Evidence: `deleting_several_swatches_uses_each_count_form` in `crates/ui-egui/src/panels/swatches/tests.rs`.