firefly_rust/graphics/
font.rs

1use crate::*;
2
3/// A loaded font file.
4///
5/// Can be loaded as [`FileBuf`] from ROM with [`load_file_buf`]
6/// and then cast using [`FileBuf::as_font`].
7pub struct Font<'a> {
8    pub(crate) raw: &'a [u8],
9}
10
11impl Font<'_> {
12    /// Check if the font encoding is ASCII.
13    #[must_use]
14    pub fn is_ascii(&self) -> bool {
15        self.raw[1] == 0
16    }
17
18    /// Calculate width (in pixels) of the given text.
19    ///
20    /// This function does not account for newlines.
21    #[must_use]
22    pub fn line_width(&self, t: &str) -> u32 {
23        t.len() as u32 * u32::from(self.char_width())
24    }
25
26    /// The width (in pixels) of one character.
27    #[must_use]
28    pub fn char_width(&self) -> u8 {
29        self.raw[2]
30    }
31
32    /// The hight (in pixels) of one character (one line).
33    #[must_use]
34    pub fn char_height(&self) -> u8 {
35        self.raw[3]
36    }
37}
38
39impl<'a> From<File<'a>> for Font<'a> {
40    fn from(value: File<'a>) -> Self {
41        Self { raw: value.raw }
42    }
43}
44
45#[cfg(feature = "alloc")]
46impl<'a> From<&'a FileBuf> for Font<'a> {
47    fn from(value: &'a FileBuf) -> Self {
48        Self { raw: &value.raw }
49    }
50}