From cc63fb507477cf4b2eb7a2ad1985fe942edb70d5 Mon Sep 17 00:00:00 2001 From: kiwi4boy Date: Thu, 12 Nov 2026 21:59:46 -0500 Subject: [PATCH] craft-math: a multi-letter script base survives linear text (port of wordcraft#6230) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The parser attaches a script to the last character before it (mc^2 is m, c²), so a base of several letters from a Word or LibreOffice file, written bare, read back split. lin_base now writes such a base in invisible brackets, 〖mc〗^2, as Word linearizes it; a number (10^3, as Word writes it) and an upright function name stay bare. Ported from storytold/wordcraft#6230 (issue wordcraft#6228), which is stacked on wordcraft#6084: its own change to math.rs and its tests/linear_multichar_base.rs. The ODT test that pinned LibreOffice's E=mc^2 (mc…) moves with it: in craft-odf it now expects E=〖mc〗^2, as in the WordCraft PR. It runs only with craft-odf's `text` feature. --- crates/math/src/math.rs | 10 ++++-- crates/math/tests/linear_multichar_base.rs | 39 ++++++++++++++++++++++ crates/odf/src/text/tests.rs | 3 +- 3 files changed, 49 insertions(+), 3 deletions(-) create mode 100644 crates/math/tests/linear_multichar_base.rs diff --git a/crates/math/src/math.rs b/crates/math/src/math.rs index 11f07f6b4d..83d232fd2f 100644 --- a/crates/math/src/math.rs +++ b/crates/math/src/math.rs @@ -551,13 +551,19 @@ fn lin_operand(a: &[MNode], s: &mut String, depth: usize) { /// `(a^b)^c` would gain a visible delimiter (#6075). A base that is itself one invisible delimiter /// (LaTeX `\left.a\right.`) gets an outer pair too, so the inner one reads back as that delimiter. /// So does an operator (`⊗`, `×`, `<`…): a script after one reads as a pre-script, so `⊗_i` would -/// come back as `⊗` and a pre-script, where `〖⊗〗_i` keeps it the base. +/// come back as `⊗` and a pre-script, where `〖⊗〗_i` keeps it the base. And so does a run of several +/// letters: the parser attaches a script to the last character before it (`mc^2` is `m`, `c²`), so +/// `mc` is written `〖mc〗^2`, as Word linearizes it (#6228). A number (`10^3`, as Word writes it) and +/// an upright function name (`sin^2`, read whole as a name) stay bare. fn lin_base(a: &[MNode], s: &mut String, depth: usize) { let mut inner = String::new(); lin_arg(a, &mut inner, depth); let invisible = matches!(a, [MNode::Delim { beg: None, end: None, elems, .. }] if elems.len() == 1); let operator = matches!(a, [MNode::Run(r)] if r.text.chars().all(crate::math_linear::is_boundary)); - if simple_operand(a) && !inner.is_empty() && !invisible && !operator { + let number = |t: &str| t.chars().all(|c| c.is_ascii_digit() || c == '.'); + let name = |r: &MRun| r.sty == Some(MSty::Plain) && crate::math_linear::FUNCTION_NAMES.contains(&r.text.as_str()); + let letters = matches!(a, [MNode::Run(r)] if r.text.chars().nth(1).is_some() && !number(&r.text) && !name(r)); + if simple_operand(a) && !inner.is_empty() && !invisible && !operator && !letters { s.push_str(&inner); } else { s.push('〖'); diff --git a/crates/math/tests/linear_multichar_base.rs b/crates/math/tests/linear_multichar_base.rs new file mode 100644 index 0000000000..3944f3102c --- /dev/null +++ b/crates/math/tests/linear_multichar_base.rs @@ -0,0 +1,39 @@ +//! A script base of several letters survives linear text (#6228). The parser attaches a script to the +//! last character before it (`ab^2` is `a`, `b²`), so `to_linear` writes such a base in invisible +//! brackets, `〖mc〗^2`, as Word linearizes it. Single characters, numbers and upright function +//! names stay bare. + +use craft_math::math::{MNode, MRun, ScriptKind, parse_linear, to_linear}; + +fn a(t: &str) -> Vec { + vec![MNode::Run(MRun::new(t))] +} + +fn sup(base: Vec, s: Vec) -> MNode { + MNode::Script { kind: ScriptKind::Sup, base, sub: Vec::new(), sup: s } +} + +#[test] +fn a_base_of_several_letters_prints_back_whole() { + for base in ["ab", "mc", "x1", "αβ"] { + let n = vec![sup(a(base), a("2"))]; + assert_eq!(to_linear(&n), format!("〖{base}〗^2")); + assert_eq!(parse_linear(&to_linear(&n)), n, "{base}"); + } + // After other text, and as the base of a base. + let e = vec![MNode::Run(MRun::new("E=")), sup(a("mc"), a("2"))]; + assert_eq!(to_linear(&e), "E=〖mc〗^2"); + assert_eq!(parse_linear(&to_linear(&e)), e); + let nested = vec![sup(vec![sup(a("ab"), a("2"))], a("3"))]; + assert_eq!(parse_linear(&to_linear(&nested)), nested, "{}", to_linear(&nested)); +} + +#[test] +fn single_characters_numbers_and_function_names_stay_bare() { + assert_eq!(to_linear(&[sup(a("x"), a("2"))]), "x^2"); + // Word writes a number base bare too (`10^3`). + assert_eq!(to_linear(&[sup(a("10"), a("3"))]), "10^3"); + assert_eq!(to_linear(&[sup(a("2.5"), a("2"))]), "2.5^2"); + let sin = vec![sup(vec![MNode::Run(MRun::plain("sin"))], a("2"))]; + assert_eq!(to_linear(&sin), "sin^2"); +} diff --git a/crates/odf/src/text/tests.rs b/crates/odf/src/text/tests.rs index 8d7af18dcc..f88ba44ee9 100644 --- a/crates/odf/src/text/tests.rs +++ b/crates/odf/src/text/tests.rs @@ -167,7 +167,8 @@ fn embedded_formula_object_reads_its_mathml() { .flat_map(|p| &p.inlines) .filter_map(|i| if let Inline::Equation { linear, display } = i { Some((linear.clone(), *display)) } else { None }) .collect(); - assert_eq!(eqs, vec![("E=mc^2".to_string(), true)]); + // The base is `mc` (`mc…`), so its linear form groups it, as Word writes it (#6228). + assert_eq!(eqs, vec![("E=〖mc〗^2".to_string(), true)]); } #[test]