Cette page est affichée en anglais. Une traduction française est en cours.
ScriptsMay 15, 2026·2 min de lecture

Pretext — Fast Accurate Text Measurement and Layout Engine

A high-performance TypeScript library for precise text measurement, shaping, and layout with sub-pixel accuracy across platforms.

Prêt pour agents

Cet actif peut être lu et installé directement par les agents

TokRepo expose une commande CLI universelle, un contrat d'installation, le metadata JSON, un plan selon l'adaptateur et le contenu raw pour aider les agents à juger l'adaptation, le risque et les prochaines actions.

Native · 98/100Policy : autoriser
Surface agent
Tout agent MCP/CLI
Type
Skill
Installation
Single
Confiance
Confiance : Established
Point d'entrée
Pretext Overview
Commande CLI universelle
npx tokrepo install 15d59a07-5079-11f1-9bc6-00163e2b0d79

Introduction

Pretext is a TypeScript library that solves the long-standing problem of accurate text measurement and layout in web and cross-platform applications. It provides pixel-perfect font metrics, line breaking, and text shaping that work consistently across browsers, Node.js, and native runtimes.

What Pretext Does

  • Measures text dimensions with sub-pixel accuracy across all platforms
  • Performs correct Unicode-aware line breaking and word wrapping
  • Handles complex text shaping including ligatures and kerning
  • Provides a layout engine for multi-line text with alignment and truncation
  • Works in browsers, Node.js, and WebAssembly environments

Architecture Overview

Pretext uses a hybrid approach combining compiled font parsing with a platform-independent shaping engine. Font metrics are extracted at load time and cached for fast repeated measurements. The layout algorithm processes text in a streaming fashion, calculating break opportunities and glyph positions in a single pass for optimal performance.

Self-Hosting & Configuration

  • Install via npm, yarn, or pnpm as a dependency
  • Import measurement and layout functions directly
  • Provide font data as ArrayBuffer for precise metrics
  • Configure default text properties like language and direction
  • Tree-shakeable exports keep bundle size minimal

Key Features

  • Sub-pixel accurate text measurement consistent across platforms
  • Full Unicode support including emoji, CJK, and bidirectional text
  • Streaming layout algorithm handles large text blocks efficiently
  • Zero native dependencies with optional WASM acceleration
  • Comprehensive TypeScript types for all APIs

Comparison with Similar Tools

  • Canvas measureText — browser-only and imprecise; Pretext is cross-platform and exact
  • opentype.js — focuses on font parsing; Pretext adds full layout and line breaking
  • Skia/CanvasKit — heavy WASM binary; Pretext is lightweight and tree-shakeable
  • Yoga — flexbox layout engine; Pretext focuses specifically on text measurement

FAQ

Q: Why not use the browser's native text measurement? A: Browser APIs produce inconsistent results across engines and lack features like line-break prediction needed for custom renderers.

Q: Does it support variable fonts? A: Yes, Pretext reads OpenType variation axes and applies them during measurement.

Q: What is the bundle size? A: The core measurement module is under 20 KB gzipped without WASM acceleration.

Q: Can I use it in a canvas renderer? A: Yes, Pretext is designed to work with any rendering backend including HTML Canvas, WebGL, and SVG.

Sources

Fil de discussion

Connectez-vous pour rejoindre la discussion.
Aucun commentaire pour l'instant. Soyez le premier à partager votre avis.

Actifs similaires