登录 注册
其他
51,037
Stars
2,756
Forks
134
Watchers
64
Open Issues
NOASSERTION
许可证
TypeScript
语言
2026-03-07
创建时间
2026-10-15
最近更新

项目能做什么

快速、精准且全面的文本测量与排版库。
来源:AI 基于仓库事实生成 · 内容以仓库实际 README 为准

使用说明 查看原始 README 原仓库文档

中文使用说明暂未生成。下面保留原仓库 README 摘录,命令与配置均以原文为准。
原始 README 摘录(英文原文,节选前 3000 字符)
# Pretext

Pure JavaScript/TypeScript library for multiline text measurement & layout. Fast, accurate & supports all the languages you didn't even know about. Allows rendering to DOM, Canvas and SVG.

Pretext side-steps the need for DOM measurements (e.g. `getBoundingClientRect`, `offsetHeight`), which trigger layout reflow, one of the most expensive operations in the browser. It implements its own text measurement logic, using the browsers' own font engine as ground truth (very AI-friendly iteration method).

## Installation

```sh
npm install @chenglou/pretext
```

## Demos

The demos are exemplary API usage patterns we encourage you to read. They don't ship in the npm package, so clone the repo, run `bun install`, then `bun start`, and open <http://localhost:3000/demos> in your browser. On Windows, use `bun run start:windows`.
Alternatively, see them live at [chenglou.me/pretext](https://chenglou.me/pretext/). Some more at [somnai-dreams.github.io/pretext-demos](https://somnai-dreams.github.io/pretext-demos/)
Building a chat or another long list? [pages/demos/markdown-chat.md](https://github.com/chenglou/pretext/blob/main/pages/demos/markdown-chat.md) walks through the Markdown chat demo's patterns and when you can skip each.

## API

Pretext serves 2 use cases:

### 1. Measure a paragraph's height _without ever touching DOM_

```ts
import { prepare, layout } from '@chenglou/pretext'

const prepared = prepare('AGI 春天到了. بدأت الرحلة 🚀‎', '16px Inter')
const { height, lineCount } = layout(prepared, 320, 20) // pure arithmetic. No DOM layout & reflow!
```

`prepare()` does the one-time work: normalize whitespace, segment the text at its break opportunities, measure the segments with canvas, and return an opaque handle. `layout()` is the cheap hot path after that: pure arithmetic over cached widths. Do not rerun `prepare()` for the same text, font and options; that'd defeat its precomputation. For example, on resize, only rerun `layout()`.

To paint the text, give one element that `font`, the `line-height` and content `width` you passed to `layout()`, and `overflow-wrap: break-word`: the browser wraps it to the same lines. Text in several fonts or with padded pieces goes through the rich inline text API below, and is painted a line at a time.

If you want textarea-like text where ordinary spaces, `\t` tabs, and `\n` hard breaks stay visible, pass `{ whiteSpace: 'pre-wrap' }` to `prepare()`:

```ts
const prepared = prepare(textareaValue, '16px Inter', { whiteSpace: 'pre-wrap' })
const { height } = layout(prepared, textareaWidth, 20)
// A <textarea> shows one more line than this when its value is empty or ends with \n

// Long text edited live: prepare each paragraph apart, keeping its \n, and re-prepare only the one an edit touches
const paragraphs = textareaValue.split(/(?<=\n)/).map(p => prepare(p, '16px Inter', { whiteSpace: 'pre-wrap' }))
const lineCount = paragraphs.reduce((n, p) => n + layout(p, textareaWidth, 20).lineCount, 0)
```

Other `pr
阅读完整原文

项目深度分析 AI 生成

高并发/零抖动虚拟聊天与内容流商业级 React/Vue 组件库

产品构想基于 Pretext 的精确离屏测量能力,封装支持可变高度、图片/多语言混排、无闪烁动态加载且零 DOM 测量损耗的高性能虚拟滚动列表商用组件。
核心功能开箱即用的 VirtualizedList、集成 Markdown 语法块与代码块高度预判、动态宽度 ResizeObserver 联动优化、滚动位置绝对锚定(防止跳动)。
目标用户需要构建高性能 AI 对话界面、社交信息流、动态评论系统的独立开发者与中小 SaaS 团队。
难度/周期中等 · 1 - 2 个月 · 成本:开发机器、文档站点托管与基础测试服务器(极低)
收费模式双重许可(开源基础版 + 付费商业 License 支持多端/高级动画)、商业组件库买断售卖(如 Tailwind UI 模式)。
竞争风险各前端框架(TanStack Virtual 等)生态已有现成通用虚拟滚动方案,用户需要有性能极致敏感场景才会买单。
依据 已验证事实

Canvas/SVG 在线设计与海报自动排版商业 SDK

产品构想针对无需依赖 DOM 的轻量级图像合成、海报生成工具,提供基于 Canvas/SVG 的高级富文本排版排版引擎(支持多字重混排、文字自适应收缩、行内徽章)。
核心功能文本框自动适配缩放(Fit-to-box)、图文环绕纯数学计算、行内标签与装饰线排版、Node.js 服务端 Canvas 兼容模块。
目标用户在线设计工具、电商营销图自动生成 SaaS、动态图片服务端渲染平台。
难度/周期高 · 2 - 3 个月 · 成本:服务端基准测试集群搭建、跨平台字体文件与排版测试成本
收费模式按企业席位授权商业 SDK,或提供无头图片排版与渲染微服务(按调用量计费 API)。
竞争风险服务端无头环境(如 headless Linux)中的字体匹配与 Canvas API 差异会导致测量误差。
依据 AI推测

CI/CD 自动化多语言 UI 文本截断与溢出检测工具

产品构想利用 Pretext 的离屏纯算术快速计算特性,在 CI 阶段直接跑批处理,验证 i18n 翻译文本在固定 UI 容器中是否会溢出或出现不良折行,替代笨重的 Headless Chrome E2E 测试。
核心功能集成 Figma 变量与代码仓库语言包、无头快速扫描所有按钮/卡片文字换行状态、自动生成截断风险报告与 Pull Request 预警卡片。
目标用户出海移动端/Web 应用团队、大型多语言网站本地化团队(L10n/i18n QA)。
难度/周期中等 · 1 - 2 个月 · 成本:GitHub Action 插件集成与静态分析服务器成本(低)
收费模式GitHub Marketplace 付费 App / SaaS 订阅(按扫描仓库数与语言量月付)。
竞争风险用户在浏览器实际渲染中的字体渲染特性(Font Smoothing、系统备用字体差异)可能导致测试出现假阳性或假阴性。
依据 尚需验证的假设
AI 基于仓库公开数据分析生成,含推测成分,不构成收益承诺。
报告会保存到 个人中心

竞品与相似项目 GitHub Search 真实检索

项目中文简介Stars语言
PreTeXtBook/pretext 中文介绍暂未生成
PreTeXt: an authoring and publishing system for scholarly do
468 XSLT
L4bF0x/PhishingPretexts 中文介绍暂未生成
A library of pretexts to use on offensive phishing engagemen
398 HTML
JubaKitiashvili/expo-pretext 中文介绍暂未生成
Predict React Native text heights before rendering. Native T
259 TypeScript
tornikegomareli/swift-pretextkit 中文介绍暂未生成
Swift port of chenglou/pretext, arithmetic text measurement
188 Swift
以上为通过 GitHub 官方 Search API 检索的相似项目,功能差异与优劣势需进一步人工确认,配置 AI 后可生成对比分析。

项目优势与风险

技术成熟度创建于 2026-03-07,近 30 天仍在推送,发布过 1 个 Release
社区活跃Star 51,037 / Fork 2,756 / Issue 64(数据支持,可自行核实)
许可证NOASSERTION
主要风险上游项目方向可能变化;同赛道竞争者较多(见竞品表);热度与需求不完全等价

安装与使用

此项目未提供 Docker 镜像,安装步骤以原仓库 README 为准(涉及依赖安装与环境配置,对新手门槛较高)。