Commit Graph
225 Commits
Author SHA1 Message Date
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
Dolan 4b607b4bfc 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 (#3615)
* 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.
2026-10-01 20:37:46 +01:00
Dolan 09a557bc7e feat(layout): lay out a paragraph kept together that is taller than a column as Word does (#3607)
* 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.
2026-10-01 13:58:20 +00:00
Dolan 2f06726259 feat(layout): measure text with Pretext in a page's fonts, and lay it out as Word does (#3610)
* 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.
2026-10-01 14:50:57 +01:00
Dolan 47ffa2a81b fix(layout): leave out contextual spacing as Word does (#3605)
* 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.
2026-10-01 14:50:04 +01:00
Dolan 6ced50f328 feat(layout): start a section in the next column as Word does (#3608)
* 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.
2026-10-01 02:58:14 +01:00
DolanandClaude Opus 5.5 91cb98e300 fix(layout): break table rows and lay out lines of only spaces as Word does (#3603)
Word's PDFs of probes of its layout (word-rules.docx and word-rules2.docx)
showed three ways docx/layout put lines on a different page from Word:

- A paragraph of only spaces is as tall as its paragraph mark, whatever the
  size of its spaces, in Word and LibreOffice. docx/layout took the larger of
  the spaces and the mark.
- Widow and orphan control, and keepLines, hold lines back in a table row that
  breaks across pages, as they do on a page. A 4-line cell after 48 lines goes
  2 and 2, after 50 the row moves, and a 4-line paragraph kept together moves
  the row. docx/layout split them as LibreOffice does, 3 and 1, and 1 and 3.
- When they hold back all of a cell's lines, the row moves to the next page
  whole, rather than leaving the other cells' first lines behind.

Columns being evened out before a continuous section break also ended above a
row broken across them, when its part in the first column was the longer: that
part didn't count toward the first column's height. It didn't show while a
row's lines only went on into a second column as long as the first.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-01 01:01:50 +01:00
DolanandClaude Opus 5.5 fe3fda05a1 fix(patcher): format a patched link's text as the placeholder is, and add its relationship only where it's used (#3601)
* fix(patcher): format the text of a link in a patch as the placeholder is

With keepOriginalStyles, a patch's runs take the placeholder's formatting, but
the runs in a hyperlink didn't. In 9.5.1 the placeholder's w:rPr was put in the
w:hyperlink itself, which the schema doesn't allow and the Open XML SDK validator
rejects, so the link could be lost (#3265). #3540 stopped giving it to the
hyperlink, so the file is valid, but the link's text lost the template's
formatting instead.

The runs in inserted content, such as a hyperlink's, now take the placeholder's
formatting, keeping what they set themselves, such as the link's color. Runs
inside a run, such as in a shape's text box, are that content's own, and are
left alone.

Closes #3265

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(patcher): add a hyperlink's relationship only to the parts that have its placeholder

A patch's ExternalHyperlink was given a relationship in every part the patcher
looked in, whether the placeholder was there or not, so a patched document had
a relationship to the link's address from its styles, settings, font table,
theme, notes, headers and footers, each in a relationships part made for it.

The relationship is now only added to a part the placeholder is in.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test(patcher): cover inserted content without runs, such as a bookmark's start and end

The placeholder's run properties are only given to runs, so a bookmark's start
and end, which have no children, are inserted as they are. Nothing tested this,
so the branch that leaves them alone wasn't covered.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-01 00:09:34 +01:00
DolanandClaude Opus 5.5 0e64641438 feat(layout): even out columns before a continuous section break, as Word does (#3600)
* feat(layout): even out columns before a continuous section break, as Word does

docx/layout stopped at columns followed by a continuous section break, and left
every page number after them blank. Word and LibreOffice even those columns out,
and start the next section below the tallest.

The layout now does the same. What is in the columns on the page is laid out
again in the shortest columns it fits in, filled from the first. It keeps
widow control, keepNext and keepLines, breaks tables between rows and rows
between lines, and evens out the columns on the last page of a section that
goes on from the page before. Word put 21 lines in 2 columns 11 and 10, and 20
in 3 columns 7, 7 and 6, as LibreOffice does. LibreOffice agreed with the
layout on probes of each of the other cases.

Three fixes, which the columns demo needs now that its sections are continuous:
- a column break at the end of a paragraph puts the paragraph's mark on a line
  at the top of the next column, as in Word and LibreOffice. Only a page
  break's mark stays on the break's line;
- the empty paragraph docx writes at the end of a section takes no room before
  a continuous section, as in Word and LibreOffice;
- paragraphs kept with the next move to a new page when they don't fit in
  columns that start low on the page, rather than only to the next column,
  which is as short.

Columns with a column break in them still stop the layout, as Word hasn't shown
how it evens those out.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NijyutGmb3bNR3dNRWJken

* fix(layout): end columns before a continuous section break as Word does

A PDF of probes saved from Word 16 for Mac showed where Word differs from
LibreOffice, which the layout followed:
- Word leaves columns with a column break in them as they are, rather than
  evening them out, and starts the next section below the longest. The layout
  stopped at them;
- the next section starts below each column's last paragraph and the space
  after it, and its space before is only as much as is more than the section's
  last paragraph's space after. LibreOffice puts that space after below the
  tallest column, whichever column it is in. Word's PDF of the columns demo
  starts its heading after three columns 7 points below them, from 12 before
  and 5 after;
- the empty paragraph docx writes at the end of a section takes no room before
  a section on a new page either, so a full page before one isn't followed by
  a blank one. A paragraph kept with the next at the end of a section is kept
  with nothing;
- a paragraph kept together that is taller than a column is moved to a new
  page and broken across only the first column of each page. The layout now
  stops at it.

The layout put the next section where Word put it in each probe, and all 24
headings of the columns demo on Word's page.

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>
2026-09-30 23:21:04 +01:00
DolanandClaude Opus 5.5 8ff4f6baac feat(patcher): repeat the rows of a table in a template for each row of data, with PatchType.TABLE_ROWS (#3598)
* feat(patcher): repeat the rows of a table in a template for each row of data, with PatchType.TABLE_ROWS

patchDocument couldn't add rows to a table designed in a template. A DOCUMENT patch
in a cell put a table inside the cell, and table rows there made a corrupt file, so
the whole table had to be made in code, losing its look.

A TABLE_ROWS patch repeats the rows that hold its fields, such as {{items.name}} for
the patch items, once for each of its rows, and patches each copy's fields with the
row's patches. The table's other rows, such as its header, are kept as they are.
- Rows next to each other are repeated together, such as a row for an item and a
  row for its notes.
- A field a row has no patch for, or one that is undefined, is left empty.
- With no rows, the rows are removed, and so is a table left with none.
- The rows are repeated before the other patches are applied, so a placeholder such
  as {{currency}} in a repeated row is patched in every copy.
- A field's patch can repeat the rows of a table in the copy in turn.
- Like content pasted in Word, only the first copy keeps the row's bookmarks and the
  ids Word gives paragraphs, rows and content controls, which must be unique.

A DOCUMENT patch that replaces the last paragraph of a cell with a table, or with
nothing, now ends the cell with an empty paragraph. Word finds a cell that doesn't
end with a paragraph unreadable, as TableCell already knows.

Closes #3202
Fixes #3038

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* docs(patcher): say what a nested table rows patch does when its fields are in the repeated row itself

A TABLE_ROWS patch for a row's field repeats the rows that hold its fields, as at
the top level. When those fields are in the repeated row itself, rather than in a
table in it, that row is the nearest to hold them, so it is repeated for each of the
field's rows: a row for each item with its order's fields, and none for an order
without items. This wasn't documented or tested.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-30 22:35:04 +01:00
DolanandClaude Opus 5.5 42aa5eb59d docs(page-numbers): show how to leave pages out of the total with PageNumber.TOTAL_PAGES_IN_SECTION (#3597)
* docs(page-numbers): show how to leave pages out of the total with PageNumber.TOTAL_PAGES_IN_SECTION

The Page Numbers page didn't mention PageNumber.TOTAL_PAGES_IN_SECTION, so
there was no way to find out how to leave a cover page or contents out of
the number of pages (#3125).

Adds a section with a live example: the cover and contents in a section of
their own, and "1 / 3" in the footers of the next section. LibreOffice
doesn't work out a section's number of pages, so the example gives the
document estimatePageNumbers, and the page says why. Also links the
existing total-pages-in-section demo, and links Layout's mention of it to
the new section.

Closes #3125

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* docs(page-numbers): fill in the section totals of the total-pages-in-section demo

The Page Numbers page now includes this demo as a live example, but its
section totals were blank in LibreOffice and in the page's preview, because
only Word works out SECTIONPAGES. The demo now gives the document
estimatePageNumbers, as the page's own example does.

Each section's paragraph also ended with a page break. LibreOffice drops the
empty page after it, but the estimator counts it, so the totals would read 3
for sections LibreOffice lays out on 2 pages. Without the trailing breaks,
each section is 2 pages, and LibreOffice shows "1 to 2" and "2 to 2".

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-30 22:27:01 +01:00
DolanandClaude Opus 5.5 d1f397b77f feat(layout): continue a footnote that doesn't fit below its reference on the next page, as Word does (#3596)
docx/layout stopped at any footnote that didn't fit below its reference, and left
every page number after it blank. Word and LibreOffice continue such a footnote on
the next page.

The layout now does the same:
- as many of the footnote's lines as fit go below its reference's line, and the page
  ends there, so the text after it goes on the next page;
- the rest goes first at the bottom of the next page, below the continuation
  separator, which is now read, and above that page's own footnotes;
- a document that ends with one gets a page for the rest.

Word split 8-line footnotes 6 and 2, and 3 and 5, in a PDF of probes saved from Word
16 for Mac, as LibreOffice does. LibreOffice agreed with the layout on a reference in
the middle of a paragraph, a page's own footnotes below a continued one, and the
page at the end.

What Word does in these cases isn't known, so they still stop the layout:
- a split that would leave one line of the footnote on either page;
- a footnote of several paragraphs or a table;
- a reference on a line that widow control, keepLines or keepNext would hold back;
- a footnote too long for the next page as well.

The footnotes-and-endnotes demo has one footnote split 3 and 8, which CI's
LibreOffice comparison checks.


Claude-Session: https://claude.ai/code/session_01NijyutGmb3bNR3dNRWJken

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-30 21:21:50 +01:00
DolanandClaude Opus 5.5 f6a926c2b8 feat(layout): size the columns of tables given no widths to their text, as Word does (#3595)
Word sizes the columns of a table whose cells have no widths to their text, whatever
its grid says, where docx/layout laid them out on the grid, in equal columns, so the
page numbers after such a table could be off. Tables made without columnWidths or cell
widths are like that.

The layout now sizes them as Word's PDFs of two probe documents showed:
- each column as wide as its longest line, or the width its cells give it, and never
  narrower than its longest word;
- widened in proportion to fill the table's own width, when it has one;
- when they don't fit, the columns sized to their text narrowed toward their longest
  words, each by its share of the width they give up, and the columns given widths
  kept.

It matched Word's widths to within 17 twips in 13 probe tables. Cells merged across
columns, and tables in cells, in a table given no widths still stop the layout, since
Word's rule for them isn't known yet.


Claude-Session: https://claude.ai/code/session_01NijyutGmb3bNR3dNRWJken

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-30 20:35:15 +01:00
DolanandClaude Opus 5.5 8a79f10344 feat(table): write cells' widths from columnWidths and Word's default cell margins, so tables look and lay out the same in Word (#3594)
* feat(table): write each cell's width from columnWidths, so Word keeps them

Word lays a table out from its cells' widths (w:tcW), and sizes the
columns of cells without one to their text, whatever the grid
(w:tblGrid) says. docx wrote columnWidths only to the grid, so in Word
they did nothing unless every cell was given the same width again, as
#517, #352, #1099 and #561 found. A table saved as a PDF from Word 16 for
Mac showed it: "Entry" shrank to its text and gave its room to the other
columns.

Each cell without a width of its own now gets the width of the columns it
spans, the cells added for rowSpan included. When the table's width is
wider than its columns add up to, the cells' widths are scaled up to fill
it, as Word and LibreOffice lay the columns out, so columnWidths given as
proportions of a 100% table, as some issues suggest, keep them. The grid
is written as given.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NijyutGmb3bNR3dNRWJken

* feat(styles): write Normal Table, the default table style, with Word's cell margins

docx wrote no table style, and without one Word gives cells no margins,
so their text touched the borders: in a PDF saved from Word, the text of
each cell started half a point inside its border. LibreOffice gives them
the 0.075 inches (108 twips) on the left and right that Word gives the
tables it makes, so the same table had narrower lines of text there.

docx now writes Normal Table as Word writes it in its own documents, the
default table style with those margins, so tables look the same in Word
as tables made in it, and as in LibreOffice. A Normal Table in
externalStyles takes its place, and margins given to a table or cell
still win.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NijyutGmb3bNR3dNRWJken

* fix(layout): lay cells out at their own widths, with the margins of their table's style

The layout took each cell's width from the grid and gave every cell
margins of 5.4 points, as LibreOffice does. Word goes by a cell's own
width in twips when it has one, and takes cell margins from the table's
style, or the default table style, and gives none without either.

Cells are now laid out at their own width in twips when they have one,
and their margins come from the table's style and the styles it is based
on, or the default table style when it has none, then the table's own.
With docx's Normal Table, that is the 5.4 points it was, and the table
demos' headings stay on LibreOffice's pages. The docs say to give tables
columnWidths for their page numbers to be right, as Word sizes the columns
of a table without widths to their text.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NijyutGmb3bNR3dNRWJken

* docs(table): don't suggest columnWidths as proportions for readers that go by the grid

The warning about readers that lay tables out from the grid alone ended by
suggesting columnWidths as proportions of a table with a width. Only the
cells' widths are scaled to the table's width, and the grid is written as
given, so those readers still take [20, 80] as 20 and 80 twips. The
warning asks for real twips again, and the paragraph on scaling says it
applies to the readers that go by the cells' widths.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NijyutGmb3bNR3dNRWJken

* fix(layout): stop at a table whose rows give a column different widths

With cells laid out at their own widths, two rows could give the same
column different widths, such as a one-column table whose first cell is
1000 twips wide and whose second is 3000. Word settles the column's width
in a way the layout doesn't follow yet, so at least one row's lines were
broken at the wrong width. The layout now stops at such a table and
leaves the page numbers after it blank, as it does for other things it
can't lay out. Widths a point apart, from rounding, still count as equal.

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>
2026-09-30 17:57:02 +01:00
DolanandClaude Opus 5.5 3f5a17cd10 feat(layout): break table rows across pages, lay out footnotes and columns, and write the numbers of pages (#3592)
* 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>
2026-09-30 12:50:47 +01:00
Dolan 0307c6556d feat: docx/layout, which writes the page numbers of tables of contents and page references (#3591) 2026-09-30 10:46:29 +01:00
Dolan 9737ae7f91 fix: write the entries of a table of contents from the headings (#3590) 2026-09-29 23:33:09 +01:00
DolanandClaude Opus 5.5 7678105585 feat: footnotes and endnotes in patchDocument (#3589)
* feat: footnotes and endnotes in patchDocument

patchDocument takes footnotes and endnotes, shaped as in a Document, for the
notes that patches refer to with a FootnoteReferenceRun or an
EndnoteReferenceRun. Before, a patch could insert a reference but not the note,
so the document pointed at a footnote that didn't exist.

Each reference a patch inserts gets a note of its own, with an id that none of
the template's notes have. Only the notes that are referred to are written. A
template without notes is given the part, its relationship and content type,
and the note styles it's missing.

Closes #3113

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NijyutGmb3bNR3dNRWJken

* test: escape the dots in the relationship types the notes tests match

CodeQL flagged a regular expression built from a relationship type's URL,
whose dots matched any character.

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>
2026-09-29 21:36:37 +01:00
DolanandClaude Opus 5.5 5da78710c5 fix: keep LEFT and RIGHT on their side of the page in a right-to-left paragraph (#3587)
Word reads w:jc left and right as the start and end of the paragraph, so a
bidirectional paragraph with AlignmentType.RIGHT was left-aligned. Write left
and right swapped when the paragraph is bidirectional.

Closes #3419


Claude-Session: https://claude.ai/code/session_01NijyutGmb3bNR3dNRWJken

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-29 19:54:50 +01:00
Dolan d95014e61f fix: keep paragraph and character styles given with externalStyles (#3584) 2026-09-29 16:59:36 +01:00
Dolan 4bf92fd1cc fix: throw for a drawing too big for Word to open (#3583) 2026-09-29 16:41:18 +01:00
Dolan f342f0d7f0 fix: work through the unresolved review comments on recently merged PRs (#3581) 2026-09-29 16:22:02 +01:00
DolanandClaude Opus 5.5 eb206519ae feat: math from LaTeX in docx/math, with accents, bars, braces, boxes, phantoms and large operators (#3570)
* 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>
2026-09-28 17:07:59 +01:00
DolanandClaude Opus 5.5 c169dd1a83 feat: live examples in the docs, which run, draw the document next to the code, and can be edited and downloaded (#3556)
* feat: live examples in the docs, which run, draw the document next to the code, and can be edited and downloaded

The docs' code examples were plain snippets, often fragments that couldn't run. A code block marked ```ts live is now
an example readers can see and change: the page runs it with docx, draws the document it makes next to the code with
docx-preview, and offers the .docx to download. Clicking the code opens an editor (Monaco) that knows docx's types,
the pages are drawn again as the code changes, and View closes the editor. Split, Code and Preview show the code and
the pages side by side, or one of them. The demos a page includes are live too.

Every example in the docs that builds or patches a document is now live: 443 of them, each a whole file with its
imports, so it runs as written. Code about other things, such as installing, bundlers or servers, stays plain.

Running them showed docs that were wrong, fixed here:
- Landscape: docx swaps a landscape page's width and height itself, so the docs' swapped sizes made a tall page
  marked as landscape
- Comments: `comments.children` takes plain objects, not `new Comment(...)`
- Custom properties are text only
- APIs that no longer exist: MathText, Row, `.then` on Packer.toStream, patchDocument(buffer, ...),
  createParagraph().heading1(), a border's `value`, TextRun's `.break()`
- The templates and patcher examples read files that aren't in the repository. They now patch the templates in
  demo/assets, or make their own

`npm run build.docs` copies docx's browser builds into docs/live-examples/lib, so the docs use the docx they
describe, and the Pages workflow runs it. The folder is gitignored, and cspell and ESLint skip it.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TmyffPd5QMJYjXiMBQ3EnZ

* docs: the bullet examples draw a bullet, and the patcher's usage writes the patched document

"ὠ" is U+1F60, a Greek omega, since \u takes four hex digits, so the two live bullet examples on
the numbering page drew it as each item's bullet. They use "•" now. The patcher's usage example
now awaits patchDocument and writes the document it makes, as the example after it does.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TmyffPd5QMJYjXiMBQ3EnZ

---------

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-28 13:00:29 +01:00
Dolan edb96fb088 fix: write Normal as the default paragraph style, so Pages draws math at its size, and draw angle brackets in LibreOffice (#3554) 2026-09-27 20:29:16 +01:00
Dolan 7ada74f6d1 feat: docx/math, with matrices, cases, equations lined up with numbers, and brackets of any characters (#3553) 2026-09-27 17:30:38 +01:00
DolanandClaude Opus 5.5 e0ff13ee3d chore: organise the demos into a folder for each topic (#3552)
* 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>
2026-09-27 01:40:45 +01:00
371fb186ef feat: support run properties on ImageRun (#3547)
* feat: support run properties on ImageRun

* fix: review fixes for the run formatting of images

- Rename ImageRun's `runProperties` to `run`, the name Paragraph, styles and numbering give run formatting
- Build an image's run the same way whether or not it is a tracked revision, so the breaks and text of a TextRun's
  options given as `run` are ignored in w:ins and w:del too, instead of adding a line break before the image
- Give ChartRun, ShapeRun, ShapeGroupRun and ShapeCanvasRun a `run` option as well, so they can be raised and lowered
- Write an image with both `insertion` and `deletion` as w:ins > w:del > w:r, instead of dropping the deletion
- Pass an image's `solidFill` on to its picture, which ignored it
- Docs: move the section out of Positioning, where it split that section from its Usage, and read pizza.gif, which
  exists, instead of pizza.png
- Test run formatting with floating, SVG, style and tracked images, and with the options of a TextRun

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test: import the chart test's run options type apart from docx's, so it merges with master

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Dolan Miu <dolan_miu@hotmail.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-27 01:18:02 +01:00
Dolan 714148ad11 docs: charts from Mermaid's pie and XY charts (#3551) 2026-09-26 23:58:35 +01:00
Dolan b04040c7b0 feat: stock, pie of pie and bar of pie charts, categories in groups, trendlines, error bars and more chart options (#3550) 2026-09-26 23:26:04 +01:00
Dolan b7a4bb66b8 feat: new data for a template's charts, with ChartDataPatch (#3549) 2026-09-26 21:57:32 +01:00
Dolan 62f4ecd42d feat: charts in templates, with patchDocument (#3548) 2026-09-26 17:58:53 +01:00
Dolan 43a4e0ce67 feat: combo, radar and bubble charts, and more chart options (#3546) 2026-09-26 13:02:03 +01:00
Dolan 6207d0067f feat: native Word charts in docx/charts (#3545) 2026-09-26 10:15:50 +01:00
DolanandClaude Opus 5.5 2a1cc45dcb fix: make every demo's document valid, and check them all with the Open XML SDK validator (#3543)
* fix(demos): give each image its type, and each revision in demo 98 an id of its own

ImageRun's type is required, but 11 demos left it out. The image was then written as <hash>.undefined, which isn't in
the package's content types, so Word can't open it and the Open XML SDK validator stops there. The demos now give the
type of the file or base64 data they read, as demos 5 and 114 do.

Demo 98 gave the same revision id to each mark of one change, such as every cell of an inserted row. Word needs each
w:ins, w:del, w:cellIns, w:cellDel and property change to have an id of its own, so they are now numbered in order.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TmyffPd5QMJYjXiMBQ3EnZ

* fix(shapes): write a custom dash or gap of 0 as the smallest length Office accepts

A custom dash of length 0 with round caps draws dots, as demo 111 does. ISO allows a:ds d="0", but Office's schema
wants d and sp to be at least 1, so the Open XML SDK validator rejected it. A dash or gap of 0 is now written as 1,
a thousandth of a percent of the line width.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TmyffPd5QMJYjXiMBQ3EnZ

* fix(numbering): write a level's alignment as left, center or right

Office's schema only allows left, center and right in w:lvlJc, where a paragraph's w:jc takes every AlignmentType.
Levels default to AlignmentType.START, so every numbering part failed the Open XML SDK validator.

A level's alignment is now written as Office allows: START and the justified alignments as left, END as right.
The alignment option's type is unchanged.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TmyffPd5QMJYjXiMBQ3EnZ

* fix(numbering): write a level's children in the schema's order

A level wrote w:suff before w:isLgl, and w:pStyle after w:lvlJc. CT_Lvl puts w:pStyle after w:numFmt, and w:isLgl
before w:suff, so a level with a paragraph style, or with legal numbering and a suffix, didn't match the schema.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TmyffPd5QMJYjXiMBQ3EnZ

* fix(demos): write demo 19's base64 string as the document it encodes

Demo 19 wrote the base64 text itself to My Document.docx, which isn't a document, so Word can't open the file and it
can't be checked. It now decodes the string before writing it, which also shows that the string is the whole document.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TmyffPd5QMJYjXiMBQ3EnZ

* fix: write paragraph borders in the schema's order

Paragraph borders were written top, bottom, left, right, where CT_PBdr wants
top, left, bottom, right, between. Word files with left or right paragraph
borders didn't match the schema, which the Open XML SDK reports as an
unexpected element.

Table, cell and page borders were already in their schemas' order.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TmyffPd5QMJYjXiMBQ3EnZ

* fix: stop writing w:highlightCs, which is in no schema

Every highlight was written twice: as w:highlight and as w:highlightCs. There
is no w:highlightCs in WordprocessingML, so documents with highlighted text
didn't match the schema, and the Open XML SDK reports it as an invalid element
in the run properties. w:highlight already highlights all of a run's text,
complex script included.

highlightComplexScript is kept, deprecated, and has no effect.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TmyffPd5QMJYjXiMBQ3EnZ

* fix: write tab stop positions as whole twips

A tab stop's position was written as given, so one worked out as a fraction,
such as TabStopPosition.MAX / 4 * 3 (6769.5), was written as a decimal. w:pos
is ST_SignedTwipsMeasure, a whole number of twips, so the document didn't
match the schema. Positions are now rounded down, as indents already are.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TmyffPd5QMJYjXiMBQ3EnZ

* fix: write shading without a pattern as clear shading

w:shd's w:val is required, but shading given only a fill or a color, with no
type, was written without it. The document didn't match the schema, and the
Open XML SDK reports a missing required attribute. Shading with no type is now
written with w:val="clear", the fill color only, which is what Word writes.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TmyffPd5QMJYjXiMBQ3EnZ

* fix(fonts): write an embedded font's key in upper case

w:fontKey is an ST_Guid, whose pattern only allows upper-case hex digits, but the key docx generates is lower case,
so the Open XML SDK validator rejected the font table of every document with an embedded font. The key is now written
in upper case. The font is obfuscated with the same bytes either way, since the obfuscation reads the hex digits in
either case.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TmyffPd5QMJYjXiMBQ3EnZ

* fix(textbox): stop writing insetmode on a VML text box

A VML text box was written with insetmode="auto", without the o: prefix it needs. With the prefix, the Open XML SDK
validator still rejects it, as Office only takes o:insetmode on the shape. It's no longer written: its default, custom,
uses the inset attribute, which defaults to Word's usual margins when it's left out, as it always is for a text box.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TmyffPd5QMJYjXiMBQ3EnZ

* fix(patcher): put merged run properties in the schema's order

With keepOriginalStyles, a patched run that has run properties of its own takes the placeholder's too. They were
written with the placeholder's first and the run's after, so a run with a font whose placeholder had a size got w:sz
before w:rFonts. ISO 29500 allows any order, but Office's schema doesn't, and the Open XML SDK validator rejected
demo 89. The merged properties are now put in Office's order: CT_RPr's elements, then Word 2010's text effects and
OpenType features, then any it doesn't know, then w:rPrChange.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TmyffPd5QMJYjXiMBQ3EnZ

* fix(styles): write each style id once, and the document defaults first

Two styles could share an id, which a styles part doesn't allow:

- with externalStyles, docx's default styles were written in front of the external ones, so a style such as Heading1
  was there twice, as were w:docDefaults. The external w:docDefaults and w:latentStyles also came after docx's styles,
  where CT_Styles wants them first;
- a paragraph or character style with a default style's id, such as Heading2, was written after the default one
  instead of replacing it.

Styles now writes the document defaults, then the latent styles, then the styles, and a style replaces an earlier one
with its id. So external styles take the place of docx's default styles, docx's fill in the ones they leave out, and
paragraph and character styles take the place of the default ones.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TmyffPd5QMJYjXiMBQ3EnZ

* fix(settings): write the settings in the schema's order

w:evenAndOddHeaders and w:updateFields were written before w:defaultTabStop and the hyphenation settings, and
w:hyphenationZone before w:consecutiveHyphenLimit, where CT_Settings wants them after. Word's validator rejected
settings.xml whenever a default tab stop or hyphenation was set, as in demo 75.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TmyffPd5QMJYjXiMBQ3EnZ

* fix(settings): write the compatibility mode last in w:compat

w:compatSetting was written first in w:compat, where CT_Compat puts it after every other element, so a document with
any compatibility option, as in demo 76, didn't match the schema.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TmyffPd5QMJYjXiMBQ3EnZ

* fix(sections): write a section's previous properties after the document grid

w:sectPrChange was written before w:docGrid, where CT_SectPr puts it after every other element, so a section with a
tracked change, as in demo 98, didn't match the schema.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TmyffPd5QMJYjXiMBQ3EnZ

* fix(tables): write a floating table's overlap beside w:tblpPr

The float option's overlap was written as w:tblOverlap inside w:tblpPr, which CT_TblPPr doesn't allow children in, so
a floating table with an overlap, as in demo 34, didn't match the schema. It's now written in w:tblPr right after
w:tblpPr, where CT_TblPrBase puts it. createTableFloatProperties no longer writes it.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TmyffPd5QMJYjXiMBQ3EnZ

* fix(tables): write a row's cantSplit and tblHeader, and a table's bidiVisual, as off when false

These were written as w:val="false". ISO allows that, but Office's schema only takes on and off for them, so Word's
validator rejected a row with cantSplit or tableHeader set to false, as in demo 98's row revision, and a table with
visuallyRightToLeft set to false. Off is now written as "off", which both schemas accept.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TmyffPd5QMJYjXiMBQ3EnZ

* fix(tables): write a table's cell spacing after its alignment

w:tblCellSpacing was written last in w:tblPr, after w:tblLook, where CT_TblPrBase puts it between w:jc and w:tblInd.
Word's validator rejected any table with cellSpacing and another property after it, such as the borders every table
has.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TmyffPd5QMJYjXiMBQ3EnZ

* fix(math): write sums, integrals and pre-scripts in the schema's order

- A sum or integral without a limit left out its m:sub or m:sup. CT_Nary needs
  both, so a hidden limit is now written empty, as Word writes it
- m:naryPr wrote supHide before subHide; the schema wants subHide first
- m:sPre wrote the base before the scripts; the schema wants sub, sup, e

The other math structures already match shared-math.xsd.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TmyffPd5QMJYjXiMBQ3EnZ

* fix: write a run given in a run's children beside it, not inside it

A FootnoteReferenceRun in a TextRun's children, as demo 60 has, wrote a w:r
inside a w:r, which the schema doesn't allow.

- A run with a run in its children now ends before it. The inner run, such as
  the footnote reference with its own style, comes next, and the children
  after it go in a run with the outer run's properties, as Word writes it
- The same goes for a DeletedTextRun's children
- A component can now be written as several side by side (writtenAs), which
  the serializer and the patcher use for such a run

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TmyffPd5QMJYjXiMBQ3EnZ

* ci: check the demos with the Open XML SDK validator, with none left out

Every demo's document now passes Microsoft's Open XML SDK validator, which checks each part against Office's schemas,
so the demos are checked with it instead of xmllint, and the list of demos left unchecked is gone.

xmllint only checked document.xml and the theme against the bundled schemas, which are Word 2007's: they don't declare
the Word 2010 extensions that templates and shapes use (w14:paraId, w14:checkbox, wps, wpg, mc:AlternateContent), so
26 demos weren't checked at all. The validator understands them, checks every part, and found the bugs fixed before
this. The validator is 80 MB, so it's installed only in this workflow rather than as a dependency.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TmyffPd5QMJYjXiMBQ3EnZ

* fix(styles): let the default styles given in styles.default take the place of external ones

With externalStyles, a default style given in styles.default, such as heading1, or the document defaults given as
document, was replaced by the external style with its id, so the option did nothing. Now the external styles replace
only the default styles that weren't given, and the ones given replace the external ones.

Before, both were written, the default one first. ISO 29500 has the first of two styles with an id keep it, so the
style given in styles.default was the one that applied, as it is again.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TmyffPd5QMJYjXiMBQ3EnZ

* fix(tables): write a cell's merge revision before its previous properties

w:cellMerge was written after w:tcPrChange, where CT_TcPr puts it before, so a cell with both a merge revision and a
property revision didn't match the schema, which the Open XML SDK validator reports as an unexpected element.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TmyffPd5QMJYjXiMBQ3EnZ

* fix(numbering): leave highlight, math and revision out of a level's run properties

Office's schema doesn't allow w:highlight, w:oMath or w:rPrChange in a numbering level's run properties, so the Open
XML SDK validator rejected the numbering part of a document whose level's run style had a highlight. None of them
applies to a level's number, so they're no longer written. The option's type is unchanged.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TmyffPd5QMJYjXiMBQ3EnZ

* fix(demos): make parrots.bmp a bitmap

demo/images/parrots.bmp, the bitmap demos 5, 23 and 114 show, held PNG data, so those documents had a part typed
image/bmp that wasn't a BMP. It's now the same picture as a 24-bit BMP, with the same pixels.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TmyffPd5QMJYjXiMBQ3EnZ

* Add schemas and validators

* Fix dependencies

---------

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-26 00:02:19 +01:00
Dolan 943124e7da fix(textbox): write a text box's picture in a run, and only that run inside a paragraph (#3541) 2026-09-25 16:28:07 +01:00
DolanandClaude Opus 5.5 e7b568f4e6 feat(shapes): custom shapes with several paths, holes, text areas and connection points (#3539)
- `paths` draws a custom shape from several paths, each filled with the
  shape's fill, not at all, or lighter or darker, and with or without its line
- A closed part of a path inside another is a hole, whichever way round it
  is drawn: parts are turned round so both fill rules cut the same holes
- `textArea` sets where a custom shape's text goes, and "fitText" fits it
- `connectionPoints` gives a custom shape its own connection points
- Connection sites and text areas are guides that keep their share of the
  shape when it is resized, as PowerPoint writes freeforms
- Demo 120, with a reference render, and docs in shapes.md


Claude-Session: https://claude.ai/code/session_01TmyffPd5QMJYjXiMBQ3EnZ

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-25 16:10:17 +01:00
DolanandClaude Opus 5.5 be95a8c1bb feat: theme colours for the page background, and templates' themes in patchDocument (#3538)
- Document's background.color takes a ThemeColor, written with w:themeColor,
  w:themeTint or w:themeShade and the hex colour it comes to. The released
  themeColor, themeTint and themeShade strings still work, and are deprecated.
- patchDocument reads the template's theme colours (a:srgbClr, a:sysClr), so
  theme colours in patches get the template's hex value rather than Office's.
- Demo 119 has a page background in a theme colour. Docs updated.


Claude-Session: https://claude.ai/code/session_01TmyffPd5QMJYjXiMBQ3EnZ

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-25 00:46:30 +01:00
Dolan 764d5423f3 feat: theme colours for text, underlines, borders and shading (#3537) 2026-09-24 23:34:57 +01:00
Dolan 811e5d9b97 feat: write a theme part, with theme fonts for text and theme colours for shapes (#3536) 2026-09-24 20:23:52 +01:00
Dolan 91cd6398e2 feat(shapes): text in document styles, swimlanes, routing, descriptions, percentages and text flows (#3535) 2026-09-24 18:46:00 +01:00