diff --git a/crates/xlsx/src/drawing.rs b/crates/xlsx/src/drawing.rs index 3d14c109e..796ba4035 100644 --- a/crates/xlsx/src/drawing.rs +++ b/crates/xlsx/src/drawing.rs @@ -257,7 +257,7 @@ fn read_shape(obj: &El, anchor: Anchor) -> Option { (false, _) => None, }; if let Some(text) = source { - let ink = equation_ink(obj.child("txBody"), &text, described_source(descr).is_some()); + let ink = equation_ink(tb, &text, described_source(descr).is_some(), has_math); return Some(Shape { id: 0, kind: ShapeKind::Equation, @@ -328,17 +328,21 @@ fn described_source(descr: &str) -> Option<(&str, &str)> { } /// The colour an equation's frame carries (its "ink": the colour of everything the source does -/// not colour itself). GridCraft saves it on the paragraph's `a:endParaRPr`. Another writer's -/// Office Math has none: its first run's colour stands in. The runs of GridCraft's own equation -/// cannot stand in when the source colours some of them (`{\color{green}a}+d` would read back -/// green), so a source with a colour command and no saved ink reads as Automatic. -fn equation_ink(tb: Option<&El>, source: &str, own: bool) -> Color { +/// not colour itself). GridCraft saves it on its own Office Math paragraph's `a:endParaRPr`; +/// another writer's paragraph-end formatting is not that convention, so its first run's colour +/// stands in, as do the runs of a body without Office Math (one colour throughout: the plain-text +/// box of an equation outside the subset). The runs of GridCraft's own Office Math cannot stand +/// in when the source colours some of them (`{\color{green}a}+d` would read back green), so such +/// a source with no saved ink reads as Automatic. +fn equation_ink(tb: Option<&El>, source: &str, own: bool, has_math: bool) -> Color { let Some(tb) = tb else { return Color::default() }; - if let Some(c) = tb.kids("p").find_map(|p| p.child("endParaRPr")).and_then(|e| e.child("solidFill")).and_then(dml_color) { - return c; - } - if own && (source.contains("\\color") || source.contains("\\textcolor")) { - return Color::default(); + if own && has_math { + if let Some(c) = tb.kids("p").find_map(|p| p.child("endParaRPr")).and_then(|e| e.child("solidFill")).and_then(dml_color) { + return c; + } + if source.contains("\\color") || source.contains("\\textcolor") { + return Color::default(); + } } first_run_color(tb, 0).unwrap_or_default() } diff --git a/crates/xlsx/src/tests/equation_tests.rs b/crates/xlsx/src/tests/equation_tests.rs index 311b8440a..9dd1ff1a6 100644 --- a/crates/xlsx/src/tests/equation_tests.rs +++ b/crates/xlsx/src/tests/equation_tests.rs @@ -512,3 +512,43 @@ fn a_coloured_run_in_gridcraft_s_own_equation_is_not_the_frames_ink() { assert_eq!(first_run_green("a+b", Color::Auto, true).line, Color::Rgb(0x008000)); assert_eq!(first_run_green("a+b", Color::Auto, false).line, Color::Rgb(0x008000)); } + +#[test] +fn another_writer_s_paragraph_end_colour_is_not_the_frames_ink() { + // Another writer's Office Math with a green first run and a blue paragraph end: the blue + // `a:endParaRPr` is that writer's formatting, not GridCraft's saved ink, so the first run's + // colour stays the frame's ink as before. + let s = first_run_green("a+b", Color::Rgb(0x0000FF), false); + assert_eq!(s.line, Color::Rgb(0x008000)); +} + +#[test] +fn a_coloured_equation_without_office_math_keeps_its_frame_ink() { + // `\begin{CD}` is outside the Office Math subset, so the equation is saved as the plain-text + // box alone, whose runs all carry the frame's ink; the colour command in the source does not + // make that ink Automatic on reopening. + let src = "{\\color{red}A} \\begin{CD} A @>f>> B \\end{CD}"; + for ink in [Color::Rgb(0x0000FF), Color::Auto] { + let bytes = write_xlsx(&book(vec![equation(1, "B2", src, ink)])).unwrap(); + assert!(!drawing(&unzip(&bytes)).contains(""), "written without Office Math"); + let s = shapes(&read_xlsx(&bytes).unwrap().0); + assert_eq!((s.len(), s[0].kind, s[0].text.as_str(), s[0].line), (1, ShapeKind::Equation, src, ink)); + } +} + +#[test] +fn nested_colours_keep_the_frame_ink_through_save_and_reopen() { + // The combined round trip: the uncoloured parts (`b`, `+d`) take the frame's ink, so saving + // and reopening, twice, must leave that ink as it was rather than the source's green. + let src = "{\\color{green}a+{\\color{black}b}+c}+d"; + for ink in [Color::Auto, Color::Rgb(0x1F4E79)] { + let mut wb = book(vec![equation(1, "B2", src, ink)]); + for _ in 0..2 { + let bytes = write_xlsx(&wb).unwrap(); + assert!(drawing(&unzip(&bytes)).contains(""), "written as Office Math"); + wb = read_xlsx(&bytes).unwrap().0; + let s = shapes(&wb); + assert_eq!((s.len(), s[0].kind, s[0].text.as_str(), s[0].line), (1, ShapeKind::Equation, src, ink)); + } + } +}