bevy_text/
font.rs

1use alloc::sync::Arc;
2
3use bevy_asset::Asset;
4use bevy_reflect::TypePath;
5use cosmic_text::skrifa::raw::ReadError;
6use cosmic_text::skrifa::FontRef;
7
8/// An [`Asset`] that contains the data for a loaded font, if loaded as an asset.
9///
10/// Loaded by [`FontLoader`](crate::FontLoader).
11///
12/// # A note on fonts
13///
14/// `Font` may differ from the everyday notion of what a "font" is.
15/// A font *face* (e.g. Fira Sans Semibold Italic) is part of a font *family* (e.g. Fira Sans),
16/// and is distinguished from other font faces in the same family
17/// by its style (e.g. italic), its weight (e.g. bold) and its stretch (e.g. condensed).
18///
19/// Bevy currently loads a single font face as a single `Font` asset.
20#[derive(Debug, TypePath, Clone, Asset)]
21pub struct Font {
22    /// Content of a font file as bytes
23    pub data: Arc<Vec<u8>>,
24}
25
26impl Font {
27    /// Creates a [`Font`] from bytes
28    pub fn try_from_bytes(font_data: Vec<u8>) -> Result<Self, ReadError> {
29        let _ = FontRef::from_index(&font_data, 0)?;
30        Ok(Self {
31            data: Arc::new(font_data),
32        })
33    }
34}