Files
docx/docs/usage
Dolan d5afebe2e3 feat(layout): size tables given no widths with cells merged across columns, or tables in cells, as Word does (#3622)
* feat(layout): size tables given no widths with cells merged across columns, or tables in cells, as Word does

docx/layout stopped at a table given no widths with a cell merged across
columns or a table in a cell. Word's PDF of a new probe document,
word-probes.docx (U1), showed how Word sizes them:

- a cell merged across columns shares what its widest line, or its own
  width, needs beyond its columns' widest lines among them, in proportion
  to those, in the rows' order. A column given a width counts with that
  width, and a column with no cell of its own is 0 wide;
- a table in a cell counts as its columns added up, its own width in
  twips, or its cells' widths, with half of its side borders.

The columns are then fitted to the room as before. 19 of the 21 probe
tables, and word-autofit.docx's D1, are within 22 twips of Word's.

A long word in a merged cell is shared in a way the probes didn't settle,
so the layout stops at one wider than its columns' widest words, when the
table is narrowed, or wider than their widest lines.

The probe, its reader, the .docx Word opened and Word's PDF are in
scripts/layout-probes.

* fix(layout): stop at a table given no widths of more than 63 columns

Sizing a table to its text counts a column for each it says it has, so a
cell merged across billions of columns threw a RangeError out of the
layout, failing the whole document, where before this branch such a table
stopped. Word's tables have at most 63 columns, and how Word lays out one
with more isn't known, so the layout now stops there instead.

* fix(layout): count a table's columns row by row, so a table of many rows is read

The count spread every row's cell edges into one Math.max call, which
throws past about 120,000 arguments, so a table of 40,000 rows of 3 cells
couldn't be read at all, whatever its widths. Each row now keeps where its
last cell ends, and the count takes the largest of those.

* fix(layout): size a table given no widths of many rows without spreading its cells into Math.max

Sizing a table to its text took the largest of its cells' widths with
Math.max(...), which throws past about 120,000 arguments, so a table given
no widths of 40,000 rows of 3 cells failed the whole document. The widths
are now found with a reduce, here and for a table in a cell of many rows.
2026-10-01 22:53:05 +01:00
..