From b969a82e432f8d9a6a6ac8cffb84cca50f673a08 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sat, 14 Nov 2026 12:22:48 -0800 Subject: [PATCH] Type: Smart Punctuation keeps ruby on its characters and skips fractions next to any digit Each run is rewritten in pieces between the ruby spans' ends, so no replacement joins characters across one, and each span is moved to where its characters land in the new text. Expert Fractions treats a digit of any script next to a fraction as part of a larger number. --- crates/engine/src/cmd/typemenu.rs | 55 +++++++++++++++++++++++++---- crates/engine/src/tests/menucmds.rs | 36 +++++++++++++++++++ docs/mcp.md | 9 ++--- 3 files changed, 90 insertions(+), 10 deletions(-) diff --git a/crates/engine/src/cmd/typemenu.rs b/crates/engine/src/cmd/typemenu.rs index e7b25778ef..d793fc12a3 100644 --- a/crates/engine/src/cmd/typemenu.rs +++ b/crates/engine/src/cmd/typemenu.rs @@ -367,7 +367,8 @@ fn smart_spaces(text: &str, after: &mut Option, n: &mut usize) -> String } /// The fraction `cs` starts with, and its length in characters: one of [`FRACTIONS`] that no digit, -/// slash or decimal part follows, so `1/25`, `1/2/2026` and `1/2.5` are left as they are. +/// slash or decimal part follows, so `1/25`, `1/2/2026` and `1/2.5` are left as they are. A digit +/// counts in any script. fn fraction_at(cs: &[char]) -> Option<(usize, char)> { FRACTIONS.iter().find_map(|(text, f)| { let len = text.chars().count(); @@ -375,8 +376,8 @@ fn fraction_at(cs: &[char]) -> Option<(usize, char)> { return None; } let ends = match &cs[len..] { - [c, ..] if c.is_ascii_digit() || *c == '/' => false, - ['.' | ',', d, ..] if d.is_ascii_digit() => false, + [c, ..] if c.is_numeric() || *c == '/' => false, + ['.' | ',', d, ..] if d.is_numeric() => false, _ => true, }; ends.then_some((len, *f)) @@ -395,7 +396,7 @@ fn expert_fractions(text: &str, prev: Option, next: [Option; 2], cov let mut i = 0; while i < len_text { let before = if i == 0 { prev } else { Some(cs[i - 1]) }; - let free = !before.is_some_and(|c| c.is_ascii_digit() || matches!(c, '/' | '.' | ',')); + let free = !before.is_some_and(|c| c.is_numeric() || matches!(c, '/' | '.' | ',')); let found = fraction_at(&cs[i..]).filter(|(len, f)| i + len <= len_text && covers(*f)); if free && let Some((len, f)) = found { out.push(f); @@ -527,27 +528,69 @@ fn smart_punctuation(s: &mut Session, p: &Value) -> Result { let Some(NodeKind::Text(t)) = d.node_mut(*id).map(|n| &mut n.kind) else { continue }; let mut st = PunctState::default(); let mut any = false; + // The ruby spans' ends, in the plain text as it was. Each run is rewritten in pieces + // between them, so no replacement crosses one, and `moved` records where each end and + // each run's start lands in the new text. + let mut edges: Vec = t.ruby.iter().flat_map(|r| [r.start, r.end]).collect(); + edges.sort_unstable(); + edges.dedup(); + let mut moved = vec![]; + let (mut old_at, mut new_at) = (0, 0); // Inline graphics are art, not characters to rewrite. Each run sees the first two // characters of the runs after it, up to the next inline graphic, as they were. for i in 0..t.runs.len() { + let len = t.runs[i].text.len(); + moved.push((old_at, new_at)); if t.runs[i].inline.is_some() { // A graphic separates the characters on either side of it for Smart Spaces and // Expert Fractions; quotes still read the character before it. st.after_period = None; st.after_graphic = true; + (old_at, new_at) = (old_at + len, new_at + len); continue; } let mut after = t.runs[i + 1..].iter().take_while(|r| r.inline.is_none()).flat_map(|r| r.text.chars()); - let next = [after.next(), after.next()]; + let following = [after.next(), after.next()]; + // Smart Spaces counts no space across a ruby span's end, so it removes no space on + // the other side of one. + if edges.binary_search(&old_at).is_ok() { + st.after_period = None; + } let r = &mut t.runs[i]; - let new = smarten(&r.text, &r.style, next, &o, &mut st, &mut n); + let mut cuts: Vec = + edges.iter().filter(|&&e| e > old_at && e < old_at + len && r.text.is_char_boundary(e - old_at)).map(|e| e - old_at).collect(); + cuts.push(len); + let mut new = String::with_capacity(len); + let mut from = 0; + for cut in cuts { + if from > 0 { + moved.push((old_at + from, new_at + new.len())); + st.after_period = None; + } + let mut after = r.text[cut..].chars().chain(following.into_iter().flatten()); + let next = [after.next(), after.next()]; + new.push_str(&smarten(&r.text[from..cut], &r.style, next, &o, &mut st, &mut n)); + from = cut; + } + (old_at, new_at) = (old_at + len, new_at + new.len()); if new != r.text { r.text = new; any = true; } } + moved.push((old_at, new_at)); if any { changed += 1; + // Each ruby end moves with the text before it; an end inside a character keeps its + // distance from the last place recorded before it. + let to_new = |v: usize| { + let k = moved.partition_point(|&(old, _)| old <= v).saturating_sub(1); + moved.get(k).map_or(v, |&(old, new)| new + (v - old.min(v))) + }; + for span in &mut t.ruby { + (span.start, span.end) = (to_new(span.start), to_new(span.end)); + } + t.normalize_ruby(); refresh_bounds(t); } } diff --git a/crates/engine/src/tests/menucmds.rs b/crates/engine/src/tests/menucmds.rs index 90ac750157..9d0f86fb3f 100644 --- a/crates/engine/src/tests/menucmds.rs +++ b/crates/engine/src/tests/menucmds.rs @@ -3671,6 +3671,42 @@ fn smart_spaces_keeps_one_space_after_a_period() { assert_eq!(r["replaced"]["spaces"], 2); } +/// Ruby stays on the characters it was set on when Smart Punctuation makes the text before them +/// longer (curly quotes) or shorter (Smart Spaces), and no replacement takes in characters on both +/// sides of a ruby span's end: the fi with ruby on its i stays as letters, and a second space after +/// a period with ruby of its own stays. +#[test] +fn smart_punctuation_keeps_ruby_on_its_characters() { + let mut s = session(); + let spans = |s: &Session, t| match node(s, t).kind { + NodeKind::Text(o) => o.ruby.iter().map(|r| (r.start, r.end)).collect::>(), + _ => vec![], + }; + let t = id_of(&s.execute("text.create", &json!({"x": 50, "y": 50, "text": "'A'. base fit X. Y", "font": "Source Sans 3"})).unwrap()); + sel(&mut s, &[t]); + for (start, end) in [(1, 2), (6, 10), (12, 13), (18, 19)] { + s.execute("type.ruby", &json!({"text": "r", "start": start, "end": end})).unwrap(); + } + let r = s.execute("type.smartPunctuation", &json!({"spaces": true, "fiLigatures": true})).unwrap(); + assert_eq!(plain(&s, t), "‘A’. base fit X. Y"); + assert_eq!((&r["replaced"]["spaces"], &r["replaced"]["fiLigatures"]), (&json!(1), &json!(0))); + let text = plain(&s, t); + let on: Vec<&str> = spans(&s, t).into_iter().map(|(a, b)| &text[a..b]).collect(); + assert_eq!(on, ["A", "base", "i", " "]); +} + +/// Expert Fractions leaves a fraction next to a digit of another script as it is. +#[test] +fn expert_fractions_skip_fractions_next_to_digits_of_any_script() { + let mut s = session(); + let words = "\u{662}1/2 1/2\u{662} 1/2.\u{662} 1/2"; + let t = id_of(&s.execute("text.create", &json!({"x": 50, "y": 50, "text": words, "font": "Source Sans 3"})).unwrap()); + sel(&mut s, &[t]); + let r = s.execute("type.smartPunctuation", &json!({"fractions": true})).unwrap(); + assert_eq!(plain(&s, t), "\u{662}1/2 1/2\u{662} 1/2.\u{662} ½"); + assert_eq!(r["replaced"]["fractions"], 1); +} + #[test] fn convert_point_area_roundtrip() { let mut s = session(); diff --git a/docs/mcp.md b/docs/mcp.md index 16d419ad72..294a2b9c24 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -8624,11 +8624,12 @@ on again. - `dashes` (default `true`): `--` becomes an en dash and `---` an em dash. - `ellipsis` (default `true`): `...` becomes an ellipsis. - `fractions` (default `false`): 1/2, 1/4, 3/4 and the other fractions Unicode encodes as one character (thirds, - fifths, sixths, sevenths, eighths, ninths and 1/10) become that character. A fraction with a digit, a slash or a - decimal point next to it, as in 1/25, 1/2/26 or 1/2.5, is left as it is, also when a change of style falls inside - it. An inline graphic separates the text before it from the text after it, for this option and for `spaces`. + fifths, sixths, sevenths, eighths, ninths and 1/10) become that character. A fraction with a digit of any script, a + slash or a decimal point next to it, as in 1/25, 1/2/26 or 1/2.5, is left as it is, also when a change of style falls + inside it. An inline graphic separates the text before it from the text after it, for this option and for `spaces`. -A ligature or a fraction is written only where the run's font has a glyph for it; elsewhere the letters stay. A value +A ligature or a fraction is written only where the run's font has a glyph for it; elsewhere the letters stay. Ruby +stays on the characters it was set on, and no replacement joins characters on both sides of a ruby span's end. A value other than true, false or null is refused. The reply gives the number of text objects changed and how many of each replacement were made; with nothing to replace, no undo step is recorded. Type › Smart Punctuation… opens a dialog with these options, Replace In (Selected Text Only or Entire Document) and Report Results, which lists the count of each