The Pretext Ecosystem

Community hub for @chenglou/pretext v0.0.3

Everything about
@chenglou/pretext

Playground, font tester, chat generator, snippets & changelog — one place. Multiline text layout without touching the DOM.

npm install @chenglou/pretext
Live — Pretext measuring text as you type
Lines
Height
Reflows0
Time
300–600× faster than DOM measurement
📦 15KB tiny package, zero dependencies
🔄 0 layout reflows in the hot path
🌐 Unicode scripts via Canvas engine

What is @chenglou/pretext?

For years, measuring text in the browser meant putting it in the DOM and reading geometry. That triggers layout reflow — work that scales poorly for virtual lists, chat, and animations.

Pretext by Cheng Lou uses canvas.measureText() to read the font engine without entering the layout tree. It caches widths and uses arithmetic for line breaks and height — no DOM on the hot path.

Results align with browser layout for loaded fonts, with compute times suited to 60fps UIs and large batches of text.

Traditional DOM approach
// Triggers layout reflow
const el = document.createElement('div');
el.style.width = width + 'px';
el.textContent = text;
document.body.appendChild(el);
const height = el.offsetHeight;
document.body.removeChild(el);
Pretext
import { prepare, layout } from '@chenglou/pretext';

const prepared = prepare(text, '16px Inter');
const { height, lineCount } = layout(
  prepared,
  width,
  lineHeight
);

Quick start with Pretext

Install, import, and measure in a few lines.

1

Install

npm install @chenglou/pretext
2

Measure

import { prepare, layout } from '@chenglou/pretext';

const prepared = prepare('Hello, world!', '16px Inter');
const { height, lineCount } = layout(prepared, 400, 24);
3

Line-by-line

import { prepareWithSegments, layoutWithLines } from '@chenglou/pretext';

const prepared = prepareWithSegments(text, '16px Inter');
const { lines } = layoutWithLines(prepared, 400, 24);

Popular Pretext snippets

Copy-paste examples for common tasks. All snippets →

📏

Simple height measurement

prepare() once, then layout() with maxWidth and lineHeight. Returns height (px) and lineCount — no DOM.

#prepare#layout#height
View snippet →

Batch height calculation

Map many strings to heights in the main thread. Compare with a hidden DOM node to see reflow cost.

#batch#performance#dom
View snippet →
♻️

Handle reuse

PreparedText is immutable measurement data. Reuse the same handle for many container widths.

#prepare#cache#layout
View snippet →

Pretext core API

See the Playground or snippets for full patterns.

Function Use case Returns
prepare(text, font) Simple measurement handle PreparedText
layout(prepared, width, lineH) Height and line count { height, lineCount }
prepareWithSegments(text, font) Rich / line APIs PreparedTextWithSegments
layoutWithLines(prepared, width, lineH) Per-line text and width { lines, height, lineCount }
layoutNextLine(prepared, cursor, maxW) Variable width per line LayoutLine | null

Font strings follow CSS font shorthand. Fonts must be loaded before prepare().

Pretext FAQ

Common questions about Pretext and this site.