# misc/uifont (vanilla.js 0.4.0)

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

UI font. (all on-screen text, groups font subsets for many scripts under one name, imported as UIFont)
- Font name, list file and folder, last group, weights and wait time are set with configureUiFont. (initially GameUI, ./assets/fonts/fonts.json, latin, 500 and 700)
- The lists are fonts.json (index { fonts: [{ group, manifest }] }) and fonts-<group>.json (subsets { faces: [{ file, weight, style, unicodeRange }] }).

Draws all on-screen languages under one font name (the family of configureUiFont).
Latin and Cyrillic use the base font (Exo 2), and other scripts use fonts with a similar feel.
Font files are not merged. A FontFace is created for each subset split by Google Fonts and registered with that subset's character range (unicode-range).
So the browser downloads only the subsets containing characters used on screen. (Korean, Chinese and Japanese fonts have hundreds of subsets)
Characters shared by several fonts, such as Han characters, are drawn by the font registered later. The selected language's font group is registered right before Latin (second from the end)
so that Japanese gets Japanese-font Han characters and Traditional Chinese gets Traditional-font ones. Latin is always last, so digits and Latin letters use the Latin font in every language.
Subsets under one font name must all declare the same weight to be selected together by character range. (because the weight is chosen first and only subsets of that weight are considered)
So every subset is registered with only the two weights in configureUiFont's weights (initially 500, 700). Variable-weight subsets are registered twice (500 and 700), and 400-only subsets as 500.
When the screen requests 400, the 500 subset draws, and for 600 the 700 subset draws.
The canvas does not wait for subsets not yet downloaded and draws with another font, so subsets for the characters to be drawn are fetched in advance. (loadUiFontText)
When a subset is downloaded, the engine (EngineConfiguration.rebakeTextOnFontLoad) discards and rebakes only the text images baked with that subset's characters.
The font lists are split per group. (fonts.json is the index, fonts-<group>.json holds that group's subsets)
At first only the lists for the selected language and Latin are fetched, and the rest (language names in the language window, other scripts such as person names) are fetched a little after play starts.
Initial subsets are waited for only up to configureUiFont's waitMilliseconds, after which play starts first and text is rebaked as subsets arrive. (shorter first load)
Paths are written as literal strings so the build's cache busting can add fingerprints. (subset fingerprints are added to the lists by the build)

Example: Configure the UI font and build a font string

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

UIFont.configureUiFont({ family: "ExampleUI", manifestPath: "./assets/fonts/fonts.json", directoryPath: "./assets/fonts/", weights: [500, 700], waitMilliseconds: 1000 });
const fontString = UIFont.buildUiFontString(700, 24);
console.log(UIFont.readUiFontFamily(), fontString, UIFont.readUiFontFace());
```

## Functions and constants

- `async loadUiFont(fontGroup, text)` (method, since 0.4.0): Loads the UI font. (registers and prioritizes the groups of the selected language and Latin, and prefetches that language's text)
  
  Subsets for the text in use are waited for only up to uiFontWaitMilliseconds, and the rest are fetched in the background.
  The remaining groups start downloading remainingGroupsDelayMilliseconds after the first call, once only.
  If loading fails, the game continues with the system font.
  - `fontGroup` string: Font group of the selected language. (fontGroup in languages.json)
  - `text` string: Text to prefetch. (all text of that language)
- `applyUiFontGroup(fontGroup)` (method, since 0.4.0): Prioritizes the selected language's font group. (places that group right before Latin and re-registers all subsets)
  - `fontGroup` string: Font group to put first. (fontGroup in languages.json, does nothing if no group has been loaded)
- `async loadUiFontText(text)` (method, since 0.4.0): Fetches the subsets for text. (only subsets containing that text, waits until all are downloaded)
  - `text` string: Characters to draw. (does nothing if empty or no group has been loaded)
- `requestUiFontText(text)` (method, since 0.4.0): Requests fetching the subsets for text. (text that appears on the fly, such as person names, does not wait, only characters not seen before)
  - `text` string: Characters to draw. (only fetches characters not requested before)
- `readUiFontFace()` (method, since 0.4.0): Returns the representative FontFace of the loaded UI font. (given to the engine's UILabel and UIInputField, only the font name is used, null if not loaded)
  - returns FontFace | null: The first face of the last group. null if not loaded.
- `buildUiFontString(fontWeight, fontSize)` (method, since 0.4.0): UI font string. (weight, size)
  - `fontWeight` number: Weight. (a number like 500 or 700)
  - `fontSize` number: Size. (pixels)
  - returns string: Text in canvas font form. (e.g. 700 24px "GameUI", sans-serif)
- `configureUiFont(options)` (method, since 0.4.0): Sets the UI font. (call before loading, fields not given stay unchanged)
  - `options` object: { family, manifestPath, directoryPath, primaryGroup, weights, waitMilliseconds, remainingDelayMilliseconds }
- `readUiFontFamily()` (method, since 0.4.0): Returns the UI font name.
  - returns string: UI font family name. ("GameUI" at first)
