diff --git a/crates/engine/src/cmd/addons/cleanup.rs b/crates/engine/src/cmd/addons/cleanup.rs index d5f241b4ee..9050f07479 100644 --- a/crates/engine/src/cmd/addons/cleanup.rs +++ b/crates/engine/src/cmd/addons/cleanup.rs @@ -64,7 +64,7 @@ pub fn specs() -> Vec { "Fix All Issues", ["Object", "Plug-ins", "Artwork Cleanup"], None, - "{scope?: \"selection\"|\"document\", kinds?: [kind…] (default: all; see addon.cleanup.check)} fix the issues addon.cleanup.check finds, in up to three rounds while a round still finds some (a fix can leave issues for another kind), as one undo step: what is empty, unpainted, invisible, duplicated or off the artboards goes, anchors merge, snap or go, tiny handles retract, paths close and needless groups, compound paths and clipping groups give way to their contents → {scope, total, fixed: {kind: count}}", + "{scope?: \"selection\"|\"document\", kinds?: [kind…] (default: all; see addon.cleanup.check)} fix the issues addon.cleanup.check finds, in up to three rounds while a round still finds some (a fix can leave issues for another kind), as one undo step: what is empty, unpainted, invisible, duplicated or off the artboards goes, anchors merge, snap or go, tiny handles retract, paths close and needless groups, compound paths and clipping groups give way to their contents. An error, with nothing fixed, when the fixes would delete the artboard an open Create Font Sheet dialog uses → {scope, total, fixed: {kind: count}}", has_doc, fix ), @@ -260,6 +260,13 @@ fn fix(s: &mut Session, p: &Value) -> Result { } // With nothing to fix, the document and its undo history stay as they are. if total > 0 { + if let Some(guard) = &s.artboard_deletion_guard { + let st = s.doc()?; + let protected = |doc: &Document| doc.artboards.iter().any(|a| a.id == guard.artboard); + if (st.uid, st.reverts) == guard.document && protected(&st.doc) && !protected(&d) { + return Err(EngineError::Other(guard.message.clone())); + } + } sel.prune(&d); s.edit("Fix Issues", |doc, selection| { *doc = d; diff --git a/crates/engine/src/cmd/addons/typetools/font_sheet.rs b/crates/engine/src/cmd/addons/typetools/font_sheet.rs index 9d71c86610..14c4f46edf 100644 --- a/crates/engine/src/cmd/addons/typetools/font_sheet.rs +++ b/crates/engine/src/cmd/addons/typetools/font_sheet.rs @@ -36,7 +36,7 @@ pub fn specs() -> Vec { "Create Font Sheet", ["Object", "Plug-ins", "Type Tools"], None, - "{text?: one line of sample text of at most 1000 characters without line or paragraph breaks or other control characters (\"The quick brown fox jumps over the lazy dog\"), size?: pt 1..1296 (12), filter?: text (only the families whose name contains it, in any case; empty: every family), families?: [family name…] (instead of filter: these families, in this order; each must be available and named once), allStyles?: false (true: one entry for each style of each family; false: the family's Regular, else its closest style), labels?: true (each entry starts with a line naming its font, at the sample's size up to 9 pt, in the document's normal type font), columns?: 1..50 (2), gap?: pt 0..1000 (12, between entries and between columns), margin?: pt 0..1000 (36, inside each page), newArtboards?: true (false: the pages after the first are laid out in the same places without artboards)} set the sample text once in every family the font menus list (the bundled, added and installed fonts), as point type labeled with the font's name, flowed down the columns of the active artboard (inset by `margin`) and on to new artboards of the same size to the right of the rightmost artboard when it fills, as one group named Font Sheet in the current layer (else the topmost layer shown and unlocked), in one undo step; the group is selected. Each row is as tall as the tallest laid-out entry. A sample wider than its column runs on past it. A family whose font can't be loaded, or with allStyles a style that can't, or a composite font with a part setting the sample whose font or style can't, is left out, listed in `skipped` and named in `warning`. An error, with nothing changed, when no font matches, when there are more than 5000 entries, when no font loads, or when the margin, gap and columns leave no room for an entry → {id, count, skipped: [family, or family and style…], artboards: [index…] (the artboards added), warning?}", + "{text?: one line of sample text of at most 1000 characters without line or paragraph breaks or other control characters (\"The quick brown fox jumps over the lazy dog\"), size?: pt 1..1296 (12), filter?: text (only the families whose name contains it, in any case; empty: every family), families?: [family name…] (instead of filter: these families, in this order; each must be available and named once; an error with any filter, an empty one included), allStyles?: false (true: one entry for each style of each family; false: the family's Regular, else its closest style), labels?: true (each entry starts with a line naming its font, at the sample's size up to 9 pt, in the document's normal type font), columns?: 1..50 (2), gap?: pt 0..1000 (12, between entries and between columns), margin?: pt 0..1000 (36, inside each page), newArtboards?: true (false: the pages after the first are laid out in the same places without artboards)} set the sample text once in every family the font menus list (the bundled, added and installed fonts), as point type labeled with the font's name, flowed down the columns of the active artboard (inset by `margin`) and on to new artboards of the same size to the right of the rightmost artboard when it fills, as one group named Font Sheet in the current layer (else the topmost layer shown and unlocked), in one undo step; the group is selected. Each row is as tall as the tallest laid-out entry. A sample wider than its column runs on past it. A family whose font can't be loaded, or with allStyles a style that can't, or a composite font with a part setting the sample whose font or style can't, is left out, listed in `skipped` and named in `warning`. An error, with nothing changed, when no font matches, when there are more than 5000 entries, when no font loads, or when the margin, gap and columns leave no room for an entry → {id, count, skipped: [family, or family and style…], artboards: [index…] (the artboards added), warning?}", has_doc, font_sheet )] @@ -91,7 +91,7 @@ fn families(p: &Value, db: &FontDb) -> Result> { } return Ok(found); } - Some(_) if filter.is_some() => return Err(bad(COMMAND, "give `families` or `filter`, not both")), + Some(_) if p.get("filter").is_some_and(|f| !f.is_null()) => return Err(bad(COMMAND, "give `families` or `filter`, not both")), Some(Value::Array(a)) => a, Some(_) => return Err(bad(COMMAND, "`families` must be a list of font family names")), }; diff --git a/crates/engine/src/cmd/addons/typetools/font_sheet/tests.rs b/crates/engine/src/cmd/addons/typetools/font_sheet/tests.rs index 1095b4428c..5119d6c339 100644 --- a/crates/engine/src/cmd/addons/typetools/font_sheet/tests.rs +++ b/crates/engine/src/cmd/addons/typetools/font_sheet/tests.rs @@ -168,6 +168,8 @@ fn bad_params_are_refused_without_a_change() { json!({"families": ["No Such Family Anywhere"]}), json!({"families": [""]}), json!({"families": ["Inter"], "filter": "Inter"}), + json!({"families": ["Inter"], "filter": ""}), + json!({"families": ["Inter"], "filter": " "}), json!({"labels": "yes"}), json!({"allStyles": 1}), json!({"newArtboards": "no"}), @@ -375,6 +377,30 @@ fn an_artboard_deletion_guard_checks_the_document_and_artboard_ids() { assert_eq!(s.doc().unwrap().doc.artboards.len(), 1, "a reverted drawing is not protected"); } +/// Cut Artboard, Delete Empty Artboards and Artwork Cleanup's Empty Artboards keep the guarded artboard too. +#[test] +fn an_artboard_deletion_guard_covers_every_command_that_deletes_artboards() { + let mut s = Session::new(); + s.execute("file.new", &json!({"artboards": 3})).unwrap(); + let state = s.doc().unwrap(); + let document = (state.uid, state.reverts); + let artboard = state.doc.artboards[1].id; + s.artboard_deletion_guard = Some(crate::ArtboardDeletionGuard { document, artboard, message: "The artboard is in use.".into() }); + let before = (s.doc().unwrap().doc.clone(), steps(&s)); + for (command, params) in [ + ("artboard.cut", json!({"index": 1})), + ("artboard.deleteEmpty", json!({})), + ("addon.cleanup.fix", json!({"kinds": ["emptyArtboards"], "scope": "document"})), + ] { + let error = s.execute(command, ¶ms).unwrap_err(); + assert_eq!(error.to_string(), "The artboard is in use.", "{command}"); + assert_eq!(s.doc().unwrap().doc, before.0, "{command}"); + assert_eq!(steps(&s), before.1, "{command}"); + } + s.execute("artboard.cut", &json!({"index": 2})).unwrap(); + assert_eq!(s.doc().unwrap().doc.artboards.len(), 2, "another artboard is cut"); +} + #[test] fn it_needs_a_document() { let mut s = Session::new(); diff --git a/crates/engine/src/cmd/layer.rs b/crates/engine/src/cmd/layer.rs index 9528bb6c86..19c1358c2a 100644 --- a/crates/engine/src/cmd/layer.rs +++ b/crates/engine/src/cmd/layer.rs @@ -147,7 +147,7 @@ pub fn specs() -> Vec { "Delete Artboard", ["Window", "Artboards"], None, - "{index | indices: [index…]} delete artboards (with their guides) in one step; one is always left, and a locked one is an error", + "{index | indices: [index…]} delete artboards (with their guides) in one step; one is always left, and a locked one, or the one an open Create Font Sheet dialog uses, is an error", has_doc, artboard_delete ), @@ -156,7 +156,7 @@ pub fn specs() -> Vec { "Delete Empty Artboards", ["Window", "Artboards"], None, - "{} delete every artboard with no artwork on it (no object, hidden, locked and template art included, whose visual bounds meet or touch it, clip groups and clipping layers by their members inside the clip; guides don't count), with its guides, in one step; locked artboards stay; when every artboard is empty and none is locked, artboard 1 stays (with a locked one left, artboard 1 can go) → {deleted: [index…]} (the indices before deleting)", + "{} delete every artboard with no artwork on it (no object, hidden, locked and template art included, whose visual bounds meet or touch it, clip groups and clipping layers by their members inside the clip; guides don't count), with its guides, in one step; locked artboards stay; when every artboard is empty and none is locked, artboard 1 stays (with a locked one left, artboard 1 can go); an error, with nothing deleted, when one of them is the artboard an open Create Font Sheet dialog uses → {deleted: [index…]} (the indices before deleting)", has_empty_artboards, artboard_delete_empty ), @@ -1155,17 +1155,25 @@ fn artboard_new(s: &mut Session, p: &Value) -> Result { fn artboard_delete(s: &mut Session, p: &Value) -> Result { let indices = super::panelcmds::artboard_indices(&s.doc()?.doc, p, "artboard.delete")?; - if let Some(guard) = &s.artboard_deletion_guard { - let state = s.doc()?; - if (state.uid, state.reverts) == guard.document && indices.iter().any(|&index| state.doc.artboards[index].id == guard.artboard) { - return Err(EngineError::Other(guard.message.clone())); - } - } + refuse_guarded(s, &indices)?; super::panelcmds::refuse_locked(&s.doc()?.doc, &indices)?; s.edit("Delete Artboard", |d, sel| remove_artboards(d, sel, &indices))?; ok() } +/// The session's artboard deletion guard's message when deleting artboards `indices` of the +/// active document would delete the artboard it protects. +pub(crate) fn refuse_guarded(s: &Session, indices: &[usize]) -> Result<()> { + let Some(guard) = &s.artboard_deletion_guard else { return Ok(()) }; + let state = s.doc()?; + if (state.uid, state.reverts) == guard.document + && indices.iter().any(|&index| state.doc.artboards.get(index).is_some_and(|a| a.id == guard.artboard)) + { + return Err(EngineError::Other(guard.message.clone())); + } + Ok(()) +} + /// Delete artboards `indices` (sorted, each once) of `d` with their guides; at least one must be /// left. The other artboards keep their order, no later artboard takes a deleted one's id, and /// `sel` forgets the deleted guides. One walk over the artboards and one over the ruler guides, @@ -1242,6 +1250,7 @@ fn artboard_delete_empty(s: &mut Session, _: &Value) -> Result { if indices.is_empty() { return Err(EngineError::Other("no empty artboards to delete".into())); } + refuse_guarded(s, &indices)?; s.edit("Delete Empty Artboards", |d, sel| remove_artboards(d, sel, &indices))?; Ok(json!({ "deleted": indices })) } diff --git a/crates/engine/src/cmd/panelcmds.rs b/crates/engine/src/cmd/panelcmds.rs index 433c69202c..4ccd3529e2 100644 --- a/crates/engine/src/cmd/panelcmds.rs +++ b/crates/engine/src/cmd/panelcmds.rs @@ -38,7 +38,7 @@ pub fn specs() -> Vec { "Cut Artboard", [], None, - "{index? | indices?, selectedArtboards?, art?, lockedAndHidden?} put the artboards and the art fully inside each on the clipboard (leaving oversized or overhanging art on the canvas and off the clipboard), then delete the artboards and that art in one undo step (one artboard is always left: the cut fails when it names every artboard, or a locked one; art also on another locked artboard stays, and is left off the clipboard, unless lockedAndHidden); edit.cut runs this while the Artboard tool is chosen → {copied: objects}", + "{index? | indices?, selectedArtboards?, art?, lockedAndHidden?} put the artboards and the art fully inside each on the clipboard (leaving oversized or overhanging art on the canvas and off the clipboard), then delete the artboards and that art in one undo step (one artboard is always left: the cut fails when it names every artboard, a locked one, or the one an open Create Font Sheet dialog uses; art also on another locked artboard stays, and is left off the clipboard, unless lockedAndHidden); edit.cut runs this while the Artboard tool is chosen → {copied: objects}", has_doc, |s, p| { boards_and_art_noted(s, p, "artboard.cut")?; @@ -366,6 +366,7 @@ pub(crate) fn artboard_copy(s: &mut Session, p: &Value, cut: bool) -> Result