Xpersona
P

pretext

CLAWHUB · Unverified

精准文本测量与布局引擎(基于开源 Pretext)。无需触碰 DOM,纯算术计算文本像素高度,支持中文、CJK、emoji 等多语言。多用于前端虚拟滚动、AI 生成 UI 布局预计算、Canvas 渲染等场景。当用户需要计算文字在指定宽度下的高度、判断换行行数、或精确布局文本时调用此 Skill。

62
Rank
84
Safety
998
Downloads

About

What it does, and when to use it.

Capability contract not published. No trust telemetry is available yet. 998 downloads reported by the source. Last updated 10/11/2026.

Avoid when

  • Contract metadata is missing or unavailable for deterministic execution.

Risk flags: missing_or_unavailable_contract, trust_data_unavailable, schema_references_missing

Install and run

Setup complexity: low.

clawhub skill install s173a6b7vatqkph605zqy4jgdh84adc0:pretext-text-measurement
  1. Install using `clawhub skill install s173a6b7vatqkph605zqy4jgdh84adc0:pretext-text-measurement` in an isolated environment before connecting it to live workloads.
  2. No published capability contract is available yet, so validate auth and request/response behavior manually.
  3. Review the upstream CLAWHUB listing at https://clawhub.ai/markcookie/pretext-text-measurement before using production credentials.

Contract: missing

curl -s "https://www.xpersona.co/api/v1/agents/clawhub-markcookie-pretext-text-measurement/snapshot"

Run-check

$0.02 USD

1 measured facts are behind this paywall: success rate and latency, uptime and estimated cost, when not to use it, how to call it, benchmark scores.

Agents pay $0.02 in USDC. A card payment is $0.50, the smallest a card allows.

Documentation

CLAWHUB

68,329 characters of source documentation, loaded on request.

Extracted files

5 files captured from the source.

SKILL.md

---
name: pretext
description: "精准文本测量与布局引擎(基于开源 Pretext)。无需触碰 DOM,纯算术计算文本像素高度,支持中文、CJK、emoji 等多语言。多用于前端虚拟滚动、AI 生成 UI 布局预计算、Canvas 渲染等场景。当用户需要计算文字在指定宽度下的高度、判断换行行数、或精确布局文本时调用此 Skill。"
description_zh: "精准文本测量与布局引擎,基于 GitHub 42K Stars 的开源项目 @chenglou/pretext。无需 DOM,快速计算任意文本在指定宽度/字体下的像素高度与行数,支持中文、CJK、emoji 等多语言。"
description_en: "Accurate text measurement and layout engine powered by @chenglou/pretext. Zero DOM, pure arithmetic. Calculate text height and line count for any font/width. Supports Chinese, CJK, emoji."
version: "1.0.0"
homepage: https://github.com/chenglou/pretext
license: MIT
allowed-tools: Bash, Read, Write, Edit
metadata:
  clawdbot:
    emoji: "📐"
    requires:
      bins:
        - node
      pip: []
      note: "需要 Node.js 运行环境(v16+)。@chenglou/pretext 为自动依赖。canvas(node-canvas)为可选依赖,在 Windows/Linux/macOS 上可获得高精度测量;未安装时自动降级为纯 JS Unicode 估算模式,功能完整但精度略低。"
    setup:
      - "cd ${CLAUDE_SKILL_DIR} && npm install"
      - "canvas(可选):npm install canvas — 在 Windows 上需要系统依赖(Cairo/Pango 等),安装失败不影响基础功能"
      - "高清模式:node scripts/install-deps.js"
  tags:
    - text-measurement
    - layout
    - frontend
    - virtual-scroll
    - canvas
    - chinese-text
    - i18n
---

# 📐 Pretext Text Measurement Skill

基于开源库 **@chenglou/pretext**(MIT License)构建的精准文本测量与布局引擎。

> GitHub: [chenglou/pretext](https://github.com/chenglou/pretext) · 42.1K Stars
> 版权声明见本文件末尾 `## 开源许可` 章节。

---

## 🎯 核心能力

当用户需要以下任何一种计算时,调用此 Skill:

| 用户意图 | Skill 做什么 |
|:---|:---|
| "这段文字在 300px 宽、16px 字体下有多高?" | 精确计算高度 + 行数 |
| "帮我判断这段文字会不会换行" | 返回行数和每行内容 |
| "这 50 条消息列表,每条在 375px 下多高?" | 批量测量所有条目 |
| "计算这段富文本(带 @提及、标签)的高度" | 富文本内联布局 |
| "找出让文本恰好不换行的最小宽度" | 收缩包裹宽度测量 |
| "这段文字在 Canvas 里怎么分行绘制?" | 返回逐行绘制坐标 |

---

## 🚀 快速开始

### 基础测量(最常用)

```bash
node "${CLAUDE_SKILL_DIR}/scripts/measure.js" \
  --text "你好,世界!这是一段测试文本。" \
  --font "16px Inter, sans-serif" \
  --width 300 \
  --lineHeight 20
```

**返回示例:**
```json
{
  "success": true,
  "text": "你好,世界!这是一段测试文本。",
  "font": "16px Inter, sans-serif",
  "width": 300,
  "lineHeight": 20,
  "height": 44.8,
  "lineCount": 3,
  "unit": "px"
}
```

---

## 📖 场景化使用指南

### 场景 1:虚拟滚动 / 聊天消息列表

```bash
node "${CLAUDE_SKILL_DIR}/scripts/batch.js" \
  --items "第一条消息内容" \
  --items "第二条消息内容" \
  --items "第三条消息内容" \
  --font "14px -apple-system, sans-serif" \
  --width 375 \
  --lineHeight 20
```

### 场景 2:获取逐行详细信息(Canvas 绘制用)

```bash
node "${CLAUDE_SKILL_DIR}/scripts/layout-lines.js" \
  --text "Hello 世界 🚀" \
  --font "18px Inter" \
  --width 320 \
  --lineHeight 26
```

### 场景 3:仅查询行数 / 最宽行(性能最优)

```bash
node "${CLAUDE_SKILL_DIR}/scripts/line-stats.js" \
  --text "多行文本内容,每行长度不一" \
  --font "16px Inter" \
  --width 200
```

### 场景 4:富文本内联(@提及、代码块、标签)

```bash
node "${CLAUDE_SKILL_DIR}/scripts/rich-inline.js" \
  --items '[{"text":"普通文字 ","font":"16px Inter"},{"text":"@用户名","font":"bold 14px Inter","break":"never","ext

README.md

# 📐 Pretext Text Measurement Skill

> 精准文本测量与布局引擎 — 基于 GitHub 42.1K Stars 开源项目 `@chenglou/pretext`

[![MIT License](https://img.shields.io/badge/License-MIT-blue.svg)](https://github.com/chenglou/pretext/blob/main/LICENSE)
[![Node.js >=16](https://img.shields.io/badge/Node.js->=16-green.svg)](https://nodejs.org/)

---

## 🎯 这个 Skill 能做什么?

当你在开发中遇到这些问题时,Pretext Skill 可以帮你解决:

| 场景 | 问题 | Pretext 能做什么 |
|:---|:---|:---|
| 虚拟滚动列表 | 渲染前不知道每条消息多高 | 预计算所有消息高度,零 DOM |
| 聊天应用 | 新消息插入导致滚动位置漂移 | 提前锁定每条消息高度 |
| Canvas 渲染 | Canvas 里无法使用 `getBoundingClientRect` | 直接计算文本宽度和高度 |
| AI 生成 UI | AI 生成代码后文本溢出容器 | 预计算验证布局是否正确 |
| SSR | 服务端无法测量文本高度 | 在 Node.js 中直接计算 |
| 响应式布局 | 窗口缩放需要重新测量 | `layout()` 可无限次调用,极快 |
| **DOM 渲染** | Accordion/Chat Bubbles/Masonry 需要 DOM 回流 | Pretext 预计算高度,直接渲染零抖动 |
| **粒子动画** | 字符需要作为粒子自由运动 | Pretext 精确宽度 + Canvas 动画引擎 |

---

## ⚡ 性能对比

| 方案 | 单次测量耗时 | 500 条批量耗时 |
|:---|:---:|:---:|
| DOM `getBoundingClientRect` | 0.5–3ms + 重排 | 250–1500ms |
| **Pretext(本 Skill)** | **0.0002ms** | **0.09ms** |
| 加速比 | **快了 300–600 倍** | — |

---

## 🚀 快速开始

### 第一步:安装依赖

```bash
cd ~/.workbuddy/skills/pretext
npm install
```

> 如果 `canvas` 安装失败,Skill 会自动降级到 fallback 模式(仍可正常工作,仅换行精度略低)。

### 第二步:运行测试

```bash
# 基础测量
node scripts/measure.js --text "你好,世界!" --font "16px Inter" --width 300

# 批量测量(虚拟滚动)
node scripts/batch.js \
  --items "第一条消息内容" \
  --items "第二条消息内容" \
  --font "14px -apple-system" \
  --width 375

# 逐行布局
node scripts/layout-lines.js --text "Hello 世界 🚀" --font "18px Inter" --width 320
```

---

## 📁 Skill 结构

```
pretext/
├── SKILL.md              ← WorkBuddy/ClawHub 入口文件(Agent 指令)
├── package.json          ← 依赖声明
├── README.md             ← 你正在看的文档
├── LICENSES.md           ← 开源协议声明
├── particle-text-demo.html ← 自动生成:粒子动画演示页
└── scripts/
    ├── measure.js        ← 核心:基础文本测量(最常用)
    ├── layout-lines.js    ← 进阶:逐行详细信息
    ├── batch.js          ← 高效:批量测量(虚拟滚动)
    ├── line-stats.js      ← 极速:仅行统计(性能最优)
    ├── rich-inline.js     ← 富文本:@提及/标签布局
    ├── wrap-layout.js     ← 进阶:文本绕排浮动元素
    ├── render-dom.js     ← 🆕 DOM 渲染(Accordion/Chat/Masonry)
    ├── particle-text.js   ← 🆕 粒子自由运动(Canvas 动画)
    ├── clear-cache.js     ← 工具:清除内部缓存
    └── install-deps.js    ← 工具:自动安装依赖
```

---

## 📖 场景示例

### 示例 1:虚拟滚动初始化

```javascript
// 用户:有 1000 条消息,每条宽 375px,帮我算总高度
// AI 调用:
node scripts/batch.js \
  --items "消息1内容..." \
  --items "消息2内容..." \
  --font "14px -apple-system" \
  --width 375

// 返回:{ totalHeight: 28400, averageHeight: 28.4, itemCount: 1000 }
```

### 示例 2:判断文本会不会溢出

```javascript
// 用户:"这段文字在 300px 宽下会换行吗?"
// AI 调用:
node scripts/line-stats.js \
  --text "这是一段可能比较长的文本内容" \
  --font "16px Inter" \
  --width 300

// 返回:{ lineCount: 2, analysis: { verdict: "会换行 2 行" } }
```

### 示例 3:Canvas 绘制代码生成

```javascript
// 用户:这段文字想在 Canvas 里居中绘制,帮我算坐标
// AI 调用:
node scripts/layout-lines.js \
  --text "Hello World!" \

_meta.json

{
  "ownerId": "kn7aw78qdw5xrvjbrymv270s4s84bvev",
  "slug": "pretext-text-measurement",
  "version": "1.0.1",
  "publishedAt": 1775752292717
}

LICENSES.md

# 开源许可声明

本 Skill 依赖以下第三方开源组件,均为 MIT License。

---

## @chenglou/pretext

```
MIT License

Copyright (c) 2026 Cheng Lou and Pretext contributors
https://github.com/chenglou/pretext

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
```

原文链接:https://github.com/chenglou/pretext/blob/main/LICENSE

---

## node-canvas

```
MIT License

Copyright (c) 2010-2024 Automattic, Nicole Subirana, TJ Holowaychuk
https://github.com/Automattic/node-canvas

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
```

原文链接:https://github.com/Automattic/node-canvas/blob/master/LICENSE

---

## Skill 封装层

Skill 封装层(SKILL.md + scripts/*.js)同样遵循 MIT License。

---

*本文件根据 MIT 协议要求保留所有版权声明。*
*Generated: 2026-04-09*

PUBLISH.md

# Pretext Skill 发布信息

---

## 🗂️ ClawHub 填表信息

| 表单字段 | 填写内容 |
|:---|:---|
| **Skill 名称** | pretext |
| **Slug** | pretext-text-measurement |
| **版本号** | 1.0.0 |
| **许可证** | MIT |
| **作者** | QiuZhenPeng |
| **分类** | Developer Tools / Frontend / Text Processing |
| **标签**(Tags) | text-measurement, layout, frontend, virtual-scroll, canvas, chinese-text, i18n, nodejs, ai-ui |
| **Icon / Emoji** | 📐 |
| **GitHub 地址** | https://github.com/chenglou/pretext |
| **价格** | 免费(Free) |
| **是否开源** | ✅ 是(基于 MIT) |
| **是否收费** | ❌ 否 |

### ClawHub 短简介(≤80 字符)

```
无需 DOM,纯算术计算任意文字在指定宽度下的像素高度与行数。
支持中文、CJK、emoji,零依赖极速运行。
```

### ClawHub 完整产品简介

```
## 📐 Pretext — 精准文本测量与布局引擎

> 「不需要 DOM reflow,不需要 setTimeout,不需要把文字渲染出来再量——直接算出来。」

### 这个 Skill 能解决什么问题?

当你开发虚拟滚动列表、聊天应用、AI 生成 UI、Canvas 文本渲染等场景时,必须提前知道每段文字会占据多高。

传统方案要么渲染后量(慢),要么字数估算(不准)。

Pretext 的解法:**提前一次性测量每个字符的宽度,之后纯算术遍历——O(n) 时间,零 DOM 操作,结果与浏览器渲染高度一致。**

### 核心能力

- **计算高度**:输入文本 + 字体 + 宽度 → 输出精确像素高度
- **判断换行**:返回总行数 + 每行具体内容
- **批量测量**:一次测量 1000 条消息(比 DOM 快 300–600 倍)
- **逐行坐标**:输出每行的起止位置(用于 Canvas 绘制)
- **富文本布局**:支持 @提及、代码块、标签等内联样式
- **DOM 渲染片段**:生成可直接粘贴的逐行 HTML,无需触碰真实 DOM

### 支持语言

中文、CJK(中日韩)、emoji、英文等所有 Unicode 文本。

> ⚠️ 暂不支持 RTL 语言(阿拉伯文、希伯来文)。

### 安装要求

- Node.js v16+
- 可选:`npm install canvas` — 安装后精度更高,未安装不影响基础功能

### 性能数据

| 方案 | 500 条批量测量耗时 |
|:---|---:|
| DOM getBoundingClientRect | 250–1500ms |
| **Pretext(本 Skill)** | **0.09ms** |

### 开源协议

MIT License — 基于 `@chenglou/pretext`(GitHub 42K Stars)
```

### ClawHub 免责声明

```
### ⚠️ 免责声明

**本 Skill 以「现状」(AS IS)提供,不附带任何明示或暗示的保证。**

#### 测量精度
- 文本高度与行数的计算结果为估算值,实际渲染可能因字体渲染引擎、内嵌字体等因素产生偏差。
- 在精确像素级 UI 开发场景中,建议以浏览器实际渲染结果为准。
- 安装 `canvas` 可显著提升精度;未安装时使用纯 JS Unicode 估算模式,功能完整但精度略低。
- 不同操作系统/浏览器环境下实际宽度可能存在 ±5% 误差,此为正常现象。

#### 版权与许可
- 本 Skill 基于 MIT 协议开源项目 `@chenglou/pretext` 构建,版权归原作者 Cheng Lou 所有。
- 本 Skill 封装层(SKILL.md + scripts/*)版权归属 Skill 作者,遵循相同 MIT License。

#### 使用限制
- 不得用于任何非法用途。
- 开发者不对因使用本 Skill 造成的任何直接或间接损失负责。
```

---

## 🗂️ WorkBuddy 技能市场填表信息

| 表单字段 | 填写内容 |
|:---|:---|
| **名称** | pretext |
| **版本** | 1.0.0 |
| **图标** | 📐 |
| **许可证** | MIT |
| **作者** | QiuZhenPeng |
| **分类** | 前端开发 / 文本处理 |
| **标签** | text-measurement, layout, frontend, virtual-scroll, canvas, chinese-text, i18n |
| **价格** | 免费 |
| **是否开源** | ✅ |

### WorkBuddy 简介(description_zh,≤100 字)

```
精准文本测量与布局引擎,基于 GitHub 42K Stars 的开源项目 @chenglou/pretext。
无需 DOM,快速计算任意文本在指定宽度/字体下的像素高度与行数,支持中文、CJK、emoji 等多语言。
```

### WorkBuddy 英文简介(description_en,≤100 字)

```
Accurate text measurement and layout engine powered by @chenglou/pretext.
Zero DOM, pure arithmetic. Calculate text height and line count for any font/width.
Supports Chinese, CJK, emoji.
```

---

## 🗂️ 通用产品简介(官网/文档用)

**Pretext — 精准文本测量与布局引擎**

你是否遇到过这些场景?

- **虚拟滚动**:列表渲染前不知道每条消息多高,插入新消息后滚动位置突然跳走
- **AI 生成 UI**:LLM 生成的代码文本溢出容器,布局全乱
- **Canvas 绘制**:Canvas 里没有 `getBoundingClientRect`,不

Sponsored

Ads related to pretext and adjacent AI workflows.