# Text

> `@typegpu/kit` 0.1.0. If it is installed, read `node_modules/@typegpu/kit/docs/` instead.

Labels and on-screen text that stay sharp at any size, from a font you bake at build time, with no WebAssembly on the device.

> `import { text } from '@typegpu/kit'`: `text.create(root, font, { capacity, length, place?, camera?, … }) → TextSet`. Creates labels drawn from a baked MSDF font atlas. **records** `drawing.draw` render: `drawing`, added to passes, after the passes that move the labels. **owns** 2 buffers, a sampler: the quads and the labels. **lifecycle** `destroy()`, `[Symbol.dispose]`. **injects** member: `move`, `setLabel`, `setQuad`, `quadAt` and `erase`, which write its own buffers in any pipeline. **needs** `cameras`: as `options.camera`; fills a hook in its pipelines; `font`: as `font`. **GPU functions** `facing`, `ground`, `screen`, `pinned`. **schemas** `Label`, `Quad`, `Fragment`, `Placed`, `Seen`, `Corner`. **types** `Place`. `src/text/text.ts`; jelly-world, solar-system, stampede, star-map.
>
> `Options`, taken by `create`:
> - `capacity: number` Sets the number of labels to an integer of at least 1.
> - `length: number` Limits glyphs drawn per label to an integer of at least 1.
> - `place?: Place` A `'use gpu'` function. Places glyph corners with `facing`, `ground`, `pinned`, `screen` or a custom function. Default `facing`.
> - `camera?: CameraBase` Supplies the camera for `facing`, `ground` and `pinned`, unless the pass description injects its own. Default none, which requires camera-dependent text to use a description that injects a camera.
> - `shade?: (fragment: Fragment) => d.v4f` A `'use gpu'` function. Returns premultiplied colour for a fragment. Default the label colour over the glyph with one pixel of antialiasing at its edge.
> - `blend?: 'over' | 'add' | 'translucent' | GPUBlendState` Selects `over` for text that covers the background, `add` for glowing text, `translucent` for the passes' translucency targets, or a custom blend state. Default `over`.
> - `label?: string` Sets the drawing pipeline label. Default `text`.
>
> `Label`, a schema:
> - `position: d.vec3f` Holds the world-space anchor.
> - `color: d.vec4f` Holds linear colour and opacity.
>
> `Quad`, a schema:
> - `pen: d.vec2f`
> - `glyph: d.u32`
> - `em: d.f32`
>
> `Fragment`, a schema:
> - `label: d.u32`
> - `glyph: d.u32` Gives the zero-based glyph index within the label.
> - `local: d.vec2f` Holds the pixel offset from the label anchor in em, with y increasing upwards.
> - `color: d.vec4f` Holds the label colour and opacity.
> - `distance: d.f32` Gives signed distance to the glyph edge in screen pixels, positive inside.
> - `round: d.f32` Gives signed distance from the fourth atlas channel in screen pixels.
> - `reach: d.f32` Gives the maximum range of `distance` and `round` in screen pixels.
>
> `Placed`, a schema:
> - `label: d.u32`
> - `glyph: d.u32` Gives the zero-based glyph index within the label.
> - `pen: d.vec2f` Holds the pen offset from the label anchor, with y increasing upwards, in the units of `em`.
> - `em: d.f32` Holds the label units per em.
>
> `Seen`, a schema:
> - `label: d.u32`
> - `color: d.vec4f` Holds the premultiplied colour returned by `shade`.
>
> `Corner`, a schema:
> - `position: d.vec4f` Holds the clip position returned by `place`.
> - `uv: d.vec2f` Holds atlas coordinates from 0 to 1.
> - `bounds: d.vec4f` Holds the glyph cell bounds as left, top, right and bottom coordinates from 0 to 1 across the atlas.
> - `color: d.vec4f` Holds the label colour and opacity.
> - `label: d.u32`
> - `glyph: d.u32` Gives the glyph index within its label.
> - `local: d.vec2f` Holds the corner offset from the label anchor in em, with y increasing upwards.
>
> `TextSet`, from `create`:
> - `capacity: number` Reports the number of labels, from the `capacity` option.
> - `length: number` Reports the most glyphs a label draws, from the `length` option.
> - `inject()` Fills `move`, `setLabel`, `setQuad`, `quadAt` and `erase` with this set, as their defaults do.
> - `move(label, position)` Moves a label anchor from a kernel.
> - `setLabel(label, value)` Sets a label anchor and colour from a kernel.
> - `setQuad(label, k, quad)` Sets glyph `k` below `length` from a kernel.
> - `quadAt(label, k)` Reads glyph `k` of a label from a kernel, such as one copying a label `write` laid out into another set.
> - `erase(label)` Erases every glyph in a label from a kernel, as `write(label, '')` does on the CPU.
> - `labels: TgpuBuffer<d.WgslArray<typeof Label>> & StorageFlag` Holds label anchors and colours, initially at the origin and white.
> - `quads: TgpuBuffer<d.WgslArray<typeof Quad>> & StorageFlag` Holds `length` quads per label in label order.
> - `write(label, text, options)` Lays out and uploads only the specified label.
> - `drawing: passes.Drawing<typeof Corner, d.Infer<typeof Seen>>` Draws all labels when added to a canvas or scene pass description.
> - `drawing.draw(pass)` Records a draw into the pass, on which the description has set the pipeline it built.
> - `destroy()` Destroys the label and quad buffers.

HTML labels over a canvas cannot move from kernels, hide behind the scene or run on React Native. GPU text needs glyph layout and an atlas. `@pmndrs/glyph` shapes strings at runtime with 1.4 MB of WebAssembly, which Hermes cannot run.

Bake fonts with Glyph at build time. It produces a **multi-channel signed distance field**, or MSDF, atlas and JSON with advances and kerning. On the device, JavaScript lays out text and the GPU draws one instanced quad per glyph.

```ts
import { font, text } from '@typegpu/kit';
import interData from '@typegpu/kit/fonts/inter.json?url';
import interAtlas from '@typegpu/kit/fonts/inter.rgba?url';

// Vite serves the shipped font as two files
const [data, atlas] = await Promise.all([
  fetch(interData).then((response) => response.json()),
  fetch(interAtlas).then((response) => response.arrayBuffer()),
]);
const sans = font.create(root, font.read(data, atlas));

const tags = text.create(root, sans, {
  camera: view,
  capacity: 16,
  length: 24,
  place: text.pinned,
});
const canvas = passes.create(root, surface).pipe(view.inject());
const drawn = canvas.add(tags.drawing);

tags.write(0, 'Lantern', { em: 18, baseline: 'bottom' });
tags.labels.patch({ 0: { position: [2, 1.5, 0], color: [1, 0.8, 0.4, 1] } });

frames.animate(
  frames.create(root, (frame) => {
    canvas.record(frame, drawn);
  }),
);
```

- **Capacity** A set holds `capacity` labels with at most `length` glyphs each. `write(label, text, options)` lays out one label and uploads its glyphs. Spaces use no glyph slots. Glyphs beyond `length` are omitted.
- **Moving and fading** `labels` stores anchors and linear colours, initially white at the origin. Move or fade labels with TypeGPU's `patch`, which uploads only supplied fields. Kernels can use `tags.move(label, position)` and `tags.setLabel(label, value)`. These updates do not repeat layout.
- **Size and anchor** This label uses 18 points per em. Their pixel size is `em` times the pass's pixels per point, the surface's `pixelRatio`, giving a consistent size across screens. Labels centre on their anchor. Use `baseline: 'bottom'` to place their bottom at the anchor.
- **Drawing** `canvas.add(tags.drawing)` builds the pipeline into a [pass description](passes.md). One draw renders `capacity * length` instances. Empty slots still run four vertices each.
- **Font bytes** Load font bytes in your code. See [Loading a font](#loading-a-font) for each platform.

The [solar system](../examples/solar-system.md) example bends its labels around each planet with a custom `place`, and types them on glyph by glyph.

Any kernel can write glyphs. `quads` stores `length` entries per label in label order. Each entry is a `text.Quad`:

- **Writing a glyph** `tags.setQuad(label, k, quad)` sets glyph `k`. Its `pen` and `em` use the units expected by `place`. `glyph` is the font index plus 1, or 0 to hide it. Read indices and advances from `font.glyphOf` and `font.advance`.
- **Erasing** `tags.erase(label)` hides all of a label's glyphs. Erase a label before a kernel writes fewer glyphs than `write` did.
- **Reading a glyph** `tags.quadAt(label, k)` reads glyph `k`. To show text a kernel picks, `write` each choice once into a set you never draw, and copy the chosen label's quads with `setQuad(0, k, bank.quadAt(choice, k))` from a kernel that reads both sets. The [stampede](../examples/stampede.md) names the horse under the pointer this way.

## Loading a font

> `import { font } from '@typegpu/kit'`: `font.create(root, baked) → Font`, `font.metrics(data) → Metrics`, `font.read(json, atlas) → Baked`. Uploads a baked font atlas and glyph quads for text sets. **owns** a texture, a buffer: the atlas, and each glyph's quad. **lifecycle** `destroy()`, `[Symbol.dispose]`. **schemas** `Glyph`. **types** `FontData`, `GlyphData`. `src/text/font.ts`; jelly-world, solar-system, stampede, star-map.
>
> `Glyph`, a schema:
> - `plane: d.vec4f` Holds left, bottom, right and top bounds with y increasing upwards from the baseline.
> - `uv: d.vec4f` Holds left, top, right and bottom coordinates from 0 to 1 across the atlas.
>
> `Font`, from `create`:
> - `glyphs: TgpuBuffer<d.WgslArray<typeof Glyph>> & StorageFlag` Holds glyph quads by index, with font characters in order followed by the missing glyph.
> - `atlas: TgpuTexture<{ size: [number, number]; format: 'rgba8unorm' }> & SampledFlag` Provides the atlas in `rgba8unorm` format.
> - `destroy()` Frees the atlas and glyph buffer.
> - `data: FontData` Holds the glyph metrics and kerning the font was made from.
> - `glyphOf(code)` Returns the glyph index for a code point, or the missing glyph index if absent.
> - `advance(glyph)` Returns the pen advance in em before kerning.
> - `inked(glyph)` Returns whether a glyph draws ink.
> - `kerning(first, second)` Returns the kerning offset in em to add to the first glyph advance.
>
> `Metrics`, from `metrics`:
> - `data: FontData` Holds the glyph metrics and kerning the font was made from.
> - `glyphOf(code)` Returns the glyph index for a code point, or the missing glyph index if absent.
> - `advance(glyph)` Returns the pen advance in em before kerning.
> - `inked(glyph)` Returns whether a glyph draws ink.
> - `kerning(first, second)` Returns the kerning offset in em to add to the first glyph advance.
>
> `Baked`, from `read`:
> - `data: FontData` Holds the glyph metrics and kerning.
> - `atlas: Uint8Array` Holds RGBA texels in rows from top to bottom, with 4 bytes per texel.

The package includes baked Inter SemiBold in `@typegpu/kit/fonts/inter.json` and `inter.rgba`. Baking a font produces the same two files. Pass the JSON as text or parsed data, plus atlas bytes, to `font.read(json, atlas)`. It returns a `Baked` with `data`, a `FontData`, and `atlas` for `font.create`. It throws for versions other than 1, missing or mistyped fields, or an atlas size other than 4 bytes per texel at the JSON dimensions. Field errors name the field.

**Web**

Vite serves package files at their own URLs with `?url` in development and builds, as in the opening example. Use types from `vite/client`. Fetch your own baked font from `public/` by its path.

**React Native**

Metro bundles the JSON as a module, and the atlas as an asset once `rgba` is in its `assetExts`:

```js title="metro.config.js"
const { getDefaultConfig } = require('expo/metro-config');
const { getBundleModeMetroConfig } = require('react-native-worklets/bundleMode');

const config = getBundleModeMetroConfig(getDefaultConfig(__dirname));
config.resolver.assetExts.push('rgba');
module.exports = config;
```

```ts
import { font } from '@typegpu/kit';
import data from '@typegpu/kit/fonts/inter.json';
import { Asset } from 'expo-asset';
import { File } from 'expo-file-system';

const [atlas] = await Asset.loadAsync(require('@typegpu/kit/fonts/inter.rgba'));
const bytes = await new File(atlas!.localUri!).arrayBuffer();
const sans = font.create(root, font.read(data, bytes));
```

Use Expo 58's `expo-asset` and `expo-file-system` to load the font files.

**Node**

Use `readFileSync` in tests or scripts to load both files. Resolve a package file with `new URL(import.meta.resolve('@typegpu/kit/fonts/inter.rgba'))`. `font.read` accepts JSON from `readFileSync(url, 'utf8')` and atlas bytes as a `Buffer`.

## Baking a font

> `import { bake, format, ASCII } from '@typegpu/kit/fontBaker'`: `bake({ input, … }) → Promise<Bake>`, `format(data)`. This Node build-time tool bakes fonts for `@typegpu/kit` with `@pmndrs/glyph`. **kind** tool. **constants** `ASCII`. `src/text/bake.ts`.
>
> `BakeOptions`, taken by `bake`:
> - `input: string` Selects a static TTF, OTF, TTC or OTC file.
> - `characters?: string` Adds characters to printable ASCII, such as `…°é`. Default none.
> - `emSize?: number` Sets atlas texels per em. Default 40.
> - `pixelRange?: number` Sets the signed-distance range of the atlas in texels. Default 8.
> - `fontFaceIndex?: number` Selects a face within a font collection. Default 0.
>
> `Bake`, from `bake`:
> - `absent: string` Lists requested characters that the font lacks.
> - `data: FontData` Holds the glyph metrics and kerning.
> - `atlas: Uint8Array` Holds RGBA texels in rows from top to bottom, with 4 bytes per texel.

`typegpu-kit-bake-font` runs on Node and requires `@pmndrs/glyph` 0.1 installed alongside it.

```sh
typegpu-kit-bake-font --input Inter-SemiBold.ttf --output public/fonts/inter --characters "…↓"
```

- The command writes `inter.json` and `inter.rgba`. They include printable ASCII, the supplied `--characters` and a fallback glyph that advances without drawing.
- It warns of characters given that the font has no glyph for.
- Glyph bakes only static fonts. Pin variable-font axes first, for example with `fonttools varLib.instancer Font.ttf wght=600`.
- The shipped Inter is baked with `…` at the defaults below, under the SIL Open Font License.

| Flag                | `bake`          | Default |                                                                                                                          |
| ------------------- | --------------- | ------- | ------------------------------------------------------------------------------------------------------------------------ |
| `--characters`      | `characters`    |         | Additional characters beyond printable ASCII. Only Latin text is supported because runtime layout does not shape glyphs. |
| `--em-size`         | `emSize`        | `40`    | Atlas texels per em. Larger values keep corners sharper on large text and make a larger atlas.                           |
| `--pixel-range`     | `pixelRange`    | `8`     | Distance range in atlas texels. It limits how far outlines and glows can extend.                                         |
| `--font-face-index` | `fontFaceIndex` | `0`     | The face of a collection.                                                                                                |

In a build script, import `bake(options)` from `@typegpu/kit/fontBaker`. Pass the font file as `input` with the options above. The result contains `Baked` for `font.create` and `absent`, the requested characters missing from the font. `format(data)` produces the command's JSON format, with one line per glyph and kerning pair.

```ts
import { writeFile } from 'node:fs/promises';
import { bake, format } from '@typegpu/kit/fontBaker';

const { data, atlas, absent } = await bake({ input: 'Inter-SemiBold.ttf', characters: '…' });
if (absent) throw new Error(`Inter has no glyph for ${absent}.`);
await writeFile('public/fonts/inter.json', format(data));
await writeFile('public/fonts/inter.rgba', atlas);
```

The atlas must fit on one page of Glyph's packer. The bake reports an error if it does not.

## Layout

> `import { typeset } from '@typegpu/kit'`: `typeset.create(capacity) → Typesetter`. This module lays out strings on the CPU using baked font advances and kerning. **kind** cpu. **types** `Layout`. `src/text/typeset.ts`; solar-system.
>
> `Options`, taken by `Typesetter.set`:
> - `em: number` Sets positive units per em.
> - `align?: 'left' | 'center' | 'right'` Sets the anchor position along each line. Default `center`.
> - `baseline?: 'top' | 'cap' | 'middle' | 'alphabetic' | 'bottom'` Sets the vertical anchor at the first line top, the top of its capitals, block middle, first baseline or last line bottom. Default `middle`.
> - `tracking?: number` Adds space after each character but the last of a line, in em. Default 0.
>
> `Typesetter`, from `create`:
> - `set(font, text, options)` Replaces the layout with `text` using a font or its `metrics`.
> - `count: number` Gives the number of placed glyphs with ink, up to capacity.
> - `lines: number` Gives the number of lines.
> - `width: number` Gives the widest line in the units of `em`.
> - `height: number` Gives the block height in the units of `em`, from the first line top to the last line bottom.
> - `glyphs: Uint32Array` Lists placed glyph indices in the font.
> - `pens: Float32Array` Holds x and y pen coordinates from the anchor, with y increasing upwards, in the units of `em`.

`write` lays text out with these options.

| Option     | Default    |                                                                                                                                                                         |
| ---------- | ---------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `em`       |            | Units per em. Use world units for world text and points for `screen` or `pinned` text.                                                                                  |
| `align`    | `'center'` | Horizontal anchor position within each line, `'left'`, `'center'` or `'right'`.                                                                                         |
| `baseline` | `'middle'` | Vertical anchor position within the block, `'top'`, `'cap'` for the top of the first line's capitals, `'middle'`, `'alphabetic'` for the first baseline, or `'bottom'`. |
| `tracking` | `0`        | Space added after each character but the last of a line, in em.                                                                                                         |

`\n`, `\r\n` and `\r` start lines. Text does not wrap to a width.

`write` returns the label's layout, with its `width`, `height` and `lines`, valid until the set's next `write`. Size a panel behind a label from it.

Use the same layout without drawing to measure text before writing it. `typeset.create(capacity).set(font, text, options)` returns the same layout. Once its arrays fit the longest text, it allocates no more. `typeset` reads only advances and kerning. Pass `font.metrics(data)` without a root to measure text in Node, a worker or before `tgpu.init`.

```ts
const { data } = font.read(json, atlas);
const panel = typeset.create(32).set(font.metrics(data), 'Game over', { em: 24 });
const [width, height] = [panel.width, panel.height]; // in points, as em is
```

## Placing text

`place` converts a glyph corner to clip space using the label's `position` and the corner offset. Choose one of four functions:

| `place`       | `position`                                                        | `em` is in  |
| ------------- | ----------------------------------------------------------------- | ----------- |
| `text.facing` | A point in the world; the text faces the camera.                  | World units |
| `text.ground` | A point in the world; the text lies on the ground, read along +x. | World units |
| `text.pinned` | A point in the world; the text stays as large wherever it is.     | Points      |
| `text.screen` | Pixels from the drawn region's top-left in x and y, depth in z.   | Points      |

- A point equals the [`passes.viewport`](passes.md#pixels-and-points)'s `pixelRatio` pixels of the target the pass draws, giving consistent label sizes across screens and in low-resolution targets.
- Screen positions use pixels. Multiply pointer-event offsets by the surface's `pixelRatio`. [Picker](picker.md) takes points and performs that multiplication itself.
- `facing`, `ground` and `pinned` use the set's `camera` or the camera injected by its pass description. `screen` needs no camera.

Write a custom `place` as a `'use gpu'` function taking `position`, `offset` and a `text.Placed`. The latter contains `label`, the glyph index within the label as `glyph`, its `pen` and its `em`. To size in points, read the pixel dimensions and pixel ratio from `passes.viewport.$`. Both `place` and `shade` can read `cameras.current.$`. If a callback reads another module, [fill that dependency](../concepts/connecting-modules.md), for example with `place: tgpu.fn(wobble).pipe(noise.inject())`.

To make a rising "+1" swell, read its scale from a buffer indexed by `label`. Multiply the offset to scale around the anchor:

```ts
const scales = root.createReadonly(d.arrayOf(d.f32, 16), Array(16).fill(1));

const swelling = (position: d.v3f, offset: d.v2f, glyph: text.Placed) => {
  'use gpu';
  return text.pinned(position, offset * scales.$[glyph.label]);
};

const pops = text.create(root, sans, {
  camera: view,
  capacity: 16,
  length: 4,
  place: swelling,
});
pops.write(3, '+1', { em: 14 });
scales.patch({ 3: 1.6 });
```

A kernel that moves the "+1" can write its scale too, through `scales.buffer.as('mutable')`.

## Shading

`shade(fragment)` returns premultiplied colour from a `text.Fragment`:

- `color` and `label` give the label colour and index. `glyph` is the glyph index within the label, starting at 0.
- `local` is the pixel position relative to the anchor, in em with y pointing up.
- `distance` is the distance to the sharp glyph edge in screen pixels, positive inside. `round` measures distance to a rounded edge from the atlas's fourth channel. The figure shows both.
- `reach` limits both distances to half the pixel range scaled to the drawn glyph. With the shipped bake, it is pixels per em divided by 10.

An outline two pixels wide, narrower where the atlas doesn't reach that far:

```ts
const outlined = (fragment: text.Fragment) => {
  'use gpu';
  const width = std.min(2, fragment.reach - 0.5);
  const fill = std.saturate(fragment.distance + 0.5);
  const outline = std.saturate(fragment.round + width + 0.5) * fragment.color.w;
  return d.vec4f(fragment.color.xyz * fill * outline, outline);
};

const score = text.create(root, sans, {
  capacity: 1,
  length: 12,
  place: text.screen,
  shade: outlined,
});
```

A full two-pixel outline needs 25 pixels per em with the shipped bake, or 12.5 points at 2×. Outlines or glows wider than `reach` fill the glyph quad. For example, `round + 2.5` produces boxes below 25 pixels per em. Bake with a wider `--pixel-range` for wide effects on small text.

Use `glyph` and `local` to animate labels without rewriting quads. This typewriter effect fades glyphs below a rising count and applies a vertical gradient from the baseline:

```ts
const typed = root.createUniform(d.f32);
const TOP = d.vec3f(1, 0.85, 0.4);

const typewriter = (fragment: text.Fragment) => {
  'use gpu';
  const shown = std.saturate(typed.$ - d.f32(fragment.glyph));
  const ink = std.mix(fragment.color.xyz, TOP, std.saturate(fragment.local.y / 0.7));
  const alpha = fragment.color.w * shown * std.saturate(fragment.distance + 0.5);
  return d.vec4f(ink * alpha, alpha);
};

const line = text.create(root, sans, {
  camera: view,
  capacity: 1,
  length: 48,
  place: text.screen,
  shade: typewriter,
});
line.write(0, 'The tide is coming in.', { em: 20, align: 'left', baseline: 'alphabetic' });
typed.write(seconds * 10); // ten glyphs a second
```

Text blends over the scene. Use `blend: 'add'` for glowing text, or `blend: 'translucent'` to draw into a description's [translucency targets](passes.md#translucent-drawings) in any order with other translucent drawings. It tests depth without writing it, so draw it after the scene. See [Passes](passes.md#what-each-target-gets) for target outputs.

## What it's made of

| Part          |                                                                                              |
| ------------- | -------------------------------------------------------------------------------------------- |
| `font.atlas`  | An `rgba8unorm` texture with three sharp-distance channels and one rounded-distance channel. |
| `font.glyphs` | A storage buffer of each glyph's quad in em and its cell of the atlas, 32 bytes a glyph.     |
| `quads`       | `capacity * length` entries of 16 bytes each, storing pen, glyph and em in a storage buffer. |
| `labels`      | A storage buffer of each label's `text.Label`, 32 bytes.                                     |

Multiple text sets can share a font. Destroy the sets before the font.

## Limits

- **Layout** `typeset` supports Latin text from left to right, with one glyph per character, advances and pair kerning. It does not support ligatures or contextual forms, which the bake disables. It also omits right-to-left and bidirectional text, scripts requiring shaping such as Arabic and Indic scripts, Chinese and Japanese character sets, width-based wrapping, ellipses, line spacing, hyphenation, tabs and fallback fonts.
- **On the web** For these capabilities on the web, use Glyph's layout engine alongside this package.
- **Atlas bytes** The atlas uses raw RGBA bytes. In react-native-webgpu 0.17.0, `createImageBitmap` premultiplies alpha while decoding, corrupting distances where the fourth channel is below 1.
- **Compression** Serve the atlas compressed. Inter's atlas is 617 KB, or 191 KB with gzip.
- **Label size** Label size belongs to layout; label rows have no scale field. Read a separate scale in a custom `place`, as in the swelling "+1" example.
