Pretext
Pure TypeScript library for fast multiline text measurement and layout without DOM reflow.
At a Glance
Pure TypeScript library for multiline text measurement and layout, released under the MIT License. Bundled third-party notices (Chromium/WebKit BSD-style, ICU/Unicode) are also permissive.
Engagement
Available On
Alternatives
Listed Nov 2026
About Pretext
Pretext is an open-source JavaScript/TypeScript library by chenglou for measuring and laying out multiline text without relying on DOM measurements. It computes paragraph height, line counts and individual lines, and its output can be rendered to DOM, Canvas and SVG. The latest listed release is v1.0.0.
What It Is
Pretext is a text measurement and layout library for web interfaces. Instead of calling DOM APIs such as getBoundingClientRect or offsetHeight, which trigger layout reflow, it implements its own measurement logic and uses the browser's font engine as ground truth. It aims to support text in many languages, including mixed scripts and emoji.
How the Workflow Works
A call to prepare() does the one-time work: it normalizes whitespace, segments text at break opportunities and measures the segments with canvas. layout() then calculates height and line count with pure arithmetic over the cached widths, so on resize only layout() needs to be rerun.
For manual layout, prepareWithSegments() unlocks layoutWithLines(), layoutNextLine() and range-based APIs such as walkLineRanges(), measureLineStats() and layoutNextLineRange(). These support shrink-wrapped chat bubbles, text flowing around obstacles and per-line variable widths. A rich-inline entry point lays out mixed fonts, code spans, chips and images as one paragraph.
Use Cases and Demos
The documentation lists virtualization and occlusion, masonry and other JS-driven layouts, development-time checks that button labels do not overflow, and preventing layout shift. Demos cover accordions, bubbles, editorial layouts, justification comparison, rich text, a 10,000-message Markdown chat, ellipsis clamping and masonry.
Caveats to Know
Pretext targets white-space normal and pre-wrap, word-break normal and keep-all, and overflow-wrap: break-word. It requires Canvas 2D text measurement and Unicode property escapes, so it needs a browser or a supplied OffscreenCanvas shim elsewhere. Fonts must be loaded before preparing text, and system-ui is unsafe for accuracy on macOS.
Community Discussions
Be the first to start a conversation about Pretext
Share your experience with Pretext, ask questions, or help others learn from your insights.
Pricing
Open Source (MIT)
Pure TypeScript library for multiline text measurement and layout, released under the MIT License. Bundled third-party notices (Chromium/WebKit BSD-style, ICU/Unicode) are also permissive.
- MIT License
- Install via npm: @chenglou/pretext
- Pure JavaScript/TypeScript text measurement & layout
- Renders to DOM, Canvas and SVG
Capabilities
Key Features
- Measure paragraph height and line count without DOM reflow
- Manual line-by-line layout with variable widths
- Shrink-wrap multiline text to its widest line
- Rich inline layout with mixed fonts, chips and images
- pre-wrap and keep-all modes plus letter-spacing option
- Soft hyphen support
- Multilingual text support including Southeast Asian scripts via Intl.Segmenter
- Rendering to DOM, Canvas and SVG
- Locale setting and cache clearing helpers
