@docen/layout

中文文档
0.7.0发布于 2026/09/07MITTypeScriptES Module近 30 天下载 7026

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

原始使用文档
文档目录3 节
  1. Design
  2. Usage
  3. License

@docen/layout

npm version npm downloads npm license

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's projection (DocumentOptions → LayoutDoc) and painted by @docen/core'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

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

版本与发布记录

@docen/layout 共发布 4 个版本,首次发布于 2026/08/31;过去 12 个月发布了 4 个版本。

版本发布日期距今
0.7.0latest2026/09/072 个月前
0.6.22026/09/022 个月前
0.6.12026/08/312 个月前
0.6.02026/08/312 个月前

关于 @docen/layout 的常见问题

@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

如何安装 @docen/layout?

在项目目录运行 npm install @docen/layout,或使用 pnpm add / yarn add / bun add。

@docen/layout 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@docen/layout 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@docen/layout 可以商用吗?

@docen/layout 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@docen/layout 的下载量有多少?

根据 npm 官方下载统计,@docen/layout 在 2026/10/13 – 2026/11/11 这 30 天内被下载 7,026 次,日均约 234 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@docen/layout 还在维护吗?

最新版本 0.7.0 发布于 2026/09/07(2 个月前);至今共发布 4 个版本,其中过去 12 个月发布了 4 个;首次发布于 2026/08/31。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。