microbit_text/
font.rs

1//! A 5×5 ascii font.
2//!
3//! This is a copy of the 'pendolino' font from the [micro:bit runtime][dal].
4//!
5//! [dal]: https://lancaster-university.github.io/microbit-docs/
6
7mod pendolino;
8
9use crate::image::BitImage;
10
11/// Index of the first character in the standard font
12pub const PRINTABLE_START: usize = 32;
13
14/// Number of characters in the standard font
15pub const PRINTABLE_COUNT: usize = 95;
16
17const UNKNOWN: BitImage = BitImage::new(&[
18    [1, 1, 1, 1, 1],
19    [1, 0, 0, 0, 1],
20    [1, 0, 0, 0, 1],
21    [1, 0, 0, 0, 1],
22    [1, 1, 1, 1, 1],
23]);
24
25
26/// Returns an image representing the requested ascii character.
27///
28/// If the requested character isn't printable, returns a 'hollow square' image.
29///
30/// # Example
31///
32/// `font::character(b'x')`
33pub fn character(index: u8) -> &'static BitImage {
34    let index = index as usize;
35    if !(PRINTABLE_START..PRINTABLE_START + PRINTABLE_COUNT).contains(&index) {
36        return &UNKNOWN;
37    }
38    &self::pendolino::PENDOLINO3[index - PRINTABLE_START]
39}
40
41const fn font_entry(data: [u8; 5]) -> BitImage {
42    // Note the data in the pendolino font uses the opposite column numbering
43    // system to BitImage.
44    const fn row_bits(byte: u8) -> [u8; 5] {[
45        ((byte & 1<<4) != 0) as u8,
46        ((byte & 1<<3) != 0) as u8,
47        ((byte & 1<<2) != 0) as u8,
48        ((byte & 1<<1) != 0) as u8,
49        ((byte & 1<<0) != 0) as u8,
50    ]}
51    BitImage::new(&[
52        row_bits(data[0]),
53        row_bits(data[1]),
54        row_bits(data[2]),
55        row_bits(data[3]),
56        row_bits(data[4]),
57    ])
58}
59