mirror of
https://github.com/storytold/gridcraft.git
synced 2026-11-28 17:30:24 +01:00
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 <noreply@anthropic.com>
This commit is contained in:
1 parent
c0ee120ac2
commit
7cd04cd023
3 files changed
+71
-2
No files matched your search
@@ -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();
|
||||
|
||||
@@ -257,7 +257,7 @@ fn read_shape(obj: &El, anchor: Anchor) -> Option<Shape> {
|
||||
(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<Color> {
|
||||
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!(
|
||||
"<a:p><a:pPr algn=\"ctr\"/><a14:m><m:oMathPara xmlns:m=\"http://schemas.openxmlformats.org/officeDocument/2006/math\"><m:oMathParaPr><m:jc m:val=\"centerGroup\"/></m:oMathParaPr>{omml}</m:oMathPara></a14:m><a:endParaRPr lang=\"en-US\" sz=\"{sz}\"/></a:p>"
|
||||
"<a:p><a:pPr algn=\"ctr\"/><a14:m><m:oMathPara xmlns:m=\"http://schemas.openxmlformats.org/officeDocument/2006/math\"><m:oMathParaPr><m:jc m:val=\"centerGroup\"/></m:oMathParaPr>{omml}</m:oMathPara></a14:m><a:endParaRPr lang=\"en-US\" sz=\"{sz}\">{fill}</a:endParaRPr></a:p>"
|
||||
);
|
||||
let _ = write!(
|
||||
s,
|
||||
|
||||
@@ -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("<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),
|
||||
}
|
||||
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));
|
||||
}
|
||||
Reference in new issue
Block a user