/pretext:pretextfrom pretext
Helps use @chenglou/pretext 1.0+ to measure and lay out multiline text without DOM reflow. Covers auto-fit font size, line clamp, shrink-wrap, text around obstacles, canvas text with moving letters, and rich inline elements like chips and mentions.
Ships in pretext. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
You are helping a developer use @chenglou/pretext, a TypeScript library by Cheng Lou that predicts the browser's line breaks and heights from canvas measurements, using ports of each browser's line breaker. prepare() segments the text and measures it once with CanvasRenderingContext2D.measureText; layout() is then arithmetic at any width, with no DOM reads. It is ESM: @chenglou/pretext is the core, and @chenglou/pretext/rich-inline handles mixed-font inline text.
Read the project's package.json (or run npm ls @chenglou/pretext) before writing any code. This skill covers 1.0.0 and later. Below 1.0.0, read migration.md before writing code: a caret range on a 0.0.x version never reaches 1.0 by itself, and several signatures changed. Don't write signatures from memory; take them from the references.
fillText in canvas, SVG or WebGL. Paint each line's text through paintableText, a helper recipe in patterns-flow.md (not a library export), so soft-hyphen breaks draw the hyphen that was measured./rich-inline.measureElement loop.npm install @chenglou/pretext
import { prepare, layout } from '@chenglou/pretext';
const prepared = prepare('Hello world', '16px Inter'); // text first, then a canvas font
const result = layout(prepared, 400, 24); // maxWidth 400px, lineHeight 24px
// → { lineCount: number, height: number }
const narrow = layout(prepared, 120, 24); // on resize, lay out again; never prepare again
Read these before writing any Pretext code.
layout() takes line height in CSS px, not a multiplier. Passing 1.5 gives a plausible lineCount and a tiny height, with no error.
const prepared = prepare(text, '14px Inter');
const lineHeightPx = 14 * 1.5; // CSS: font-size 14px, line-height 1.5
layout(prepared, 500, lineHeightPx); // not layout(prepared, 500, 1.5)
prepare(text, font). Swapped arguments throw a RangeError, unless the swapped text happens to be a valid font string, and then the numbers are silently wrong.
getComputedStyle(el).font can be an empty string (an unmounted element, or a longhand such as font-variant-numeric), which throws.'1.5em Arial' or 'caption': see accuracy.md. The safe form is a px size and a named family, '600 16px Inter'.document.fonts.load() rejects a malformed string with a SyntaxError first, so that is the error you see.A handle is for one text, font and options. Prepare each string. Pretext caches segment widths per font, so many strings in one font are cheap.
Before a web font loads, Canvas measures the fallback font. Await document.fonts.load(font, text), which also loads the unicode-range parts the text needs, and handle its rejection. If text was prepared before its font arrived, call clearCache() and prepare it again.
async function prepareLoaded(text, font) {
await document.fonts.load(font, text); // rejects for a malformed font string
return prepare(text, font);
}
On macOS, Canvas resolves system-ui and -apple-system to a different font from the one the page paints. Use a named font: see accuracy.md.
Without a Canvas 2D context or a global OffscreenCanvas, prepare() throws Text measurement requires OffscreenCanvas or a DOM canvas context. Node and server rendering throw; upstream says Bun, jsdom and happy-dom do too. Call it only on the client, or see patterns-integration.md for a shim. Intl.Segmenter is needed only for Southeast Asian scripts such as Thai.
When you add fixed parts to a text height, include border-width: a 1px border adds 2px to the box. It is easy to miss and adds up across a list.
layout() of '' gives 0 lines and a height of 0. That matches the browser, where an empty block is 0 tall, so don't clamp it. A <textarea> shows one more line than layout() counts when its value is empty or ends in \n, because it keeps a line for the caret. Add it when you size one.
{ letterSpacing } is CSS px. Convert em or % first: 0.05em on a 16px font is 0.8. A non-finite value such as NaN throws a RangeError.
Since 1.0.0 the order is (prepared, maxWidth, start?), for layoutNextLine and layoutNextLineRange. Before 1.0 the cursor came second. A call in the old order still runs in JavaScript but never returns null, so the loop hangs. Omit the cursor or pass undefined for the first line, never null, which throws.
const prepared = prepareWithSegments(longText, '16px Inter');
let cursor; // undefined: start at the beginning
for (let y = 0; ; y += 20) {
const line = layoutNextLine(prepared, getWidthAroundObstacle(y), cursor);
if (line === null) break;
renderLine(line.text, y);
cursor = line.end;
}
Pretext measures the characters you give it. Apply text-transform, .toUpperCase(), .normalize(), smart quotes or number formatting before prepare(), then paint that same string. Capitals are wider, so a box sized for mixed case overflows. Example: accuracy.md.
Paint each paragraph as one element with the same font, the same line-height in px, the width you passed to layout(), and overflow-wrap: break-word. The browser then wraps it to the same lines in nearly all cases; check the painted height in your browser once. Paint per line only where the browser can't wrap for you: Canvas, SVG <text>, varying widths, and rich inline text with mixed fonts or padded pieces. For CSS that Pretext doesn't model, such as text-wrap: balance or hyphens: auto, see accuracy.md.
Gotchas #11 and #12 share a theme: a mismatch comes from changing the inputs after measuring. Paint the string, font, width and CSS you measured.
A handle belongs to the Pretext version that made it, and another version's handle throws a TypeError. Store the text, font and options, and prepare again rather than persisting handles across versions (IndexedDB, a worker on another bundle). Don't read internals the types don't expose. A handle holds a typed array: a shallow Object.freeze, or Immer's freeze and produce, work, but a hand-written recursive freeze throws, so don't write one.
| Developer wants to... | prepare variant | layout function |
|---|---|---|
| Text height or line count at a width | prepare | layout |
| Auto-fit font size | prepare per size tried | layout in a binary search |
| Auto-height a textarea | prepare with { whiteSpace: 'pre-wrap' } | layout, plus one line when empty or ending in \n (gotcha #8) |
| Widest line (shrink-wrap) | prepare | measureLineStats or walkLineRanges |
| Line count plus widest line | prepare | measureLineStats |
| Intrinsic width (widest forced line) | prepare | measureNaturalWidth |
| Variable widths without strings | prepare | layoutNextLineRange |
| Truncate with an ellipsis | prepare to test; prepareWithSegments for the strings | layout; the clamp recipe |
| Per-line text (canvas, SVG, animation) | prepareWithSegments | layoutWithLines, painted via paintableText |
| Flow text around obstacles | prepareWithSegments | layoutNextLine in a loop (recipes) |
| Text in several slots per band (both sides of an obstacle, a mask) | prepareWithSegments | layoutNextLine per slot, layoutInBands |
| Per-character positions (particles, games) | prepareWithSegments | layoutWithLines, then glyphPositions |
| Variable widths, text only for painted lines | prepareWithSegments | layoutNextLineRange + materializeLineRange |
| Mentions, chips, code spans inline | prepareRichInline (/rich-inline) | walkRichInlineLineRanges / layoutNextRichInlineLineRange |
| Inline images or emoji | prepareRichInline with a { width } box | the same (boxes) |
Choosing the prepare call:
prepare unless a function must return text. Its handle works with layout, layoutNextLineRange, walkLineRanges, measureLineStats and measureNaturalWidth.prepareWithSegments is for layoutWithLines, layoutNextLine and materializeLineRange, and for reading segments, kinds and widths. widths is typed ArrayLike<number>: copy it with Array.from before using array methods.prepareRichInline is for mixed fonts, padded pieces, atomic items and boxes. It takes an items array, and { whiteSpace, wordBreak } as its second argument; letterSpacing, break and extraWidth go on each item. Its handle doesn't work with the core functions.prepare and prepareWithSegments take { whiteSpace, wordBreak, letterSpacing } third. wordBreak: 'keep-all' is CSS word-break: keep-all, for CJK and Hangul.
Common mistakes:
prepare then layoutWithLines (or layoutNextLine, materializeLineRange) throws a TypeError..map(prepareRichInline) passes the index as options. Write .map(items => prepareRichInline(items)).layoutWithLines when the width varies per line: use layoutNextLine or layoutNextLineRange.measureNaturalWidth to shrink-wrap soft-wrapped text: it gives the widest forced line. Use measureLineStats.prepare again on resize: call layout again instead.| File | What's in it | Read when |
|---|---|---|
| api.md | The 12 core functions, types and handle fields | Exact signatures or types for the main entry |
| rich-inline.md | The 5 /rich-inline functions, items and boxes, painting fragments, recipes | Mixed fonts, chips, mentions, code spans, inline images |
| patterns-sizing.md | Wrapper, auto-fit, cards, shrink-wrap, iterative search, truncation, live-edited text, virtualized lists, streaming | Heights, widths, line counts, fitting or clamping |
| patterns-flow.md | paintableText, obstacles, both-side slots, masks, SVG per line, balanced layout, editorial, 3D, animation | Placing lines yourself: variable widths, canvas, SVG |
| patterns-glyphs.md | Per-character positions, ink bounds, proportional ASCII | Letters that move on their own: particles, games, ASCII art |
| patterns-integration.md | Frameworks and SSR, progressive enhancement, vendoring, outside the browser | Wiring into an app, a build, a server or a worker |
| accuracy.md | Painting, fonts, language, widths, CSS it doesn't model, runtimes, engines, bundle size | Results differ from what the page shows |
| migration.md | 0.0.x to 1.0.0: finding old usage, breaking and silent changes | The project is below 1.0.0, or old code is pasted |
| Upstream markdown-chat.md | The Markdown chat demo's patterns, and when to skip each | Chats and other long lists |
| Upstream README Caveats | Upstream's own accuracy notes | Anything accuracy.md doesn't cover |
Live at https://chenglou.me/pretext/. In the repository: clone, bun install, bun start.
accordion: expand and collapse with predicted heightsbubbles: chat bubbles shrink-wrapped to their widest linedynamic-layout: text routed around moving obstacleseditorial-engine: a magazine spread with columns and obstaclesellipsis: line clamp with an ellipsis, and a path cut in its middlejustification-comparison: Knuth-Plass, greedy and CSS justificationmarkdown-chat: virtualized Markdown chat; the virtualization referencemasonry: masonry from predicted heightsrich-note: notes with mentions and chipsvariable-typographic-ascii: ASCII art from glyph widths; patterns-glyphs.md has a fixed-grid recipeCommunity demos (pretext.cool, awesome-pretext) mostly predate 1.0: they pass layoutNextLine its cursor second, and some read handle internals. Use them for ideas, and take signatures from the references (migration.md).
prepare() walks the whole text and measures new segments. layout() and the other line functions are arithmetic over the handle, safe for resize handlers and animation frames.prepare() out of searches over width or line count, such as balanced shrink-wrap: every iteration lays out the same handle. An auto-fit search prepares once per font size it tries, never per width.measureLineStats or walkLineRanges on a prepare handle is cheaper than layoutWithLines.VERSION (opens in a new tab)references/accuracy.md (opens in a new tab)references/api.md (opens in a new tab)references/migration.md (opens in a new tab)references/patterns-flow.md (opens in a new tab)references/patterns-glyphs.md (opens in a new tab)references/patterns-integration.md (opens in a new tab)references/patterns-sizing.md (opens in a new tab)references/rich-inline.md (opens in a new tab)What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
npx claudepluginhub yaniv-golan/pretext-skill --plugin pretextOther plugins' skills for the same job