Skip to main content

embedded_gui/
font.rs

1include!(concat!(env!("OUT_DIR"), "/generated_ascii_3x5.rs"));
2
3#[derive(Clone, Copy, Debug, PartialEq, Eq)]
4pub struct PackedFont {
5    pub first_char: u8,
6    pub advance: u8,
7    pub line_height: u8,
8    pub glyphs: &'static [[u8; 5]],
9}
10
11pub static ASCII_3X5_FONT: PackedFont = PackedFont {
12    first_char: 32,
13    advance: 4,
14    line_height: 6,
15    glyphs: &ASCII_3X5_GLYPHS,
16};
17
18pub static ASCII_4X7_FONT: PackedFont = PackedFont {
19    first_char: 32,
20    advance: 5,
21    line_height: 8,
22    glyphs: &ASCII_4X7_GLYPHS,
23};
24
25#[derive(Clone, Copy, Debug, PartialEq, Eq)]
26pub enum FontId {
27    Tiny3x5,
28    Medium4x7,
29    Scaled6x10,
30}
31
32impl FontId {
33    pub const fn advance(self) -> u32 {
34        match self {
35            Self::Tiny3x5 => 4,
36            Self::Medium4x7 => 5,
37            Self::Scaled6x10 => 7,
38        }
39    }
40
41    pub const fn line_height(self) -> u32 {
42        match self {
43            Self::Tiny3x5 => 6,
44            Self::Medium4x7 => 8,
45            Self::Scaled6x10 => 11,
46        }
47    }
48}
49
50pub const fn packed_font(font: FontId) -> &'static PackedFont {
51    match font {
52        FontId::Tiny3x5 => &ASCII_3X5_FONT,
53        FontId::Medium4x7 => &ASCII_4X7_FONT,
54        FontId::Scaled6x10 => &ASCII_3X5_FONT,
55    }
56}
57
58pub fn glyph_rows(font: FontId, ch: char) -> [u8; 5] {
59    let packed = packed_font(font);
60    let code = ch as u32;
61    if code >= packed.first_char as u32 {
62        let idx = (code as usize).saturating_sub(packed.first_char as usize);
63        if idx < packed.glyphs.len() {
64            return packed.glyphs[idx];
65        }
66    }
67    let fallback = b'?'.saturating_sub(packed.first_char) as usize;
68    packed.glyphs.get(fallback).copied().unwrap_or([0; 5])
69}