
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-codeInstalls 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, notprepare().- 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- Claude Code Source Recovery648
- Sheets Terminal Spreadsheet646
- Omnivoice Tts627
- Metatron Pentest Assistant602
- Tong Jincheng Relationship Skill595
- Awesome Design Md Jp594
- Parlor On Device Ai588
- Paper2code Arxiv Implementation585
- Free Code Claude Cli584
- Fieldtheory Cli Bookmarks582
- Tong Jincheng Interpersonal Skill574
- Claude Code Source Study569
- Cabinet Ai Knowledge Base562
- Binance Futures Signal Bot554
- Karpathytalk Community553
- X Mentor Skill Nuwa552
- Clawgod Claude Code Patch551
- Gemma Gem Browser Ai548
- Claude Skill App Onboarding Questionnaire539
- Gemma Tuner Multimodal537
- Tailslayer Dram Hedged Reads537
- Bluehammer Vulnerability Poc527
- Alayarenderer Generative World522
- Llm Wiki Skill516