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 <noreply@anthropic.com>
This commit is contained in:
vangel76
2026-11-15 13:54:06 +01:00
co-authored by Claude Sonnet 5.5
parent 8b9f9527f1
commit fe07dbc654
2 changed files with 33 additions and 1 deletions
+5 -1
View File
@@ -249,15 +249,19 @@ fn read_shape(obj: &El, anchor: Anchor) -> Option<Shape> {
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,
+28
View File
@@ -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("<m:t>b</m:t>"), "the second operand is its own run: {d}");
d = d.replacen("<m:t>b</m:t>", "<m:t>c</m:t>", 1);
let at = d.find("<a:rPr lang=\"en-US\"").unwrap();
let close = d[at..].find('>').unwrap() + at + 1;
let green = "<a:solidFill><a:srgbClr val=\"008000\"/></a:solidFill>";
match d[close..].starts_with("<a:solidFill>") {
true => {
let end = d[close..].find("</a:solidFill>").unwrap() + close + "</a:solidFill>".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