Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/crates/bevy_text/src/font_loader.rs
9550 views
1
use crate::Font;
2
use bevy_asset::{io::Reader, AssetLoader, LoadContext};
3
use bevy_reflect::TypePath;
4
use thiserror::Error;
5
6
#[derive(Default, TypePath)]
7
/// An [`AssetLoader`] for [`Font`]s, for use by the [`AssetServer`](bevy_asset::AssetServer)
8
pub struct FontLoader;
9
10
/// Possible errors that can be produced by [`FontLoader`]
11
#[non_exhaustive]
12
#[derive(Debug, Error)]
13
pub enum FontLoaderError {
14
/// The contents that could not be parsed
15
#[error("Failed to parse font.")]
16
Content,
17
/// An [IO](std::io) Error
18
#[error(transparent)]
19
Io(#[from] std::io::Error),
20
}
21
22
impl AssetLoader for FontLoader {
23
type Asset = Font;
24
type Settings = ();
25
type Error = FontLoaderError;
26
async fn load(
27
&self,
28
reader: &mut dyn Reader,
29
_settings: &(),
30
load_context: &mut LoadContext<'_>,
31
) -> Result<Font, Self::Error> {
32
let path = load_context.path();
33
let mut bytes = Vec::new();
34
reader.read_to_end(&mut bytes).await?;
35
let font = Font::try_from_bytes(bytes, &path.to_string());
36
Ok(font)
37
}
38
39
fn extensions(&self) -> &[&str] {
40
&["ttf", "otf"]
41
}
42
}
43
44