use embedded_graphics::iterator::raw::RawDataSlice;
use embedded_graphics::pixelcolor::PixelColor;
use embedded_graphics::pixelcolor::raw::BigEndian;
use crate::glyph::Glyph;
pub type CharmapEntryKey<'a> = &'a str;
#[derive(Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct CharmapEntry<'a, C, const N: usize>
where
C: PixelColor + From<C::Raw>,
RawDataSlice<'a, C::Raw, BigEndian>: IntoIterator<Item = C::Raw>,
{
pub key: CharmapEntryKey<'a>,
pub advance_chars: usize,
pub advance_width_to: fn(CharmapEntryKey<'a>) -> f32,
pub glyph: Glyph<'a, C, N>,
}
#[derive(Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum Charmap<'a, C, const N: usize>
where
C: PixelColor + From<C::Raw>,
RawDataSlice<'a, C::Raw, BigEndian>: IntoIterator<Item = C::Raw>,
{
Leaf(CharmapEntry<'a, C, N>),
Branch(fn(char) -> &'a Charmap<'a, C, N>),
}
impl<'a, C, const N: usize> CharmapEntry<'a, C, N>
where
C: PixelColor + From<C::Raw>,
RawDataSlice<'a, C::Raw, BigEndian>: IntoIterator<Item = C::Raw>,
{
pub const NULL: Self = Self {
key: "",
advance_chars: 0,
advance_width_to: |_| 0.0,
glyph: Glyph::NULL,
};
}
impl<'a, C, const N: usize> Charmap<'a, C, N>
where
C: PixelColor + From<C::Raw>,
RawDataSlice<'a, C::Raw, BigEndian>: IntoIterator<Item = C::Raw>,
{
pub fn get(&self, slice: &str) -> &CharmapEntry<'a, C, N> {
let mut chars = slice.chars();
let first = chars.next().unwrap_or_default();
let slice = chars.as_str();
match self {
Self::Leaf(entry) => entry,
Self::Branch(map) => map(first).get(slice),
}
}
}