Skip to content

Host-provided Fonts

Instead of packing a font into every template that needs it, your application can register the font once. Every template it loads afterwards can use it. The fonts page compares this with packed fonts and explains how a template declares the families it expects. This guide is the host side.

Nothing deduplicates fonts. Registering the same file twice adds its faces twice, so register each font once per process.

All integrations can register fonts from raw bytes. Everywhere except the browser, fonts can also be registered by path: either a font file, or a directory that adds every .ttf, .ttc, .otf, and .otc file in its tree. Both return the number of font faces that were added; data that holds no font Typst can read is ignored and counts as zero, and so does a path that does not exist.

import { registerFonts, Template } from '@oicana/browser';
const response = await fetch('/fonts/corporate.ttf');
const font = new Uint8Array(await response.arrayBuffer());
registerFonts([font]);
const templateResponse = await fetch('/template.zip');
const template = new Template(
new Uint8Array(await templateResponse.arrayBuffer()),
);

There is no path-based variant in the browser, since there is no filesystem to read fonts from.

Fonts are registered per process and shared by every template, so a large font costs memory once instead of once per template.

Every integration except Rust can list what is currently registered and drop it again. Each entry has a family, and a path for fonts registered by path.

Integration List registered fonts Clear the registry
Browser / Node.js registeredFonts() clearFonts()
C# Configuration.RegisteredFonts() Configuration.ClearFonts()
Java Configuration.registeredFonts() Configuration.clearFonts()
PHP Configuration::registeredFonts() Configuration::clearFonts()
Python oicana.registered_fonts() oicana.clear_fonts()

Clearing only affects templates registered afterwards. Templates that already exist keep the fonts they were created with.

The family values are the names to use in Typst’s text(font: ...). Listing them after startup is a quick way to check that a font file provides the family you expected.