Skip to content

Fonts & grid sizing

ghostty-webgpu derives one integer device-pixel cell grid from your font metrics and uses it across layout, pointer projection, selection, and every renderer. That keeps columns sharp and aligned across Retina displays and fractional zoom levels like 1.25x or 1.5x.

You will learn

  • Why loading web fonts before open() prevents a fallback-font flash

  • How size, lineHeight, and letterSpacing map to physical cell dimensions

  • How Unicode cell widths, DEC mode 2027 graphemes, GSUB ligatures, and BiDi work
fonts.ts
import { Terminal } from 'ghostty-webgpu'
export async function mountWithFont(host: HTMLElement, fontUrl: string) {
const face = new FontFace('Terminal Mono', `url(${fontUrl})`)
document.fonts.add(await face.load())
const terminal = await Terminal.create({
appearance: { font: { family: '"Terminal Mono", monospace', size: 14 } },
})
await terminal.open(host)
terminal.writeln('Font loaded before measuring the grid.')
return () => terminal.dispose()
}

Await your FontFace (or document.fonts.load('400 14px "Your Font"') for CSS @font-face rules) before calling open(). When the face is already loaded, the synchronous initial fit measures your real font on frame one. If a font finishes loading after open(), the built-in document.fonts observer automatically re-measures the grid and clears the glyph atlas.

For ghostty-webgpu/worker, supply faces through the fonts creation option instead of document.fonts (see Run in a worker).

Call terminal.setFont(partial) to update family, size, lineHeight, letterSpacing, weight, or boldWeight live.

  • size is the CSS em size in pixels. Measured cell height comes from the font’s ascent plus descent, so a 14 px font at lineHeight: 1.2 produces a taller cell than 14 px. Inspect terminal.appearance.grid.cellHeight to see the exact fitted pixel height.
  • lineHeight multiplies the natural font box (minimum 0.5). Values below 1 center glyphs inside a shorter cell and clip them to their cell span.
  • letterSpacing adjusts horizontal advance in CSS pixels; negative values work as long as the fitted device cell width stays positive.
How integer device-pixel rounding prevents subpixel seams

At fractional pixel ratios such as 1.25 or 1.5, naive CSS pixel cell widths drift by a fraction of a pixel on every column, leaving visible gaps in box-drawing characters and TUI borders.

ghostty-webgpu rounds cell width and height to whole device pixels so cssCellWidth * pixelRatio and cssCellHeight * pixelRatio are exact integers. Pointer hit-testing, DOM overlays, and GPU vertex positions all read that same canonical grid. See the

font geometry specification

for the exact equations.

  • Cell widths and graphemes: libghostty-vt owns whether a code point occupies 1 or 2 cells; browser font fallback only draws inside that span. When an application enables DEC mode 2027 (grapheme clustering), ZWJ sequences such as 👩‍💻 occupy 2 cells and stay intact even across byte-by-byte writes.
  • Programming ligatures: Off by default. Pass ligatures to Terminal.create() to read the font’s GSUB table and shape joined runs without changing underlying cell ownership.
  • Bidirectional text: Off by default. Pass bidi: true to lazy-load Unicode 13.0 bidirectional row reordering (bidi-js) for right-to-left scripts.

Recap

  • Await FontFace.load() or document.fonts.load() before open() so frame one uses your real font metrics.

  • Use terminal.setFont() for runtime size, family, or line-height updates; the grid and atlas refit automatically.

  • Enable ligatures or bidi: true in Terminal.create() when you need GSUB shaping or RTL reordering.