From b0ce7bcc8f114b89b5f96bc2fbd5d3e4b2fd3604 Mon Sep 17 00:00:00 2001 From: Daniele Mori Date: Wed, 18 Nov 2026 12:13:12 +0100 Subject: [PATCH] craft-docx: gridless tables keep an empty grid's inch, the narrowest column before spans, and cut spans' rest Review fixes: a file with an empty w:tblGrid keeps the inch per column it had; a cell narrower than the narrowest column counts as that column before a span shares out what it lacks, so the span keeps its width; a span cut off at the last column the reader keeps sizes its kept columns by their share and gives the rest to no other column. --- crates/docx/src/read/story/blocks.rs | 13 +++++++++++-- crates/docx/tests/it/fixtures.rs | 18 ++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/crates/docx/src/read/story/blocks.rs b/crates/docx/src/read/story/blocks.rs index 0d43a6060..0a723b7e2 100644 --- a/crates/docx/src/read/story/blocks.rs +++ b/crates/docx/src/read/story/blocks.rs @@ -416,7 +416,8 @@ impl Reader<'_> { return None; } if table.grid.is_empty() { - table.grid = grid_from_cells(&table); + // A file with an empty `w:tblGrid` keeps the inch per column it always had. + table.grid = if t.child("w:tblGrid").is_none() { grid_from_cells(&table) } else { vec![72.0; table.cols().clamp(1, MAX_COLS)] }; } // The cells' paragraphs, with the run formatting the table style gives each cell (a // table inside has marked its own). @@ -458,6 +459,8 @@ fn grid_from_cells(table: &Table) -> Vec { for cell in &row.cells { let span = cell.span(); if let Some(w) = cell.props.width.filter(|w| w.is_finite() && *w > 0.0) { + // The narrowest column counts before a span shares out what it lacks. + let w = w.max(MIN_GRID_COL); if span == 1 { if let Some(slot) = grid.get_mut(g) { *slot = Some(slot.map_or(w, |cur| cur.max(w))); @@ -471,7 +474,12 @@ fn grid_from_cells(table: &Table) -> Vec { } spanning.sort_by(|a: &(usize, usize, f32), b| (a.0 + a.1, a.0).cmp(&(b.0 + b.1, b.0)).then(a.2.total_cmp(&b.2))); for (g, span, w) in spanning { - let Some(cs) = grid.get_mut(g..(g + span).min(cols)) else { continue }; + let end = (g + span).min(cols); + let Some(cs) = grid.get_mut(g..end) else { continue }; + // A span cut off at the last column the reader keeps sizes only its kept columns, by + // their share of it, and has no last column to give the rest to. + let clipped = end < g + span; + let w = if clipped { w * cs.len() as f32 / span as f32 } else { w }; let known: f32 = cs.iter().flatten().sum(); let unknown = cs.iter().filter(|c| c.is_none()).count(); if unknown > 0 { @@ -482,6 +490,7 @@ fn grid_from_cells(table: &Table) -> Vec { } let lacking = w - cs.iter().flatten().sum::(); if lacking > 0.0 + && !clipped && let Some(Some(last)) = cs.last_mut() { *last += lacking; diff --git a/crates/docx/tests/it/fixtures.rs b/crates/docx/tests/it/fixtures.rs index 250b4cd5a..66c8cd475 100644 --- a/crates/docx/tests/it/fixtures.rs +++ b/crates/docx/tests/it/fixtures.rs @@ -684,6 +684,24 @@ fn spanning_cells_size_a_gridless_table_as_in_word() { ); let Block::Table(t) = &*d.body[0] else { panic!() }; assert_eq!(t.grid, vec![72.0, 72.0]); + // An empty `w:tblGrid` is a grid all the same: the columns keep their inch. + let d = + read_body(r#""#); + let Block::Table(t) = &*d.body[0] else { panic!() }; + assert_eq!(t.grid, vec![72.0]); + // The narrowest column counts before a span shares out what it lacks: the span keeps its width. + let d = read_body( + r#""#, + ); + let Block::Table(t) = &*d.body[0] else { panic!() }; + assert_eq!(t.grid[0], 1.3); + assert!((t.grid.iter().sum::() - 100.0).abs() < 0.01, "{:?}", t.grid); + // A span cut off at the last column kept gives the rest to no other column. + let ones = tc(20, 1).repeat(62); + let d = read_body(&format!("{ones}{}{ones}{}", tc(50, 1), tc(300, 2))); + let Block::Table(t) = &*d.body[0] else { panic!() }; + assert_eq!(t.grid.len(), 63); + assert_eq!(t.grid[62], 50.0); } #[test]