# resource/fontasset (vanilla-engine 0.4.0)

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

글꼴 애셋. (원본 resource/fontasset.js)
- 원본은 FontFace 를 만들어 document.fonts 에 더한다. 그 뒤로 폰트 문자열("16px 이름")에서 그 이름을 쓴다.
- 러스트 판은 바이트를 받은 뒤 엔진이 다음 프레임 시작에 Graphic 의 글자 캐시에 이름으로 등록한다.
  WOFF, WOFF2 도 된다. (글자 캐시가 TTF 로 푼다)
- 원본 descriptors(굵기, 기울기, 글자 범위)는 FontFaceDescriptors 로 받는다. (load_font_with_descriptors, 굵기만이면 load_font_with_bold)
  같은 이름에 굵기나 기울기가 다른 파일을 여러 개 올리면 글자 캐시가 CSS 처럼 골라 그린다. 진짜 기울인 얼굴이 있으면 가짜 기울임 대신 그것을 쓴다.
- unload 는 원본처럼 그 얼굴을 글꼴 목록에서 뺀다. (document.fonts.delete)
- load: 원본 load(assetPath) 처럼 파일 이름에서 확장자를 뗀 것을 패밀리 이름으로 load_font 한다. ("assets/DOSGothic.woff" 면 "DOSGothic")
- load_font_from_bytes: 이미 가진 글꼴 바이트로 읽는다. (원본 loadFontFromBytes, 서술자의 source_key 가 비면 이름을 쓴다)
- load_font 들은 원본처럼 await 할 수 있는 FontLoadHandle 을 돌려준다. 바이트가 오고 엔진이 글꼴 목록에 올리면 끝난다.
  (원본 await fontFace.load() 와 document.fonts.add 뒤, 그 뒤로 그 이름으로 그린다) 받기나 올리기에 실패하면 까닭으로 끝난다.

## FontLoadHandle (struct) (Clone, Debug, IntoFuture)

글꼴 받기 손잡이. (FontAsset 의 load_font 들, Engine::register_font_face_when_loaded 가 돌려준다. 원본 loadFont 의 Promise 자리)
- 엔진이 글꼴 목록에 올리면(또는 실패하면) 끝난다. 복제한 손잡이는 같은 올리기를 가리킨다.

예제: 바이트 받기는 그 자리에서 끝나도 올리기는 엔진이 다음 프레임 시작에 한다

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

let engine = Engine::new(EngineConfiguration::new());
let mut font_asset = FontAsset::new();
let font_load_handle = font_asset.load_font(engine, "Title", "examples/assets/missing.ttf");
assert!(font_load_handle.get_load_handle().is_failed());
assert!(!font_load_handle.is_finished());
```

- `pub fn get_load_handle(&self) -> &AssetLoadHandle` (method, since 0.4.0): 바이트 읽기 손잡이. (씬에 맡길 때)
  - returns &AssetLoadHandle: 글꼴 파일 바이트의 읽기 손잡이.
- `pub fn is_finished(&self) -> bool` (method, since 0.4.0): 끝났는지. (글꼴 목록에 올렸거나 실패했거나)
  - returns bool: 엔진이 글꼴 목록에 올렸거나 받기나 올리기에 실패했으면 true.

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

글꼴 받기를 기다리는 퓨처. (FontLoadHandle 을 await 하면 된다)
- 올리면 Ok(()), 받기나 올리기에 실패하면 Err(까닭)으로 끝난다.

예제: 엔진 작업 안에서 글꼴 손잡이를 기다리면 엔진이 올릴 때까지 끝나지 않는다

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

let engine = Engine::new(EngineConfiguration::new());
let mut font_asset = FontAsset::new();
let font_load_handle = font_asset.load_font_from_bytes(engine, "Pixel", vec![0, 1, 0, 0]);
let task_handle = engine.spawn_task(|_engine_context| async move {
	let _ = font_load_handle.await;
});
assert!(!task_handle.is_finished());
```

## FontAsset (struct) (Default, Asset)

글꼴 애셋.
- 바이트를 받으면 엔진이 다음 프레임 시작에 family 이름으로 글꼴 목록에 올린다. 그 뒤로 폰트 문자열("16px 이름")에서 그 이름을 쓴다.

예제: 가진 바이트로 읽기를 맡기고 내리기

```rust
use vanilla::{Asset, Engine, EngineConfiguration, FontAsset, FontFaceDescriptors};

let engine = Engine::new(EngineConfiguration::new());
let mut font_asset = FontAsset::new();
font_asset.load_font_from_bytes_with_descriptors(engine, "Pixel", vec![0, 1, 0, 0], FontFaceDescriptors::create_with_bold(true));
assert!(font_asset.is_loaded());
assert_eq!(font_asset.get_family(), "Pixel");
assert!(font_asset.is_bold());
assert_eq!(font_asset.get_descriptors().source_key, "Pixel");
assert_eq!(engine.get_pending_font_face_count(), 1);
font_asset.unload(engine);
assert!(!font_asset.is_loaded());
assert_eq!(engine.get_pending_font_face_count(), 0);
```

- `pub fn new() -> FontAsset` (constructor, since 0.3.2): 생성.
  - returns FontAsset: 요청 전의 빈 글꼴 애셋. (이름은 빈 글, 서술자는 FontFaceDescriptors::new())
- `pub fn load(&mut self, engine: &Engine, asset_path: &str) -> FontLoadHandle` (method, since 0.4.0): 글꼴 읽기 요청. (원본 load(assetPath), 파일 이름에서 확장자를 뗀 것을 패밀리 이름으로 load_font 한다)
  - `engine` &Engine: 글꼴을 받아 등록할 엔진.
  - `asset_path` &str: 글꼴 파일 경로. ("assets/DOSGothic.woff" 면 패밀리 이름은 "DOSGothic")
  - returns FontLoadHandle: await 할 수 있는 글꼴 받기 손잡이. (받기나 올리기에 실패하면 까닭으로 끝난다)
- `pub fn load_font(&mut self, engine: &Engine, family: &str, asset_path: &str) -> FontLoadHandle` (method, since 0.3.2): 글꼴 읽기 요청. (원본 loadFont(family, assetPath). 다 오면 엔진이 family 이름으로 등록한다)
  - 굵기 400, 기울이지 않음, 모든 글자의 얼굴이다. 이미 요청했으면 다시 하지 않고 그 손잡이를 돌려준다.
  - `engine` &Engine: 바이트를 부탁하고 글꼴을 올릴 엔진.
  - `family` &str: 글꼴 이름. (폰트 문자열에서 쓸 이름)
  - `asset_path` &str: 글꼴 파일 경로. (ttf, otf, woff, woff2. 데스크탑은 애셋 폴더 기준, 웹은 asset_base_url 기준)
  - returns FontLoadHandle: 엔진이 글꼴 목록에 올리면(또는 실패하면) 끝나는 손잡이.
- `pub fn load_font_with_bold(&mut self, engine: &Engine, family: &str, asset_path: &str, is_bold: bool) -> FontLoadHandle` (method, since 0.3.2): 굵은 글꼴 읽기 요청. (원본 descriptors 의 weight 자리)
  - 이미 요청했으면 다시 하지 않고 그 손잡이를 돌려준다.
  - `engine` &Engine: 바이트를 부탁하고 글꼴을 올릴 엔진.
  - `family` &str: 글꼴 이름.
  - `asset_path` &str: 글꼴 파일 경로. (데스크탑은 애셋 폴더 기준, 웹은 asset_base_url 기준)
  - `is_bold` bool: true 면 굵기 700, false 면 400 의 얼굴로 올린다.
  - returns FontLoadHandle: 엔진이 글꼴 목록에 올리면(또는 실패하면) 끝나는 손잡이.
- `pub fn load_font_with_descriptors(&mut self, engine: &Engine, family: &str, asset_path: &str, mut descriptors: FontFaceDescriptors) -> FontLoadHandle` (method, since 0.4.0): 서술자를 주어 글꼴 읽기 요청. (원본 loadFont(family, assetPath, descriptors), 서술자의 source_key 가 비면 경로를 쓴다)
  - 이미 요청했으면 다시 하지 않고 그 손잡이를 돌려준다. (원본도 fontFace 가 있으면 바로 끝난다)
  - `engine` &Engine: 바이트를 부탁하고 글꼴을 올릴 엔진.
  - `family` &str: 글꼴 이름.
  - `asset_path` &str: 글꼴 파일 경로. (데스크탑은 애셋 폴더 기준, 웹은 asset_base_url 기준)
  - `descriptors` FontFaceDescriptors: 굵기, 기울기, 글자 범위, 얼굴 열쇠. (열쇠가 비면 asset_path)
  - returns FontLoadHandle: 엔진이 글꼴 목록에 올리면(또는 실패하면) 끝나는 손잡이.

  예제: 기울인 얼굴과 글자 범위를 주어 맡기기 (열쇠를 비우면 경로가 열쇠다, 없는 파일이면 바이트 손잡이가 실패다)

  ```rust
  use vanilla::{Engine, EngineConfiguration, FontAsset, FontFaceDescriptors};
  
  let engine = Engine::new(EngineConfiguration::new());
  let mut descriptors = FontFaceDescriptors::new();
  descriptors.is_italic = true;
  descriptors.unicode_range_list = vec![(0x0020, 0x007e)];
  let mut font_asset = FontAsset::new();
  let font_load_handle = font_asset.load_font_with_descriptors(engine, "Body", "examples/assets/missing.ttf", descriptors);
  assert_eq!(font_asset.get_descriptors().source_key, "examples/assets/missing.ttf");
  assert!(font_asset.get_descriptors().is_italic);
  assert!(!font_asset.is_bold());
  assert!(font_load_handle.get_load_handle().is_failed());
  ```

- `pub fn load_font_from_bytes(&mut self, engine: &Engine, family: &str, bytes: Vec<u8>) -> FontLoadHandle` (method, since 0.4.0): 이미 가진 글꼴 바이트로 읽기. (ttf, otf, woff, woff2, 원본 loadFontFromBytes, 엔진이 다음 프레임 시작에 family 이름으로 올린다)
  - 서술자는 FontFaceDescriptors::new() 이고 얼굴 열쇠는 family 다. 이미 요청했으면 다시 하지 않고 그 손잡이를 돌려준다.
  - `engine` &Engine: 글꼴을 올릴 엔진.
  - `family` &str: 글꼴 이름.
  - `bytes` Vec<u8>: 글꼴 파일 바이트.
  - returns FontLoadHandle: 엔진이 글꼴 목록에 올리면(또는 실패하면) 끝나는 손잡이.
- `pub fn load_font_from_bytes_with_descriptors(&mut self, engine: &Engine, family: &str, bytes: Vec<u8>, mut descriptors: FontFaceDescriptors) -> FontLoadHandle` (method, since 0.4.0): 서술자를 주어 이미 가진 글꼴 바이트로 읽기. (원본 loadFontFromBytes(family, bytes, descriptors))
  - 이미 요청했으면 다시 하지 않고 그 손잡이를 돌려준다.
  - `engine` &Engine: 글꼴을 올릴 엔진.
  - `family` &str: 글꼴 이름.
  - `bytes` Vec<u8>: 글꼴 파일 바이트.
  - `descriptors` FontFaceDescriptors: 굵기, 기울기, 글자 범위, 얼굴 열쇠. (열쇠가 비면 family, 읽기 손잡이의 경로도 이 열쇠다)
  - returns FontLoadHandle: 엔진이 글꼴 목록에 올리면(또는 실패하면) 끝나는 손잡이.
- `pub fn unload(&mut self, engine: &Engine)` (method, since 0.4.0): 애셋 내리기. (원본 unload: 글꼴 목록에서 이 얼굴을 뺀다)
  - 요청 전이면 아무것도 하지 않는다. 아직 바이트를 기다리는 등록은 바로 빼고, 올린 얼굴은 다음 프레임 시작에 뺀다.
  - `engine` &Engine: 얼굴을 뺄 엔진.
- `pub fn get_descriptors(&self) -> &FontFaceDescriptors` (method, since 0.4.0): 얼굴 서술자.
  - returns &FontFaceDescriptors: 요청할 때 준 서술자. (열쇠를 채운 것, 요청 전이면 FontFaceDescriptors::new())
- `pub fn get_family(&self) -> &str` (method, since 0.3.2): 글꼴 이름.
  - returns &str: 요청할 때 준 글꼴 이름. 요청 전이면 빈 글.
- `pub fn is_bold(&self) -> bool` (method, since 0.3.2): 굵은 글꼴인지.
  - returns bool: 서술자의 굵기 범위 처음이 600 이상이면 true.
