# resource/fontasset (vanilla.js 0.4.0)

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

폰트 애셋.

## FontAsset extends Asset

폰트 애셋.

예제: 글꼴 파일을 읽어 글꼴 목록에 올리기

```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): 등록한 글꼴 패밀리 이름. (읽기 전에는 빈 글)
  - returns string
- `fontFace = null` (field, since 0.0.8): 등록한 FontFace. (읽기 전이나 언로드 뒤에는 null)
  - returns FontFace
- `constructor()` (constructor, since 0.0.8): 생성.
- `async load(assetPath)` (method, since 0.0.8): 비동기 애셋 로드. (파일 이름을 글꼴 패밀리 이름으로 loadFont 한다)
  - `assetPath` string: 글꼴 파일 경로. (파일 이름에서 확장자를 뗀 것이 패밀리 이름이 된다, "assets/DOSGothic.woff" 면 "DOSGothic")
  - returns Promise<void>: 글꼴을 읽어 document.fonts 에 올리면 풀리는 프로미스. (이미 읽었으면 바로 풀린다, 읽지 못하면 거부된다)

  예제: 파일 이름을 패밀리 이름으로 글꼴 올리기

  ```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): 비동기 애셋 로드.
  - `family` string: 등록할 글꼴 패밀리 이름. (CSS font-family 로 쓴다)
  - `assetPath` string: 글꼴 파일 경로.
  - `descriptors` object: FontFace 서술자. (weight, style, stretch, unicodeRange, display 등)
  - returns Promise<void>: 글꼴을 읽어 document.fonts 에 올리면 풀리는 프로미스. (이미 읽었으면 바로 풀린다, 읽지 못하면 거부된다)
- `async loadFontFromBytes(family, bytes, descriptors)` (method, since 0.4.0): 이미 가진 글꼴 바이트로 읽기. (ttf, otf, woff, woff2, 다 읽히면 document.fonts 에 family 이름으로 오른다)
  - `family` string: 등록할 글꼴 패밀리 이름. (CSS font-family 로 쓴다)
  - `bytes` ArrayBuffer | ArrayBufferView: 글꼴 파일의 바이트. (넘긴 것은 바뀌지 않는다)
  - `descriptors` object: FontFace 서술자. (weight, style 등, 없으면 생략)

  예제: 받아 둔 바이트로 글꼴 올리기

  ```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): 애셋 언로드.
