# Font preview — library for developers

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.

## Installation

```sh
npm install @openlibsdev/font-forge
```

```js
import FontForge from "https://cdn.jsdelivr.net/npm/@openlibsdev/font-forge@1.0.0/+esm";
```

## Usage

```js
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

- dejavu-fonts-ttf 2.37.3 (Bitstream Vera Fonts License (free, redistributable); DejaVu changes are public domain)

- GitHub: https://github.com/OpenLibsDev/font-forge
- npm: https://www.npmjs.com/package/@openlibsdev/font-forge
- Open the app: https://openlibs.dev/en/app/font-forge/
