Implementation:AIScientists Dev WorldSeed subtitle measure
| Knowledge Sources | |
|---|---|
| Domains | Frontend, Subtitles |
| Last Updated | 2026-10-26 18:00 GMT |
Overview
Concrete pretext-based subtitle measurement module that pre-computes text dimensions without DOM rendering, the only file that calls pretext directly, provided by the WorldSeed frontend.
Description
The subtitle-measure module is the sole consumer of the `@chenglou/pretext` library. `measure(formatted)` takes a `FormattedCue` and returns a `MeasuredCue` with pure text dimensions (textW, textH) — no chrome (avatar, padding, gap). For narrative cues, it uses `layoutWithLines` with a fixed max width and relaxed line height. For speech/description, it truncates to max 5 lines, then uses `layout` + `walkLineRanges` to shrink-wrap. For chips, single-line `layout` is used. `measureTextWidth` and `measureEntityCardWidth` provide standalone measurement. `truncateEntityText` truncates to fit entity card widths via binary search. `processRecord` is the full pipeline: StreamRecord → classify → format → measure → MeasuredCue.
Usage
Import `processRecord` from the SSE/polling hook to convert incoming stream records into measured subtitle cues. Import `measureEntityCardWidth` from EntityCard for card sizing.
Code Reference
Source Location
- Repository: WorldSeed
- File: frontend/src/lib/subtitle-measure.ts
Signature
export function measure(formatted: FormattedCue): MeasuredCue;
export function measureTextWidth(text: string, font: string): number;
export function measureEntityCardWidth(name: string, compact: boolean): number;
export function truncateEntityText(text: string, kind: 'fallback' | 'value', compact?: boolean): { text: string; truncated: boolean };
export function processRecord(record: StreamRecord, actionDefs: Record<string, any>): MeasuredCue | null;
Import
import { processRecord, measureEntityCardWidth, truncateEntityText } from '@/lib/subtitle-measure';
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| formatted | FormattedCue | Yes (measure) │ Cue with displayText + kind | |
| record | StreamRecord | Yes (processRecord) │ Raw stream record | |
| actionDefs | Record | Yes (processRecord) │ Action definitions | |
| name | string | Yes (entity) │ Entity name text |
Outputs
| Name | Type | Description |
|---|---|---|
| measure returns | MeasuredCue │ Cue with textW, textH, lines | |
| processRecord returns | null │ Full pipeline result, or null if dropped | |
| measureEntityCardWidth returns | number │ Total card width in px (text + padding + border) |
Usage Examples
import { processRecord } from '@/lib/subtitle-measure';
// In SSE onmessage handler
const cue = processRecord(record, actionDefs);
if (cue) {
subtitleStore.enqueue(cue);
}
import { measureEntityCardWidth } from '@/lib/subtitle-measure';
// Measure card width for entity layout
const cardW = measureEntityCardWidth('old_chen', false);