typstream

@typstream/docx

The DOCX writer behind exportDocx() — OOXML from the laid-out pages, with Typst's fonts embedded.

@typstream/docx writes the DOCX files exportDocx() returns (on EngineDocument, mountTypstream, <Typstream> and render(source, "docx")). You don't call it yourself: the engine's server loads it the first time a document is exported.

What it writes. engine-wasm reads the laid-out pages into a document model (compile_docx_model): paragraphs and their runs (font, size, colour), headings, lists with Word numbering, tables and grids with Typst's column widths and repeated header rows, framed blocks, ruled lines, columns (as Word sections), placed text (as frames at its page position), page headers and footers with page-number fields, page breaks, every gap Typst made, and equations as trees shaped after Word's math (OMML), mapped from Typst's own math layout's first step (fractions, scripts and limits, roots, delimiters, matrices, big operators, functions, accents, aligned lines). What it can't express otherwise (images, CeTZ canvases, mind maps, edu's drawings, boxes with rounded corners) is a picture of Typst's own drawing: SVG from typst-svg, and a 300 DPI PNG from typst-render's rasterizer, made in the wasm. This package turns the model into OOXML with the layout profile's settings (exact line pitch, no hyphenation, Word 2013+ layout), embeds the fonts the runs use, obfuscated as ECMA-376 asks, writes each picture as Word does an SVG (the PNG as the picture, the SVG in Word's svgBlip extension) over a paragraph that holds its room, or anchored to the page where it was placed, and zips it.

Scripts. A run is written in logical order whatever direction Typst drew it in, with its language (w:lang), and right-to-left text as Word's right-to-left runs (w:rtl) in right-to-left paragraphs (w:bidi), whose alignment, indents and list numbers are measured from the right; right-to-left tables keep their columns where Typst drew them. Arabic dots and Indic vowel signs, which Typst draws apart from their letters, are written once.

How it's checked. tools/docx-check exports the corpus, has LibreOffice lay each file out and compares its lines and page breaks with the PDF's (on documents with equations, the text around them, and every equation's letters and digits; on documents with pictures, the text outside them and each picture's pixels; on documents in other scripts, each line's text or where its ink starts and ends), and runs Microsoft's Open XML validator on every file.

The package's exports (writeDocx and the model's types) are internal: other typstream packages use them.