bevy_text/
font_loader.rs

1use crate::Font;
2use bevy_asset::{io::Reader, AssetLoader, LoadContext};
3use thiserror::Error;
4
5#[derive(Default)]
6/// An [`AssetLoader`] for [`Font`]s, for use by the [`AssetServer`](bevy_asset::AssetServer)
7pub struct FontLoader;
8
9/// Possible errors that can be produced by [`FontLoader`]
10#[non_exhaustive]
11#[derive(Debug, Error)]
12pub enum FontLoaderError {
13    /// The contents that could not be parsed
14    #[error(transparent)]
15    Content(#[from] cosmic_text::ttf_parser::FaceParsingError),
16    /// An [IO](std::io) Error
17    #[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}