Commit Graph
324 Commits
Author SHA1 Message Date
Dolan 621bfbfee4 feat(layout): kern text and join letters into ligatures in the width tables' fonts and fonts' files, as Word does (#3661)
* feat(layout): kern text and join letters into ligatures in the width tables' fonts and fonts' files, as Word does

Word kerns text that asks for it and joins letters into ligatures, and its Normal template turns both on. The width
tables now have Word's kerning of every pair of the characters of Windows-1252, and the ligatures of each setting of
Word's Font dialog, in Calibri, Cambria, Arial, Times New Roman and Courier New, from Word's PDF of
scripts/layout-probes/word-kerning.docx, and fonts given as files join letters with their GSUB ligatures. Where Word's
PDFs don't show how it kerns or joins text, the layout stops: across runs measured together, beside a soft hyphen or a
character that takes no room, in a word longer than its line, and at kerning and ligatures the tables don't have.

The kerning demo's 202 lines are all where Word's PDF of it puts them.

* fix(layout): look for kerning and ligatures not yet followed in the parts of text between tabs, as they are measured

measureTextWidthAsDrawn measures the parts of text either side of a tab typed in it apart, but unknownShaping left the
tabs out and looked at the text as one, so it checked pairs and ligatures across a tab the measurement never has, and
could miss a pair it does. It now checks each part, as the font-file measurer does.

Raised by CodeRabbit's review of #3661.
2026-10-03 00:53:51 +01:00
Dolan af1078f592 feat(layout): lay out the .docx files a template imports (w:altChunk) as Word does (#3669)
* feat(layout): lay out the .docx files a template imports (w:altChunk) as Word does

Word's PDFs of the word-imported-* probes (AC1 to AC16) show what Word does with a .docx
a document imports when it opens it:

- its body goes where w:altChunk is, in the body, a table cell, a header, a footnote or
  another imported document, without its section's properties and headers, and with an
  empty paragraph at its end when it doesn't end with one
- a style of the same type and name, whatever its id or the case of its name, is the
  document's, as are its defaults, theme and settings; a style only it has keeps how it
  looks there, with new ids where the document has its ids
- its lists are lists of their own, and its footnotes are numbered with the document's
- a bookmark of a name the document has is the document's

patchDocument unzips the .docx files a template imports for the estimator, and the .docx
adapter turns them into the template's own before laying it out. An imported document of
several sections, one whose formatting is kept (w:matchSrc), one with a page reference or
number of pages, and one in another format, such as HTML, RTF or plain text, still stop,
now saying why.

* feat(layout): keep the look of the styles only an imported document has, as word-imported-styles.docx shows

Word's PDF of word-imported-styles.docx (AS1 to AS9) settles how Word keeps the look of
an imported document's own styles:

- a paragraph style keeps what the styles it is based on there and the imported
  document's defaults give it, whatever the document's of their names (AS1, AS2, AS4)
- a character style keeps only what it gives, not the defaults (AS3)
- a table style is based on the document's Normal Table (AS6)
- paragraphs of no style are in the imported document's default style (AS5)

Where the imported document's defaults leave out some of the document's, which Word's
PDFs haven't shown, the layout stops. Keeping source formatting (w:matchSrc) still
stops: Word keeps the text's look, but gives the last paragraph none of its space
after (AS9), and keeps the document's table defaults and theme (AS7, AS8).

* docs: say the imported documents' reader reads all six of the probe's documents

The reader's usage comment still said four documents, from before the probe wrote
word-imported-bookmarks and word-imported-styles, so following it left those two out.

* docs: say the best guess still stops only at the imported documents it doesn't lay out

Since the .docx adapter lays out imported .docx files as Word does, a guess only stops
at those it doesn't, such as one in HTML, rather than at every imported document.

* fix(layout): read backslashes in a relationship's target as slashes, as patchDocument does

patchDocument unzips an imported .docx at its relationship's target with backslashes
read as slashes, as some tools write them, but the .docx adapter resolved the target
as it was written, so it didn't find the document and stopped at it.
2026-10-03 00:33:42 +01:00
Dolan c46cba414b feat(layout): lay out a document grid, and text that runs down the page, as Word does (#3670)
* feat(layout): lay out a document grid as Word does

Word's PDFs of word-grid, word-grid2 and word-grid3 (scripts/layout-probes) showed how Word lays out a section's
document grid (w:docGrid): each line takes as many of the grid's lines as its own height needs, with multiple and
at-least spacing on them and the room below its text allowed below the page; a grid of lines and characters adds its
charSpace after each character and a quarter of a character between East Asian characters and Latin letters; and a
grid that snaps to characters puts them in cells of each column. The layout follows them, and stops where Word's
PDFs didn't show what it does. A section whose text runs across the page with a direction of its own no longer stops.

* feat(layout): lay out text that runs down the page as Word does

Word's PDF of word-vertical.docx (scripts/layout-probes) showed that Word lays out a section whose text runs down the
page as a page turned on its side: each line from the top margin to the bottom one, the lines across the page from the
right (tbRl, btLr) or the left (tbRlV, tbLrV), ideographs an em down the line and Latin text on its side, and a header
that doesn't push the lines (word-grid3.docx VH1). The layout follows it, and layoutDocument gives each line where it
is on the page, with textRunsDown on the page. It stops at what Word's PDFs didn't show down the page.

* fix(layout): stop at a grid that snaps to characters on text that runs down the page

A grid that snaps to characters measures its cells across a column of the page, where the lines of text that runs down
the page run along its height, and Word's PDFs showed only a grid of lines and characters there (word-vertical.docx V3).

* docs(layout): name the reader of word-grid3's PDF, word-grid.py

word-grid.py reads the H probes of word-grid3 too, as the probes' README says; there is no word-grid3.py.

* fix(layout): read V10 and V11's lines from the left in word-vertical.py

tbRlV and tbLrV put their lines across the page from the left, so the reader printed them in reverse. It now also takes
each page's direction from the probe on it, as its words don't show it, and leaves out the header and footer of a page
of text down it.
2026-10-03 00:23:49 +01:00
Dolan edbb770e3f feat(layout): lay out past what it would stop at with a best guess, when asked (#3672)
* feat(layout): lay out past what it would stop at with a best guess, on request

Decision D6 of the watertight report: an opt-in best guess past the stops.
estimatePageNumbersWith({ guess: true }), and layoutDocument(doc, { guess: true }),
lay out past each stop with the best guess the layout has, and say where they
guessed: the estimate's guesses give why and the page it was laying out then,
and each page of layoutDocument's result says what was guessed at on it.

Every stop goes through one path. In paginate, a stop is stopAt(reason): it
throws without a guess, and with one notes the guess on the page and goes on
with what the layout has. What the reader marks on a block, section or the
document is laid out as it was read; a line marked by the line breaker as it
was broken; text in a font or with a character the measurer doesn't know as
the measurer measures it, the most similar font in the width tables. In the
reader, read with a guess, what it can't read is left out of its paragraph,
which keeps why, unless the place it stops at gives a better guess: a date as
written, a run's formatting ignored, a ruby's base, a content control bound to
custom XML as written. A stop added later gets one of those paths, or, written
as throw new Unsupported in paginate, or as a block with nothing read in it, a
clear no guess. Off by default, so the default stays watertight, and the
default layout of every committed probe and Word-saved demo is unchanged.

* test(layout): check the guesses against Word's PDFs, and cover their last branches

scripts/layout-probes/guess-lines.ts lays out a document saved from Word with a guess, and finds each line from the first page it guessed on among the lines of Word's PDF. The docs say which floating drawing stops still have no guess.
2026-10-02 23:49:42 +01:00
Dolan 85773e0f01 feat(layout): lay out documents with the compatibility settings Word lays out lines alike with, as Word does (#3668)
* feat(layout): lay out documents with the compatibility settings Word lays out lines alike with, as Word does

docx/layout stopped at any of the schema's compatibility settings turned on,
and at Word's allowHyphenationAtTrackBottom and
allowTextAfterFloatingTableBreak turned on (decision D4), as several change
lines by their definitions. Word's PDFs of word-compat-settings.docx, with
each of eight groups of them on, of word-compat-settings2.docx, with each
setting of the two groups that changed lines on alone, and of
word-compat-settings2-together.docx, with all the others on at once, showed
that in compatibility mode 15 only suppressTopSpacing and useFELayout change
Word's lines. The other 62, and Word's two settings, at most move the text
inside a line of exact height or leave a justified line before a line break
unstretched, so they no longer stop the layout.

* fix(layout-probes): report only the probe that moves the pages after it, in the settings probe's reader

compare took each probe's page shift from its first line and reported any
shift, so a probe that added a page made every probe after it read as
starting on another page, though its docstring says they don't differ. It
now carries on from the page the probe before ended on, and reports a move
only where a probe starts somewhere that doesn't explain. Word's PDFs had
no such move, so what the reader printed for them is unchanged.

* fix(layout-probes): don't report a probe as moved when it is on its own page, in the settings probe's reader

A probe whose last line moved onto the next probe's page made that probe read
as starting a page early, though it was on the page it had been. compare now
reports a move only when a probe starts on another page than it did and the
probe before didn't end as many pages later. Its own lines' places are still
compared, so a probe pushed down its page reads as such.
2026-10-02 23:25:17 +01:00
Dolan 8a86bfd5f8 feat(layout): lay out text around pictures and shapes that it wraps around, as Word does (#3667)
* feat(layout): lay out text around pictures and shapes that it wraps around, as Word does

docx/layout stopped at any drawing that text wraps around. Word's PDF of a new
probe, word-floats.docx (F1 to F40), showed how Word lays the text out around
them, and this follows it: lines beside a drawing have the room beside it, with
its distances and effects, on the sides its wrapping says, both sides in rows,
a room the next word doesn't fit left empty, and lines with no room below it.
Drawings are placed against the page, margins, column, paragraph or line, with
offsets, alignments and percentages. The text before a drawing's paragraph on
its page goes round it too, a paragraph whose drawing would go past the bottom
of the page moves to the next page with it, and a table that doesn't fit beside
a drawing goes below it. It keeps stopping where Word hasn't shown its way, such
as drawings that overlap, paragraphs kept with the next beside a drawing, and
floating tables.

A new layout demo, text-around-pictures, checks it against LibreOffice.

* fix(layout): put a line below a drawing when its indents leave none of the room beside it

The room beside a drawing that a paragraph's indents left nothing of gave the
line no room of its own, so it was laid out across the paragraph, over the
drawing. It goes below the drawings beside it now, as a line with no room
beside them does. Only a paragraph whose indents leave no room even without a
drawing keeps its lines as they are.

* docs(layout): say what each function of the word-floats reader reads
2026-10-02 23:10:46 +01:00
Dolan 3a3494221d feat(layout): give a paragraph whose text and mark are all hidden no room, as Word does (#3664)
* feat(layout): give a paragraph whose text and mark are all hidden no room, as Word does

word-seq.docx Q8 and word-hidden-paragraphs.docx HP1 to HP8: a paragraph with nothing shown and its
mark hidden takes no room in Word, whatever its formatting, before a paragraph or a table and at the
end of the document, and a line at the end of a table cell. It is read where it is, so its SEQ
fields and list number count in order (Q8, HP5), and then left out; its bookmarks start with the
block after it (HP3), and the paragraphs around it go by its style for their contextual spacing
(HP6c, HP6d). Page breaks and pictures in hidden text take no room (HP4). Where Word hasn't shown
what it does, the layout stops: a hidden note reference, a paragraph kept with the next before a
hidden one, a box of borders around one, and one at the end of a header, footer or note.

* fix(layout): start the bookmarks of hidden paragraphs at the end of the document where its text ends

A paragraph with nothing shown and its mark hidden at the end of the body is left out, as it takes
no room (word-hidden-paragraphs.docx HP8), but its bookmarks were dropped, so a page reference to one
was blank. They now start after the last line: of the last paragraph, or of the last cell of a table.
2026-10-02 23:09:25 +01:00
Dolan bb2b173d9d feat(layout): lay out documents that hyphenate their words, and stop where Word may hyphenate, as Word does (#3666)
* feat(layout): lay out documents that hyphenate their words, and stop where Word may hyphenate, as Word does

docx/layout stopped at any document with automatic hyphenation. Word hyphenates
by its own dictionaries, which the layout can't have, so it now lays out each
line where Word certainly leaves the word after it whole, and stops at a line
Word may end with a part of a word and a hyphen.

Word's PDFs of word-hyphenation.docx and its -zone, -limit and -manual versions
showed that in compatibility mode 15 Word keeps no hyphenation zone, hyphenates
wherever a part of two letters or more fits, breaks no word of fewer than five
letters, nor numbers, nor text not checked for spelling or in no language,
leaves capitals whole with doNotHyphenateCaps, squeezes a word onto a justified
line rather than hyphenate it, breaks a word with soft hyphens where its
dictionary does too, and sizes a table's columns to the parts of the words it
hyphenates. consecutiveHyphenLimit leaves soft hyphens' breaks as they are.

* docs(layout): say why a word partly in text Word doesn't hyphenate stops the layout

Word's probes haven't shown how it hyphenates a word whose runs differ in
proofing, so the layout takes it as one Word may break from its first
characters, rather than assume Word never breaks inside the part it leaves
whole.
2026-10-02 22:48:51 +01:00
Dolan 55a1ba932e feat(layout): fill pages of endnotes after the first, and lay out their separators, as Word does (#3659)
Word's PDFs of word-watertight-endnotes1 to 3 and word-continued-endnotes1 to 5
put as many endnote lines below the continuation separator as fit on the page
without it, so the last goes past the margin by up to its height, rather than
a line going on while its top is above the margin. And Word lays out both
endnote separators a line of the document's text tall, whatever their space
before and after, line spacing and size, where docx/layout laid out the
separator on the first page with its own formatting.

Take out the stop at endnotes that fill a page after the first, and read both
separators as an empty paragraph of their style, keeping their bookmarks. One
with text in it, or of more than a paragraph, stops, as no probe had one.
Commit Word's PDFs, the documents it opened, the follow-up probe and its
reader.
2026-10-02 22:18:16 +01:00
Dolan 7e5dbe5466 feat(layout): join differently formatted paragraphs by a hidden mark, and break soft hyphens, line up decimal tabs and place tabs past the margin, as Word's probe showed (#3665)
Word's PDF of word-breaks-and-tabs.docx settles most of what #3650 stopped at: a paragraph joined to the next by its
hidden mark takes its own alignment, left indent and space before and the next one's space after (HM1a to HM1f), counts
the next one's list number without showing it (HM4), and takes no room when it shows nothing (HM3); specVanish alone
joins nothing (HM5a). A justified line squeezes a word with soft hyphens on whole (SH1), a hyphen needs about a point of
room (SH2), and a word longer than its line breaks at its soft hyphens (SH4). A decimal stop lines up the first full
stop, or the end of a number before it (DT1 to DT15). Stops past the margin line up or move their text as TP1 to TP9
show. What the PDF doesn't settle still stops.
2026-10-02 22:03:45 +01:00
Dolan c81d06d2b5 feat(layout): break at soft hyphens, join paragraphs whose mark is hidden, and line up decimal tabs and tabs past the margin, as Word does (#3650)
* feat(layout): break at soft hyphens, join paragraphs whose mark is hidden, and line up decimal tabs and tabs past the margin as Word does

* test(layout): lint and spelling

* docs(layout): reflow a comment

* fix(layout): leave a paragraph whose hidden mark has no paragraph after it as it is, as Word does

Word's PDF of word-breaks-and-tabs.docx shows a paragraph whose mark is hidden before a table, alone in a table cell or
last in one, each on its own line, with the rows as tall as without (HM2a, HM2b), so the layout no longer stops there.
A hidden mark at the edge of a content control still stops, as no probe has had one. The probe's .docx and Word's PDF
are committed with its script and reader.

* fix(layout): join a hidden mark's paragraph to one naming the default style, as naming none is the same

A paragraph that names the default paragraph style and one that names none are formatted the same, so a hidden mark
between them joins them rather than stopping the layout.

* docs(layout): say apart how decimal tab stops and tab stops past the end of the line are laid out
2026-10-02 21:22:08 +01:00
Dolan e70b43dd8f feat(layout): size tables whose rows give a column different widths, and long words in tables, as Word does (#3655)
* feat(layout): even out table rows that give a column different widths, and size long words in tables, as Word does

Word's round-9 probes (word-watertight-stops.docx SP14, SP15 and SP17) show how it sizes
these tables, so the layout now follows them where it stopped:

- rows that give a column different widths, in a table with a width of its own in twips,
  laid out fixed or not: each column as wide as the widest a row gives it, then fitted to
  the table's width (SP14a, SP14b)
- a word longer than the page in a table with a width of its own: the column as wide as
  the word, past the margin (SP15a)
- a long word in a table wider than its cells: widened for the word, then in proportion
  (SP15b)
- a long word across columns given widths: shared among them, the others narrowed (SP15c)
- a long word across columns given no widths: shared in proportion to each column's
  widest word and widest line added together (SP17, U1e)

word-table-widths.ts is a probe for what these leave open.

* feat(layout): count a table in a cell as it is laid out in its own width

* feat(layout): keep stopping at uneven rows of a table whose rows skip grid columns

* feat(layout): size tables whose cells all have widths as Word's word-table-widths.docx shows

Word's PDF of word-table-widths.docx (TW1 to TW23) settles what round 9 left open:

- a table laid out fixed is sized by its cells' widths alone: fitted to its own width in
  twips, its columns' text narrowed in proportion (TW6, TW8) or the columns widened in
  proportion (TW4, TW10), and kept as its rows give it without one (TW2)
- a table whose cells all have widths is fitted to its own width in twips, rows that
  agree or not (TW3, TW5, TW7, TW9), and narrowed to the room without one (TW1)
- a cell across columns wider than they are widens the last of them (TW19)
- a long word across columns given widths is shared by their widest words and widths
  added up (TW16 to TW18), and the columns keep it when a table given no widths is
  narrowed around it (TW23)
- a word longer than a table given no width of its own, or a share of the width, stops,
  as Word breaks it (TW12, TW13); one with a width in twips grows (TW11, TW14, TW15)

Commits Word's PDF of the probe, and reads tables that start on the next page.

* style: format the table widths probe
2026-10-02 21:04:31 +01:00
Dolan cddcca0b37 feat(layout): break a table in a table cell across pages as Word does (#3652)
* feat(layout): break a table in a table cell across pages as Word does

* docs: say why the tables-in-cells demo's tables in cells have no borders

docx/layout now breaks tables in cells with borders, so the demo's header no longer gave the right reason. LibreOffice breaks them where Word doesn't, and draws no top border above the rest on the next page (word-nested-tables.docx N1b, N1c), so the demo leaves them out to match LibreOffice.

* docs: end the sentence before the one on tables in cells in the layout guide

The sentence on cells merged down rows ran into the new one on tables in cells without a full stop.
2026-10-02 20:52:08 +01:00
Dolan 1aa027d70a feat(layout): lay out deleted table rows' borders, and a table style's rows around them, as Word does (#3663)
Word's PDF of word-tracked-tables.docx (MK14) settles what #3636 stopped at. Word draws one border where deleted rows
are, a deleted row's own too, and leaves out the space between cells around them. It applies a table style's first
row, last row and bands by each row's place among all the rows, the deleted ones too. The layout now does the same,
and stops only where the borders that make that one border take different room, at a deleted row in a table with
borders and space between its cells, and at a deleted row in a header of several rows where counting it changes which
parts of the style apply.
2026-10-02 20:39:44 +01:00
Dolan 673e7d5963 feat(layout): number lists and place their numbers as Word does (#3656)
docx/layout counted each list on its own, ignored w:lvlOverride, w:lvlJc,
w:lvlRestart and w:isLgl, wrote "1" for a number format it couldn't write,
and measured a number in its paragraph style's formatting, as text. Word's PDF
of a new probe, word-lists, settles each, and the layout now follows it:

- lists made from one definition count on from one another, and a list's own
  first number for a level starts that level there at its first paragraph of
  the level, once; a level a list gives starts at its own first number
- a right-aligned number ends at the start of the first line, with the space
  after it, and a centred one is centred there; the text goes on from its end
- w:lvlRestart and legal numbering, as the schema has them
- a number is in its paragraph mark's formatting, but for its level's, and
  makes its line taller only above the baseline; the space after it is
  measured in Arial, as Word draws it

Where Word hasn't shown what it does, the layout stops: formats it can't
write, a level not counted yet that its list starts elsewhere, a centred
number followed by a space, the tab after a right-aligned number at a stop
Word hasn't been seen to use, picture bullets, Word 6 numbering and lists
defined by list styles.
2026-10-02 20:06:17 +01:00
Dolan aa58a2c05f feat(layout): stop at compatibility settings not yet followed, and format a header of several rows, as Word does (#3653)
* feat(layout): stop at compatibility settings that change how Word lays out lines

Word's own settings, which it writes in every document it makes, leave its lines as
they are and don't stop the layout.

* feat(layout): make a header of several rows a table's first row, as Word does

word-compat-on.docx and word-compat-off.docx, saved from Word 16 for Mac with and
without the compatibility settings Word writes in its documents, laid out alike. They
showed that Word makes all of a header of several rows the table's first row, and
starts its bands of rows below it.

* fix(layout): read the compatibility mode from Word's own setting

Another application's compatSetting of the same name, written before Word's, was
read in its place, so a document in Word 2010's mode could be laid out as if it
weren't.

* docs(layout): say which compatibility settings stop the layout, on or off

A setting of Word's the layout doesn't know stops it even when it is off, and
another application's doesn't stop it even when it is on.
2026-10-02 19:41:41 +01:00
Dolan 5c5cab7015 feat(layout): lay out a next-column section's next page and footnotes, and paragraphs kept together taller than some columns, as Word does (#3654)
Word's PDF of word-column-stops.docx (CS1 to CS9) settles what #3649 left
stopping:

- CS1: a section laid out in the columns of the page it starts in goes on in
  its own columns on the next page
- CS2: its footnotes are laid out in its own columns
- CS3 to CS6: a paragraph kept together taller than some of the columns of
  different widths but not others goes in the first it fits in, moving on
  from the top of a column it is taller than
- CS7 to CS9: paragraphs kept with the next before one taller than a column
  move as #3649 moves them

The page's columns are kept in the layout's snapshot, as a section laid out
again from the page before goes back into them.
2026-10-02 19:25:16 +01:00
Dolan 050f9b0457 feat(layout): lay out footnotes in columns, before a new section and with kept paragraphs, as Word does (#3651)
* feat(layout): lay out footnotes in columns, before a new section and with kept paragraphs, as Word does

Word's PDF of word-watertight-stops.docx (round 9) showed what it does at the
footnote stops SP1 to SP9 were written for:

- SP1, SP9: in columns, a line or row whose footnotes don't fit below it moves
  to the next column or page with all of them, even where part would fit
- SP2: before a section on a new page, the rest of a footnote goes on pages of
  its own, which are the section's, and the next section starts after them
- SP3: a footnote that starts with a paragraph kept together, or kept with the
  next, moves its reference with it; a row widow control keeps whole goes on
  the next page; a table's header rows aren't repeated in the rest
- SP4: the footnote of a line kept with a longer paragraph goes below the
  first lines it is kept with, and takes the rest of the page
- SP5: a line at the top of a page whose footnote can't go on a page with it
  stays, and the footnote starts on the next page
- SP6, SP7: footnotes of two sections of the same columns share them, and in
  columns of different widths each part is broken at its column's width

What the probe leaves open still stops, with narrower reasons, and
word-watertight-notes.docx (round 10, FN1 to FN15) probes it.

* docs(layout): say the pages of a footnote's rest belong to the section its reference is in

"Which are the section's" followed "a section that starts on a new page",
so it read as the new section's pages, where they are the section before's
(word-watertight-stops.docx SP2).
2026-10-02 19:10:17 +01:00
Dolan f0700c14f7 feat(layout): lay out next-column sections of other widths, kept paragraphs taller than a column and tables going into narrower columns as Word does (#3649)
Word's PDF of word-watertight-stops.docx (round 9) showed what it does at five
stops in columns:

- SP10: a section that starts in the next column of columns of other widths
  is laid out in the page's columns, where they are and as wide as they are
- SP11: columns a section started in the next column of aren't evened out
  before a continuous break, from any column, and the next section starts
  below the longest
- SP12: a paragraph kept together taller than each of the columns of
  different widths goes down the first column of each page
- SP13: paragraphs kept with the next before one taller than a column go to a
  new page with it, unless at the top of a page, and it moves on to the next
- SP16: a table sized to its text keeps its widths in a narrower column

What the probe leaves open still stops, with narrower reasons.
2026-10-02 18:37:28 +01:00
Dolan e2f1a06789 feat(layout): move a row that can't break, taller than a page, to a new page and break it there, as Word does (#3648) 2026-10-02 18:27:45 +01:00
Dolan 4f84a3c64a docs: say how theme fonts and colors in external styles work (#3647) 2026-10-02 17:46:49 +01:00
Dolan 79d9932b63 feat(layout): stop at fonts not in the width tables, and measure the fonts a document embeds from their files, as Word draws them (#3639) 2026-10-02 16:27:19 +01:00
Dolan 96dad6048d feat(layout): write page references with \p and in formats of their own, and lay out PAGE and SECTION, as Word does (#3635)
* feat(layout): write page references with \p and in formats of their own, and lay out PAGE and SECTION, as Word does

Page references with \p write "above", "below" or "on page N", and page
references, numbers of pages and page numbers are written and laid out in
their own formats, pictures and capitals, as Word's PDFs of
word-watertight-fields, word-watertight-pages and word-page-fields show.
Bookmarks, PAGE and SECTION in footnotes and endnotes take their
reference's page and section. A date or time in the body stops the layout
(decision D2).

* docs(layout-probes): keep word-character-widths' instructions together, after which word-page-fields' follow

The word-page-fields block split them, so "It reads word-italic-widths the same way" read as if word-page-fields.py did.

* perf(layout): collect the page references with \p to a bookmark in place

Each one copied the list of those before it, so a document with many to one bookmark took time growing with their square, as CodeRabbit's review pointed out.

* fix(layout): read a date, or a field in a format not yet written, in hidden text as nothing

Hidden text takes no room, so its field results can't move a line, and the layout shouldn't stop at them. A DATE or TIME field, a date block, or a page field in a format not followed stopped it even in hidden text.

* test(layout): count each page reference with \p once in a table sized with tracked changes, as docx counts them

#3636 reads a cell with deleted text a second time to size its table's columns, and reads deleted rows and their deleted
text to size them. Rebasing onto it, those reads were kept from counting the page references with \p again or in deleted
text, which docx's writer doesn't count, so each result is written into the field it was worked out for.
2026-10-02 06:37:45 +01:00
Dolan f5e5d35d1f feat(layout): join a paragraph whose mark is deleted to the next, and leave deleted rows out, as Word shows tracked changes (#3636)
* feat(layout): lay out tracked changes as Word shows them in balloons and Simple Markup

A paragraph whose mark is deleted joins the next, in the next one's formatting, a deleted section break leaves its
section to the next, deleted rows take no room, and a deleted footnote is still counted. Word sizes a table's columns by
its deleted rows and text all the same. Where Word hasn't been seen, the layout stops.

* fix(layout): read a deleted row of a table laid out fixed as the layout reads deleted runs, keeping its bookmarks

It was read as Word sizes columns, with its deleted text shown, so a deleted tab or break in it stopped its paragraph and
lost its bookmarks, though nothing sizes a fixed table's columns by it.

* fix(layout): say whether MK11g's deleted header row is on the table's first page and on the next, apart

The probe reader said the header was shown if it was on any page, so it couldn't tell the table's first page from a
repeat on the next.

* test(layout): expect a footnote reference's number in its run's font, as #3641 writes it

* fix(layout): stop at a field only part of which is deleted in a tracked change

A deleted run's field characters were dropped, so a deletion of a field's end or separator, and not its start, left the
field open, and the text after it was left out without a stop. They now keep their place on the field stack: a field
deleted whole is left out, as Word shows it, and one deleted only in part stops the layout.

* test(layout): number notes read again to size a table's columns as #3638 numbers them

* fix(layout): stop at a deleted footnote reference with a mark of its own, as at one not deleted

Word may not count such a note in the numbers of the others, so counting it when its reference is deleted could number
the footnotes after it one too high without a stop.
2026-10-02 06:10:25 +01:00
Dolan 5eac9c0446 feat(layout): number the pages after a continuous section numbered afresh, put a gutter at the top, and put the continuation separator above endnotes, as Word does (#3638) 2026-10-02 05:47:20 +01:00
Dolan f6a911c938 feat(layout): lay out superscript, small capitals, raised text, emphasis marks and borders around text as Word does (#3641) 2026-10-02 05:34:28 +01:00
Dolan 1fb088ad42 feat(layout): break the text of cells merged down rows with the row that crosses the page, as Word does (#3640)
* feat(layout): break the text of cells merged down rows with the row that crosses the page, as Word does

Word's PDF of word-probes.docx (U4a, U4b) showed that the text of a cell
merged down rows starts at the top of the merge's first row and goes down
across its rows, so the row of them that crosses the page breaks it, as far
down the page as its lines go. The rest goes on from the top of the merge's
rows on the next page. docx/layout stopped at any row with merged cells that
broke across pages, and at a merge that didn't fit with its cell's text.

Where Word hasn't shown what it does, it still stops: text that goes on
across a page break between the merge's rows, or across more than two
pages, and text with a footnote, or from a table's header rows, that goes
on across pages.

A row of a set height taller than its text already moved whole (#3616), as
U4e to U4g show; a test now covers U4f, whose text doesn't fit either.

Adds the merged-cells-across-pages layout demo.

* docs(layout): say what the merged-cells demo keeps to for LibreOffice, as checked
2026-10-02 05:27:50 +01:00
Dolan 59b0807150 feat(layout): make a line as tall as its fonts' ascents and descents, with pictures on the baseline, as Word does (#3631)
* feat(layout): make a line as tall as its fonts' ascents and descents, with pictures on the baseline, as Word does

A line of two fonts takes the tallest ascent and the deepest descent, a
picture stands on the baseline, multiple spacing adds a share of the
tallest font's own line, and a page's last line can have that spacing
below the bottom of the page, from Word's PDFs of word-watertight-text
(TX8, TX9) and word-mixed-heights (MH1 to MH7).

* docs(layout): say that a column's last line can have its multiple spacing below the bottom too

The layout lets the space multiple line spacing adds below the last line of
a column go below the bottom of the page, as it does for a page's, and stops
in the cases Word hasn't shown, which the list of stops gives.
2026-10-02 05:02:47 +01:00
Dolan d4d7a23ac6 feat(layout): lay out table borders, space between cells, rows kept with the next, vertical text and table style parts as Word does (#3637)
* feat(layout): lay out table borders, space between cells, rows kept with the next, vertical text and table style parts as Word does

* fix(layout): count the largest margins of a row's cells in the first lines a kept row needs, as the row has them

leastPartOf added each cell's own margins to its first lines and took the largest, where rowHeights and splitRow put the
row's largest margins above and below around every cell's text (word-table-formats2.docx MG1 to MG4). A cell of one
line well below the top, beside a cell of several lines, then made the next row's first part look shorter than
splitRow needed, so the kept rows stayed and the layout stopped. Both now use rowMarginsOf.
2026-10-02 04:42:56 +01:00
Dolan 86f56ff5b1 feat(layout): measure text in the fonts the caller gives from their files, as Word does (#3613)
* feat(layout): measure text in fonts from the font files the caller gives, as Word does

* chore(layout): keep the word-fonts probe, its .docx and Word's PDF of it in scripts/layout-probes

* perf(layout): choose each font's face once when measuring text from font files

* feat(layout): stop at a character a font's file has no glyph for, as Word draws it in another font

Measured from a font file, a character the font has no glyph for is one whose
width isn't known, as the width tables' are since #3628, so the layout stops at
it, but for characters that take no room. A tab typed in the text moves to the
next half inch, as the width tables and measureWidth measure it.

* fix(layout): throw when a font file cut short or damaged is read, rather than when text is laid out in it

A font file's tables were only checked when they were read, and its kerning only
when text was first kerned, so a truncated file passed readFontFile and failed
during layout with a RangeError that didn't say what was wrong. Each table is now
checked to end within the file, the kerning is read with the rest (a few
milliseconds), and a read past the end is turned into an error that says the
file is damaged.

* fix(layout): measure the pieces of a word together only when they are kerned

Joining the pieces of a word in the same font is only for kerning across runs.
Measuring them together otherwise changed what a measurer that measures a piece,
such as a page number, on its own gave for it, which #3632's tests of pages that
keep changing rely on.

* fix(layout): keep the pieces of a word apart when they are smaller than the size kerning starts at

Text smaller than its w:kern size isn't kerned, as Word showed (word-fonts.docx
F3), so its pieces from different runs are measured apart, as they are without
kerning. isKerned is the font file measurer's check too.

* fix(layout): measure the pieces of a word together when one gives formatting as Word's default and the other none

A run with bold, italic, size, scale or spacing written as Word's default is in
the same face as a run that doesn't give it, so their pieces are kerned across
the runs as Word kerns them.

* fix(layout): measure the pieces of a word together when their fonts' names differ only in capitals

The measurers find a font by its name in any case, so runs in "Probe Sans" and
"probe sans" are in the same face, and are kerned across as Word kerns them.

* fix(layout): measure the pieces of a word together when both are kerned, from whatever size

Two runs kerned from different sizes are both kerned at theirs, so whether they
are kerned is what is compared, not the size kerning starts at.
2026-10-02 04:23:10 +01:00
Dolan 170070d279 feat(layout): lay out paragraph borders, automatic spacing, and indents and spacing in characters and lines, as Word does (#3634)
Paragraph borders take their width and space above and below, and paragraphs with
the same borders and indents are one box. Word's automatic spacing is 14 points,
with none at the document's start, at a cell's or header's top, at a cell's bottom
or between a list's items. Indents in characters and spacing in lines are taken in
place of twips, in the sizes Word measures them in. Where Word's way isn't known,
the layout stops. Word's evidence is word-watertight-text TX5 to TX7 and the new
word-paragraph-formats probe, committed with its reader and Word's PDF.
2026-10-02 03:56:30 +01:00
Dolan 27401618ba feat(layout): squeeze the spaces of justified and distributed lines to fit one more word, as Word does (#3629)
* feat(layout): squeeze the spaces of justified and distributed lines to fit one more word, as Word does

Word squeezes the spaces of a justified line to fit one more word on it,
when that takes less than a quarter of their width and less than half of
what the spaces before the word would stretch by without it
(word-justify.docx J01 to J18, word-watertight-text.docx TX20,
word-watertight-stops.docx SP18). Spaces at the start of a line, before
a tab, and no-break spaces aren't squeezed.

A distributed line squeezes less often, as its letters could stretch
too: Word weighs the stretch as if each space took 7.2 times what each
letter does (word-justify2.docx K01 to K07, K12). Latin text justified
for Thai or with a low kashida breaks as justified text does (K08, K09).

Read w:jc into a paragraph's formatting, squeeze its lines as Word does,
and stop at medium and high kashidas, and at Thai or Arabic text in a
paragraph justified for it, which Word hasn't been seen laying out.

* feat(layout): stop at a justified line that only fits squeezed at an en, em or ideographic space

Since #3628, en, em, four-per-em and ideographic spaces are spaces a line
breaks after, so they were counted among the spaces Word squeezes, but
Word's probes only squeezed U+0020. A line with one of them that a word
past its end could be squeezed onto now stops the layout, rather than
guessing. One the word is too far past the end of to squeeze in, under
any assumption, wraps as before.
2026-10-02 03:34:59 +01:00
Dolan 3061519334 feat(layout): measure italic and bold italic text with Word's widths of the fonts' italics, as Word draws them (#3633)
* feat(layout): measure italic and bold italic text with Word's widths of the fonts' italics, as Word draws them

Generate italic and bold italic width tables from the open fonts' italics,
checked against Word's PDF of a new probe, word-italic-widths, that has every
character of the tables in italic and bold italic in the five fonts. Read
w:iCs, and lay out right-to-left and complex-script runs in their own
italics, as their boldness. Read each character's word in Word's PDFs only
as the pieces that follow on from its code point, so characters drawn with
boxes off their line aren't counted in another's width.

* test(layout): compare where docx/layout breaks the lines of the character width probes with where Word breaks them

word-character-widths.py --json now also reads where each line of W
starts, and word-character-widths-layout.ts lays the same paragraphs out
with docx/layout and compares. 839 of 851 italic paragraphs and 849 of
864 upright ones break where Word breaks them.

* test(layout): count a word broken across lines on the line it starts on, as the reader does

A line with only the end of a broken word starts no code point or word, so
it has no marker, and the reader records nothing for it either.
2026-10-02 03:34:00 +01:00
Dolan 16bdaa1b25 feat(layout): write caption numbers (SEQ fields) clean with pageNumbers, counted as Word counts them (#3630)
A SEQ field's number counts the SEQ fields of its identifier before it, so it
doesn't depend on the layout. With pageNumbers each is written clean with its
number, counted as Word's PDF of word-seq showed, and left blank where Word's
count isn't known. Footnotes, endnotes and comments have their page references
and SEQ fields written clean too, the page numbers of a table of contents with
seqFieldIdentifierForPrefix are left blank, and a simple field written without a
result no longer throws when it is given one.

Adds scripts/layout-probes/word-seq.ts, with Word's PDFs and word-seq.py.
2026-10-02 02:56:04 +01:00
Dolan 2424d51b50 feat(layout): stop at what Word lays out in ways not yet followed, and keep bookmarks between table rows and cells, as Word does (#3632)
* feat(layout): stop at what Word lays out in ways not yet followed, and keep bookmarks between table rows and cells, as Word does

- Stop at a floating table (w:tblpPr), whose following text Word puts
  beside it (word-watertight-tables.docx TB11), and at what docx/layout
  read nothing of: w:ruby, w:contentPart, w:fitText, w:eastAsianLayout
  that combines or turns text, w:subDoc, w:divId in paragraphs and rows,
  w:hMerge, w:noWrap, w:tcFitText, w15:footnoteColumns, book-fold and
  two-on-one printing, w:linkStyles, content controls bound to custom
  XML, and equations outside a paragraph.
- Start a bookmark between table rows, before a cell or before a table
  where the next row's, cell's or table's text starts, rather than
  dropping it or moving it past the table.
- Give no page numbers when they still change after three passes.
- Test that a document of no sections is laid out and written.
- Commit the round-8 tables probe and Word's PDF of it, which word-watertight.py reads.

* fix(layout): start a bookmark before a row, cell or table in the first cell after it with any text

A bookmark before a row whose first cell had no text was dropped, and one
before a table whose first cell had none moved on to the paragraph after
the table, which can be on a later page than the table's text. Both now
start in the first block after them that has text, in a later cell or row
when it has to be, as the page references to them would show it.
2026-10-02 02:53:19 +01:00
Dolan 96d7c6d67f feat(layout): give the pages of a document and what is on each, as Word lays them out (#3620)
* feat(layout): give the pages of a document and what is on each, as Word lays them out

layoutDocument, from docx/layout, lays out a document's pages as
estimatePageNumbers does, and gives what is on each page: the lines of its
paragraphs, with their text and where they are, the rows of its tables, its
footnotes and endnotes, and which of its section's headers and footers it
shows. Lengths are in pixels. Where the layout stops, it gives the pages up to
there and why, in stoppedAt, rather than throwing.

Word's PDF of a new probe, word-positions.docx, showed that each line starts
where its paragraph's indents, its column and the margin put it, to within 0.2
points, that the blank page Word adds before a section on an odd or even page
has no header or footer, and two ways docx/layout differed from Word, fixed
here:

- A tab typed in a TextRun's text, which docx writes in w:t, is a tab in Word.
  docx/layout measured it as a letter.
- Word decides whether a section on an odd or even page needs a blank page
  before it by the page's number before the section's own first number: after
  page 6, a section on an even page numbered from 2 starts on page 8.
  docx/layout went by the section's first number.

* fix(layout): give a page's footnotes in the width they were laid out in

The room for a page's footnotes is worked out in the text width of the section
being laid out when they are added, but footnotesOnPage laid them out again in
the width of the section the page starts in. On a page where a continuous
section with other margins refers to a footnote, the lines given broke at
another width and took another height than the room left for them. The page now
keeps the section its footnotes were last laid out in, and gives them in its
width and from its left margin.
2026-10-02 02:35:09 +01:00
Dolan 0db297363a feat(layout): read lengths written with units, such as "1in" and "12pt", as Word reads them (#3627)
docx writes a length given as a string as OOXML's universal measure, which docx/layout read as a bare number: "1in"
as 1 twip. Word's PDFs of word-units and word-units2 show it reads one as a whole number of twips, rounded down from
inches, points and picas and to the nearest from centimeters and millimeters, with a minus sign for the whole number
only, and a size only in points, rounded down to a half-point. The layout reads them so, and stops at a size in another
unit and at a negative fraction of a centimeter or millimeter, which Word's PDFs don't settle.
2026-10-02 02:11:53 +01:00
Dolan 66743729ef feat(layout): measure Greek, Cyrillic, Vietnamese, Latin Extended, punctuation and symbols with Word's widths, and break lines after the spaces Word breaks after (#3628)
* test(layout): probe how wide Word draws the characters of the width tables, and how lines break after other spaces

* feat(layout): measure Latin Extended, Greek, Cyrillic, Vietnamese, punctuation and symbols with Word's widths, and break lines after the spaces Word breaks after

* chore(layout): spell the generator's tool names

* fix(layout): stop at a symbol whose character isn't four hexadecimal digits, rather than read part of it or throw

w:char is ST_ShortHexNumber, two bytes, but SymbolRun writes whatever string it is given. parseInt read "F0FCzz" as
F0FC, and a code past U+10FFFF, such as "110000", made String.fromCodePoint throw, which made Packer reject.

* docs(layout): give a mathematical symbol in Calibri as the example of a character whose width isn't known

Word's PDF showed its Cambria has Greek, so Greek in Cambria no longer stops the layout.
2026-10-02 02:01:00 +01:00
Dolan e008c45c0c feat(layout): read a .docx into the layout, to write a template's page numbers and check Word's own pages (#3619)
* feat(layout): read a .docx into the layout, to write a template's page numbers and check Word's own pages

docx/layout read only the document being written, from its formatted
XML. A second adapter, read-docx.ts, reads a parsed .docx into the same
blocks: it follows the package's relationships to the main document and
its styles, theme, numbering, settings, headers, footers and notes, and
reads them with the same reader, which now takes a document's parts
rather than context.file.

patchDocument takes the same estimatePageNumbers as Document, as its
pageNumbers. Once the patches are in, it gives the estimator the
template's parsed parts and writes the page numbers into the PAGEREF,
NUMPAGES and SECTIONPAGES fields of its body, headers and footers. The
numbers a template was saved with are those from before it was filled
in, so one the layout can't work out is left blank. The writing of the
fields is shared between formatted and parsed trees.

A document in compatibility mode stops the layout: Word lays it out as
an older version of Word did, which isn't followed. LibreOffice writes
mode 12, and docx writes 15.

compare-layout.ts lays out documents Word saved through the adapter, and
compares them with the pages Word marked with w:lastRenderedPageBreak,
read with the rules Word's PDFs of the layout demos showed, and with
Word's PDFs. The 7 demos whose marks have all their pages are committed
in scripts/layout-probes, so CI checks docx/layout against Word's own
pages. A new demo fills a template in with patchDocument and writes its
page numbers.

* fix(layout): fail the layout comparison when there are no documents saved from Word to compare

compare-layout.sh copied the documents saved from Word in
scripts/layout-probes into its output directory only if they were there,
so when the glob found none, as when the script runs from another
directory, Word's own pages weren't compared and the comparison passed
anyway. It now stops with an error. compare-layout.ts still compares a
directory without them, as it does when run on its own.

* fix(layout): compare documents saved from Word only with the pages Word marked in them

compare-layout.ts compared each document saved from Word with Word's PDF
of the demo of the same name too. When a demo changes, as columns did in
#3611, Word's PDF is made again of the new demo, and no longer is of the
document Word saved, so the two disagreed although docx/layout matched
both. A document saved from Word has Word's own pages in its marks, so it
is compared only with them, and one whose marks don't have all its pages
isn't compared.

* fix(layout): fail the layout comparison when a committed document saved from Word can't be compared

The documents saved from Word in scripts/layout-probes are committed
because Word's marks have all their pages. One whose marks no longer read
as having them all was skipped without failing, so a mistake in reading
Word's pages would have turned off the check in CI. It now fails. A
document only in the output directory whose marks don't have all its
pages, as Word's columns and long-table-of-contents, is still left out.
2026-10-02 01:48:30 +01:00
Dolan eee355bff2 feat(layout): write page references and tables of contents clean with pageNumbers, so Word opens the document without asking to update them (#3626)
* feat(layout): write the fields whose page numbers docx/layout writes clean, so Word doesn't ask to update them

Page references, and tables of contents filled in from the headings, were always
written dirty, so Word asked to update the fields when it opened the document,
even without updateFields. Once docx/layout writes a page reference's number, it
is written clean, and so is a table of contents once all of its page numbers
are. Those it couldn't write stay dirty, a beginDirty the caller gives is kept,
and documents without pageNumbers are written as before.

Adds scripts/layout-probes/word-no-prompt.ts, a document to open in Word, with
its reader.

* docs: say a table of contents only opens without the prompt when updateFields is off and every number is written

The page's own examples turn updateFields on, which makes Word ask whatever
pageNumbers writes, so point to what still makes Word ask.

* fix: count a number missing from word-no-prompt's PDF as wrong

The reader skipped entries without a number and lines it didn't find, so a
number taken out lowered the count rather than showing as wrong. It now expects
all 17 entries, both page references in the text, and a header and footer on
every page.

* feat(layout): write page references and tables of contents clean whenever the document has pageNumbers

The maintainer decided that a page number docx/layout couldn't work out is written
clean and blank, rather than dirty for Word to fill in. So with pageNumbers no page
reference or table of contents is written dirty, and Word opens the document without
asking to update the fields. A beginDirty the caller gives is still kept, and without
pageNumbers the fields are written dirty as before.

* test: add Word's PDF of word-no-prompt, which has all 43 of its numbers on Word's pages

Saved from Word 16 for Mac on 2026-10-02. Every entry of its table of contents,
page reference and number of pages docx/layout wrote is the page Word put it on.

* docs: record that Word opened word-no-prompt without asking to update the fields
2026-10-02 01:25:01 +01:00
Dolan dec8a497ee feat(layout): break and measure Chinese, Japanese, Korean, Thai and right-to-left text as Word does (#3621)
* feat(layout): break lines of Chinese, Japanese and Korean, and measure East Asian and right-to-left text, as Word does

* refactor(layout): keep the language and East Asian marks of text out of shapes' spans

* test(layout): cover the remaining branches, and check docx/layout against Word's kinsoku probes

* docs(layout): list word-unicode and word-unicode2 among the layout probes

* style(layout): format the wrapping of a word wider than a line

* fix(layout): keep a character with its marks and what a zero-width joiner joins to it when a long word is split, across runs

A word wider than a line was split after the last character that fits, grouping each character with its marks only within
one run, and breaking after a zero-width joiner, where findLineBreaks never breaks. An accent in a run of its own, or a
joined emoji, could be split across lines.
2026-10-02 01:12:45 +01:00
Dolan 66b5b330f2 feat(layout): lay out footnotes in table rows as Word does (#3625)
A table row refers to footnotes as a line does, as word-probes.docx's U3
shows: a row whose footnote doesn't fit below it moves to the next page
with it, one whose footnote can continue stays, each line of a row broken
across pages has its footnote on its own page, and the row breaks after
the line whose footnote continues. Stops where a footnote holds back the
lines of a cell beside it, which Word hasn't shown.
2026-10-02 00:22:44 +01:00
Dolan 21d3769623 feat(layout): write page numbers in every format, with chapter numbers, as Word does (#3618)
* feat(layout): write page numbers in every format, with chapter numbers, as Word does

docx/layout wrote page numbers in decimal, roman numerals, letters and a few
more, and stopped at the rest and at chapter numbers. Two probes saved from
Word settle how Word writes each of ST_NumberFormat's formats in page numbers,
page references and lists, and how it writes chapter numbers, and the layout
now writes them so: the scripts of other languages, words, enclosed numbers and
counting systems, up to the numbers Word writes, and the chapter number of the
last heading its style numbers before each page reference.

Lists a paragraph's style numbers it in are numbered too. pageNumbers takes
chapterHeadingLevel, written as w:chapStyle, and PageNumberSeparator.EN_DASH is
written as the schema's enDash.

* fix(layout): take a style's list and level each from the nearest style that gives it

A paragraph style based on another can give its level (w:ilvl) alone and take
its list (w:numId) from the style it is based on, as Word's styles inherit
each property on its own. The reader only kept a style's numbering when it gave
the list, so such a style's paragraphs were numbered at the base style's level.

* docs(layout): say page numbers are written in Word's formats but for those listed as left blank

The line said every format was written, but the list of what is left blank names
Thai and Hindi words, and page numbers Word writes as an error.
2026-10-01 23:56:35 +01:00
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
Dolan 14e3e246fe feat(layout): continue footnotes across pages as Word does (#3617)
* feat(layout): continue footnotes across pages as Word does

Word's PDF of word-probes.docx (U2a to U2q) showed how it continues the
footnotes docx/layout stopped at: a footnote keeps its own widow and
orphan control, and breaks between its paragraphs and table rows as the
body does; where too little of it can stay, its reference's line moves
to the next page with it; the body's widow control, keepLines and
keepNext apply first; and one longer than a page fills whole pages.

The footnote area is now a stack of blocks laid out as the body is, and
the four stops are gone. Narrow ones stay where Word hasn't shown its
rule: a footnote's own paragraph kept together or with the next, a
table row of more than a line or header rows in one, a footnote
continued below a paragraph kept with a longer one, and a long
footnote's rest after a section break.

A line or table row that doesn't fit below the end of a footnote
continued from the page before goes on the next page, and lines kept
with the next decide where their footnote goes on the page they move
to.

* fix(layout): stop at a table in a footnote whose columns can't be sized, as in the text

A footnote's table was measured with the widths fitColumns gave it,
even when it marked them as unknown, so the layout went on with them
rather than stopping. It is sized with sizedToPlace now, as a table in
the text is, and as master measured footnotes before the footnote area
was laid out in blocks.
2026-10-01 22:25:59 +01:00
Dolan ab97f75111 fix(layout): keep lines as tall as Word has them, unrounded, and break table rows as Word does (#3616)
* fix(layout): keep lines as tall as Word has them, unrounded, and break table rows as Word does

Word doesn't round the height of a line: Calibri 11 is 268.55 twips, where LibreOffice
and docx/layout had 269. The width tables keep each font's height unrounded, Cambria's as
Word's 2401/2048 of an em, and lines are no longer rounded to whole twips.

With exact heights, tables-across-pages showed that docx/layout broke table rows across
pages differently from Word. word-line-heights.docx, saved from Word, settled how: where
a table breaks across pages, the last of it on the page needs room for the table's bottom
border; a paragraph that ends in a cell's part on the page needs room for its space
after; and a row of an at-least height only breaks where the page has room for its
height, which takes out the stop at a row of a set height across pages.

* docs(layout): say a line is as tall as Word makes it only for one font

Word's heights were checked for lines in one font. A line in several fonts is as tall as
its tallest, which hasn't been checked in Word, so the docs say what the layout does.
2026-10-01 21:44:54 +01:00
Dolan 27d9a41a27 feat(layout): break the lines of columns of different widths at each one's width, as Word does (#3614)
Word breaks each column's lines at its own width, so a paragraph that goes on
into a column of another width is broken again there, and evens out columns
of different widths by height (word-rules2.docx Q7). docx/layout broke a
paragraph's lines once, at one width, so it stopped at columns of different
widths.

Each line can now have its own width in layoutLines, and the layout breaks a
paragraph's lines column by column: the part of it that goes on into a column
of another width, after a column break or where it doesn't fit, is broken
again at that column's width. Paragraphs kept with the next are measured at
the width of the column they move to. A paragraph kept together that is taller
than one of columns of different widths stops the layout, as how Word lays one
out down the first column of each page (#3607) isn't known for them.

A probe saved from Word (scripts/layout-probes/word-column-widths.ts) settled
the rest:

- Widow control counts the lines left for the next column as they are broken
  in the column before. 2 narrow lines left go on 1 wide line, alone at the top
  of the wide column, and 1 wide line left moves a line with it, though it
  would go on 3 narrow ones. LibreOffice counts them as they are broken in the
  next column, and moves lines until 2 are there.
- A table sized to its text keeps the widths it has in a narrow column in a
  wider one, given no width or 100%. What Word does in a narrower column, where
  they might not fit, isn't known, so the layout stops there.
2026-10-01 21:32:11 +01:00
Dolan 3f70f537d9 feat(layout): lay out footnotes in columns as Word does (#3612)
* feat(layout): lay out footnotes in columns as Word does

docx/layout stopped at the first footnote in a section in columns. Word's PDFs
of word-rules2.docx Q6 and a new probe, word-footnotes-in-columns.docx, show
how Word lays them out: in the section's columns, at the column's width, one
after the other from the first column, evened out as columns before a
continuous section break are, with widow control. Every column of the page ends
above them, whichever column they are referred to from. LibreOffice puts each
below the column its reference is in instead.

The layout now does the same. When a footnote referred to from a later column
needs more room than the columns before it left, the page's columns are laid
out again from where they start, with that room kept for the footnotes. The
page each of the probes breaks at is the line Word breaks it at.

It still stops at a footnote in columns that doesn't fit below its reference,
at one that moves its reference to the next page when the columns are laid out
again above it, at footnotes referred to from two sections on a page, the
first of them in columns, and at a footnote in a section that starts in the
next column when a column of the section before goes down too far for it, which
Word hasn't been seen to lay out.

The probe, its reader, the .docx Word opened and Word's PDF are in
scripts/layout-probes, and the new demo layout/footnotes-in-columns has
chapters that open with a summary in two columns, with a footnote.

* fix(layout): read the second line of a footnote that wraps in the probe of footnotes in columns

The reader of word-footnotes-in-columns found each line by the probe name it
starts with, so the second line of N7's footnote, which wraps in its column,
was left out, and the footnote read as one line ending a line above the
bottom of the page. A line without a probe name now goes with the line before
it in its block of pdftotext's HTML, the paragraph it goes on from. N7 reads
as 2 lines, down to the bottom of the page, and nothing else changes.

* feat(layout): stop at footnotes in columns of different widths

The footnotes of a page in columns are laid out in the section's columns at
the width of the first, which is every column's while columns of different
widths stop when the document is read. Word has only been seen to lay out
footnotes in columns of the same width, so once columns of different widths are
laid out, a page whose footnotes are in them stops, rather than laying them out
at a width that might not be Word's.
2026-10-01 21:18:33 +01:00
Dolan 88c6a201fb feat(layout): repeat a table's header rows at the top of each column, as Word does (#3611)
* feat(layout): repeat a table's header rows at the top of each column, as Word does

Word and LibreOffice repeat a table's header rows at the top of each column it
goes on into, as at the top of each page (word-rules2.docx Q4). docx/layout
stopped there. Word's PDF of a new probe, word-header-columns.docx, showed the
same in columns evened out before a continuous section break, and that the
empty paragraph that ends a section after a table has a line of its own below
the last column, where LibreOffice gives it no room.

* test(layout): keep the space after the empty paragraph's line after a table below it, as Word does

The empty paragraph that ends a section after a table is a line of its own,
and its space after is the larger with the next section's space before, as
between any two paragraphs. Word's PDF of the columns demo puts the heading
after its table a line and 12 points below the columns: the larger of the
empty paragraph's 5 after and the heading's 12 before, not 7.
2026-10-01 21:06:21 +01:00
Dolan 2cc65801e4 feat(layout): widen a column for a word longer than its cells give it, as Word does (#3609)
* feat(layout): widen a column for a word longer than its cells give it, as Word does

In a table whose cells all have widths, Word makes a column as wide as its
longest word, grows a table with no width of its own up to the room, keeps the
width of one with its own, and narrows the other columns toward their longest
words, each by its share of the width they can give up. Probed in
scripts/layout-probes/word-long-words.ts. A word longer than the table can make
room for, a long word in a table wider than its cells, and one in a table with
cells merged across columns still stop the layout.

* chore(layout): keep Word's PDF of word-long-words beside it, read by word-autofit.py

* docs(layout): list word-long-words among the layout probes

* fix(layout): lay out a paragraph kept with a table whose columns can't be widened before stopping there

The lookahead for a paragraph kept with the next sized the table, which threw
when Word's widening of it isn't known, so the layout stopped before placing the
paragraph and its bookmark went without a page number. It now stops only where
the table is placed, as it does for a table it can't read.
2026-10-01 20:50:32 +01:00