# misc/dynamicfont (vanilla.js 0.4.0)

> https://vanillapunch.com/docs/vanilla.js/0.4.0/api/misc/dynamicfont/

다이나믹 폰트.

## GlyphInfo extends Object

글리프 정보.

예제: 구운 글자의 글리프 정보 보기

```js
import { DynamicFont, GlyphInfo } from "./build/vanilla.js";

const dynamicFont = new DynamicFont();
dynamicFont.setFontSize(32);
dynamicFont.buildAtlas("AB");
const glyphInfo = dynamicFont.getGlyph("A");
console.log(glyphInfo instanceof GlyphInfo, glyphInfo.char, glyphInfo.advance, glyphInfo.uvRect.size.x, glyphInfo.bearingY);
```

- `char` (field, since 0.0.10): 문자.
  - returns string
- `uvRect` (field, since 0.0.10): 아틀라스 내 UV 좌표 및 크기.
  - returns Rect
- `advance` (field, since 0.0.10): 수평 이동량 (advance width + 자간, px).
  - returns number
- `bearingX` (field, since 0.0.10): 커서에서 글리프 좌측 픽셀까지 거리.
  - returns number
- `bearingY` (field, since 0.0.10): 베이스라인에서 글리프 상단 픽셀까지 거리.
  - returns number
- `constructor()` (constructor, since 0.0.10): 생성.

## DynamicFont extends Object

다이나믹 폰트.
- 정한 글자들을 한 글꼴, 한 크기로 글자 아틀라스(캔버스 한 장)에 모아 굽고, 글을 그릴 때 아틀라스에서 글리프를 잘라 그린다.
  글자마다 같은 그림을 쓰므로 묶어 그리기가 끊기지 않는다. (문자열마다 텍스처를 굽는 Text 와 다르다)
  점수, 피해 숫자, 시계처럼 글자가 자주 바뀌고 같은 글자를 많이 그리는 곳에 쓴다.
- 글자는 글자 묶음(받침, 결합 부호, 이모지 묶음) 단위로 센다. 아틀라스에 없는 글자는 글자 크기의 반만큼 비우고 넘어간다.
- buildAtlas 로 글자를 더하면 든 글자를 모두 새 캔버스에 다시 놓는다. (앞서 구운 글자의 자리가 새 캔버스와 어긋나지 않게)

예제: 숫자 아틀라스를 굽고 시계 그리기

```js
import { Engine, EngineConfiguration, Scene, DynamicFont, FontAsset, Vector2 } from "./build/vanilla.js";

const fontAsset = new FontAsset();
await fontAsset.loadFont("DOSGothic", "examples/assets/DOSGothic.woff");
const dynamicFont = new DynamicFont();
dynamicFont.setFontAsset(fontAsset);
dynamicFont.setFontSize(48);
dynamicFont.setLetterSpacing(2);
dynamicFont.buildAtlas("0123456789:");
console.log(dynamicFont.measureText("12:34"));

class ClockScene extends Scene {
	draw(graphic) {
		super.draw(graphic);
		const position = Vector2.create(100, 200);
		dynamicFont.drawText("12:34", position, graphic);
	}
}

const engineConfiguration = new EngineConfiguration();
engineConfiguration.referenceResolutionSize = Vector2.create(1280, 720);
const engine = new Engine(engineConfiguration);
engine.run(new ClockScene());
```

- `constructor()` (constructor, since 0.0.10): 생성.
- `setFontAsset(fontAsset)` (method, since 0.0.10): 폰트 애셋 설정.
  - `fontAsset` FontAsset: 구울 때 쓸 폰트 애셋. (null 이면 시스템 글꼴, 바꾼 뒤에는 rebuildAtlas 로 다시 굽는다)
- `setFontSize(fontSize)` (method, since 0.0.10): 폰트 크기 설정.
  - `fontSize` number: 글자 크기. (픽셀, 처음은 32)
- `getFontSize()` (method, since 0.0.10): 폰트 크기 반환.
  - returns number: 글자 크기. (픽셀)
- `setLetterSpacing(letterSpacing)` (method, since 0.0.10): 자간 설정.
  - `letterSpacing` number: 글자마다 더할 자간. (픽셀, 처음은 0)
- `getLetterSpacing()` (method, since 0.0.10): 자간 반환.
  - returns number: 더하는 자간. (픽셀)
- `getAtlasCanvas()` (method, since 0.0.10): 아틀라스 캔버스 반환.
  - returns HTMLCanvasElement: 글자 아틀라스 캔버스. 아직 굽지 않았으면 null.
- `getGlyph(char)` (method, since 0.0.10): 글리프 정보 반환.
  - `char` string: 글자 하나. (글자 묶음 단위)
  - returns GlyphInfo | null: 그 글자의 글리프 정보. 아틀라스에 없으면 null.
- `buildAtlas(charset)` (method, since 0.0.10): 아틀라스 빌드. (charset 의 글자 가운데 아직 없는 글자를 더하고, 아틀라스를 든 글자 모두로 다시 굽는다)
  - `charset` string: 아틀라스에 넣을 글자들. (이미 든 글자는 건너뛴다)
- `rebuildAtlas()` (method, since 0.4.0): 든 글자를 모두 다시 굽기. (글꼴, 크기, 자간을 바꾼 뒤 부른다)
- `drawText(text, position, graphic)` (method, since 0.0.10): 텍스트 렌더링 (아틀라스에서 글리프 샘플링).
  - `text` string: 그릴 글.
  - `position` Vector2: 베이스라인 기준 좌상단 위치.
  - `graphic` Graphic: 그릴 그래픽.
- `measureText(text)` (method, since 0.4.0): 글 너비 재기. (아틀라스의 전진폭 합, 없는 글자는 글자 크기의 반)
  - `text` string: 잴 글.
  - returns number: 글 너비. (픽셀)
- `clearAtlas()` (method, since 0.0.10): 글리프 맵 초기화.
