# misc/dynamicfont (vanilla.js 0.4.0)

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

Dynamic font.

## GlyphInfo extends Object

Glyph info.

Example: Inspect the glyph info of a baked character

```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): Character.
  - returns string
- `uvRect` (field, since 0.0.10): UV coordinates and size within the atlas.
  - returns Rect
- `advance` (field, since 0.0.10): Horizontal advance (advance width + letter spacing, px).
  - returns number
- `bearingX` (field, since 0.0.10): Distance from the cursor to the glyph's left pixel.
  - returns number
- `bearingY` (field, since 0.0.10): Distance from the baseline to the glyph's top pixel.
  - returns number
- `constructor()` (constructor, since 0.0.10): Constructor.

## DynamicFont extends Object

Dynamic font.
- Bakes the chosen characters in one font and one size into a glyph atlas (a single canvas), and when drawing text it cuts the glyphs out of the atlas.
  Every character uses the same image, so sprite batching is not broken. (unlike Text, which bakes a texture per string)
  Used where characters change often and the same characters are drawn many times, such as scores, damage numbers and clocks.
- Characters are counted by grapheme cluster (final consonants, combining marks, emoji sequences). Characters missing from the atlas are skipped, leaving a gap of half the font size.
- Adding characters with buildAtlas places all contained characters again on a new canvas. (so the positions of previously baked characters do not mismatch the new canvas)

Example: Bake a digit atlas and draw a clock

```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): Constructor.
- `setFontAsset(fontAsset)` (method, since 0.0.10): Sets the font asset.
  - `fontAsset` FontAsset: Font asset used for baking. (system font if null, call rebuildAtlas after changing it)
- `setFontSize(fontSize)` (method, since 0.0.10): Sets the font size.
  - `fontSize` number: Font size. (pixels, 32 at first)
- `getFontSize()` (method, since 0.0.10): Returns the font size.
  - returns number: Font size. (pixels)
- `setLetterSpacing(letterSpacing)` (method, since 0.0.10): Sets the letter spacing.
  - `letterSpacing` number: Letter spacing added to each character. (pixels, 0 at first)
- `getLetterSpacing()` (method, since 0.0.10): Returns the letter spacing.
  - returns number: Added letter spacing. (pixels)
- `getAtlasCanvas()` (method, since 0.0.10): Returns the atlas canvas.
  - returns HTMLCanvasElement: The glyph atlas canvas. null if not baked yet.
- `getGlyph(char)` (method, since 0.0.10): Returns the glyph info.
  - `char` string: One character. (a grapheme cluster)
  - returns GlyphInfo | null: Glyph info for that character. null if not in the atlas.
- `buildAtlas(charset)` (method, since 0.0.10): Builds the atlas. (adds the characters in charset that are not yet present, and rebakes the atlas with all contained characters)
  - `charset` string: Characters to put in the atlas. (characters already in it are skipped)
- `rebuildAtlas()` (method, since 0.4.0): Rebakes all contained characters. (call after changing the font, size or letter spacing)
- `drawText(text, position, graphic)` (method, since 0.0.10): Text rendering (samples glyphs from the atlas).
  - `text` string: Text to draw.
  - `position` Vector2: Top-left position relative to the baseline.
  - `graphic` Graphic: Graphic to draw with.
- `measureText(text)` (method, since 0.4.0): Measures the text width. (sum of the advance widths in the atlas, half the font size for missing characters)
  - `text` string: Text to measure.
  - returns number: Text width. (pixels)
- `clearAtlas()` (method, since 0.0.10): Initializes the glyph map.
