United Stateschenglou/pretext
Fast, accurate & comprehensive text measurement & layout
Thành tựu nổi bật
Những danh hiệu mới nhất của dự án
#1 Repository Của Tháng
#1 Repository Của Tuần
#1 Repository Của Tháng
📏 Layout Text Without Touching the DOM
Pretext measures and lays out multiline text in pure TypeScript, skipping DOM reflows entirely.
Pretext is a pure JavaScript/TypeScript library for multiline text measurement and layout. Built by chenglou, it avoids expensive DOM operations like getBoundingClientRect or offsetHeight by using the browser's own font engine as ground truth via canvas measurement. Supports all languages, including CJK, Arabic, emoji, and mixed scripts.
✨ Highlights
- DOM-free measurement: Uses canvas font metrics instead of triggering layout reflow
- Multilingual support: Handles CJK, RTL, emoji, and mixed-language text
- Two APIs:
prepare()+layout()for height-only;prepareWithSegments()+layoutWithLines()for full line control - Cached precomputation:
prepare()does heavy lifting once;layout()is pure arithmetic whiteSpace: 'pre-wrap': Keeps spaces, tabs, and newlines visible for textarea-like layoutswordBreak: 'keep-all': Prevents breaking CJK characters, matching CSS behaviorletterSpacingsupport: Matches CSSletter-spacingwith px values- Rich inline helper:
@chenglou/pretext/rich-inlinefor inline flows with mixed fonts and styles
💡 Why it matters
Pretext eliminates layout thrashing by precomputing text metrics without DOM access. This is critical for performant UIs with dynamic text, virtual lists, or server-side rendering. Especially useful for AI-driven content where text dimensions must be known before rendering.
Ideal for developers building canvas, SVG, or WebGL text renderers; implementing text-aware virtualization; or verifying label overflow during development without a browser.
#TextLayout #TypeScript #WebPerformance #TiniX
Similar Projects
Discover other similar projects and AI models.
Fair-code workflow automation platform with native AI capabilities. Combine visual building with custom code, self-host or cloud, 400+ integrations.
An enterprise-class UI design language and React UI library
Generation of diagrams like flowcharts or sequence diagrams from text in a similar manner as markdown
Open-source AI video editor: edit your own footage, or create and remake any video with Claude Code, Codex or the free Studio app. Write HTML, render MP4. By HeyGen.
A progressive TypeScript framework for Node.js backends: from a small API in one file to efficient, scalable, enterprise-grade systems 🚀