From 58fda7ee219a885cd89a52ab70a3bdd1c718aa8f Mon Sep 17 00:00:00 2001 From: Faabyy <68284158+Faabyy@users.noreply.github.com> Date: Wed, 21 Oct 2026 06:37:28 +0000 Subject: [PATCH] ODT: table column widths are read, so a resized column survives a save The importer ignored table:table-column and style:column-width and gave every table equal columns, so widths set in LibreOffice (or changed in WordCraft and exported) were lost on the next open. Co-Authored-By: Claude --- crates/formats/src/odt.rs | 86 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 83 insertions(+), 3 deletions(-) diff --git a/crates/formats/src/odt.rs b/crates/formats/src/odt.rs index 2df9d4519a..526655bbdf 100644 --- a/crates/formats/src/odt.rs +++ b/crates/formats/src/odt.rs @@ -1063,6 +1063,8 @@ struct StyleInfo { border_bottom: bool, /// `fo:background-color` of a table-cell style's `style:table-cell-properties`. cell_background: Option, + /// `style:column-width` of a table-column style's `style:table-column-properties`, in points. + column_width: Option, /// `style:master-page-name` of a paragraph or table style: its paragraphs (tables) start a /// page of that master page. Empty: explicitly none, which stops the lookup up the chain. master_page: Option, @@ -1264,6 +1266,13 @@ impl Styles { s.page_number = Some(v.trim().parse::().ok().map(|n| n.min(32_767))); } } + "table-column-properties" => { + if let Some((_, s)) = &mut cur + && let Some(w) = get(&a, "style:column-width").and_then(length) + { + s.column_width = Some(w); + } + } "table-cell-properties" => { if let Some((_, s)) = &mut cur && let Some(c) = get(&a, "fo:background-color").and_then(model::parse_color) @@ -1385,6 +1394,11 @@ impl Styles { self.chain(name).iter().find_map(|(_, s)| s.columns.clone()) } + /// Width in points of a table-column style (whole chain). + fn column_width(&self, name: &str) -> Option { + self.chain(name).iter().find_map(|(_, s)| s.column_width) + } + /// Background colour of a table-cell style (whole chain). fn cell_background(&self, name: &str) -> Option { self.chain(name).iter().find_map(|(_, s)| s.cell_background) @@ -1441,6 +1455,16 @@ struct TableB { row: Vec, /// Covered cells still owed to the previous cell's column span. hcover: usize, + /// Widths in points of the `table:table-column`s so far, 0 where the style gives none. + widths: Vec, +} + +impl TableB { + fn finish(self) -> FTable { + // A column without a width leaves the whole grid to the equal split. + let widths = if self.widths.iter().all(|w| *w > 0.0) { self.widths } else { Vec::new() }; + FTable { rows: self.rows, widths, borderless: false } + } } /// A `text:changed-region` read from `text:tracked-changes`. @@ -1903,7 +1927,15 @@ impl Body<'_> { self.skip = 1; return; } - self.tables.push(TableB { rows: Vec::new(), row: Vec::new(), hcover: 0 }); + self.tables.push(TableB { rows: Vec::new(), row: Vec::new(), hcover: 0, widths: Vec::new() }); + } + "table-column" => { + let w = get(a, "table:style-name").and_then(|n| self.styles.column_width(n)).unwrap_or(0.0); + let n = get(a, "table:number-columns-repeated").and_then(|v| v.trim().parse::().ok()).unwrap_or(1).max(1); + if let Some(t) = self.tables.last_mut() { + let room = wordcraft_doc::table::MAX_COLS.saturating_sub(t.widths.len()); + t.widths.extend(std::iter::repeat_n(w, n.min(room))); + } } "table-row" => { if let Some(t) = self.tables.last_mut() { @@ -2197,7 +2229,7 @@ impl Body<'_> { if let Some(t) = self.tables.pop() && !t.rows.is_empty() { - self.container().push(FBlock::Table(FTable { rows: t.rows, widths: Vec::new(), borderless: false })); + self.container().push(FBlock::Table(t.finish())); } } "desc" | "title" => self.in_desc = false, @@ -2417,7 +2449,7 @@ fn parse_tracked(bytes: &[u8]) -> Result<(Flow, SectionProps, Option, Opti let mut blocks = body.containers.pop().unwrap_or_default(); for t in body.tables.drain(..) { if !t.rows.is_empty() { - blocks.push(FBlock::Table(FTable { rows: t.rows, widths: Vec::new(), borderless: false })); + blocks.push(FBlock::Table(t.finish())); } } // An open section with nothing in it (after a column section at the end) isn't one: the @@ -2783,6 +2815,54 @@ mod tests { } } + #[test] + fn table_column_styles_are_the_grid_widths() { + // A column's `style:column-width` (and `table:number-columns-repeated`) sets its width. + let content = format!( + r##"ABC"## + ); + let f = parse(&package(&content, None)).unwrap(); + let Some(FBlock::Table(t)) = f.blocks.iter().find(|b| matches!(b, FBlock::Table(_))) else { panic!("no table") }; + assert_eq!(t.widths, vec![72.0, 144.0, 144.0]); + } + + #[test] + fn table_column_widths_survive_a_round_trip() { + // A width changed in the document is written and read back; the file's own widths are + // what the first import shows. + let content = format!( + r##"AB"## + ); + let grid = |doc: &Document| match doc + .body + .iter() + .find_map(|b| if let wordcraft_doc::Block::Table(t) = &**b { Some(t.grid.clone()) } else { None }) + { + Some(g) => g, + None => panic!("no table"), + }; + let mut doc = import(&package(&content, None)).unwrap(); + assert_eq!(grid(&doc), vec![72.0, 216.0]); + let widths = vec![150.0, 100.0]; + for b in doc.body.iter_mut() { + if let wordcraft_doc::Block::Table(t) = std::sync::Arc::make_mut(b) { + t.grid = widths.clone(); + } + } + let back = import(&export(&doc).unwrap()).unwrap(); + assert_eq!(grid(&back), widths); + } + + #[test] + fn table_without_column_widths_keeps_the_equal_split() { + let content = format!( + r##"AB"## + ); + let f = parse(&package(&content, None)).unwrap(); + let Some(FBlock::Table(t)) = f.blocks.iter().find(|b| matches!(b, FBlock::Table(_))) else { panic!("no table") }; + assert!(t.widths.is_empty()); + } + #[test] fn table_cell_style_background_is_shading() { // #437: `fo:background-color` on `style:table-cell-properties` is the cell's shading.