# resource/fontasset (vanilla-engine 0.4.0)

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

Font asset. (the original resource/fontasset.js)
- The original creates a FontFace and adds it to document.fonts. After that the name is used in font strings ("16px name").
- In the Rust version, after the bytes arrive the engine registers it by name in Graphic's text cache at the start of the next frame.
  WOFF, WOFF2 work too. (the text cache decodes them to TTF)
- The original descriptors (weight, style, character range) are taken as FontFaceDescriptors. (load_font_with_descriptors, load_font_with_bold for weight only)
  When several files with different weights or styles are registered under the same name, the text cache picks and draws like CSS. If a real italic face exists, it is used instead of fake slanting.
- unload removes that face from the font list as in the original. (document.fonts.delete)
- load: like the original load(assetPath), calls load_font with the file name minus its extension as the family name. ("DOSGothic" for "assets/DOSGothic.woff")
- load_font_from_bytes: reads from font bytes already in hand. (the original loadFontFromBytes, uses the name if the descriptor's source_key is empty)
- The load_font functions return a FontLoadHandle that can be awaited as in the original. It finishes once the bytes arrive and the engine registers it in the font list.
  (after the original await fontFace.load() and document.fonts.add, from then on it draws with that name) If receiving or registering fails, it finishes with the reason.

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

Font fetch handle. (returned by FontAsset's load_font functions and Engine::register_font_face_when_loaded. Stands in for the Promise of the original loadFont)
- Finishes once the engine registers it in the font list (or fails). A cloned handle points to the same registration.

Example: Even if receiving the bytes finishes on the spot, the engine registers it at the start of the next frame

```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): Byte read handle. (for handing to a scene)
  - returns &AssetLoadHandle: Load handle of the font file bytes.
- `pub fn is_finished(&self) -> bool` (method, since 0.4.0): Whether it finished. (registered in the font list or failed)
  - returns bool: true if the engine registered it in the font list or receiving or registering failed.

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

Future that waits for a font fetch. (just await FontLoadHandle)
- Finishes with Ok(()) once registered, Err(reason) if receiving or registering fails.

Example: Waiting on a font handle inside an engine task does not finish until the engine registers it

```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)

Font asset.
- Once the bytes arrive, the engine registers it in the font list under the family name at the start of the next frame. After that the name is used in font strings ("16px name").

Example: Hand off a read with bytes in hand and unload

```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): Constructor.
  - returns FontAsset: Empty font asset before a request. (the name is empty text, the descriptor is FontFaceDescriptors::new())
- `pub fn load(&mut self, engine: &Engine, asset_path: &str) -> FontLoadHandle` (method, since 0.4.0): Requests a font read. (the original load(assetPath), calls load_font with the file name minus its extension as the family name)
  - `engine` &Engine: Engine that receives and registers the font.
  - `asset_path` &str: Font file path. (the family name is "DOSGothic" for "assets/DOSGothic.woff")
  - returns FontLoadHandle: Font load handle that can be awaited. (finishes with the reason if receiving or registering fails)
- `pub fn load_font(&mut self, engine: &Engine, family: &str, asset_path: &str) -> FontLoadHandle` (method, since 0.3.2): Requests a font read. (the original loadFont(family, assetPath). Once everything arrives the engine registers it under the family name)
  - A face with weight 400, not italic, for all characters. If already requested, does not request again and returns that handle.
  - `engine` &Engine: Engine to ask for the bytes and register the font on.
  - `family` &str: Font name. (the name used in font strings)
  - `asset_path` &str: Font file path. (ttf, otf, woff, woff2. Relative to the asset folder on desktop, relative to asset_base_url on web)
  - returns FontLoadHandle: Handle that finishes once the engine registers it in the font list (or fails).
- `pub fn load_font_with_bold(&mut self, engine: &Engine, family: &str, asset_path: &str, is_bold: bool) -> FontLoadHandle` (method, since 0.3.2): Requests a bold font read. (in place of weight in the original descriptors)
  - If already requested, does not request again and returns that handle.
  - `engine` &Engine: Engine to ask for the bytes and register the font on.
  - `family` &str: Font name.
  - `asset_path` &str: Font file path. (relative to the asset folder on desktop, relative to asset_base_url on web)
  - `is_bold` bool: If true, registers it as a weight 700 face, if false as 400.
  - returns FontLoadHandle: Handle that finishes once the engine registers it in the font list (or fails).
- `pub fn load_font_with_descriptors(&mut self, engine: &Engine, family: &str, asset_path: &str, mut descriptors: FontFaceDescriptors) -> FontLoadHandle` (method, since 0.4.0): Requests a font read with a descriptor. (the original loadFont(family, assetPath, descriptors), uses the path if the descriptor's source_key is empty)
  - If already requested, does not request again and returns that handle. (the original also finishes right away if fontFace exists)
  - `engine` &Engine: Engine to ask for the bytes and register the font on.
  - `family` &str: Font name.
  - `asset_path` &str: Font file path. (relative to the asset folder on desktop, relative to asset_base_url on web)
  - `descriptors` FontFaceDescriptors: Weight, style, character range, face key. (asset_path if the key is empty)
  - returns FontLoadHandle: Handle that finishes once the engine registers it in the font list (or fails).

  Example: Hand off with an italic face and a character range (if the key is left empty the path is the key, and for a missing file the bytes handle fails)

  ```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): Reads from font bytes already in hand. (ttf, otf, woff, woff2, the original loadFontFromBytes, the engine registers it under the family name at the start of the next frame)
  - The descriptor is FontFaceDescriptors::new() and the face key is family. If already requested, does not request again and returns that handle.
  - `engine` &Engine: Engine to register the font on.
  - `family` &str: Font name.
  - `bytes` Vec<u8>: Font file bytes.
  - returns FontLoadHandle: Handle that finishes once the engine registers it in the font list (or fails).
- `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): Reads from font bytes already in hand with a descriptor. (the original loadFontFromBytes(family, bytes, descriptors))
  - If already requested, does not request again and returns that handle.
  - `engine` &Engine: Engine to register the font on.
  - `family` &str: Font name.
  - `bytes` Vec<u8>: Font file bytes.
  - `descriptors` FontFaceDescriptors: Weight, style, character range, face key. (family if the key is empty, and the load handle's path is this key too)
  - returns FontLoadHandle: Handle that finishes once the engine registers it in the font list (or fails).
- `pub fn unload(&mut self, engine: &Engine)` (method, since 0.4.0): Unloads the asset. (the original unload: removes this face from the font list)
  - Does nothing before a request. A registration still waiting for bytes is removed right away, and a registered face is removed at the start of the next frame.
  - `engine` &Engine: Engine to remove the face from.
- `pub fn get_descriptors(&self) -> &FontFaceDescriptors` (method, since 0.4.0): Face descriptors.
  - returns &FontFaceDescriptors: Descriptor given at request. (with the key filled in, FontFaceDescriptors::new() before a request)
- `pub fn get_family(&self) -> &str` (method, since 0.3.2): Font name.
  - returns &str: Font name given at request. Empty text before a request.
- `pub fn is_bold(&self) -> bool` (method, since 0.3.2): Whether it is a bold font.
  - returns bool: true if the start of the descriptor's weight range is 600 or more.
