chenglou/pretext

Fast, accurate & comprehensive text measurement & layout

View on GitHub ↗Jump to charts ↓Open shareable report

Summary Information

Updated 10 minutes ago
Added to GitGenius on August 31st, 2026
Created on March 7th, 2026
Open Issues & Pull Requests: 91 (+0)
Number of forks: 2,734
Total Stargazers: 50,128 (+0)
Total Subscribers: 148 (+0)

Repository Insights (GitGenius)

Median issue/PR response: 25.7 hours
Mean response time: 5.2 days
90th percentile: 15.6 days
Tracked items: 76

How this project is maintained

Around half of the issues opened in the past year never receive a reply. 100% of open issues come from outside the core team, so the backlog reflects real-world use rather than internal planning. 63% of tracked open issues have had no activity in three months, so the open count overstates what is actively being worked. Only 11% of issues opened in the past year have been closed. Three people close 88% of everything that gets resolved.

Charts & Analytics

Fetching additional details & charts...

Issue Activity (beta)

Open issues: 46
New in 7 days: 0
Closed in 7 days: 0
Avg open age: 125 days
Stale 30+ days: 46
Stale 90+ days: 29

Recent activity

Opened in 7 days: 0
Closed in 7 days: 0
Comments in 7 days: 0
Events in 7 days: 0

Top labels

No label distribution available yet.

Most active issues this week

No issue events were indexed in the last 7 days.

Detailed Description

Pretext is a pure JavaScript/TypeScript library for multiline text measurement and layout that avoids expensive DOM operations.

The problem Pretext solves is the performance cost of measuring text through DOM APIs like getBoundingClientRect, which trigger layout reflow. Instead, it implements its own text measurement logic using the browser's font engine as ground truth. The library separates work into two phases: prepare() performs one-time normalization, segmentation, and canvas-based measurement to cache segment widths, while layout() runs the cheap hot path using pure arithmetic over those cached values. This approach supports all languages and allows rendering to DOM, Canvas, SVG, and eventually server-side.

Pretext suits projects that need accurate text dimensions without DOM measurement overhead, particularly those building custom layouts, virtualization systems, or dynamic text rendering. It excels for preventing layout shift when text loads, enabling proper scroll anchoring, and supporting development-time verification that text fits within UI constraints. The library offers two main use cases: measuring paragraph height directly without touching the DOM, or manually laying out lines with fine-grained control via functions like layoutWithLines() and layoutNextLineRange(). For manual layout scenarios requiring hyphenation, users can insert soft hyphens before preparation, and the library treats them as optional break points. A separate helper module at @chenglou/pretext/rich-inline provides utilities for inline-only rich text with code spans, mentions, and chips while maintaining normal whitespace behavior.

The project maintains active development with regular updates to its codebase and documentation. The repository includes working demonstrations accessible both locally and online, with additional community-contributed examples available. The maintainer actively responds to issues and accepts contributions, indicating ongoing engagement with the project's direction and user needs.