diff --git a/crates/flow/src/to_doc.rs b/crates/flow/src/to_doc.rs
index ce29a4448..0375c448c 100644
--- a/crates/flow/src/to_doc.rs
+++ b/crates/flow/src/to_doc.rs
@@ -841,7 +841,8 @@ impl<'a> Builder<'a> {
tb.props.style = None;
}
if t.widths.len() == cols && t.widths.iter().all(|w| w.is_finite() && *w > 1.0) {
- tb.grid = t.widths.iter().map(|w| w.clamp(6.0, 1584.0)).collect();
+ // Narrow columns stay as they are: raising them would widen the table past its source.
+ tb.grid = t.widths.iter().map(|w| w.min(1584.0)).collect();
}
tb.rows.clear();
if let Some(x) = &t.extra {
diff --git a/crates/odf/src/text/tests.rs b/crates/odf/src/text/tests.rs
index 8d7af18dc..da00b350c 100644
--- a/crates/odf/src/text/tests.rs
+++ b/crates/odf/src/text/tests.rs
@@ -442,6 +442,41 @@ fn table_column_widths_survive_a_round_trip() {
assert_eq!(grid(&back), widths);
}
+#[test]
+fn narrow_table_columns_keep_their_width() {
+ // Columns of a few points stay as the file gives them, so the table stays as wide as its source.
+ let content = format!(
+ r##"AB"##
+ );
+ let doc = import(&package(&content, None)).unwrap();
+ let grid_of = |doc: &craft_wordmodel::Document| {
+ doc.body.iter().find_map(|b| if let craft_wordmodel::Block::Table(t) = &**b { Some(t.grid.clone()) } else { None })
+ };
+ assert_eq!(grid_of(&doc), Some(vec![3.0, 100.0]));
+ // Saved and opened again, the narrow column is still the file's width.
+ let back = import(&export(&doc).unwrap()).unwrap();
+ let grid = grid_of(&back).unwrap();
+ assert_eq!(grid.len(), 2, "{grid:?}");
+ assert!((grid[0] - 3.0).abs() < 0.01 && (grid[1] - 100.0).abs() < 0.01, "{grid:?}");
+}
+
+#[test]
+fn table_columns_of_a_point_or_less_give_equal_columns() {
+ // A column of a point or less, nothing or NaN (a damaged or hostile file) is no width to
+ // lay out: the whole grid falls back to equal columns, and nothing reaches layout below a
+ // point, negative or not a number.
+ for bad in ["1pt", "0.5pt", "0pt", "-3pt", "NaNpt"] {
+ let content = format!(
+ r##"AB"##
+ );
+ let doc = import(&package(&content, None)).unwrap();
+ let grid = doc.body.iter().find_map(|b| if let craft_wordmodel::Block::Table(t) = &**b { Some(t.grid.clone()) } else { None }).unwrap();
+ assert_eq!(grid.len(), 2, "{bad}: {grid:?}");
+ assert!(grid.iter().all(|w| w.is_finite() && *w > 1.0), "{bad}: {grid:?}");
+ assert_eq!(grid[0], grid[1], "{bad}: equal columns, {grid:?}");
+ }
+}
+
#[test]
fn table_without_column_widths_keeps_the_equal_split() {
let content = format!(
diff --git a/crates/rtf/src/doc/parse.rs b/crates/rtf/src/doc/parse.rs
index f44d666ce..01978da1e 100644
--- a/crates/rtf/src/doc/parse.rs
+++ b/crates/rtf/src/doc/parse.rs
@@ -267,10 +267,6 @@ fn finalize(mut r: Reader) -> Parsed {
/// Most grid columns a table gets (the document model's own cap).
const MAX_GRID_COLS: usize = 63;
-/// Grid columns under this width, in twips (6 pt), merge while the rows leave room: import widens
-/// any narrower column to 6 pt, which grows the table, and files exported from spreadsheets put
-/// edges a few twips apart.
-const MIN_GRID_GAP: i64 = 120;
/// Grid columns of this width or less, in twips (1 pt), always merge: the document model refuses
/// them, and would then give the table equal columns.
const NO_GRID_GAP: i64 = 20;
@@ -282,9 +278,9 @@ const NO_GRID_GAP: i64 = 20;
///
/// One odd row never costs the whole table its grid (a form exported from a spreadsheet, with
/// many narrow columns, otherwise had its wide title squeezed into one of four equal columns):
-/// - Over [`MAX_GRID_COLS`] edges, or edges closer than [`MIN_GRID_GAP`], merge, leaving a column
-/// for each cell of the widest row ([`coarse_edges`]); each cell edge moves to the nearest
-/// grid edge.
+/// - Over [`MAX_GRID_COLS`] edges, or edges [`NO_GRID_GAP`] or less apart, merge
+/// ([`coarse_edges`]); each cell edge moves to the nearest grid edge. Narrow columns over a
+/// point stay: import keeps them as narrow as the file gives them.
/// - A cell that ends at or before the previous one (a zero-width cell, or a cell with no
/// `\cellx` of its own) still gets a grid column of its own; cells pushed past the table's
/// right edge move back.
@@ -298,7 +294,7 @@ pub(super) fn column_grid(rows: &mut [Vec], rights: &[Vec]) -> Vec> = Vec::with_capacity(rows.len());
for (row, x) in rows.iter().zip(rights) {
@@ -339,10 +335,9 @@ pub(super) fn column_grid(rows: &mut [Vec], rights: &[Vec]) -> Vec Vec {
+/// [`MAX_GRID_COLS`] columns, or columns of [`NO_GRID_GAP`] or less. The table's right edge is
+/// always kept.
+fn coarse_edges(edges: &[i32]) -> Vec {
let n = edges.len();
// Each edge's neighbours among the edges still kept.
let mut left: Vec