From a5a8829a14e0fc51c3ead29d0fbb4d7c007a6351 Mon Sep 17 00:00:00 2001 From: kiwi4boy Date: Thu, 12 Nov 2026 22:02:35 -0500 Subject: [PATCH] craft-math: no m:sty or m:scr on normal text in an equation (port of wordcraft#6082) CT_RPR holds m:nor or m:scr/m:sty, not both, and Word keeps only the m:nor of such a run, so bold normal text lost its bold there. Normal text now writes neither; its bold and italic go in w:rPr (w:b/w:i), which the reader already reads back for normal text. Ported from storytold/wordcraft#6082 (issue wordcraft#6073). The writer change moves from WordCraft's docx/src/write/math.rs to omml/write.rs, where the OMML writer now lives. Its test saves and reopens a .docx, so it goes into craft-docx's integration tests (tests/it/math_normal_text.rs) with only the crate names changed. --- crates/docx/tests/it/main.rs | 1 + crates/docx/tests/it/math_normal_text.rs | 66 ++++++++++++++++++++++++ crates/math/src/omml/write.rs | 13 +++++ 3 files changed, 80 insertions(+) create mode 100644 crates/docx/tests/it/math_normal_text.rs diff --git a/crates/docx/tests/it/main.rs b/crates/docx/tests/it/main.rs index b0bab58331..726010a7ac 100644 --- a/crates/docx/tests/it/main.rs +++ b/crates/docx/tests/it/main.rs @@ -24,6 +24,7 @@ mod legacy_forms; mod ligatures; mod line_numbering; mod malformed; +mod math_normal_text; mod missing_pictures; mod nested_block_controls; mod note_comments; diff --git a/crates/docx/tests/it/math_normal_text.rs b/crates/docx/tests/it/math_normal_text.rs new file mode 100644 index 0000000000..6bb0867527 --- /dev/null +++ b/crates/docx/tests/it/math_normal_text.rs @@ -0,0 +1,66 @@ +//! Normal text in an equation (`m:nor`) is saved without `m:scr`/`m:sty` (#6073). In the OMML +//! schema `CT_RPR` holds `m:nor` or `m:scr`/`m:sty`, not both, and Word keeps only the `m:nor` of +//! such a run, so the bold or italic of normal text goes in `w:rPr` (`w:b`/`w:i`), which the +//! reader takes back for normal text. + +use craft_math::math::{MNode, MRun, MScr, MSty, Math}; +use craft_wordmodel::InlineObject; +use craft_wordmodel::props::CharProps; +use craft_wordmodel::{Document, Paragraph, para_block}; + +/// Save an equation holding one normal-text run; the run's `m:r` XML and the reopened run. +fn save(run: MRun) -> (String, MRun) { + let math = Math { nodes: vec![MNode::Run(run)], ..Default::default() }; + let mut p = Paragraph::new(); + p.insert_object(0, InlineObject::Equation { linear: String::new(), display: false, math }, &CharProps::default()).unwrap(); + let mut doc = Document::new(); + doc.body = vec![para_block(p)]; + let bytes = craft_docx::write(&doc).unwrap(); + let mut zip = zip::ZipArchive::new(std::io::Cursor::new(&bytes)).unwrap(); + let mut xml = String::new(); + std::io::Read::read_to_string(&mut zip.by_name("word/document.xml").unwrap(), &mut xml).unwrap(); + let start = xml.find("").unwrap(); + let end = start + xml[start..].find("").unwrap() + "".len(); + let back = craft_docx::read(&bytes).unwrap(); + let math = back.body.iter().filter_map(|b| b.as_para()).flat_map(|p| p.objects.iter()).find_map(|o| match o { + InlineObject::Equation { math, .. } => Some(math.clone()), + _ => None, + }); + let Some(MNode::Run(r)) = math.unwrap().nodes.into_iter().next() else { panic!("no run") }; + (xml[start..end].to_string(), r) +} + +fn normal(sty: Option) -> MRun { + MRun { nor: true, sty, font: Some("Arial".into()), ..MRun::new("normal text") } +} + +#[test] +fn normal_text_has_no_math_style() { + for sty in [None, Some(MSty::Plain), Some(MSty::Bold), Some(MSty::Italic), Some(MSty::BoldItalic)] { + let (run, _) = save(normal(sty)); + assert!(run.contains(""), "{sty:?}: {run}"); + assert!(!run.contains("m:sty") && !run.contains("m:scr"), "{sty:?}: {run}"); + } + // A script style isn't written for normal text either. + let (run, _) = save(MRun { scr: MScr::Fraktur, ..normal(None) }); + assert!(!run.contains("m:scr"), "{run}"); +} + +#[test] +fn bold_and_italic_normal_text_survive_a_save() { + for (sty, b, i) in [(MSty::Bold, true, false), (MSty::Italic, false, true), (MSty::BoldItalic, true, true)] { + let (run, back) = save(normal(Some(sty))); + assert_eq!((run.contains(""), run.contains("")), (b, i), "{sty:?}: {run}"); + assert_eq!(back, normal(Some(sty)), "{sty:?}"); + } +} + +#[test] +fn math_runs_keep_their_style() { + for sty in [MSty::Plain, MSty::Bold, MSty::Italic, MSty::BoldItalic] { + let math = MRun { sty: Some(sty), ..MRun::new("x") }; + let (run, back) = save(math.clone()); + assert!(run.contains("m:sty") && !run.contains(""), "{sty:?}: {run}"); + assert_eq!(back, math); + } +} diff --git a/crates/math/src/omml/write.rs b/crates/math/src/omml/write.rs index 9f09aab676..046c4499ad 100644 --- a/crates/math/src/omml/write.rs +++ b/crates/math/src/omml/write.rs @@ -434,7 +434,11 @@ fn run(w: &mut W, r: &MRun) { MSty::Italic => "i", MSty::BoldItalic => "bi", }); + // `CT_RPR` holds `m:nor` or `m:scr`/`m:sty`, not both, and Word drops the `m:sty` of a normal-text + // run: its bold and italic go in `w:rPr` below, where the reader takes them back (#6073). + let sty = sty.filter(|_| !r.nor); let scr = match r.scr { + _ if r.nor => None, MScr::Roman => None, MScr::Script => Some("script"), MScr::Fraktur => Some("fraktur"), @@ -466,6 +470,15 @@ fn run(w: &mut W, r: &MRun) { w.open("w:rPr", &[]); let font = r.font.as_deref().filter(|_| r.nor).unwrap_or("Cambria Math"); w.empty("w:rFonts", &[("w:ascii", font), ("w:hAnsi", font)]); + // Normal text's bold and italic (see `sty` above). + if r.nor { + if matches!(r.sty, Some(MSty::Bold | MSty::BoldItalic)) { + w.empty("w:b", &[]); + } + if matches!(r.sty, Some(MSty::Italic | MSty::BoldItalic)) { + w.empty("w:i", &[]); + } + } if let Some(c) = r.color { w.val("w:color", &c.hex()); }