From afd7303e48f5d32ea4689418a8ac84b7d288e5ed Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sat, 14 Nov 2026 12:56:37 +0100 Subject: [PATCH] Draw the equation plain-text stand-in at a normal size in xlsx The text box that holds the LaTeX source (for an equation Office Math cannot hold, and as the fallback of one it can) was written at the size the typeset equation is drawn at, so the much wider source overflowed its frame in Excel. It now uses 11 pt, or less when the frame itself is smaller. Fixes #4161 Co-Authored-By: Claude Sonnet 5.5 --- crates/xlsx/src/drawing.rs | 9 ++++++++- crates/xlsx/src/tests/equation_tests.rs | 15 +++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/crates/xlsx/src/drawing.rs b/crates/xlsx/src/drawing.rs index 6ef49f175..6b4d538bc 100644 --- a/crates/xlsx/src/drawing.rs +++ b/crates/xlsx/src/drawing.rs @@ -342,6 +342,9 @@ fn first_run_color(e: &El, depth: usize) -> Option { // ---------------------------------------------------------------- writing +/// The size, in points, of the plain-text box that stands in for an equation. +const PLAIN_TEXT_PT: f32 = 11.0; + /// An equation shape (inside the anchor): Office Math with a plain-text fallback when the source /// is in the subset OMML covers, else the plain-text box alone. Both carry the source. fn write_equation(s: &mut String, sh: &Shape, id: usize, theme: &Theme) { @@ -376,7 +379,11 @@ fn write_equation(s: &mut String, sh: &Shape, id: usize, theme: &Theme) { xfrm(&sh.anchor, "a:xfrm") ) }; - let plain = plain_paragraphs(&sh.text, &format!("{fill}")); + // The plain text is the LaTeX source, much wider than the typeset equation: at the equation's + // size it would overflow the frame in Excel, so it uses a normal text size (11 pt, Excel's + // default for a text box), smaller only when the frame itself is. + let plain_sz = (size_pt.min(PLAIN_TEXT_PT) * 100.0).round() as u32; + let plain = plain_paragraphs(&sh.text, &format!("{fill}")); match gridcraft_math::to_omml(&sh.text, size_pt, sh.line.resolve(theme)) { Ok(omml) => { let math = format!( diff --git a/crates/xlsx/src/tests/equation_tests.rs b/crates/xlsx/src/tests/equation_tests.rs index 998364465..53867c125 100644 --- a/crates/xlsx/src/tests/equation_tests.rs +++ b/crates/xlsx/src/tests/equation_tests.rs @@ -469,3 +469,18 @@ fn a_paragraph_split_in_an_equation_without_office_math_reads_back_as_the_edit() // back is the source with a newline after that space, and nothing else moved or dropped. assert_eq!(s[0].text, cd.replacen(' ', " \n", 1), "the split text, exactly"); } + +#[test] +fn the_plain_text_stand_in_for_an_equation_is_drawn_at_a_normal_size() { + // A source Office Math cannot hold is saved as the text box alone; a source it can hold has + // the same box as its fallback. Neither takes the (large) size the equation is drawn at. + let wb = book(vec![equation(1, "B2", "\\begin{CD} A @>f>> B \\end{CD}", Color::Auto), equation(2, "B8", "x^2", Color::Auto)]); + let d = drawing(&unzip(&write_xlsx(&wb).unwrap())); + let text_runs: Vec<&str> = d.match_indices("").unwrap()]).collect(); + assert_eq!(text_runs.len(), 2, "{d}"); + for r in text_runs { + assert!(r.contains(" sz=\"1100\""), "{r}"); + } + // The Office Math box keeps the size of the equation. + assert!(!d.contains(""), "{d}"); +}