# chenglou/pretext architecture

> Architecture diagram of the GitHub repository chenglou/pretext, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-17). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/chenglou/pretext
- Repository: https://github.com/chenglou/pretext

## Overview

1. **Repository purpose**
   - This is a TypeScript library called **Pretext** for multiline text measurement and layout.
   - Its core goal is to compute text height, line breaks, and per-line ranges **without relying on DOM reflow**.
   - The repo also contains a large demo/validation surface to prove correctness across scripts, fonts, and browser engines.

2. **Primary architectural boundary: core library vs. everything else**
   - The **`src/`** directory is the actual library.
   - Everything under **`pages/`**, **`scripts/`**, **`corpora/`**, **`accuracy/`**, **`benchmarks/`**, and **`status/`** exists to demo, validate, benchmark, or visualize the library.
   - This separation is important: the runtime library is intentionally small and algorithmic, while the rest is tooling and data.

3. **Core layout pipeline**
   - The main flow is:
     1. **prepare** text using font and whitespace/word-break rules
     2. **measure** segment widths using browser text metrics
     3. **break lines** using pure arithmetic and cached widths
     4. **materialize** line text only when needed
   - The README makes this explicit: `prepare()` is the expensive precomputation stage, while `layout()` is the cheap hot path for resizing and reflow-free height calculation.

4. **Core subsystems in `src/`**
   - `measurement.ts` handles font/text measurement and cache-aware width computation.
   - `line-break.ts` and `line-text.ts` appear to implement segmentation and line assembly.
   - `bidi.ts` plus `src/generated/bidi-data.ts` provide bidirectional text support.
   - `rich-inline.ts` adds a narrower inline-rich-text flow model for chips, mentions, and fragments.
   - `layout.ts` is the central wrapper for converting prepared text into line counts, heights, and line ranges.
   - `analysis.ts` likely contains text normalization/segmentation utilities used during prepare-time analysis.

5. **Key data model and runtime split**
   - The library distinguishes between:
     - **opaque prepared handles** for fast repeated layout
     - **segment-aware prepared handles** for manual line rendering
   - This is an important architectural boundary: the prepare stage captures immutable analysis results, and the layout stage consumes them without redoing text analysis.
   - The richer APIs expose cursors/ranges rather than raw string offsets, which matters for grapheme-safe and bidi-aware iteration.

6. **Rendering-agnostic output**
   - The library is not tied to one rendering target.
   - Its output can feed **DOM**, **Canvas**, **SVG**, and future server-side rendering.
   - The manual line APIs are designed to let consumers render text themselves while the library only handles measurement and break decisions.

7. **Browser font engine as the source of truth**
   - The repo explicitly treats the browser’s own font engine as the measurement ground truth.
   - This implies the library depends on browser canvas/text measurement behavior, but avoids DOM layout APIs like `getBoundingClientRect()` and `offsetHeight`.
   - That choice is central to the architecture: measurement is browser-backed, layout is library-owned arithmetic.

8. **Demo and showcase layer under `pages/`**
   - The `pages/` directory contains static demo entries such as `benchmark`, `accuracy`, `corpus`, `probe`, and multiple `demos/*`.
   - These are not just marketing pages; they are functional examples for different layout scenarios:
     - editorial text
     - markdown/chat-like content
     - masonry-style text
     - justification comparisons
     - dynamic width/height flow
     - rich notes and variable typography
   - `shared/navigation-state.ts` suggests the demo site has shared client-side navigation state across pages.

9. **Validation, benchmarking, and research data flow**
   - The repo contains dedicated datasets for:
     - **accuracy** across browsers (`accuracy/chrome.json`, `firefox.json`, `safari.json`)
     - **benchmarks** across browsers
     - **corpora** of multilingual sample texts
     - **research-data** for font-size scans and related experiments
   - This indicates a strong validation pipeline: real text corpora are run through the library and compared against browser behavior and/or prior baselines.

10. **Automation and scripts**
   - `scripts/` contains the operational tooling for the repo:
     - browser automation
     - benchmark and accuracy checks
     - corpus sweeps and taxonomy/status generation
     - bidi data generation from Unicode source files
     - static demo-site build
     - report/status/dashboard generation
   - These scripts are the bridge between source code and the validation artifacts in the repository.

11. **Unicode and bidi support as a separate generated input**
   - `scripts/unicode/DerivedBidiClass-17.0.0.txt` and `src/generated/bidi-data.ts` show that bidi behavior is generated from Unicode data rather than hand-maintained.
   - This is a meaningful subsystem boundary: text direction logic is data-driven and updated through scripts, not embedded as static logic.

12. **Testing and quality gates**
   - There is at least one direct library test file, `src/layout.test.ts`, plus repository-level checks such as `accuracy-check`, `benchmark-check`, `corpus-check`, `keep-all-check`, and `probe-check`.
   - Quality is validated both through unit-style tests and through browser-backed golden/empirical checks.
   - That combination matters because text layout correctness depends on browser/font behavior, not just algorithmic logic.

13. **Build and runtime environment**
   - The repo is configured around **Bun** rather than Node/npm/pnpm, per the local rules and `bun.lock`.
   - Tooling includes TypeScript configs for build/runtime separation.
   - A GitHub Pages workflow exists (`.github/workflows/pages.yml`), which supports hosting the demo and validation pages as a static site.

14. **Overall architecture in one sentence**
   - Pretext is a **browser-measured, cache-backed text layout engine** whose core library is algorithmic and rendering-agnostic, surrounded by a static demo site, multilingual corpora, browser automation, and generated Unicode data to keep accuracy and bidi behavior trustworthy.

Main entry points: [`src/analysis.ts`](https://github.com/chenglou/pretext/blob/main/src/analysis.ts) (Analysis), [`src/layout.ts`](https://github.com/chenglou/pretext/blob/main/src/layout.ts) (Layout), [`scripts/accuracy-check.ts`](https://github.com/chenglou/pretext/blob/main/scripts/accuracy-check.ts) (Checks), [`corpora/README.md`](https://github.com/chenglou/pretext/blob/main/corpora/README.md) (Corpora)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_core["Core library"]
  node_src_analysis["Analysis<br/>[analysis.ts]"]
  node_src_measurement["Measurement<br/>font metrics<br/>[measurement.ts]"]
  node_src_line_break["Line breaks<br/>break engine<br/>[line-break.ts]"]
  node_src_line_text["Line text<br/>render assembly<br/>[line-text.ts]"]
  node_src_layout["Layout<br/>public API<br/>[layout.ts]"]
  node_src_bidi["Bidi<br/>directionality<br/>[bidi.ts]"]
  node_src_bidi_data["Bidi data<br/>generated unicode data<br/>[bidi-data.ts]"]
  node_src_rich_inline["Rich inline<br/>inline model<br/>[rich-inline.ts]"]
  node_src_test_data["Test data<br/>fixtures<br/>[test-data.ts]"]
end

subgraph group_demo["Demo & validation"]
  node_pages_index["Demo site<br/>static pages<br/>[index.html]"]
  node_pages_demos["Demos<br/>showcase flows"]
  node_pages_accuracy["Accuracy<br/>browser report<br/>[accuracy.ts]"]
  node_pages_benchmark["Benchmark<br/>perf report<br/>[benchmark.ts]"]
  node_pages_corpus["Corpus view<br/>text corpus UI<br/>[corpus.ts]"]
  node_pages_probe["Probe<br/>diagnostic page<br/>[probe.ts]"]
  node_shared_nav["Navigation state<br/>shared UI state"]
end

subgraph group_ops["Tooling & data"]
  node_scripts_checks["Checks<br/>validation scripts<br/>[accuracy-check.ts]"]
  node_scripts_automation["Automation<br/>browser runner"]
  node_scripts_data["Data pipeline<br/>corpus/status tooling<br/>[corpus-taxonomy.ts]"]
  node_scripts_bidi["Bidi generation<br/>unicode transform"]
  node_corpora["Corpora<br/>multilingual texts<br/>[README.md]"]
  node_accuracy_data["Accuracy data<br/>browser baselines<br/>[chrome.json]"]
  node_benchmarks_data["Benchmarks<br/>perf baselines<br/>[chrome.json]"]
  node_research_data["Research data<br/>scan results"]
  node_status_dashboard["Status<br/>dashboard output<br/>[dashboard.json]"]
end

node_src_analysis -->|"feeds widths"| node_src_measurement
node_src_analysis -->|"normalizes direction"| node_src_bidi
node_src_bidi -->|"uses data"| node_src_bidi_data
node_src_measurement -->|"widths to breaks"| node_src_line_break
node_src_line_break -->|"ranges to text"| node_src_line_text
node_src_layout -->|"prepares"| node_src_analysis
node_src_layout -->|"computes"| node_src_line_break
node_src_layout -->|"exposes"| node_src_line_text
node_src_rich_inline -->|"specializes"| node_src_analysis
node_pages_index -->|"links"| node_pages_demos
node_pages_demos -->|"shares state"| node_shared_nav
node_pages_accuracy -->|"renders"| node_accuracy_data
node_pages_benchmark -->|"renders"| node_benchmarks_data
node_pages_corpus -->|"explores"| node_corpora
node_pages_probe -->|"drives"| node_scripts_automation
node_scripts_checks -->|"validates"| node_pages_accuracy
node_scripts_checks -->|"validates"| node_pages_benchmark
node_scripts_checks -->|"validates"| node_pages_corpus
node_scripts_automation -->|"exercises"| node_pages_demos
node_scripts_data -->|"generates"| node_status_dashboard
node_scripts_bidi -->|"generates"| node_src_bidi_data
node_corpora -->|"feeds"| node_benchmarks_data
node_corpora -->|"feeds"| node_accuracy_data
node_research_data -->|"informs"| node_pages_benchmark

click node_src_analysis "https://github.com/chenglou/pretext/blob/main/src/analysis.ts"
click node_src_measurement "https://github.com/chenglou/pretext/blob/main/src/measurement.ts"
click node_src_line_break "https://github.com/chenglou/pretext/blob/main/src/line-break.ts"
click node_src_line_text "https://github.com/chenglou/pretext/blob/main/src/line-text.ts"
click node_src_layout "https://github.com/chenglou/pretext/blob/main/src/layout.ts"
click node_src_bidi "https://github.com/chenglou/pretext/blob/main/src/bidi.ts"
click node_src_bidi_data "https://github.com/chenglou/pretext/blob/main/src/generated/bidi-data.ts"
click node_src_rich_inline "https://github.com/chenglou/pretext/blob/main/src/rich-inline.ts"
click node_src_test_data "https://github.com/chenglou/pretext/blob/main/src/test-data.ts"
click node_pages_index "https://github.com/chenglou/pretext/blob/main/pages/demos/index.html"
click node_pages_demos "https://github.com/chenglou/pretext/tree/main/pages/demos"
click node_pages_accuracy "https://github.com/chenglou/pretext/blob/main/pages/accuracy.ts"
click node_pages_benchmark "https://github.com/chenglou/pretext/blob/main/pages/benchmark.ts"
click node_pages_corpus "https://github.com/chenglou/pretext/blob/main/pages/corpus.ts"
click node_pages_probe "https://github.com/chenglou/pretext/blob/main/pages/probe.ts"
click node_shared_nav "https://github.com/chenglou/pretext/blob/main/shared/navigation-state.ts"
click node_scripts_checks "https://github.com/chenglou/pretext/blob/main/scripts/accuracy-check.ts"
click node_scripts_automation "https://github.com/chenglou/pretext/blob/main/scripts/browser-automation.ts"
click node_scripts_data "https://github.com/chenglou/pretext/blob/main/scripts/corpus-taxonomy.ts"
click node_scripts_bidi "https://github.com/chenglou/pretext/blob/main/scripts/generate-bidi-data.ts"
click node_corpora "https://github.com/chenglou/pretext/blob/main/corpora/README.md"
click node_accuracy_data "https://github.com/chenglou/pretext/blob/main/accuracy/chrome.json"
click node_benchmarks_data "https://github.com/chenglou/pretext/blob/main/benchmarks/chrome.json"
click node_research_data "https://github.com/chenglou/pretext/blob/main/research-data/system-ui-size-scan.json"
click node_status_dashboard "https://github.com/chenglou/pretext/blob/main/status/dashboard.json"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_src_analysis,node_src_measurement,node_src_line_break,node_src_line_text,node_src_layout,node_src_bidi,node_src_bidi_data,node_src_rich_inline,node_src_test_data toneBlue
class node_pages_index,node_pages_demos,node_pages_accuracy,node_pages_benchmark,node_pages_corpus,node_pages_probe,node_shared_nav toneAmber
class node_scripts_checks,node_scripts_automation,node_scripts_data,node_scripts_bidi,node_corpora,node_accuracy_data,node_benchmarks_data,node_research_data,node_status_dashboard toneMint
```

## Components

### Core library

Browser-measured text layout engine and its internal pipeline.

- [Analysis](https://github.com/chenglou/pretext/blob/main/src/analysis.ts): prepare-time analysis · Normalizes text, segments input, and prepares immutable layout inputs. · `src/analysis.ts`
- [Measurement](https://github.com/chenglou/pretext/blob/main/src/measurement.ts): font metrics · Measures text using browser font metrics and caches widths. · `src/measurement.ts`
- [Line breaks](https://github.com/chenglou/pretext/blob/main/src/line-break.ts): break engine · Computes line break decisions from prepared segments and measured widths. · `src/line-break.ts`
- [Line text](https://github.com/chenglou/pretext/blob/main/src/line-text.ts): render assembly · Materializes line content and ranges only when needed for rendering. · `src/line-text.ts`
- [Layout](https://github.com/chenglou/pretext/blob/main/src/layout.ts): public API · Converts prepared text into line counts, heights, and line ranges. · `src/layout.ts`
- [Bidi](https://github.com/chenglou/pretext/blob/main/src/bidi.ts): directionality · Implements bidirectional text handling for mixed-script layout. · `src/bidi.ts`
- [Bidi data](https://github.com/chenglou/pretext/blob/main/src/generated/bidi-data.ts): generated unicode data · Generated Unicode bidi class data consumed by bidi logic. · `src/generated/bidi-data.ts`
- [Rich inline](https://github.com/chenglou/pretext/blob/main/src/rich-inline.ts): inline model · Supports chips, mentions, and fragment-aware inline content. · `src/rich-inline.ts`
- [Test data](https://github.com/chenglou/pretext/blob/main/src/test-data.ts): fixtures · Shared input cases used by library tests and checks. · `src/test-data.ts`

### Demo & validation

Static pages, showcase flows, and browser-backed evaluation surfaces.

- [Demo site](https://github.com/chenglou/pretext/blob/main/pages/demos/index.html): static pages · Entry point for the showcase and validation pages under pages/. · `pages/demos/index.html`
- [Demos](https://github.com/chenglou/pretext/tree/main/pages/demos): showcase flows · Interactive scenarios for editorial, markdown/chat, masonry, justification, and rich text layouts. · `pages/demos`
- [Accuracy](https://github.com/chenglou/pretext/blob/main/pages/accuracy.ts): browser report · Visualizes browser-backed accuracy comparisons and golden results. · `pages/accuracy.ts`
- [Benchmark](https://github.com/chenglou/pretext/blob/main/pages/benchmark.ts): perf report · Runs and displays benchmark-oriented layout measurements. · `pages/benchmark.ts`
- [Corpus view](https://github.com/chenglou/pretext/blob/main/pages/corpus.ts): text corpus UI · Explores multilingual corpora and script-specific examples. · `pages/corpus.ts`
- [Probe](https://github.com/chenglou/pretext/blob/main/pages/probe.ts): diagnostic page · Interactive diagnostic surface for checking text behavior across browsers. · `pages/probe.ts`
- [Navigation state](https://github.com/chenglou/pretext/blob/main/shared/navigation-state.ts): shared UI state · Client-side navigation state shared across the demo pages. · `shared/navigation-state.ts`

### Tooling & data

Automation, corpora, benchmarks, accuracy data, and generated inputs.

- [Checks](https://github.com/chenglou/pretext/blob/main/scripts/accuracy-check.ts): validation scripts · Repository checks for accuracy, benchmarks, corpus sweeps, keep-all, and probe behavior. · `scripts/accuracy-check.ts`
- [Automation](https://github.com/chenglou/pretext/blob/main/scripts/browser-automation.ts): browser runner · Browser automation and report generation used by the validation pipeline. · `scripts/browser-automation.ts`
- [Data pipeline](https://github.com/chenglou/pretext/blob/main/scripts/corpus-taxonomy.ts): corpus/status tooling · Corpus taxonomy, status generation, dashboards, and static demo-site build. · `scripts/corpus-taxonomy.ts`
- [Bidi generation](https://github.com/chenglou/pretext/blob/main/scripts/generate-bidi-data.ts): unicode transform · Generates bidi data from Unicode source files. · `scripts/generate-bidi-data.ts`
- [Corpora](https://github.com/chenglou/pretext/blob/main/corpora/README.md): multilingual texts · Sample texts and taxonomy data used for validation across scripts and browsers. · `corpora/README.md`
- [Accuracy data](https://github.com/chenglou/pretext/blob/main/accuracy/chrome.json): browser baselines · Browser-specific accuracy artifacts for Chrome, Firefox, and Safari. · `accuracy/chrome.json`
- [Benchmarks](https://github.com/chenglou/pretext/blob/main/benchmarks/chrome.json): perf baselines · Browser benchmark artifacts for performance comparison. · `benchmarks/chrome.json`
- [Research data](https://github.com/chenglou/pretext/blob/main/research-data/system-ui-size-scan.json): scan results · Experimental font-size scan data used to understand layout behavior. · `research-data/system-ui-size-scan.json`
- [Status](https://github.com/chenglou/pretext/blob/main/status/dashboard.json): dashboard output · Generated status dashboard artifacts for the repository. · `status/dashboard.json`

## Connections

- Analysis → Measurement: feeds widths · Prepared text is measured using cached browser metrics.
- Analysis → Bidi: normalizes direction · Analysis prepares bidi-aware text structure.
- Bidi → Bidi data: uses data · Directionality logic depends on generated Unicode bidi classes.
- Measurement → Line breaks: widths to breaks · Measured widths drive arithmetic line breaking.
- Line breaks → Line text: ranges to text · Break decisions are materialized into line text on demand.
- Layout → Analysis: prepares · Public layout entry points start with prepare-time analysis.
- Layout → Line breaks: computes · Layout consumes prepared state to produce lines and height.
- Layout → Line text: exposes · Layout can surface per-line ranges for manual rendering.
- Rich inline → Analysis: specializes · Rich inline flows reuse the core prepare/analysis model.
- Demo site → Demos: links · The demo entry point organizes the showcase pages.
- Demos → Navigation state: shares state · Demo pages coordinate through shared navigation state.
- Accuracy → Accuracy data: renders · Accuracy UI reads browser comparison artifacts.
- Benchmark → Benchmarks: renders · Benchmark UI reads performance baselines.
- Corpus view → Corpora: explores · Corpus UI presents multilingual sample texts and taxonomy.
- Probe → Automation: drives · Probe pages are exercised by browser automation checks.
- Checks → Accuracy: validates · Accuracy checks compare library behavior against browser-backed expectations.
- Checks → Benchmark: validates · Benchmark checks run performance evaluations.
- Checks → Corpus view: validates · Corpus checks sweep multilingual samples and classifications.
- Automation → Demos: exercises · Automation drives demos and diagnostics in browsers.
- Data pipeline → Status: generates · Status and dashboard scripts produce repository reporting artifacts.
- Bidi generation → Bidi data: generates · Unicode source data is transformed into generated bidi tables.
- Corpora → Benchmarks: feeds · Corpora are used to produce benchmark and validation datasets.
- Corpora → Accuracy data: feeds · Sample texts support accuracy comparisons.
- Research data → Benchmark: informs · Research scans help interpret layout and sizing behavior.
