$ show chenglou/pretext
Pretext is a high-performance JavaScript/TypeScript library for text measurement and layout that operates entirely without DOM reflow. It accurately predicts line breaks, widths, and heights for any text, matching the behavior of Chrome (Blink), Firefox (Gecko), and Safari (WebKit) by porting each engine's line-breaking rules and embedding their ICU break tables. The library uses a two-phase architecture: a preparation phase that measures each character segment using Canvas `measureText()` and caches the results, followed by a pure-arithmetic layout phase that computes line breaks and positions with sub-millisecond performance. It supports plain text, rich inline spans (styled runs, atomic chips), and advanced features like letter spacing, soft hyphens, emoji handling, and CJK punctuation kerning. A comprehensive test harness records actual browser layouts of thousands of cases and scores predictions against them, with automated gates that block regressions. The repository also includes demos showing virtualized chat, editorial layouts with obstacles, justification comparison, and shrink-wrapping. While primarily an application/library, it contains significant data/research componentsβgenerated break tables from browser engines, multilingual corpora for testing, and offline invariantsβmaking it a hybrid project with a strong research character in cross-browser text rendering.
The LLM identified 6 files worth highlighting for reuse:
Implements line-break scanning algorithms for Blink, WebKit, and Gecko browsers based on their ICU break tables. Self-contained module that could be extracted into a separate package for cross-engine line breaking. Demonstrates deep understanding of Unicode Line Break Algorithm and browser-specific differences in handling ambiguous characters and rules.
Implements grapheme cluster segmentation using packed ICU character tables. Matches `Intl.Segmenter` behavior across browsers. Reusable as a lightweight standalone grapheme cluster library without relying on the native API.
Compares two builds' predictions using a fake Canvas that simulates width measurements (fontSize * 0.6 per character, etc.). Enables fast, deterministic regression detection without browser launches. The pattern of simplified width approximation is clever and reusable for testing any text layout engine.
Analyzes text into segments with flags (UNBROKEN, RETURNABLE, SPACED, ONE_CLUSTER) and break kinds. Table-driven, deterministic analysis that handles Unicode properties. Could be reused for any text processing pipeline that needs segment classification.
Build script that extracts binary break tables from Chrome, Safari, and Firefox browser files and compresses them into base64 strings using a custom packing scheme. Demonstrates a pattern for embedding large, versioned data tables compactly in JavaScript. The LZ77-like compression could be reused for similar tasks.
Implements a scoring and gating system for comparing predicted vs recorded browser layouts. Produces fine-grained outcomes (line failures, new failures, fixed), supports acceptance lists, and blocks regressions. The pattern is reusable for any system that needs to compare computed output against recorded ground truth.