bevy_text/
font.rs

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