1 use super::textui::GlyphMapping; 2 3 pub mod spleen_font; 4 5 pub use spleen_font::SPLEEN_FONT_8x16 as FONT_8x16; 6 7 /// Stores the font bitmap and some additional info for each font. 8 #[derive(Clone, Copy)] 9 pub struct BitmapFont<'a> { 10 /// The raw bitmap data for the font. 11 bitmap: RawBitMap<'a>, 12 13 /// The char to glyph mapping. 14 glyph_mapping: &'a dyn GlyphMapping, 15 16 /// The size of each character in the raw bitmap data. 17 size: Size, 18 19 bytes_per_char: usize, 20 } 21 22 #[allow(dead_code)] 23 impl<'a> BitmapFont<'a> { 24 pub const fn new( 25 bitmap: RawBitMap<'a>, 26 glyph_mapping: &'a dyn GlyphMapping, 27 size: Size, 28 ) -> Self { 29 Self { 30 bitmap, 31 glyph_mapping, 32 size, 33 bytes_per_char: (size.width + 7) / 8 * size.height, 34 } 35 } 36 /// Return the width of each character. 37 pub const fn width(&self) -> u32 { 38 self.size.width as u32 39 } 40 41 /// Return the height of each character. 42 pub const fn height(&self) -> u32 { 43 self.size.height as u32 44 } 45 46 #[inline(always)] 47 pub fn char_map(&self, character: char) -> &'a [u8] { 48 let index = self.glyph_mapping.index(character); 49 let pos = index * self.bytes_per_char; 50 51 return &self.bitmap.data[pos..pos + self.bytes_per_char]; 52 } 53 } 54 55 #[derive(Clone, Copy)] 56 pub struct Size { 57 pub width: usize, 58 pub height: usize, 59 } 60 61 impl Size { 62 pub const fn new(width: usize, height: usize) -> Self { 63 Self { width, height } 64 } 65 } 66 67 #[derive(Clone, Copy)] 68 pub struct RawBitMap<'a> { 69 pub data: &'a [u8], 70 pub size: Size, 71 } 72 73 #[allow(dead_code)] 74 impl RawBitMap<'_> { 75 pub const fn new(data: &'static [u8], width: usize) -> Self { 76 let size = Size { 77 width: 128, 78 height: data.len() / width / 8, 79 }; 80 Self { data, size } 81 } 82 83 pub const fn size(&self) -> Size { 84 self.size 85 } 86 87 pub const fn len(&self) -> usize { 88 self.data.len() 89 } 90 } 91