# @docen/layout

> Pure TypeScript layout engine for docen — text line breaking (UAX #14 + CJK kinsoku), OOXML paragraph/table block layout, and flow/fixed/grid strategies with zero DOM dependencies

Latest version **0.7.0** (published 2026-09-07) · MIT license · 0 weekly downloads

## Install

```sh
npm install @docen/layout
pnpm add @docen/layout
yarn add @docen/layout
bun add @docen/layout
```

## Health

**Score 65/100 (B)** — status: active.

Positive: has types; esm support; no vulnerabilities; recently updated; high maintenance score.

Warnings: low downloads; pre 1.0.

## Facts

| | |
|---|---|
| Version | 0.7.0 |
| Published | 2026-09-07 |
| First published | 2026-08-30 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 171.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 71 |
| Author | Demo Macro |
| Maintainers | demomacro |
| Keywords | cjk, docen, kinsoku, layout, line-breaking, office, ooxml, pagination, text-measurement, uax14 |

## Links

- npm: https://www.npmjs.com/package/@docen/layout
- Repository: https://github.com/DemoMacro/docen
- Homepage: https://github.com/DemoMacro/docen#readme
- Issues: https://github.com/DemoMacro/docen/issues
- npm.io page: https://npm.io/package/@docen/layout

## Dependencies (1)

- [@docen/pretext](https://npm.io/package/@docen/pretext.md) 0.7.0

## Alternatives

- [babylon](https://npm.io/package/babylon.md) — 5.1M weekly downloads
- [csscolorparser](https://npm.io/package/csscolorparser.md) — 3.7M weekly downloads
- [expr-eval-fork](https://npm.io/package/expr-eval-fork.md) — 1.5M weekly downloads
- [@leeoniya/ufuzzy](https://npm.io/package/@leeoniya/ufuzzy.md) — 247.7K weekly downloads
- [xml-parser](https://npm.io/package/xml-parser.md) — 78.4K weekly downloads

## Recent versions

- 0.7.0 (latest) — 2026-09-07
- 0.6.2 — 2026-09-01
- 0.6.1 — 2026-08-31
- 0.6.0 — 2026-08-30

## README

# @docen/layout

![npm version](https://img.shields.io/npm/v/@docen/layout)
![npm downloads](https://img.shields.io/npm/dw/@docen/layout)
![npm license](https://img.shields.io/npm/l/@docen/layout)

> Pure TypeScript layout engine for docen — zero DOM, zero rendering libraries. The inner ring (`text/`, `block/`) measures and breaks text and lays out paragraphs/tables; the outer ring builds on it (`flow/` — docx page boxing — is in; `fixed/` for pptx shape text and `grid/` for xlsx come with their editors).

Consumed by [`@docen/docx`](../docx/README.md)'s projection (DocumentOptions → LayoutDoc) and painted by [`@docen/core`](../core/README.md)'s canvas stage.

## Design

- **Input is a `LayoutDoc` projection, all-px, style-cascades already resolved.** Adapters (docx from Tiptap/ProseMirror, pptx from its shape tree, xlsx from its grid) convert their document units and resolve their style chains exactly once; the engine never sees a `styleId`, a twip, or a Tiptap node. Only layout semantics keep their OOXML shape (line rules, grid pitch, snap flags) — those are the rules this engine exists to implement.
- **FontMetrics is the measuring seam.** The engine works from per-code-unit advances supplied by a `FontMetrics` provider (`browserFontMetrics` measures through canvas 2d today; headless and server providers slot into the same interface). Misses fall back with a warning — Word's behavior.
- **One packer for text, hard breaks, and inline pictures** — the unified breaker the DOM route never had. UAX #14 line breaking (linebreak.js) with CJK kinsoku, trailing-space hanging, first-line indent, float-zone width reduction, and per-line OOXML line-height semantics (exact / atLeast / multiple × docGrid pitch, CJK ceil snap).
- **Determinism is a contract.** Same input → same output every pass — the property the paginator's convergence depends on.

Known boundaries: no GSUB shaping (ligature substitution — advances run slightly wide, conservative for breaking); rowspan cell content counts fully on its start row.

## Usage

```ts
import { TextMeasurer, browserFontMetrics, layoutBlock } from "@docen/layout";

const measurer = new TextMeasurer(browserFontMetrics);
const laid = layoutBlock(paragraph, 612, { linePitchPx: 25 }, measurer);
// laid.lines — y, height, positioned items, split points
```

## License

- [MIT](../../LICENSE) &copy; [Demo Macro](https://www.demomacro.com/)

---
_Source: https://npm.io/package/@docen/layout · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
