aradotso avatar

Pretext Text Measurement

If you're measuring text height in JavaScript, you're probably calling getBoundingClientRect and triggering expensive DOM reflows. Pretext sidesteps that entirely by using canvas-based measurement once, then doing pure math for every subsequent layout calculation. The pattern is clean: call prepare() when text or font changes, call layout() on every resize. It handles the tricky stuff like CJK line breaking, rich inline fragments with mixed fonts, and variable-width columns for floating images. The shrinkwrap and binary search examples show it's built for real layout problems, not just basic wrapping. Solid choice if you're building virtualized lists, preventing layout shift, or rendering text to canvas without DOM overhead.

skills.sh installs
649
GitHub stars
61
last updated
Jul 14, 2026

Install

$ npx -y skills add aradotso/trending-skills --skill pretext-text-measurement --agent claude-code

Installs into .claude/skills of the current project.

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')

Sponsors

Details

Repository
github.com/aradotso/trending-skills
First seen
May 16, 2026

More from aradotso/trending-skills

All 251

Recommended

1juliusbrussee avatarcavemanjuliusbrussee/cavemanUltra-compressed communication mode. Cuts output tokens 65% (measured) by speaking like caveman while keeping full technical accuracy. Supports intensity levels618.3Kskills.sh installs
2mattpocock avatargrill-memattpocock/skillsA relentless interview to sharpen a plan or design.1.5Mskills.sh installs
3shadcn avatarimproveshadcn/improveSurvey any codebase as a senior advisor and produce prioritized, self-contained implementation plans for OTHER models/agents to execute. Strictly read-only on s40Kskills.sh installs
4obra avatarsystematic-debuggingobra/superpowersUse when encountering any bug, test failure, or unexpected behavior, before proposing fixes326.7Kskills.sh installs
5forrestchang avatarkarpathy-guidelinesforrestchang/andrej-karpathy-skillsBehavioral guidelines to reduce common LLM coding mistakes through explicit assumptions, simplicity, and verifiable success criteria.17.9Kskills.sh installs
6vercel-labs avatarfind-skillsvercel-labs/skillsHelps users discover and install agent skills when they ask questions like "how do I do X", "find a skill for X", "is there a skill that can...", or express int4.3Mskills.sh installs