# resource/fontasset (vanilla.js 0.4.0)

> https://vanillapunch.com/docs/vanilla.js/0.4.0/api/resource/fontasset/

Font asset.

## FontAsset extends Asset

Font asset.

Example: Load a font file and add it to the font set

```js
import { FontAsset } from "./build/vanilla.js";

const fontAsset = new FontAsset();
await fontAsset.loadFont("DOSGothic", "examples/assets/DOSGothic.woff", { weight: "normal", style: "normal" });
console.log(fontAsset.family, fontAsset.isLoaded(), document.fonts.check("16px DOSGothic"));
```

- `family = ""` (field, since 0.0.8): Registered font family name. (empty string before loading)
  - returns string
- `fontFace = null` (field, since 0.0.8): Registered FontFace. (null before loading or after unloading)
  - returns FontFace
- `constructor()` (constructor, since 0.0.8): Constructor.
- `async load(assetPath)` (method, since 0.0.8): Loads the asset asynchronously. (calls loadFont with the file name as the font family name)
  - `assetPath` string: Font file path. (the file name without its extension becomes the family name, "DOSGothic" for "assets/DOSGothic.woff")
  - returns Promise<void>: A promise resolved once the font is loaded and added to document.fonts. (resolves at once if already loaded, rejects if it cannot be loaded)

  Example: Registering a font with its file name as the family name

  ```js
  import { FontAsset } from "./build/vanilla.js";
  
  const fontAsset = new FontAsset();
  await fontAsset.load("examples/assets/DOSGothic.woff");
  console.log(fontAsset.family, fontAsset.isLoaded(), document.fonts.check("16px DOSGothic"));
  ```

- `async loadFont(family, assetPath, descriptors)` (method, since 0.0.8): Loads the asset asynchronously.
  - `family` string: Font family name to register. (used as the CSS font-family)
  - `assetPath` string: Font file path.
  - `descriptors` object: FontFace descriptors. (weight, style, stretch, unicodeRange, display, etc)
  - returns Promise<void>: A promise resolved once the font is loaded and added to document.fonts. (resolves at once if already loaded, rejects if it cannot be loaded)
- `async loadFontFromBytes(family, bytes, descriptors)` (method, since 0.4.0): Loads from font bytes already held. (ttf, otf, woff, woff2, once loaded it is added to document.fonts under the family name)
  - `family` string: Font family name to register. (used as the CSS font-family)
  - `bytes` ArrayBuffer | ArrayBufferView: The bytes of the font file. (What is passed is not changed)
  - `descriptors` object: FontFace descriptors. (weight, style, etc, omitted if none)

  Example: Add a font from bytes already fetched

  ```js
  import { FontAsset } from "./build/vanilla.js";
  
  const response = await fetch("examples/assets/DOSGothic.woff");
  const bytes = await response.arrayBuffer();
  const fontAsset = new FontAsset();
  await fontAsset.loadFontFromBytes("DOSGothicBytes", bytes);
  console.log(fontAsset.family, fontAsset.isLoaded());
  ```

- `unload()` (method, since 0.0.8): Unloads the asset.
