Esta página se muestra en inglés. Una traducción al español está en curso.
ScriptsMay 15, 2026·2 min de lectura

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.

Listo para agents

Este activo puede ser leído e instalado directamente por agents

TokRepo expone un comando CLI universal, contrato de instalación, metadata JSON, plan según adaptador y contenido raw para que los agents evalúen compatibilidad, riesgo y próximos pasos.

Native · 98/100Política: permitir
Superficie agent
Cualquier agent MCP/CLI
Tipo
Skill
Instalación
Single
Confianza
Confianza: Established
Entrada
Pretext Overview
Comando CLI universal
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

Discusión

Inicia sesión para unirte a la discusión.
Aún no hay comentarios. Sé el primero en compartir tus ideas.

Activos relacionados