From 190fdf8d9b3bff07623ecbaf66f38f324e747cc2 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Wed, 2 Dec 2026 06:25:26 -0800 Subject: [PATCH] Type Tools: Make Numbers Sequence skips threaded type with nothing to replace With `replace` set to numbers or placeholder, type that holds no number or placeholder is skipped, but a threaded frame was refused before that check, so a selection holding threaded type with nothing to replace failed as a whole. The command now refuses a threaded frame only when its story holds a number or placeholder, so a placeholder split across two frames is still refused. Each story is checked once, through the thread index. The whole-text mode still refuses threaded type before reading any text. Follows #10448. --- crates/engine/src/cmd/addons/typetools.rs | 2 +- .../src/cmd/addons/typetools/numbers.rs | 47 +++++++++++++------ .../src/cmd/addons/typetools/numbers/tests.rs | 45 ++++++++++++++++++ crates/engine/src/cmd/threads.rs | 11 +++++ docs/mcp.md | 3 +- docs/target-app-parity.md | 2 +- 6 files changed, 93 insertions(+), 17 deletions(-) diff --git a/crates/engine/src/cmd/addons/typetools.rs b/crates/engine/src/cmd/addons/typetools.rs index e8e512a448..ef010d7f32 100644 --- a/crates/engine/src/cmd/addons/typetools.rs +++ b/crates/engine/src/cmd/addons/typetools.rs @@ -71,7 +71,7 @@ pub fn specs() -> Vec { "Make Numbers Sequence…", ["Object", "Plug-ins", "Type Tools"], None, - "{start?: number (1), step?: number (1; its sign is set to run toward `end`; 0: every object gets `start`), end?: number (omitted: the numbers run to the last type object; given: they stop past `end`, and the objects after the last number keep their text), order?: \"stacking\" (stacking order, from the back) | \"rows\" (top to bottom, each row left to right) | \"columns\" (left to right, each column top to bottom) (\"stacking\"; rows and columns group objects whose centers are within half the median height or width), replace?: \"all\" (the whole text) | \"numbers\" (every number in the text: digits, with any `.` or `,` between digits and a `-` before them that does not follow a letter or digit) | \"placeholder\" (every {%n}, in any case) (\"all\"; with numbers or placeholder, objects that hold none are skipped, get no number and order leaves them out; the remaining objects are ordered before end limits the sequence, so objects past end still count for rows and columns; at most 10000 numbers or placeholders are replaced in one run (matches that already read their number do not count); more is an error), shuffle?: false (true: the numbers go to the objects in random order, and order is not used), seed?: whole number (0; the same seed gives the same order; checked even with shuffle off), leadingZeros?: true (non-negative numbers are padded with zeros to the whole digits of the widest of start and end, or of the last number), digits?: 1..30 (with leadingZeros, at least this many whole digits), trailingZeros?: false (true: every number gets as many decimal places as the one with the most), removeUnused?: false (true, with end: the objects left without a number are deleted, except a clipping mask), ids?: [id…], id? (instead of ids; neither: the selection)} number the type objects among the objects (inside groups too), as one undo step. Locked or hidden type is left alone. A frame of threaded type among them is an error. Values are -1e12..1e12, rounded to the most decimal places among start, step and end (at most 6). Replaced text takes the style of the text it replaces, and a match that already reads its number keeps its style. Objects that already read so are left as they are, and when nothing changes no undo step is made → {count (objects changed), ids (numbered, in order), removed}", + "{start?: number (1), step?: number (1; its sign is set to run toward `end`; 0: every object gets `start`), end?: number (omitted: the numbers run to the last type object; given: they stop past `end`, and the objects after the last number keep their text), order?: \"stacking\" (stacking order, from the back) | \"rows\" (top to bottom, each row left to right) | \"columns\" (left to right, each column top to bottom) (\"stacking\"; rows and columns group objects whose centers are within half the median height or width), replace?: \"all\" (the whole text) | \"numbers\" (every number in the text: digits, with any `.` or `,` between digits and a `-` before them that does not follow a letter or digit) | \"placeholder\" (every {%n}, in any case) (\"all\"; with numbers or placeholder, objects that hold none are skipped, get no number and order leaves them out; the remaining objects are ordered before end limits the sequence, so objects past end still count for rows and columns; at most 10000 numbers or placeholders are replaced in one run (matches that already read their number do not count); more is an error), shuffle?: false (true: the numbers go to the objects in random order, and order is not used), seed?: whole number (0; the same seed gives the same order; checked even with shuffle off), leadingZeros?: true (non-negative numbers are padded with zeros to the whole digits of the widest of start and end, or of the last number), digits?: 1..30 (with leadingZeros, at least this many whole digits), trailingZeros?: false (true: every number gets as many decimal places as the one with the most), removeUnused?: false (true, with end: the objects left without a number are deleted, except a clipping mask), ids?: [id…], id? (instead of ids; neither: the selection)} number the type objects among the objects (inside groups too), as one undo step. Locked or hidden type is left alone. A frame of threaded type among them is an error, unless replace skips it because its story holds no number or placeholder. Values are -1e12..1e12, rounded to the most decimal places among start, step and end (at most 6). Replaced text takes the style of the text it replaces, and a match that already reads its number keeps its style. Objects that already read so are left as they are, and when nothing changes no undo step is made → {count (objects changed), ids (numbered, in order), removed}", has_selection, numbers::make_numbers ), diff --git a/crates/engine/src/cmd/addons/typetools/numbers.rs b/crates/engine/src/cmd/addons/typetools/numbers.rs index c83c306496..642854de4f 100644 --- a/crates/engine/src/cmd/addons/typetools/numbers.rs +++ b/crates/engine/src/cmd/addons/typetools/numbers.rs @@ -292,24 +292,43 @@ pub(crate) fn make_numbers(s: &mut Session, p: &Value) -> Result { if texts.len() > MAX_TEXTS { return Err(bad(C, format!("too many type objects: at most {MAX_TEXTS}"))); } - let threads = threads::ThreadIndex::of(d); - if texts.iter().any(|id| threads.threaded(*id)) { - return Err(bad(C, "threaded type can't be numbered: its frames share one story")); - } let plain = |id: NodeId| match d.node(id).map(|n| &n.kind) { Some(NodeKind::Text(t)) => t.plain_text(), _ => String::new(), }; - // Only objects that hold a number or the placeholder count, for the order too. - let mut texts: Vec<(NodeId, String)> = texts - .into_iter() - .map(|id| (id, plain(id))) - .filter(|(_, t)| match o.replace { - "numbers" => t.bytes().any(|b| b.is_ascii_digit()), - "placeholder" => !placeholder_spans(t).is_empty(), - _ => true, - }) - .collect(); + let threads = threads::ThreadIndex::of(d); + let threaded = || bad(C, "threaded type can't be numbered: its frames share one story"); + if o.replace == "all" && texts.iter().any(|id| threads.threaded(*id)) { + return Err(threaded()); + } + let holds = |t: &str| match o.replace { + "numbers" => t.bytes().any(|b| b.is_ascii_digit()), + "placeholder" => !placeholder_spans(t).is_empty(), + _ => true, + }; + // Only objects that hold a number or the placeholder count, for the order too. Threaded type + // is skipped like other type when its whole story holds none, so a placeholder split across + // two frames is still refused. + let mut kept: Vec<(NodeId, String)> = Vec::with_capacity(texts.len()); + let mut checked_threads = std::collections::HashSet::::new(); + for id in texts { + if threads.threaded(id) { + if threads.first_thread_index(id).is_some_and(|index| !checked_threads.insert(index)) { + continue; + } + let frames = threads.story(id); + let story: String = frames.iter().copied().map(plain).collect(); + if holds(&story) { + return Err(threaded()); + } + continue; + } + let t = plain(id); + if holds(&t) { + kept.push((id, t)); + } + } + let mut texts = kept; if o.order != "stacking" && o.shuffle.is_none() { let placed: Vec<(NodeId, Rect)> = texts .iter() diff --git a/crates/engine/src/cmd/addons/typetools/numbers/tests.rs b/crates/engine/src/cmd/addons/typetools/numbers/tests.rs index e7d51d47b4..94f55422c2 100644 --- a/crates/engine/src/cmd/addons/typetools/numbers/tests.rs +++ b/crates/engine/src/cmd/addons/typetools/numbers/tests.rs @@ -93,6 +93,51 @@ fn threaded_type_is_refused() { assert_eq!((plain(&s, NodeId(a)), steps(&s)), ("1 2 3".to_string(), n)); } +/// Replacing numbers or placeholders skips threaded type that holds none, as it skips other type. +#[test] +fn threaded_type_with_nothing_to_replace_is_skipped() { + let mut s = session(); + let a = + s.execute("text.create", &json!({"x": 10, "y": 10, "text": "a b c", "area": {"width": 150, "height": 60}})).unwrap()["id"].as_u64().unwrap(); + let b = s.execute("shape.rectangle", &json!({"x": 200, "y": 10, "width": 150, "height": 200})).unwrap()["id"].as_u64().unwrap(); + s.execute("select.set", &json!({"ids": [a, b]})).unwrap(); + s.execute("text.thread.create", &json!({})).unwrap(); + let seat = text_at(&mut s, 40.0, 300.0, "Seat 0"); + let scans = super::threads::thread_scans(); + let builds = super::threads::story_builds(); + let out = s.execute(C, &json!({"ids": [a, b, seat.0], "replace": "numbers", "start": 4})).unwrap(); + assert_eq!(super::threads::thread_scans() - scans, 1, "one thread index"); + assert_eq!(super::threads::story_builds() - builds, 1, "selected frames of one story share its match check"); + assert_eq!(out["count"], json!(1)); + assert_eq!((plain(&s, NodeId(a)), plain(&s, seat)), ("a b c".to_string(), "Seat 4".to_string())); + let n = steps(&s); + let scans = super::threads::thread_scans(); + let builds = super::threads::story_builds(); + assert!(s.execute(C, &json!({"ids": [a, b], "replace": "placeholder"})).is_ok(), "no placeholder in the story"); + assert_eq!(super::threads::thread_scans() - scans, 1, "one thread index"); + assert_eq!(super::threads::story_builds() - builds, 1, "selected frames of one story share its match check"); + assert!(s.execute(C, &json!({"ids": [a, b]})).is_err(), "the whole text of threaded type is still refused"); + assert_eq!(steps(&s), n); +} + +/// A placeholder in a threaded story is refused even when no one frame holds all of it. +#[test] +fn threaded_type_whose_story_holds_a_placeholder_is_refused() { + let mut s = session(); + let words = format!("{} {{%n}}", "word ".repeat(40)); + let a = s.execute("text.create", &json!({"x": 10, "y": 10, "text": words, "area": {"width": 60, "height": 30}})).unwrap()["id"].as_u64().unwrap(); + let b = s.execute("shape.rectangle", &json!({"x": 200, "y": 10, "width": 300, "height": 300})).unwrap()["id"].as_u64().unwrap(); + s.execute("select.set", &json!({"ids": [a, b]})).unwrap(); + s.execute("text.thread.create", &json!({})).unwrap(); + let first = plain(&s, NodeId(a)); + let second = plain(&s, NodeId(b)); + assert!(!first.contains("{%n}"), "the selected frame alone has no placeholder"); + assert!((first + &second).contains("{%n}"), "the story contains the placeholder in thread order"); + let n = steps(&s); + assert!(s.execute(C, &json!({"ids": [a], "replace": "placeholder"})).is_err()); + assert_eq!(steps(&s), n); +} + #[test] fn the_spans_find_numbers_and_placeholders() { assert_eq!(number_spans("No. 12, page 3.5 of 1,000."), [(4, 6), (13, 16), (20, 25)]); diff --git a/crates/engine/src/cmd/threads.rs b/crates/engine/src/cmd/threads.rs index 1a79fbfbc9..1d15de3a3b 100644 --- a/crates/engine/src/cmd/threads.rs +++ b/crates/engine/src/cmd/threads.rs @@ -509,6 +509,17 @@ impl<'a> ThreadIndex<'a> { pub(crate) fn threaded(&self, id: NodeId) -> bool { self.first.get(&id).and_then(|i| self.live.get(*i)).is_some_and(|n| *n >= 2) && text_of(self.doc, id).is_some() } + + /// The index in the document's threads of the first thread that lists `id`. + pub(crate) fn first_thread_index(&self, id: NodeId) -> Option { + self.first.get(&id).copied() + } + + /// [`story_of`] for `id`, its thread found through the index. + pub(crate) fn story(&self, id: NodeId) -> Vec { + let Some(thread) = self.first.get(&id).and_then(|i| self.doc.text_threads.get(*i)) else { return vec![] }; + story_in(self.doc, thread, id) + } } /// `ids` followed by the other frames of the threads they are in, each once, in thread order. diff --git a/docs/mcp.md b/docs/mcp.md index 27ec011196..9e6ce1b53f 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -18454,7 +18454,8 @@ script. - Values are rounded to the most decimal places among `start`, `step` and `end` (at most 6), so 0.1 by 0.1 gives 0.1, 0.2, 0.3. - Objects that already read so are left as they are; when nothing changes no undo step is made. -- A frame of threaded type among the objects is an error, and nothing changes. +- A frame of threaded type among the objects is an error, and nothing changes. With `replace` set to numbers or + placeholder, a frame whose story holds none is skipped instead. | Parameter | Values (default) | |---|---| diff --git a/docs/target-app-parity.md b/docs/target-app-parity.md index f6a2d5bcd1..e6297ea11a 100644 --- a/docs/target-app-parity.md +++ b/docs/target-app-parity.md @@ -3195,7 +3195,7 @@ A VectorCraft addition, not a feature of the reference app (it has no command fo ## Type Tools add-on: Make Numbers Sequence (2026-12-01) -A VectorCraft addition, so no area score moves. Object › Plug-ins › Type Tools › Make Numbers Sequence… numbers the selected type objects from a start value by a step, optionally stopping at an end value, and each number replaces the whole text, every number in it, or every `{%n}` placeholder (#10448). The objects are taken in stacking order, by rows or by columns, or in a seeded random order, with optional leading and trailing zeros, and the objects past the end can be deleted. When numbers or placeholders are replaced, objects that hold none are skipped before the ordering, so they take no number and are never deleted. Locked and hidden type is left alone, a clipping mask is not deleted, and threaded type and bad parameters are refused. A run that changes the document is one undo step, and a run that changes nothing makes none. The dialog has Preview. It is based on Sergey Osokin's MIT-licensed MakeNumbersSequence script. Tests: `cmd::addons::typetools::numbers::tests` in `vectorcraft-engine`, `addons::typetools::numbers::tests` in `vectorcraft-ui-egui` and `command_sweep`. Commands: `docs/mcp.md` › Type Tools add-on. +A VectorCraft addition, so no area score moves. Object › Plug-ins › Type Tools › Make Numbers Sequence… numbers the selected type objects from a start value by a step, optionally stopping at an end value, and each number replaces the whole text, every number in it, or every `{%n}` placeholder (#10448). The objects are taken in stacking order, by rows or by columns, or in a seeded random order, with optional leading and trailing zeros, and the objects past the end can be deleted. When numbers or placeholders are replaced, objects that hold none are skipped before the ordering, so they take no number and are never deleted. Locked and hidden type is left alone, a clipping mask is not deleted, and bad parameters are refused. Threaded type is refused unless numbers or placeholder replacement skips the frame because its story holds none. A run that changes the document is one undo step, and a run that changes nothing makes none. The dialog has Preview. It is based on Sergey Osokin's MIT-licensed MakeNumbersSequence script. Tests: `cmd::addons::typetools::numbers::tests` in `vectorcraft-engine`, `addons::typetools::numbers::tests` in `vectorcraft-ui-egui` and `command_sweep`. Commands: `docs/mcp.md` › Type Tools add-on. ## Unit Converter add-on (2026-11-14)