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.
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.
* 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.
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.
* 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).
* 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.
* 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.
* 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.
* 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.
* 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.
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.
* 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.
* 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.
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.
* 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.
* 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.
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.
* 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.
* 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.
* 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
* 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.
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.
* 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.
* 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.
* 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.
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.
* 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.
* 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.
* 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.
* fix(layout): keep the space before a continuous section's first paragraph after a page break, and stop at merges across pages, as Word does
Word's PDF of word-probes.docx showed three places where docx/layout put lines
on a different page from Word without stopping:
- U7a to U7c: the first paragraph of a continuous section with a page break
before it keeps its space before at the top of the new page, less the space
after of the empty paragraph that ends the section before: 1440 before after
200 is 1238 in Word (LibreOffice 1240). docx/layout left it out.
- U8a7: spaces before a page break that ends a paragraph are on a line as tall
as the paragraph's mark, not the break. docx/layout made a 28-point break's
line 28 points, moved it to the next page, and put the text after the break
a page later than Word.
- U4a: a cell merged down several rows whose text doesn't fit on the page is
broken across pages by Word. docx/layout moved the merge's last row whole and
counted all the merged lines on the page before. It now stops there, before
the merge's first row, until merged rows are laid out across pages.
The probe script, its reader, the .docx and Word's PDF are committed under
scripts/layout-probes, and section-start-space.ts checks U7a to U7c.
* fix(layout): move a merge's first row kept whole to the next page before stopping at the merge
A row kept whole (cantSplit, or of an exact height) that doesn't fit moves to
the next page. When it starts a merge, it moved only after the merge was
checked against the room left on the page, so the layout stopped at a merge
that fits on the next page. The row now moves first, and the merge is checked
where it goes.
* chore(layout): create the folder of the probe document's given path
word-probes.ts created build/word-probes whatever path it was given, so a path
in a folder that didn't exist failed.
* feat(layout): lay out a paragraph kept together that is taller than a column as Word does
Word lays such a paragraph out down only the first column of each page,
from the top of a new page unless it is at the top of the page's first
column, and what follows it goes on below it in that column and into the
next (word-balance.docx W3, and word-keep-together.docx K1 to K5).
LibreOffice breaks it across all the columns. The layout stopped there.
* fix(layout): stop where a paragraph kept together taller than a column would move away from one kept with it
A paragraph kept with the next before it, such as a heading, would be left
on its own at the bottom of the page. Word's probes didn't have one, so what
Word does there isn't known, and the layout stops rather than guessing.
* feat(layout): measure text with Pretext in a page's fonts, and lay it out as Word does
docx/layout can now measure how wide text is with a function the caller
gives it, through estimatePageNumbersWith({ measureWidth }). measureWithPretext
builds one on Pretext, taken from the caller, so docx doesn't depend on it.
Lines still break, tabs still move to their stops, and lines are still as
tall as Word makes them.
* fix: answer only the DevTools messages the Pretext comparison is waiting for
CodeQL flagged calling a callback looked up by an id from Chrome's
messages. Only a number id with a callback waiting for it is answered now.
* fix(docs): open the live examples' editor without a package's types when they can't be fetched
A package's types only help with editing, so a failed fetch or an error
page leaves them out rather than stopping the editor.
* fix(layout): leave out contextual spacing as Word does
Word's PDFs of word-rules.docx (P1) and word-rules2.docx (Q1) showed how it
leaves out the space between two paragraphs of the same style when one has
contextual spacing: it leaves out only that paragraph's own share of the
space, the first's space after, or the part of the second's space before that
is more than the first's space after. 200 after a contextual paragraph and
400 before the next leave 200, and 400 and 400 leave none. docx/layout, as
LibreOffice, left out the whole of the contextual paragraph's space and took
the larger of what was left, 400 and 400.
scripts/layout-probes/word-contextual.ts probes the cases those PDFs don't
settle: next to the empty paragraph that ends a section, and with
doNotUseHTMLParagraphAutoSpacing.
* fix(layout): put the space after the empty paragraph that ends a section only where the next section's space before is less, as Word does
Word's PDFs of word-contextual.docx and word-contextual-adding.docx settle
the cases of contextual spacing the earlier probes didn't:
- With doNotUseHTMLParagraphAutoSpacing, contextual spacing leaves out the
paragraph's own space, as docx/layout had it: 600, 200, 400, 400, 200, 0.
- Next to the empty paragraph that ends a section, Word put 0, 0 and 400
twips where docx/layout put 200, 200 and 600. The empty paragraph's 200
after is never on the page itself: it only takes from the next section's
space before, as it does at the top of a page. word-rules2.docx's Q6b and
Q7b show the same without contextual spacing: 0 after the last line, 200
after the empty paragraph and 0 before leave none, where docx/layout had
200.
Records Word's results in the probe script.
* chore(layout): commit the contextual spacing probes' .docx and Word's PDFs, as the other layout probes are
Follows scripts/layout-probes/README.md, which came with the earlier probes:
the .docx Word opened and the PDF it saved go beside the script, which
writes to build/word-probes by default, and the README lists the probe.
* feat(layout): start a section in the next column as Word does
A section that starts in the next column (SectionType.NEXT_COLUMN) starts in the
next column of the page when the section before has the same columns and one is
left, without evening out the columns before it, and on a new page otherwise, as
Word lays out word-rules2.docx's Q5a to Q5d. It stops at what Word hasn't shown
yet, which scripts/layout-probes/word-next-column.ts probes.
* feat(layout): keep the space before a section's first paragraph in the next column, as Word does
Word's PDF of word-next-column.docx showed the rest: the space before is kept at
the top of the column as at the top of a page (N1, N2), 3 columns into 2 starts
a new page (N3), as many columns of other widths start in the next column (N4),
and columns below text aren't evened out, without the empty paragraph's space
after below them (N6). It still stops at columns of other widths, whose line
width isn't known, and at evening out after a section that started before the
last of 3 or more columns.
* chore(layout): keep the next-column probe's .docx and Word's PDF of it beside it, as the other probes are
* test(layout): say what the next-column test of a paragraph kept with the next checks
Its comment said the paragraph moves to the column after, but a paragraph at the
top of a column stays there, with its space before, as at the top of a page: the
keepNext check only moves what is below something in the column.
Word's PDF of word-rules2.docx showed that the first paragraph of a section
that starts on a new page keeps as much of its space before as is more than
the space after the empty paragraph that ends the section before: 1440 before
after 200 is 1240 (Q2a), and 100 before is 0 (Q2b). With a page break before
the paragraph it is the same, 1240 (Q2c), as the page is already new.
docx/layout already did this since #3602, as it takes no page break at the
start of a new-page section, but nothing tested it. This adds tests of Q2a to
Q2c, a comment where the page break before a paragraph is taken, and a probe in
scripts/layout-probes that checks docx/layout against Word's result.
The documents that showed how Word lays out pages, and the PDFs Word saved of
them, were only in a git-ignored build folder. They are now committed in
scripts/layout-probes, with the .docx Word opened for each PDF and the scripts
that read them, and a README that says what each probe settles, how to make
its .docx, lay it out in LibreOffice and read Word's PDF.
The scripts import from docx, as the demos do, and write their .docx to
build/word-probes.
* fix(layout): make a paragraph's last line as tall as its text, not its mark
The layout made the last line of every paragraph at least as tall as the
paragraph's mark. Word and LibreOffice don't: a line of 8-point text in a
paragraph whose mark is 11 points is 8 points tall, as a document of
probes saved as a PDF from Word 16 for Mac and laid out by LibreOffice
26.8 showed. Text given a smaller size than its style's, as a TextRun's
size does, was laid out too tall, which put a heading of the
text-and-spacing demo on the page after Word's.
The mark now only sets the height of a line with no text on it, such as
an empty paragraph's, or a page break's at the end of a paragraph.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NijyutGmb3bNR3dNRWJken
* ci: draw Calibri Light in Carlito in the shape demos' LibreOffice
LibreOffice has no font for Calibri Light, the heading font of Office's
theme and of docx's default heading styles, and drew it in Noto Serif,
whose lines are 12% taller. So the layout comparison checked docx/layout,
which measures Calibri Light as Calibri, against headings taller than
Word's: Calibri Light is as tall as Calibri. A fontconfig alias makes
LibreOffice draw it in Carlito, as wide and tall as Calibri.
The title of charts-in-templates, in the template's heading font, is now
drawn in Carlito, so its reference image is drawn again. The other 30
pages don't change.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NijyutGmb3bNR3dNRWJken
---------
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
* feat(layout): break table rows across pages between the lines of their cells
Word and LibreOffice break a table row that doesn't fit across the pages
unless it is kept whole (cantSplit), but the layout moved every row to the
next page whole, which put the headings after a table of long rows on the
wrong page. Rows now break between the lines of their cells, with the
header rows repeated above the rest, and without widow control inside
them, as LibreOffice does. Rows kept whole, of an exact height, or whose
text fits but not their set height, still move whole. A row that would
break with merged cells, a table in it, or a height set taller than its
text stops the layout, as nothing checks how those break yet.
LibreOffice's lines are whole twips tall, so lines are rounded to twips:
that matches its line pitch for 80 of 90 font sizes measured, against none
before. The tables demo is at 12 points, as LibreOffice's lines of Times
New Roman at 10 points are a twip taller than the font's metrics.
A new layout demo, tables-across-pages, has rows of each kind across page
breaks. All 185 headings of the 7 demos are on LibreOffice's page.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NijyutGmb3bNR3dNRWJken
* feat(layout): lay out footnotes at the bottom of their pages, and endnotes after the text
The layout stopped at the first footnote, so most reports lost every page
number after it. Now a page's footnotes take room at its bottom, below
their separator, with the space between them, but not before the
separator or after the last, as LibreOffice lays them out. A line whose
one-line footnote doesn't fit below it moves to the next page with it, as
in LibreOffice. LibreOffice continues a longer footnote on the next page,
splitting even one widow control would keep together, and which of its
lines stay isn't followed yet, so the layout stops there, and at a
footnote in a table row that breaks across pages.
Endnotes follow the text, after their separator, as Word and LibreOffice
lay them out. Footnotes are numbered 1, 2, 3 and endnotes i, ii, iii, and
their numbers are as narrow as superscript and as tall as their font.
A new layout demo, footnotes-and-endnotes, has 40 footnotes over 8 pages.
All 18 of its headings are on LibreOffice's page.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NijyutGmb3bNR3dNRWJken
* feat(layout): lay out sections in columns
The layout stopped at the first section in columns. Now each page's
columns are filled in turn, each as wide as the section gives, before the
next page: text moves to the next column when it doesn't fit, or is kept
with what is there, and a column break starts the next column, or a new
page in the last. A section in columns that starts on the same page as
the text before it starts its columns below that text.
Word and LibreOffice even out a section's columns before a continuous
section break, so the next section starts below the longest, and which
lines go in which column isn't followed yet, so the layout stops there,
as it does at a footnote in columns and at a table's header rows repeated
at the top of a column.
scripts/compare-layout.sh reads LibreOffice's text in reading order, so a
page's columns come one after the other rather than side by side. A new
layout demo, columns, has sections of one, two and three columns. All 227
headings of the 9 demos are on LibreOffice's page.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NijyutGmb3bNR3dNRWJken
* feat(layout): write the numbers of pages into NUMPAGES and SECTIONPAGES fields
paginate counted the pages, but nothing wrote the count, so "Page 3 of"
footers were written without their number, which applications that show
fields as they are written show blank. Now docx/layout works out the
number of pages of the document, when it lays all of it out, and of each
section whose pages are its own, laying the pages out again until the
numbers, which can change how lines wrap, stay the same.
EstimatedPageNumbers gains pageCount and sectionPageCounts, and docx
writes them into the NUMPAGES and SECTIONPAGES fields of the body, and
of the headers and footers once the body is written, with the page
references in them. A header or footer shows the number of pages of the
sections it is on only when they all have the same. A section that
shares a page with another, or has a blank page before or after it, is
left blank, as is a field in a number format of its own. Like PAGEREF
fields, those with a numeric picture (\#) are left as they are.
scripts/compare-layout.ts checks the numbers of pages written into the
demos against the pages of LibreOffice's PDFs: both demos with them match.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NijyutGmb3bNR3dNRWJken
* fix(layout): keep a paragraph with the next on a new page when they fit there
Whether what is kept together fits in a fresh column was measured from
where the page's columns start, which is partway down the page below a
continuous section break. When the column is the last, the kept
paragraphs go on a new page, whose text starts at the top, so a heading
that would fit there with the next paragraph stayed at the bottom of the
page, and the paragraph moved on alone.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NijyutGmb3bNR3dNRWJken
* fix(layout): stop at a section whose columns differ in width
A paragraph is broken into lines at the width of the column it starts
in, and a paragraph that goes on into a column of another width kept
those lines, so its height, and the pages after it, were wrong. Breaking
the rest of a paragraph again needs to know where each line starts in
its text, so a section with columns of different widths stops the
layout, and the page numbers after it are left blank.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NijyutGmb3bNR3dNRWJken
* fix: fail the layout comparison when LibreOffice didn't lay out a document
The comparison skipped a document without the text of LibreOffice's
pages, as when LibreOffice fails to convert it, and reported its
headings as matching, so CI could pass without comparing it. Now it
fails, and names the documents LibreOffice didn't lay out.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NijyutGmb3bNR3dNRWJken
---------
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
* feat: math from LaTeX in docx/math, with accents, bars, braces, boxes, phantoms and large operators
latexToMath turns LaTeX math into docx's math components, to go in a Math.
It reads what LaTeX, MathJax and KaTeX write: fractions, roots, scripts,
large operators, functions, \left and \right, accents, braces, boxes,
fonts, \text, and the environments for matrices, cases and aligned
equations, with \tag. It throws for what it can't read, saying where.
The parts LaTeX needs are public components too: MathLargeOperator,
MathAccent, MathBar, MathBrace, MathBox and MathPhantom in docx/math, and
in docx, MathRun's style, script and literal, MathFraction's type, and
limits on MathSum and MathIntegral.
Closes#2994
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* fix: draw MathRun alphabets upright unless given a style, and escape the \middle| table cell
A run with a script but no style wrote no m:sty, which Word draws as italic, so sansSerif came out italic
sans-serif rather than the upright 𝖠 its docs show. A script other than roman now defaults to a plain style,
as LaTeX draws its alphabets; a given style is kept. latexToMath already gave each alphabet its style.
The | after \middle in the LaTeX docs' table ended the cell, splitting the row into three.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
docx 9.8.0 is the first release with docx/math, and its types import the math classes from docx and
export them again. The check renamed only the classes declared in an entry point's own types, then
looked for that renamed copy of every exported class, so it reported all 24 math classes as breaking.
It now renames only the classes an entry point declares. The ones it imports from docx resolve to the
build's classes, and are compared in docx's own check.
Claude-Session: https://claude.ai/code/session_01TmyffPd5QMJYjXiMBQ3EnZ
Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
* chore: organise the demos into a folder for each topic
The demos were one flat list of 124 numbered files, which made it hard to
find the one you wanted. Each is now in a folder for its topic, such as
demo/tables/basic-table.ts, with a name that says what it shows instead of
a number.
npm run demo lists every demo and narrows the list as you type. It also
takes a demo's path or name, a topic, or search words.
The demos' images moved to demo/assets/images, as images is now a topic.
The docs' links, the Demos workflow, the shape render script and its
reference images use the new paths and names.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TmyffPd5QMJYjXiMBQ3EnZ
* fix: list only a topic's own demos, and drop stale output file names
npm run demo text also offered the text-boxes demos and others with
"text" in their name, so a topic now searches for "text/". Three demos
logged their old numbered file name after writing My Document.docx.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>