masked-text-diffusion: Escaping the DOM: Inside chenglou/pretext and the Userland Text Rebellion
How a two-phase architecture severed text measurement from the browser's synchronous layout cycle, unlocking high-performance typography for custom renderers.

When UI components independently measure text heights with DOM reads like `getBoundingClientRect()`, each read can force synchronous layout. If those reads interleave with writes, the browser can end up relaying out the whole document repeatedly.
- Pretext eliminates DOM layout thrashing by moving text measurement entirely into userland.
- Its two-phase architecture separates expensive font engine caching from pure arithmetic layout calculations.
- The library replaces fragmented toolchains for bidirectional text and shaping with a single unified engine.
- Custom renderers in WebGL and WebGPU can now achieve high-performance typography without relying on hidden browser internals.
The Synchronous Trap
Text rendering in the browser is a trap. For developers building high-performance WebGL, WebGPU, or Canvas applications, the browser DOM is a black box. Measuring how wide a word is often requires injecting invisible elements and asking the browser for their dimensions.
This seemingly innocuous request carries a massive hidden cost. Every measurement forces the browser to halt and recalculate the layout of the entire page. This phenomenon destroys frame rates and introduces noticeable stuttering in complex visualizations.
The Two-Phase Escape Velocity
The solution requires decoupling measurement from the browser's render cycle. Pretext achieves this through a strict two-phase architecture. It splits the work into a preparation phase and a layout phase.
During the `prepare()` phase, the library interacts with the underlying font engine to cache glyph metrics. This is the expensive operation, but it only happens once. The subsequent `layout()` phase handles wrapping, alignment, and positioning purely through mathematics.
Taming the Chaos of Human Language
Multiline text layout is notoriously difficult. It involves complex rules for line breaks, kerning, and ligatures. Bringing this logic into userland means rebuilding decades of accumulated browser wisdom.
Historically, developers relied on a patchwork of specialized libraries to handle bidirectional text (BiDi) and complex script shaping. Pretext replaces this fragile pipeline with a unified layout engine.
I've always been troubled by these issues(Shaping, BiDi, clusters...) with text rendering in WebGL/WebGPU. ... now I can finally replace them with this solution.
The Paradigm Shift
Pretext proves that complex layout algorithms do not need to be hidden inside C++ browser internals. They can be exposed as composable, high-performance JavaScript primitives.
For projects building custom renderers like infinitecanvas.cc, this is a critical unlock. It provides the typographical fidelity of the web without the performance penalties of the DOM.
| Feature | DOM Measurement | Naive Canvas | chenglou/pretext |
|---|---|---|---|
| Layout Thrashing Risk | High (Synchronous) | Medium (Per-frame API calls) | Zero (Pure Math) |
| Execution Environment | Browser C++ Internals | Browser API Bindings | Userland JavaScript |
| Multiline Wrapping | Automatic but slow | Manual and fragile | Built-in arithmetic |
| WebGL/WebGPU Ready | No | Requires bridging | Yes |