From 7cd04cd0233263f8fc3708abfe83c78e89e417fe Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sat, 14 Nov 2026 13:01:06 +0100 Subject: [PATCH] Keep the frame colour of an equation apart from its coloured runs in xlsx An equation saved with colour commands has coloured runs; reading the file took the first run's colour as the frame's ink, so the parts the source leaves uncoloured turned that colour and the whole equation was painted in one colour. The writer now saves the ink on the paragraph, and the reader of GridCraft's own equations reads it from there. Fixes #4162 Co-Authored-By: Claude Sonnet 5.5 --- crates/math/src/lib.rs | 10 ++++++ crates/xlsx/src/drawing.rs | 20 ++++++++++-- crates/xlsx/src/tests/equation_tests.rs | 43 +++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 2 deletions(-) diff --git a/crates/math/src/lib.rs b/crates/math/src/lib.rs index 408afc4135..c3083364da 100644 --- a/crates/math/src/lib.rs +++ b/crates/math/src/lib.rs @@ -637,6 +637,16 @@ mod tests { assert_eq!(c, vec![Some([255, 0, 0]), None]); } + #[test] + fn nested_colours_are_kept_per_glyph() { + // Green a, + and c; black b and the plain +d are left to the equation's own colour. + let l = layout("{\\color{green}a+{\\color{black}b}+c}+d").unwrap(); + let g = Some([0, 128, 0]); + let c: Vec<(char, Option<[u8; 3]>)> = + l.items.iter().filter_map(|i| if let Item::Glyph(g) = i { Some((g.ch, g.color)) } else { None }).collect(); + assert_eq!(c, vec![('a', g), ('+', g), ('b', None), ('+', g), ('c', g), ('+', None), ('d', None)]); + } + #[test] fn errors_point_at_the_source() { let e = layout("a + \\frac{1}{").unwrap_err(); diff --git a/crates/xlsx/src/drawing.rs b/crates/xlsx/src/drawing.rs index 6ef49f1757..3d14c109e2 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 = obj.child("txBody").and_then(|tb| first_run_color(tb, 0)).unwrap_or_default(); + let ink = equation_ink(obj.child("txBody"), &text, described_source(descr).is_some()); return Some(Shape { id: 0, kind: ShapeKind::Equation, @@ -327,6 +327,22 @@ fn described_source(descr: &str) -> Option<(&str, &str)> { (rest.as_bytes().get(16) == Some(&b':') && marker.bytes().all(|b| b.is_ascii_hexdigit()) && !src.trim().is_empty()).then_some((marker, src)) } +/// 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 { + 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(); + } + first_run_color(tb, 0).unwrap_or_default() +} + /// The colour of the first run (DrawingML `rPr` with a fill) under `e`. fn first_run_color(e: &El, depth: usize) -> Option { if depth > 12 { @@ -380,7 +396,7 @@ fn write_equation(s: &mut String, sh: &Shape, id: usize, theme: &Theme) { match gridcraft_math::to_omml(&sh.text, size_pt, sh.line.resolve(theme)) { Ok(omml) => { let math = format!( - "{omml}" + "{omml}{fill}" ); let _ = write!( s, diff --git a/crates/xlsx/src/tests/equation_tests.rs b/crates/xlsx/src/tests/equation_tests.rs index 998364465e..311b8440a8 100644 --- a/crates/xlsx/src/tests/equation_tests.rs +++ b/crates/xlsx/src/tests/equation_tests.rs @@ -469,3 +469,46 @@ 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"); } + +/// The saved Office Math of `src` with its first run coloured green (as a coloured source is +/// written when the converter carries `\color`), read back; `keep_descr` keeps GridCraft's own +/// source in the description, else the equation is another writer's Office Math. +fn first_run_green(src: &str, ink: Color, keep_descr: bool) -> Shape { + let wb = book(vec![equation(1, "B2", src, ink)]); + let mut parts = unzip(&write_xlsx(&wb).unwrap()); + let name = parts.keys().find(|k| k.starts_with("xl/drawings/drawing")).unwrap().clone(); + let mut d = drawing(&parts); + let at = d.find("').unwrap() + at + 1; + let green = ""; + match d[close..].starts_with("") { + true => { + let end = d[close..].find("").unwrap() + close + "".len(); + d.replace_range(close..end, green); + } + false => d.insert_str(close, green), + } + if !keep_descr { + let a = d.find(" descr=\"gridcraft:latex:").unwrap(); + let b = d[a + 8..].find('"').unwrap() + a + 9; + d.replace_range(a..b, ""); + } + parts.insert(name, d.into_bytes()); + let s = shapes(&read_xlsx(&rezip(&parts)).unwrap().0); + assert_eq!(s.len(), 1); + assert_eq!(s[0].kind, ShapeKind::Equation); + s[0].clone() +} + +#[test] +fn a_coloured_run_in_gridcraft_s_own_equation_is_not_the_frames_ink() { + // `b` and `+d` have no colour of their own: they take the frame's ink, not the green of the + // first run, which would paint the whole equation green after reopening the file. + let src = "{\\color{green}a+{\\color{black}b}+c}+d"; + assert_eq!(first_run_green(src, Color::Auto, true).line, Color::Auto); + assert_eq!(first_run_green(src, Color::Rgb(0x1F4E79), true).line, Color::Rgb(0x1F4E79)); + // A source without a colour command keeps reading the run colour (files saved before the + // paragraph carried the ink), and another writer's Office Math too. + 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)); +}