From fe07dbc654ed4cccb70ff2623cb431eb16221e36 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sun, 15 Nov 2026 13:54:06 +0100 Subject: [PATCH] Treat an equation as GridCraft's own only while its marker matches When another application rewrote the Office Math, the source is rebuilt from it and the saved paragraph-end ink no longer applies: the first run's colour is the frame ink. Co-Authored-By: Claude Sonnet 5.5 --- crates/xlsx/src/drawing.rs | 6 +++++- crates/xlsx/src/tests/equation_tests.rs | 28 +++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/crates/xlsx/src/drawing.rs b/crates/xlsx/src/drawing.rs index 796ba4035..aa7d85026 100644 --- a/crates/xlsx/src/drawing.rs +++ b/crates/xlsx/src/drawing.rs @@ -249,15 +249,19 @@ fn read_shape(obj: &El, anchor: Anchor) -> Option { let descr = obj.path(&["nvSpPr", "cNvPr"]).and_then(|c| c.attr("descr")).unwrap_or(""); let tb = obj.child("txBody"); let has_math = tb.is_some_and(|tb| crate::omml::find_math(tb, 0).is_some()); + // The description's source is GridCraft's own only while its marker matches; the saved frame + // ink belongs to that body alone, not to Office Math another application rewrote. + let mut own = false; let source = match (has_math, described_source(descr)) { (_, Some((marker, src))) if tb.is_some_and(|tb| crate::omml::body_marker(tb) == marker && (has_math || holds_written_text(tb, src))) => { + own = true; Some(src.to_string()) } (true, _) => tb.and_then(crate::omml::equation_source), (false, _) => None, }; if let Some(text) = source { - let ink = equation_ink(tb, &text, described_source(descr).is_some(), has_math); + let ink = equation_ink(tb, &text, own, has_math); return Some(Shape { id: 0, kind: ShapeKind::Equation, diff --git a/crates/xlsx/src/tests/equation_tests.rs b/crates/xlsx/src/tests/equation_tests.rs index 9dd1ff1a6..253920729 100644 --- a/crates/xlsx/src/tests/equation_tests.rs +++ b/crates/xlsx/src/tests/equation_tests.rs @@ -522,6 +522,34 @@ fn another_writer_s_paragraph_end_colour_is_not_the_frames_ink() { assert_eq!(s.line, Color::Rgb(0x008000)); } +#[test] +fn an_equation_whose_office_math_was_rewritten_reads_its_first_run_as_the_frames_ink() { + // GridCraft's description is still there, but another application changed the Office Math + // (`b` became `c`), so the marker no longer matches and the equation is rebuilt from the + // Office Math. The blue paragraph end GridCraft saved is not its ink any more: the green + // first run stands in, and the next save writes that back instead of the stale blue. + let wb = book(vec![equation(1, "B2", "a+b", Color::Rgb(0x0000FF))]); + 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); + assert!(d.contains("b"), "the second operand is its own run: {d}"); + d = d.replacen("b", "c", 1); + 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), + } + parts.insert(name, d.into_bytes()); + let s = shapes(&read_xlsx(&rezip(&parts)).unwrap().0); + assert_eq!((s.len(), s[0].kind, s[0].text.as_str()), (1, ShapeKind::Equation, "a+c"), "rebuilt from the Office Math"); + assert_eq!(s[0].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