Files
docx/scripts/layout-probes/word-split-rows-and-boxes.ts
DolanandClaude Opus 5.5 5f626c02ba fix(layout): lay out rows that repeat their table's properties, split rows at multiple line spacing and overfull text boxes, as Word does (#3918)
* fix(layout): lay out rows that repeat their table's properties, split rows at multiple line spacing and overfull text boxes, as Word does

estimatePageNumbers stopped at three things report templates have (#3912):

- A row whose w:tblPrEx repeats the table's own w:tblW, w:tblLayout or
  w:tblInd. Properties that are the same as the table's change nothing,
  so only those that differ still stop the layout.
- A cell's line at multiple spacing at the foot of a row that breaks
  across pages, where it fits only without the space below its text.
  word-split-rows-and-boxes.docx shows Word moves it to the next page,
  unlike in the body: 69 probes, with and without borders and above a
  cell's bottom margin, all now on Word's pages.
- An inline VML text box of its own height whose text is taller than
  it. Word cuts the text off and keeps the box's height (OB1 in the same
  probe), so the box is laid out at its own height.

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

* fix(layout): compare a row's table properties with the table's whatever order their attributes are in

XML gives attributes no order, so a row's w:tblPrEx written with
w:type before w:w is the same as the table's w:tblW written the other
way round. The comparison now sorts each element's attributes first,
as a text frame's are compared, rather than stopping the layout there.

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

* fix(layout): put the probe's text box shape type before whichever box ONLY leaves first

The shape type was added to OB1a's box, so a run with ONLY=OB1b wrote a
box referring to a shape type the document didn't declare. It now goes
before the first box in the document, whichever that is. A full run
writes the same document as before.

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

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-19 04:14:34 +00:00

109 lines
5.7 KiB
TypeScript

/**
* Probes of two stops docx/layout met in reports laid out from Word templates (#3912):
*
* - "a table row across pages whose line's multiple spacing goes below the page": whether a cell's line at 1.15 lines that
* fits at the foot of its row's part on the page only without the space its multiple spacing adds below its text goes
* on that page, as in the body (`word-mixed-heights.docx` MH1c), or on to the next (SR1 to SR3)
* - "a text box whose text is taller than it": a text box in the line, of its own height, with more text than fits in it
* (OB1)
*
* Calibri 11 on A4 with inch margins, so the text is 13958 twips tall; each probe on a page of its own.
*
* SR1a to SR1w: below its line above, a paragraph exactly 11299 twips tall, less 5 for each probe after the first, so
* the row below it starts 5 twips further up the page each time, then a table of one row and one cell, 9026 wide, with
* no borders and no margins above or below, of 10 paragraphs at 1.15 lines (`w:line="276"`, auto), each a line
* SR2a to SR2w: as SR1, with single borders of half a point all round
* SR3a to SR3w: as SR1, its cell with a margin of 300 below
* OB1a: a text box in the line, `width:150pt;height:30pt`, with no insets and no outline, not sized to its text, of 5
* lines; OB1b: the same with 1 line; OB1c: as OB1a, with Word's insets and an outline of 0.75 points; OB1d: as OB1c with
* 1 line. Each box is in a paragraph of its own, after a line of text and before another
*
* Opened in Word 16 for Mac on 2026-11-19 and read with `word-split-rows-and-boxes.applescript`, which writes each
* paragraph's page and how far down it it starts. Word put 7 of SR1's lines on the probe's first page up to SR1q and 8
* from SR1r, 7 of SR2's up to SR2u and 8 from SR2v, and 6 of SR3's up to SR3o and 7 from SR3p: a line goes on the page
* only when all of it fits, the space below its text too, as docx/layout now lays out all 69. Letting that space go below
* the page, as in the body, would put SR1's eighth line on the page from SR1i. The line below OB1a is where it is below
* OB1b, and below OB1c where it is below OB1d: Word cuts off the text that doesn't fit, and the box is as tall as it says.
*
* Usage: npm run run-ts -- scripts/layout-probes/word-split-rows-and-boxes.ts [folder]
*/
import { LineRuleType, Paragraph, Table, TableBorders, TableCell, TableRow, WidthType } from "docx";
import { ALL_BORDERS, type Child, TEXT_WIDTH, line, marker, probe, replaceMarkerRun, replaceText, write } from "./stops2/kit";
const STEPS = 23;
const LETTERS = "abcdefghijklmnopqrstuvwxyz";
/** A table of one row and one cell of 10 lines at 1.15 lines, with the borders and the margin below given */
const splitRow = (name: string, { bordered, marginBottom }: { readonly bordered: boolean; readonly marginBottom: number }): Table =>
new Table({
width: { size: TEXT_WIDTH, type: WidthType.DXA },
columnWidths: [TEXT_WIDTH],
borders: bordered ? ALL_BORDERS : TableBorders.NONE,
rows: [
new TableRow({
children: [
new TableCell({
width: { size: TEXT_WIDTH, type: WidthType.DXA },
margins: { top: 0, bottom: marginBottom, left: 108, right: 108 },
children: Array.from({ length: 10 }, (_, index) =>
line(`${name} line ${index + 1}`, { spacing: { line: 276, lineRule: LineRuleType.AUTO } }),
),
}),
],
}),
],
});
const splitRows = (prefix: string, options: { readonly bordered: boolean; readonly marginBottom: number }): Child[] =>
Array.from({ length: STEPS }, (_, step) => {
const name = `${prefix}${LETTERS[step]}`;
return probe(name, [
line(`${name} spacer`, { spacing: { line: 11299 - 5 * step, lineRule: LineRuleType.EXACT } }),
splitRow(name, options),
]);
}).flat();
/** A text box in the line, 150 by 30 points, of lines of text, as VML, not sized to its text */
const box = (name: string, count: number, framed: boolean): string => {
const text = Array.from(
{ length: count },
(_, index) => `<w:p><w:r><w:t xml:space="preserve">${name} box ${index + 1}</w:t></w:r></w:p>`,
).join("");
const outline = framed ? `strokeweight=".75pt"` : `stroked="f"`;
const inset = framed ? "" : ` inset="0,0,0,0"`;
return (
`<w:r><w:pict><v:shape id="${name}" type="#_x0000_t202" style="width:150pt;height:30pt" ${outline}>` +
`<v:textbox${inset}><w:txbxContent>${text}</w:txbxContent></v:textbox></v:shape></w:pict></w:r>`
);
};
const BOXES = [
["OB1a", 5, false],
["OB1b", 1, false],
["OB1c", 5, true],
["OB1d", 1, true],
] as const;
// The shape type of a text box, which Word writes once before the first shape of it
const SHAPE_TYPE =
`<v:shapetype id="_x0000_t202" coordsize="21600,21600" o:spt="202" path="m,l,21600r21600,l21600,xe">` +
`<v:stroke joinstyle="miter"/><v:path gradientshapeok="t" o:connecttype="rect"/></v:shapetype>`;
const children: Child[] = [
...splitRows("SR1", { bordered: false, marginBottom: 0 }),
...splitRows("SR2", { bordered: true, marginBottom: 0 }),
...splitRows("SR3", { bordered: false, marginBottom: 300 }),
...BOXES.flatMap(([name]) => probe(name, [new Paragraph({ children: [marker(name)] })])),
];
await write({
name: "word-split-rows-and-boxes",
children,
injections: [
...BOXES.map(([name, count, framed]) => replaceMarkerRun(name, box(name, count, framed))),
// The shape type goes before the first box in the document, whichever boxes ONLY leaves in it
replaceText(/<w:pict>(?=<v:shape id="OB1)/, `<w:pict>${SHAPE_TYPE}`),
],
});