Files
docx/scripts/compare-layout.sh
Dolan e008c45c0c feat(layout): read a .docx into the layout, to write a template's page numbers and check Word's own pages (#3619)
* feat(layout): read a .docx into the layout, to write a template's page numbers and check Word's own pages

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

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

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

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

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

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

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

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

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

The documents saved from Word in scripts/layout-probes are committed
because Word's marks have all their pages. One whose marks no longer read
as having them all was skipped without failing, so a mistake in reading
Word's pages would have turned off the check in CI. It now fails. A
document only in the output directory whose marks don't have all its
pages, as Word's columns and long-table-of-contents, is still left out.
2026-10-02 01:48:30 +01:00

77 lines
3.9 KiB
Bash
Executable File

#!/usr/bin/env bash
# Runs the layout demos, which write the page numbers of their tables of contents with docx/layout, and has LibreOffice
# lay out the same documents, then compares the page docx/layout gave each heading with the page LibreOffice put it on.
# LibreOffice lays out pages differently from Word in places, so it stands in for Word to catch mistakes, rather than
# being what docx/layout matches.
#
# Usage: scripts/compare-layout.sh [output directory] [demo ...]
#
# Each demo is its topic and name, such as layout/text-and-spacing. The output directory (default build/layout) gets each
# demo's .docx, and the text of each page of LibreOffice's PDF of it, split by form feeds, in a .txt named after the demo.
# Without demos, all the layout demos are run. Needs the package built (npm run build).
#
# To compare with Word too, open each .docx in Word, choose No when it asks to update the fields, and save it as a PDF
# named after the demo with .word.pdf, such as text-and-spacing.word.pdf, in the output directory. Its text is written to
# a .word.txt the next time this runs. Word's PDFs are kept, so delete them when the demos change.
#
# Documents saved from Word, as .docx files named with .word.docx, such as text-and-spacing.word.docx, are compared with
# the pages Word wrote into them (see scripts/compare-layout.ts). Those committed in scripts/layout-probes are copied in,
# unless the output directory has one of the same name, and any in the output directory are kept.
#
# The pages are laid out with LibreOffice (set SOFFICE if soffice isn't on the PATH) and pdftotext from Poppler. When
# SHAPE_RENDER_IMAGE names a Docker image built from scripts/shape-demos/Dockerfile, they are laid out in it, with its
# pinned LibreOffice and fonts, as CI does.
set -euo pipefail
OUT="${1:-build/layout}"
shift || true
DEMOS=("$@")
if [ ${#DEMOS[@]} -eq 0 ]; then
DEMOS=()
for demo in demo/layout/*.ts; do
DEMOS+=("layout/$(basename "$demo" .ts)")
done
fi
SOFFICE="${SOFFICE:-soffice}"
mkdir -p "$OUT"
find "$OUT" -maxdepth 1 \( \( -name "*.docx" -not -name "*.word.docx" \) -o -name "*.txt" -o \( -name "*.pdf" -not -name "*.word.pdf" \) \) -delete
# Without them, Word's own pages wouldn't be compared at all, and the comparison would pass without them
SAVED=(scripts/layout-probes/*.word.docx)
if [ ! -f "${SAVED[0]}" ]; then
echo "No documents saved from Word in scripts/layout-probes: run this from the root of the repository" >&2
exit 1
fi
for saved in "${SAVED[@]}"; do
if [ ! -f "$OUT/$(basename "$saved")" ]; then
cp "$saved" "$OUT/"
fi
done
FILES=()
for demo in "${DEMOS[@]}"; do
npm run --silent run-ts -- "./demo/$demo.ts"
cp "My Document.docx" "$OUT/$(basename "$demo").docx"
FILES+=("$(basename "$demo").docx")
done
# The PDF has the blank pages LibreOffice adds before a section that starts on an odd or even page, as Word prints them.
# Its text is written in reading order, so the columns of a page come one after the other, rather than side by side
LAY_OUT='for docx in "$@"; do
soffice ${PROFILE:+"-env:UserInstallation=file://$PROFILE"} --headless \
--convert-to "pdf:writer_pdf_Export:{\"IsSkipEmptyPages\":{\"type\":\"boolean\",\"value\":\"false\"}}" "$docx" > /dev/null 2>&1
pdftotext "${docx%.docx}.pdf" "${docx%.docx}.txt"
if [ -f "${docx%.docx}.word.pdf" ]; then
pdftotext "${docx%.docx}.word.pdf" "${docx%.docx}.word.txt"
fi
done'
if [ -n "${SHAPE_RENDER_IMAGE:-}" ]; then
# LibreOffice keeps its settings in a profile, which has to be somewhere the user running it can write
docker run --rm --platform linux/amd64 --user "$(id -u):$(id -g)" -e PROFILE=/tmp/profile -v "$(cd "$OUT" && pwd):/work" -w /work \
"$SHAPE_RENDER_IMAGE" sh -c "$LAY_OUT" sh "${FILES[@]}"
else
(cd "$OUT" && PATH="$(dirname "$(command -v "$SOFFICE")"):$PATH" sh -c "$LAY_OUT" sh "${FILES[@]}")
fi
npm run --silent run-ts -- scripts/compare-layout.ts "$OUT"