reason-machines avatar

Pretext Text Measurement

Pure JavaScript and TypeScript library for DOM-free multiline text measurement, wrapping, and layout.

skills.sh installs
649
GitHub stars
98
last updated
Jul 14, 2026
View on GitHub

Own this skill? Claim it

What it does

  • Covers paragraph height, manual line layout, shrinkwrap widths, variable-width flow around images, and rich inline text with mixed fonts
  • Provides prepare()/layout() for one-time analysis and cheap per-resize arithmetic, plus layoutWithLines, measureLineStats, and layoutNextLineRange for line-level control
  • Avoids getBoundingClientRect and offsetHeight reflows by measuring text through the browser's canvas font engine as ground truth
  • Designed for canvas rendering, virtualized list row heights, and preventing layout shift when dynamic text loads

Install

$ npx -y skills add reason-machines/trending-skills --skill pretext-text-measurement -y

Installs into .agents/skills for every agent it finds here, and .claude/skills for Claude Code.

Files

Pretext Text Measurement & Layout

Skill by ara.so — Daily 2026 Skills collection.

Pretext is a pure JavaScript/TypeScript library for fast, accurate, DOM-free multiline text measurement and layout. It avoids getBoundingClientRect and offsetHeight (which trigger expensive layout reflows) by implementing its own measurement logic using the browser's font engine as ground truth.

Installation

npm install @chenglou/pretext

Core Concepts

  • prepare() / prepareWithSegments() — one-time analysis: normalize whitespace, segment text, measure via canvas. Cache and reuse this result.
  • layout() / layoutWithLines() etc. — cheap hot path: pure arithmetic over cached widths. Call this on every resize, not prepare().
  • Font string format — same as CanvasRenderingContext2D.font, e.g. '16px Inter', '700 18px "Helvetica Neue"'.

Use Case 1: Measure Paragraph Height (No DOM)

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

// One-time per unique (text, font) combination
const prepared = prepare('AGI 春天到了. بدأت الرحلة 🚀', '16px Inter')

Details

Repository
github.com/reason-machines/trending-skills
Categories
Frontend & UI Code, Animation & Motion
First seen
May 16, 2026

More from reason-machines/trending-skills

All 163

Similar to Pretext Text Measurement

More Frontend & UI Code