Merge pull request #4165 from vangel76/fix/4161-equation-fallback-font

Equation fallback text in xlsx is drawn at a normal font size
This commit is contained in:
Dylan M. Taylor authored and GitHub committed 2026-11-16 20:32:51 -05:00
commit d0f20a31da
2 files changed
+23 -1

No files matched your search

+8 -1
View File
@@ -342,6 +342,9 @@ fn first_run_color(e: &El, depth: usize) -> Option<Color> {
// ---------------------------------------------------------------- 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!("<a:rPr lang=\"en-US\" sz=\"{sz}\">{fill}</a:rPr>"));
// 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!("<a:rPr lang=\"en-US\" sz=\"{plain_sz}\">{fill}</a:rPr>"));
match gridcraft_math::to_omml(&sh.text, size_pt, sh.line.resolve(theme)) {
Ok(omml) => {
let math = format!(
+15
View File
@@ -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("<a:r><a:rPr").map(|(i, _)| &d[i..i + d[i..].find("</a:rPr>").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("<a:endParaRPr lang=\"en-US\" sz=\"1100\"/>"), "{d}");
}