Files
craft-libs/crates/math/tests/linear_multichar_base.rs
T
kiwi4boy cc63fb5074 craft-math: a multi-letter script base survives linear text (port of wordcraft#6230)
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 (<msup><mi>mc</mi>…) 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.
2026-11-12 21:59:46 -05:00

40 lines
1.6 KiB
Rust

//! 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<MNode> {
vec![MNode::Run(MRun::new(t))]
}
fn sup(base: Vec<MNode>, s: Vec<MNode>) -> 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");
}