1use crate::Font;
2use bevy_asset::{io::Reader, AssetLoader, LoadContext};
3use thiserror::Error;
4
5#[derive(Default)]
6pub struct FontLoader;
8
9#[non_exhaustive]
11#[derive(Debug, Error)]
12pub enum FontLoaderError {
13 #[error(transparent)]
15 Content(#[from] cosmic_text::ttf_parser::FaceParsingError),
16 #[error(transparent)]
18 Io(#[from] std::io::Error),
19}
20
21impl AssetLoader for FontLoader {
22 type Asset = Font;
23 type Settings = ();
24 type Error = FontLoaderError;
25 async fn load(
26 &self,
27 reader: &mut dyn Reader,
28 _settings: &(),
29 _load_context: &mut LoadContext<'_>,
30 ) -> Result<Font, Self::Error> {
31 let mut bytes = Vec::new();
32 reader.read_to_end(&mut bytes).await?;
33 let font = Font::try_from_bytes(bytes)?;
34 Ok(font)
35 }
36
37 fn extensions(&self) -> &[&str] {
38 &["ttf", "otf"]
39 }
40}