# misc/uifont (vanilla-engine 0.4.0)

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

UI 글꼴. (원본 misc/uifont.js, 화면 위의 모든 글자, 여러 문자의 글꼴 조각을 한 이름 아래 묶음)
- 글꼴 이름, 목록 파일과 폴더, 맨 뒤 묶음, 굵기, 기다리는 시간은 configure_ui_font 로 정한다. (처음은 GameUI, ./assets/fonts/fonts.json, latin, 500 과 700)
- 목록은 fonts.json(색인 { fonts: [{ group, manifest }] })과 fonts-<묶음>.json(조각 { faces: [{ file, weight, style, unicodeRange }] })이다.
- 화면의 모든 언어를 한 글꼴 이름 아래에 모아 그린다. 글꼴 파일을 합치지 않고 조각마다 글꼴 얼굴을 만들어 그 조각의 글자 범위로 올린다.
  그래서 화면에 쓰는 글자가 든 조각만 받는다. (한, 중, 일 글꼴은 조각이 수백 개이다)
  한자처럼 여러 글꼴이 함께 가진 글자는 나중에 올린 글꼴이 그린다. 고른 언어의 묶음을 라틴 바로 앞(뒤에서 둘째)에 두어
  일본어면 일본 글꼴, 중국어 번체면 번체 글꼴의 한자가 나오게 한다. 라틴은 늘 맨 뒤라 숫자와 영문은 어느 언어에서나 라틴 글꼴이다.
- 모든 조각을 configure_ui_font 의 weights(처음은 500, 700) 두 굵기로만 올린다. 가변 굵기 조각은 두 번(500 과 700), 400 뿐인 조각은 500 으로 올린다.
- 쓸 글자의 조각은 미리 받는다. (load_ui_font_text) 조각을 받으면 엔진(EngineConfiguration.rebake_text_on_font_load)이 그 조각의 글자로 구운 글자만 버려 다시 굽는다.
  미리 받지 않은 글자도 그 글꼴로 재거나 그리면 update 가 그 글자의 조각을 받는다. (원본 브라우저는 올려 둔 FontFace 의 범위에 든 글자를 그릴 때 그 얼굴을 받는다)
  받기 전에는 원본 캔버스처럼 다른 글꼴로 그리고, 받으면 (rebake_text_on_font_load 를 켰을 때) 다시 굽는다.
- 처음에는 고른 언어와 라틴의 목록만 받고, 나머지 묶음은 놀이를 시작하고 조금 뒤(remaining_delay_milliseconds)에 받는다.
  처음의 조각은 wait_milliseconds 까지만 기다린다. (is_ui_font_ready 가 그 시간이 지나거나 다 받으면 참)
- 원본과 다른 것:
  원본은 FontFace 를 만들어 document.fonts 에 넣고 async 로 기다린다. 러스트 판은 데스크탑과 웹 모두 조각 파일의 바이트를 받아
  엔진의 글자 캐시에 같은 이름의 얼굴로 올린다. (Engine.register_font_face_when_loaded, 차례는 Engine.set_font_face_order)
  그래서 웹에서도 브라우저 글꼴이 아니라 엔진의 모양 짓기로 그린다. 받는 일은 요청만 하고 update(engine) 를 프레임마다 불러 이어 간다.
  load_ui_font 는 원본처럼 await 할 수 있는 UiFontLoadHandle 을 돌려준다. (기다리는 동안 update 를 불러 주고, 처음 글자를 다 올렸거나 wait_milliseconds 가 지나면 끝난다)
  load_ui_font_text 는 그 글자의 얼굴들을 글꼴 목록에 올리면 끝나는 UiFontTextLoadHandle 을 돌려준다. (원본 await loadUiFontText(글자))
  원본 readUiFontFace(FontFace) 는 read_ui_font_face(올린 이름, 못 불러왔으면 None) 다. options 객체는 UiFontOptions 구조체다.

## UiFontOptions (struct) (Clone, Debug, Default, PartialEq)

UI 글꼴 설정. (원본 configureUiFont 의 options, None 인 칸은 그대로)
- 기본값은 모든 칸이 None 이다. 바꿀 칸만 Some 으로 채우고 ..UiFontOptions::default() 로 나머지를 비운다.

예제: 글꼴 이름과 목록 경로 바꾸기

```rust
use vanilla::{uifont, UiFontOptions};

uifont::configure_ui_font(UiFontOptions {
	family: Some(String::from("MenuFont")),
	manifest_path: Some(String::from("./assets/menufonts/fonts.json")),
	directory_path: Some(String::from("./assets/menufonts/")),
	..UiFontOptions::default()
});
assert_eq!(uifont::read_ui_font_family(), "MenuFont");
assert_eq!(uifont::build_ui_font_string(700, 24.0), "700 24px \"MenuFont\", sans-serif");
```

- `pub family: Option<String>` (field, since 0.4.0): 올릴 글꼴 이름. (처음은 "GameUI", 글자 컴포넌트의 set_font_family 에 줄 이름)
  - returns Option<String>
- `pub manifest_path: Option<String>` (field, since 0.4.0): 글꼴 색인 파일 경로. (애셋 경로, 처음은 "./assets/fonts/fonts.json")
  - returns Option<String>
- `pub directory_path: Option<String>` (field, since 0.4.0): 묶음 목록과 조각 파일이 있는 폴더. (뒤에 파일 이름을 그대로 붙이므로 / 로 끝낸다, 처음은 "./assets/fonts/")
  - returns Option<String>
- `pub primary_group: Option<String>` (field, since 0.4.0): 맨 뒤에 둘 묶음. (처음은 "latin", 숫자와 영문을 늘 이 묶음으로 그린다)
  - returns Option<String>
- `pub weights: Option<Vec<u16>>` (field, since 0.4.0): 조각을 올릴 굵기 목록. (처음은 500 과 700)
  - returns Option<Vec<u16>>
- `pub wait_milliseconds: Option<f64>` (field, since 0.4.0): 처음 글자의 조각을 기다리는 시간. (밀리초, 처음은 1500)
  - returns Option<f64>
- `pub remaining_delay_milliseconds: Option<f64>` (field, since 0.4.0): 처음 불러오기 뒤 나머지 묶음을 받기까지 쉬는 시간. (밀리초, 처음은 3000)
  - returns Option<f64>

## UiFontLoadHandle (struct) (Clone, Copy, Debug, Default, IntoFuture)

UI 글꼴 불러오기 손잡이. (load_ui_font 가 돌려준다. 원본 loadUiFont 의 Promise 자리)
- await 하면 처음 글자를 다 올렸거나 wait_milliseconds 가 지날 때까지 기다린다.

예제: 글꼴 목록이 없으면 바로 끝나고 시스템 글꼴로 이어 간다

```rust
use std::cell::Cell;
use std::rc::Rc;
use vanilla::{uifont, Engine, EngineConfiguration, UiFontOptions};

uifont::configure_ui_font(UiFontOptions {
	manifest_path: Some(String::from("missing-fonts/fonts.json")),
	..UiFontOptions::default()
});
let engine = Engine::new(EngineConfiguration::new());
let is_ready = Rc::new(Cell::new(false));
let is_ready_for_task = is_ready.clone();
engine.spawn_task(move |mut engine_context| async move {
	engine_context.with(|engine| uifont::load_ui_font(engine, "korean", "Start")).await;
	is_ready_for_task.set(true);
});
assert!(is_ready.get());
assert!(uifont::is_ui_font_ready());
assert_eq!(uifont::read_ui_font_face(), None);
```

## UiFontLoadFuture (struct) (Clone, Copy, Debug, Default, Future)

UI 글꼴 처음 불러오기를 기다리는 퓨처.
- UiFontLoadHandle 을 await 하거나 wait_for_load 가 돌려준다. 폴링할 때마다 update 를 부른다.

예제: 불러오기를 시작하지 않았으면 바로 끝난다

```rust
use std::cell::Cell;
use std::rc::Rc;
use vanilla::{uifont, Engine, EngineConfiguration};

let engine = Engine::new(EngineConfiguration::new());
let is_finished = Rc::new(Cell::new(false));
let is_finished_for_task = is_finished.clone();
engine.spawn_task(move |_engine_context| async move {
	uifont::wait_for_load().await;
	is_finished_for_task.set(true);
});
assert!(is_finished.get());
```

## UiFontTextLoadHandle (struct) (Clone, Debug, Default, IntoFuture)

글자 조각 받기 손잡이. (load_ui_font_text 가 돌려준다. 원본 loadUiFontText 의 Promise 자리, 청한 얼굴을 모두 올리면 끝난다)

예제: 끝났는지 묻거나 그대로 await 하기

```rust
use vanilla::{uifont, Engine, EngineConfiguration};

let engine = Engine::new(EngineConfiguration::new());
let text_load_handle = uifont::load_ui_font_text(engine, "Ready");
if !text_load_handle.is_finished() {
	engine.spawn_task(move |_engine_context| async move {
		text_load_handle.await;
	});
}
```

- `pub fn get_font_load_handles(&self) -> &[FontLoadHandle]` (method, since 0.4.0): 청한 얼굴의 글꼴 받기 손잡이 목록. (씬에 맡길 때는 저마다의 get_load_handle)
  - returns &[FontLoadHandle]: 청한 얼굴마다 하나인 글꼴 받기 손잡이 목록. 청한 것이 없으면 빈 목록.
- `pub fn is_finished(&self) -> bool` (method, since 0.4.0): 다 끝났는지. (올렸거나 실패했거나, 청한 것이 없으면 참)
  - returns bool: 모든 손잡이가 끝났으면 true.

## UiFontTextLoadFuture (struct) (Clone, Debug, Future)

글자 조각 받기를 기다리는 퓨처. (원본처럼 받지 못한 조각이 있어도 실패로 끝내지 않는다)
- UiFontTextLoadHandle 을 await 하면 이것이 된다.

예제: 청한 것이 없으면 첫 폴링에 끝난다

```rust
use std::cell::Cell;
use std::future::IntoFuture;
use std::rc::Rc;
use vanilla::{uifont, Engine, EngineConfiguration, UiFontTextLoadFuture};

let engine = Engine::new(EngineConfiguration::new());
let text_load_future: UiFontTextLoadFuture = uifont::load_ui_font_text(engine, "Ready").into_future();
let is_finished = Rc::new(Cell::new(false));
let is_finished_for_task = is_finished.clone();
engine.spawn_task(move |_engine_context| async move {
	text_load_future.await;
	is_finished_for_task.set(true);
});
assert!(is_finished.get());
```

## Functions and constants

- `pub fn read_target_weights(weight_descriptor: &str, weights: &[u16]) -> Vec<u16>` (method, since 0.4.0): 조각의 굵기 → 올릴 굵기. ("400 700" → [500, 700], "400" → `[500]`, "700" → `[700]`, 원본 readTargetWeights)
  - 범위면 그 안의 굵기 모두, 한 값이면 가장 가까운 하나이다.
  - 숫자가 없으면 400 으로 본다. 가까운 굵기가 둘이면 weights 의 앞 것이다.
  - `weight_descriptor` &str: 조각 목록의 weight 글. ("400" 같은 한 값, "100 900" 같은 범위)
  - `weights` &[u16]: 올릴 굵기 목록.
  - returns Vec<u16>: 올릴 굵기 목록. 범위면 범위 안(양 끝 포함)의 굵기 모두, 한 값이면 가장 가까운 하나. weights 가 비면 빈 목록.

  예제: 가변 굵기 조각과 한 굵기 조각

  ```rust
  use vanilla::uifont;
  
  let weights = [500, 700];
  assert_eq!(uifont::read_target_weights("400 700", &weights), vec![500, 700]);
  assert_eq!(uifont::read_target_weights("400", &weights), vec![500]);
  assert_eq!(uifont::read_target_weights("700", &weights), vec![700]);
  assert!(uifont::read_target_weights("100 450", &weights).is_empty());
  ```

- `pub fn load_ui_font(engine: &Engine, font_group: &str, text: &str) -> UiFontLoadHandle` (method, since 0.4.0): UI 글꼴 불러오기. (고른 언어와 라틴의 묶음을 올려 앞세우고, 그 언어의 글자를 미리 받는다. 원본 loadUiFont)
  - 받는 동안은 update 를 프레임마다 부른다. 처음 글자가 준비됐는지는 is_ui_font_ready 로 본다. (돌려준 손잡이를 await 하면 준비될 때까지 기다린다)
  - `engine` &Engine: 애셋을 받고 글꼴 얼굴을 올릴 엔진.
  - `font_group` &str: 고른 언어의 글꼴 묶음 이름. (색인 fonts.json 의 group)
  - `text` &str: 미리 받을 글자. (그 언어의 화면 글자 모두 같은 것)
  - returns UiFontLoadHandle: await 할 수 있는 불러오기 손잡이.
- `pub fn update(engine: &Engine)` (method, since 0.4.0): 이어 가기. (받은 색인과 묶음 목록을 들고, 처음 불러오기를 마저 하고, 때가 되면 나머지 묶음을 받는다. 프레임마다 부른다)
  - 이 글꼴로 재거나 그렸는데 받은 조각에 없던 글자의 조각도 여기서 받는다.
  - `engine` &Engine: 애셋을 받고 글꼴 얼굴을 올릴 엔진.
- `pub fn apply_ui_font_group(engine: &Engine, font_group: &str)` (method, since 0.4.0): 고른 언어의 글꼴 묶음 앞세우기. (그 묶음을 라틴 바로 앞에 두고 모든 얼굴의 차례를 다시 정한다, 원본 applyUiFontGroup)
  - 받은 묶음이 하나도 없으면 아무것도 하지 않는다. 차례를 바꾸면 엔진의 글자 캐시를 비운다.
  - `engine` &Engine: 얼굴 차례를 걸 엔진.
  - `font_group` &str: 앞세울 묶음 이름. (언어를 바꿀 때 그 언어의 묶음)
- `pub fn load_ui_font_text(engine: &Engine, text: &str) -> UiFontTextLoadHandle` (method, since 0.4.0): 글자의 조각 받기. (그 글자를 그릴 얼굴만, 원본 loadUiFontText. 청한 얼굴을 모두 올리면 끝나는 손잡이를 돌려준다)
  - 같은 파일을 여러 굵기로 올리면 바이트는 한 번 받고 굵기마다 올린다.
  - 받은 묶음이 없거나 글이 비면 아무것도 청하지 않는다. 이미 받기를 청한 얼굴은 다시 청하지 않는다.
  - `engine` &Engine: 애셋을 받고 글꼴 얼굴을 올릴 엔진.
  - `text` &str: 쓸 글자. (글자마다 굵기마다 그릴 얼굴을 찾는다)
  - returns UiFontTextLoadHandle: 이번에 청한 얼굴들을 모두 올리면(또는 실패하면) 끝나는 손잡이. 청한 것이 없으면 바로 끝난 손잡이.

  예제: 받은 묶음이 없으면 청할 것이 없다

  ```rust
  use vanilla::{uifont, Engine, EngineConfiguration};
  
  let engine = Engine::new(EngineConfiguration::new());
  let text_load_handle = uifont::load_ui_font_text(engine, "Player One");
  assert!(text_load_handle.get_font_load_handles().is_empty());
  assert!(text_load_handle.is_finished());
  ```

- `pub fn request_ui_font_text(engine: &Engine, text: &str)` (method, since 0.4.0): 글자의 조각 받기 청하기. (사람 이름처럼 그때그때 나오는 글자, 처음 보는 글자만. 원본 requestUiFontText)
  - 받은 묶음이 없으면 아무것도 하지 않는다. 손잡이는 돌려주지 않는다.
  - `engine` &Engine: 애셋을 받고 글꼴 얼굴을 올릴 엔진.
  - `text` &str: 쓸 글자. (앞서 청한 글자는 뺀다)
- `pub fn wait_for_load() -> UiFontLoadFuture` (method, since 0.4.0): 처음 불러오기를 기다리는 퓨처. (원본 await loadUiFont(묶음, 글자) 의 await 자리, load_ui_font 가 돌려준 손잡이를 await 하는 것과 같다)
  - 기다리는 동안 프레임마다 update 를 부른다. 원본처럼 처음 글자를 다 올렸거나 wait_milliseconds 가 지나면 끝난다. (is_ui_font_ready)
  - returns UiFontLoadFuture: is_ui_font_ready 가 참이 되면 끝나는 퓨처.
- `pub fn is_ui_font_ready() -> bool` (method, since 0.4.0): 처음 불러오기가 준비됐는지. (처음 글자의 조각을 다 받았거나 wait_milliseconds 가 지났으면 참, 원본 loadUiFont 의 기다림)
  - 불러오기를 시작하지 않았거나 목록을 못 읽었으면 참이다. (시스템 글꼴로 이어 간다)
  - returns bool: 처음 불러오기가 준비됐으면 true.
- `pub fn read_ui_font_face() -> Option<String>` (method, since 0.4.0): 불러온 UI 글꼴의 이름. (엔진의 글자 컴포넌트에 set_font_family 로 준다, 못 불러왔으면 None. 원본 readUiFontFace)
  - returns Option<String>: 처음 묶음을 받아 올렸으면 글꼴 이름. 아직이거나 못 불러왔으면 None.
- `pub fn build_ui_font_string(font_weight: u16, font_size: f64) -> String` (method, since 0.4.0): UI 글꼴 문자열. (굵기, 크기. 원본 buildUiFontString)
  - `font_weight` u16: 글자 굵기. (500, 700 같은 CSS 굵기)
  - `font_size` f64: 글자 크기. (픽셀)
  - returns String: Canvas2D font 꼴 글. (`500 16px "GameUI", sans-serif` 꼴)
- `pub fn configure_ui_font(options: UiFontOptions)` (method, since 0.4.0): UI 글꼴 설정. (불러오기 전에 부른다, None 인 칸은 그대로. 원본 configureUiFont)
  - primary_group 을 주면 지금 고른 묶음도 그것으로 돌린다.
  - `options` UiFontOptions: 바꿀 칸만 Some 으로 채운 설정.
- `pub fn read_ui_font_family() -> String` (method, since 0.4.0): UI 글꼴 이름 반환. (원본 readUiFontFamily)
  - returns String: 올릴 글꼴 이름. (처음은 "GameUI", 불러오지 않았어도 돌려준다)
