Web SDKv1.0.0

FontRegistry

Loads fonts outside a viewer — for a font picker of your own, or to check what a font covers. A viewer builds its own registry from fonts; one you construct is never seen by a viewer. No member needs a license feature or a document.

The catalog is BUILTIN_FONTS plus what you registered, as FontDefinitions: { id, displayName, category, source, cssFallback, ttfPath?, ttfUrl?, ttfBytes? }, where category is 'sans-serif' | 'serif' | 'monospace' | 'display', source is 'standard' | 'bundled' | 'custom', cssFallback the CSS font-family stack for previews, ttfPath the file name under fonts.fontsBaseUrl (self-hosting), and ttfUrl / ttfBytes set for custom fonts.

classnew FontRegistry(fontsConfig?)

Usage

TypeScript
import { FontRegistry } from '@kernstra/pdf-web-sdk';

const fonts = new FontRegistry(fontsConfig); // optional: the same FontsConfig a viewer takes
fonts.registerCustomFontUrl({ id: 'brand', displayName: 'Brand Sans', ttfUrl: '/fonts/brand.ttf' });

for (const font of fonts.getAll()) {
  picker.add(new Option(font.displayName, font.id));
}

fonts.destroy();

Constructor

new FontRegistry(config?: FontsConfig)

Constructing one with fontsBaseUrl under a license without self-hosting throws FEATURE_NOT_LICENSED.

ParameterTypeDescription
config?FontsConfigThe same FontsConfig a viewer takes.

Registering

registerCustomFontmethodsince 1.0.0
registerCustomFont(font: CustomFont & { ttfBytes: Uint8Array }): void
Adds a font from TTF/OTF bytes.
ParameterTypeDescription
fontCustomFont & { ttfBytes: Uint8Array }{ id, displayName, ttfBytes, category?, cssFallback? } — the same bag as a fonts.customFonts entry. category defaults to 'sans-serif'; cssFallback to the display name, then sans-serif.
Returns void
TypeScript
const ttfBytes = new Uint8Array(await (await fetch('/fonts/brand.ttf')).arrayBuffer());
fonts.registerCustomFont({ id: 'brand', displayName: 'Brand Sans', ttfBytes, category: 'sans-serif' });
registerCustomFontUrlmethodsince 1.0.0
registerCustomFontUrl(font: CustomFont & { ttfUrl: string }): void
Adds a font fetched from ttfUrl on first use.
ParameterTypeDescription
fontCustomFont & { ttfUrl: string }{ id, displayName, ttfUrl, category?, cssFallback? } — the same bag as a fonts.customFonts entry. category defaults to 'sans-serif'.
Returns void
TypeScript
fonts.registerCustomFontUrl({ id: 'brand', displayName: 'Brand Sans', ttfUrl: '/fonts/brand.ttf' });

Catalog

getAllmethodsince 1.0.0
getAll(): FontDefinition[]
The catalog: BUILTIN_FONTS plus what you registered.
Returns FontDefinition[]
TypeScript
const serifs = fonts.getAll().filter((f) => f.category === 'serif');
getDefinitionmethodsince 1.0.0
getDefinition(id: string): FontDefinition | undefined
One entry of the catalog, by id.
ParameterTypeDescription
idstringThe font's id.
Returns FontDefinition | undefined undefined when no font has that id.
TypeScript
const brand = fonts.getDefinition('brand');

Lifecycle

destroymethodsince 1.0.0
destroy(): void
Drops the loaded bytes and the preview faces it added to document.fonts.
Returns void
TypeScript
fonts.destroy();