DOCX: read the placeholder chart title from its text, leave the frame unfilled

Take the title of an Office 2016 chart from its rich text (all runs) or its text data, and draw the placeholder frame without a fill so it does not become a white box on a coloured page. The run-fallback test is a guard and says so.

Part of storytold/wordcraft#6820

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
vangel76andClaude Sonnet 5.5 committed 2026-11-16 02:18:25 +01:00
1 parent e76bf4bad4
commit 5e3632533f
2 files changed
+11 -12

No files matched your search

+3 -6
View File
@@ -150,15 +150,12 @@ pub(crate) fn chartex_items(space: Option<&El>, w: f32, h: f32) -> Vec<GraphicIt
if !(w.is_finite() && h.is_finite() && w > 0.0 && h > 0.0) {
return out;
}
out.push(GraphicItem::Shape { rect: [0.0, 0.0, w, h], kind: ShapeKind::Rectangle, fill: Some(Rgb::WHITE), stroke: Some(AXIS), stroke_width: 0.75 });
out.push(GraphicItem::Shape { rect: [0.0, 0.0, w, h], kind: ShapeKind::Rectangle, fill: None, stroke: Some(AXIS), stroke_width: 0.75 });
let mut area = Rect { x: PAD, y: PAD, w: (w - 2.0 * PAD).max(1.0), h: (h - 2.0 * PAD).max(1.0) };
let chart = space.and_then(|s| s.child("cx:chart"));
let title = chart.and_then(|c| c.child("cx:title")).and_then(|t| {
let mut s = String::new();
t.find_all("a:t").for_each(|r| s.push_str(&r.text()));
if s.is_empty() {
s = t.find("cx:v").map(|v| v.text().into_owned()).unwrap_or_default();
}
// Rich text keeps its runs together ("Sales " + "funnel"); a text-data title is one value.
let s = t.find("cx:rich").or_else(|| t.find("cx:v")).map(|e| e.deep_text()).unwrap_or_default();
let s = label(s.trim());
(!s.is_empty()).then_some(s)
});
+8 -6
View File
@@ -1893,7 +1893,10 @@ fn office_2016_chart_with_a_text_fallback_keeps_its_size_through_saves() {
assert_eq!((*w, *h), (432.0, 252.0));
assert_eq!((graphic.w, graphic.h), (432.0, 252.0));
assert!(
graphic.items.iter().any(|i| matches!(i, craft_wordmodel::graphic::GraphicItem::Text { text, .. } if text.starts_with("Waterfall chart"))),
graphic
.items
.iter()
.any(|i| matches!(i, craft_wordmodel::graphic::GraphicItem::Text { text, .. } if text.starts_with("Waterfall chart"))),
"{:?}",
graphic.items
);
@@ -1911,13 +1914,12 @@ fn office_2016_chart_with_a_text_fallback_keeps_its_size_through_saves() {
}
}
/// A fallback that holds a run (not run content) can't be written back inside the object's own
/// run, so it is read as before: its text, no chart object.
/// Guard, not fix proof (it passes before the change too): a fallback that holds a run can't be
/// written back inside the object's own run, so it is not turned into a chart placeholder.
#[test]
fn office_2016_chart_with_a_run_fallback_is_read_as_before() {
fn office_2016_chart_with_a_run_fallback_is_not_made_a_placeholder() {
let d = craft_docx::read(&chartex_text_fallback_docx("<w:r><w:t>Chart not available.</w:t></w:r>")).unwrap();
assert!(graphics(&d).is_empty());
assert!(paras(&d).iter().any(|p| p.text.contains("Chart not available.")));
assert!(graphics(&d).is_empty(), "{:?}", graphics(&d));
}
#[test]