* 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.
Layout probes
Documents made to find out how Word lays out pages, where its rules aren't written down. docx/layout follows what they
showed. Each line of text names its probe, such as Q3a fill 12, so it can be found in a PDF of the document, and each
probe starts on a page of its own or is marked off by lines above and below it.
Each probe has the .docx Word opened and the PDF Word saved from it, beside the script that makes it. They were saved
from Word 16 for Mac on 2026-09-30 and 2026-10-01 (Save As, PDF, Best for printing). docx writes some of them differently
now, so the committed .docx is the one each PDF is of: word-rules and word-tables were made before docx wrote a
table style and each cell's width.
| Probe | What it settles | Reader |
|---|---|---|
word-rules |
P0 to P10: the space before the first paragraph and at the top of a page, the space between paragraphs and contextual spacing, widow and orphan control, the height of headers and footers and of table rows, the line pitch of each font and size, footnotes that don't fit on the page, table rows broken across pages, columns, and paragraphs of only spaces | word-rules.py |
word-rules2 |
Q1 to Q7: contextual spacing with each paragraph's share, the space before a section's first paragraph on a new page, keepLines and widow control inside table rows, header rows in columns, sections that start in the next column, footnotes in columns, and columns of different widths | word-rules2.py |
word-balance |
W1 to W9: how the columns before a continuous section break are evened out, with column breaks, space after, paragraphs kept together or with the next, tables, lines of two sizes, columns that go on to the next page or start low on it, and a page of exactly 51 lines before a new section | word-balance.py |
word-balance2 |
S1 to S7 and C1 to C2: the space after columns and before the next section, which the empty paragraph that ends a section keeps, and column breaks in columns that are evened out | word-balance.py |
word-keep-together |
K1 to K5: a paragraph kept together that is taller than a column, which Word lays out down only the first column of each page, from a new page unless it is at the top of one, and what follows it | word-balance.py |
word-next-column |
N1 to N6: where a section that starts in the next column starts after columns of other numbers and widths, the space before its first paragraph there, three in a row, and in columns below text, followed by a continuous section break | word-next-column.py |
word-column-widths |
R1 to R6: widow control in a paragraph that goes on into a column of another width, where its lines are broken again, and the widths of a table sized to its text that goes on into a wider column | word-column-widths.py |
word-contextual |
X1 and Y1: contextual spacing next to the empty paragraph that ends a section, whose space after is never on the page itself, and with doNotUseHTMLParagraphAutoSpacing. word-contextual-adding is the second document it writes, with that setting |
word-contextual.py |
word-tables |
T1 to T13: the widths Word gives table columns from columnWidths, cell widths and table widths, and a word longer than its column (T7) |
pagelines.py |
word-autofit |
A1 to D1: the widths Word gives the columns of tables whose cells have no widths, which it sizes to their text | word-autofit.py |
word-long-words |
L1 to L8: how Word widens a column for a word longer than its cells give it, in tables whose cells all have widths, and what it takes from the other columns. word-long-words-layout.ts prints docx/layout's widths for them |
word-autofit.py |
word-probes |
U1 to U8: the widths of tables given no widths with merged cells or tables in cells, footnotes continued, footnotes in table rows and in columns, rows with merged cells, a table in a cell or a set height across pages, rows taller than a page, the space before a continuous section's first paragraph with a page break before it, and lines of only spaces before a break | word-probes.py |
word-header-columns |
H1 to H8: a table's header rows repeated at the top of each column, in columns evened out before a continuous section break and on a second page, the columns of a table that goes on from the page before evened out, and the line of the empty paragraph that ends a section after a table | word-header-columns.py |
word-footnotes-in-columns |
N1 to N12: footnotes in columns, which Word lays out in the columns too, one after the other from the first and evened out, whichever column they are referred to from, with every column ending above them. Below columns that start below text across the page, or are evened out above it, a footnote that wraps in a column, one too long for the room below its reference, and footnotes referred to from text across the page and from columns below it | word-footnotes-in-columns.py |
word-line-heights |
H and T1 to T4: the height of lines in each font, size and line spacing, measured over a page, and how a table row breaks across pages: with room for the table's bottom border below it and for the space after a cell's paragraph, rows of an at-least height with less room than their height, and a page that ends between rows | word-line-heights.py |
word-page-number-formats |
P: page numbers and page references in each number format at 4, 1234 and 0, L: lists in each format from 1 to 60 and at larger numbers, and C1 to C11: chapter numbers, with each separator, from headings of each level and numbering, before and after a heading on the same page | word-page-number-formats.py |
word-page-number-formats2 |
P: page numbers in each format where its letters run out and at 0, and C12 to C15: chapter numbers after a heading that isn't numbered, from one numbered on its own too, from a level with no number in its text, and from a style based on heading 1 | word-page-number-formats.py |
word-unicode |
K, E, L, H, N, W, A, P, KR, T, R and G: which characters Word keeps from starting or ending a line of Chinese and Japanese, with no language and in each language, hanging punctuation, kinsoku and wordWrap off, Latin words and numbers after ideographs, breaks after dashes, slashes and zero-width spaces, the space between ideographs and Latin text, punctuation compression, Korean and Thai, right-to-left paragraphs and runs, and the height of lines of East Asian, Thai and Hebrew fonts | word-unicode.read.ts |
word-unicode2 |
S, E, H, A, W, T, F and G: every character of Word's lists in Japanese, Chinese, Korean and English, hanging punctuation and the space around Latin text in Japanese, wordWrap off, zero-width spaces, Thai without marks above or below its letters, the font and size of Hebrew and Latin in runs that are right to left and runs that aren't, and the height of lines of 21 East Asian fonts. word-unicode2-layout.ts compares where docx/layout breaks the lines of S and E with where Word does |
word-unicode2.read.ts |
fsplit |
How a footnote that doesn't fit below its reference goes on to the next page. Laid out in LibreOffice only, so it has no PDF from Word: Word's split of an 8-line footnote is word-rules P7b |
pagelines.py |
Make a probe's .docx
From the root of the repository:
npm run run-ts -- scripts/layout-probes/word-rules2.ts
It writes build/word-probes/word-rules2.docx. fsplit imports docx/layout, so build the package first
(npm run build). The others import only docx, as the demos do.
Lay it out in LibreOffice
To compare with Word, convert the .docx to a PDF in the Docker image CI lays out the layout demos in, which has a
pinned LibreOffice and its fonts. Build it from scripts/shape-demos/Dockerfile, then:
docker run --rm --platform linux/amd64 --user "$(id -u):$(id -g)" -v "$PWD/build/word-probes:/work" -w /work \
<image> soffice -env:UserInstallation=file:///tmp/profile --headless --convert-to pdf word-rules2.docx
LibreOffice keeps its settings in a profile, which has to be somewhere the user running it can write, hence
-env:UserInstallation.
LibreOffice's PDF takes the .docx's name, so it replaces a PDF from Word of the same name in that folder. Keep
Word's PDFs somewhere else, or rename LibreOffice's.
Read a PDF
Each reader reads where each line is from pdftotext -bbox-layout, from Poppler, which is in the image above and
in Homebrew (brew install poppler). For Word's PDF of word-rules2:
pdftotext -bbox-layout scripts/layout-probes/word-rules2.pdf build/word-probes/word-rules2.html
python3 scripts/layout-probes/word-rules2.py build/word-probes/word-rules2.html
word-unicode.read.ts and word-unicode2.read.ts are TypeScript, for Intl.Segmenter, which they check Thai's line breaks with:
pdftotext -bbox-layout scripts/layout-probes/word-unicode2.pdf build/word-probes/word-unicode2.html
npm run run-ts -- scripts/layout-probes/word-unicode2.read.ts build/word-probes/word-unicode2.html
word-page-number-formats.py reads both page number probes, from the text of pdftotext -layout rather than where each
line is:
pdftotext -layout -enc UTF-8 scripts/layout-probes/word-page-number-formats.pdf build/word-probes/word-page-number-formats.txt
python3 scripts/layout-probes/word-page-number-formats.py build/word-probes/word-page-number-formats.txt
word-balance.py reads word-balance, word-balance2 and word-keep-together, and word-contextual.py reads both
documents word-contextual.ts writes, word-contextual and word-contextual-adding. word-autofit.py reads the borders
of the tables of word-autofit and word-long-words too, from each page as SVG:
pdftotext -bbox-layout scripts/layout-probes/word-autofit.pdf build/word-probes/word-autofit.html
pdftocairo -svg -f 1 -l 1 scripts/layout-probes/word-autofit.pdf build/word-probes/word-autofit-1.svg
pdftocairo -svg -f 2 -l 2 scripts/layout-probes/word-autofit.pdf build/word-probes/word-autofit-2.svg
python3 scripts/layout-probes/word-autofit.py build/word-probes/word-autofit
word-probes.py takes the name of the PDF without its extension, and reads its HTML and, for U1's tables, its first
five pages as SVG:
pdftotext -bbox-layout scripts/layout-probes/word-probes.pdf build/word-probes/word-probes.html
for n in 1 2 3 4 5; do pdftocairo -svg -f $n -l $n scripts/layout-probes/word-probes.pdf build/word-probes/word-probes-$n.svg; done
python3 scripts/layout-probes/word-probes.py build/word-probes/word-probes
word-line-heights.py also measures the height of lines in other PDFs, such as word-rules' P3 and Word's PDFs of the
layout demos, and the lines of the rows of tables-across-pages that break across pages: see the top of it.
pagelines.py prints the lines of one page with their top, height, the gap from the line above in twips, and left
edge, which shows where each column of a table starts:
python3 scripts/layout-probes/pagelines.py build/word-probes/word-tables.html 1
Word puts text on a grid of 1/300 inch (4.8 twips), so a gap between two lines is only good to about 5 twips. Measure heights over many lines.
A new probe
When Word's rule for something isn't known, write a probe in the shape of word-rules2.ts, with a reader for its PDF, and have
it saved from Word as a PDF with the same name as its .docx. Commit the .docx, the PDF and the reader beside the
script, and leave out LibreOffice's PDFs, pdftotext's HTML, SVG pages and Word's ~$ lock files.