Font preview: the JavaScript library

Font families for web pages, canvas and other engines: Google Fonts, system fonts and your own files, with regular, bold and italic. Fonts are registered in the browser and handed to engines that need font bytes.

  • @openlibsdev/font-forge
  • v1.0.0
  • ES module
  • MIT

Installation

With a bundler or Node-style imports:

npm install @openlibsdev/font-forge
import FontForge from "@openlibsdev/font-forge";

Or straight in a page, without a build step (jsDelivr resolves the dependencies):

<script type="module">
import FontForge from "https://cdn.jsdelivr.net/npm/@openlibsdev/font-forge@1.0.0/+esm";
</script>

Usage

import FontForge from "@openlibsdev/font-forge";

FontForge.defineGoogleFamily("Roboto", [400, 700]);
const name = await FontForge.useFamily("Roboto", { bold: true });   // name to use in CSS or canvas
const ctx = document.createElement("canvas").getContext("2d");
ctx.font = `32px "${name}"`;

API

FontForge.useFamily(family, opts)

Loads a family style ({bold, italic}) and returns the font name to use.

FontForge.defineGoogleFamily(name, weights)

Defines a family from Google Fonts with the given weights.

FontForge.defineSystemFamily(name)

Defines a system font family.

FontForge.defineFamily(name, variants, opts)

Defines a family from your own files.

FontForge.listFamilies()

Lists the defined families.

FontForge.addFont(name, bytes)

Registers a font from bytes.

FontForge.addFontFile(file)

Registers a font from a file chosen by the user.

FontForge.isRegistered(name)

Whether a font is registered.

FontForge.addBackend(key, fn)

Adds an engine that receives every registered font (as image-forge does for ImageMagick).

FontForge.removeBackend(key)

Removes an engine.

FontForge.setUrls(o)

Sets the addresses of font files.

FontForge.DEFAULT_FAMILY

Family used when none is given.

FontForge.TEXT_DEFAULTS

Default values of a text block: text, family, bold, italic, underline, size, letter spacing, line height.

FontForge.listCatalogFonts()

Names of the fonts added to the catalog with registerCatalogFont (loaded only when used).

FontForge.listRegisteredFonts()

Names of the fonts already registered in the browser.

FontForge.registerCatalogFont(name, url)

Adds a font to the catalog by name and file address without loading it; useCatalogFont loads it later.

FontForge.textFontData

Data for a text and font block: the font list, loading a font style, adding a font file, the block's labels in a language and the default values.

FontForge.textFontTexts(lang)

Labels of the text and font block in the given language (English for a missing language).

FontForge.useCatalogFont(name)

Loads and registers a catalog font on first use and returns its name.

Dependencies

What the library loads, with the licenses (all are listed on the Licenses page).

NameVersionLicenseFiles
dejavu-fonts-ttf2.37.3Bitstream Vera Fonts License (free, redistributable); DejaVu changes are public domain License · Source code