use crate::{TextLut, is, unwrap};
#[doc = crate::_tags!(font)]
#[doc = crate::_doc_location!("media/font")]
#[derive(Clone, Debug, PartialEq, Eq, Hash)] pub struct FontArt<'g> {
pub glyphs: &'g [&'g [&'g str]],
pub first_glyph: char,
pub last_glyph: char,
pub width: u8,
pub height: u8,
pub baseline: u8,
pub advance_x: u8,
pub advance_y: u8,
}
impl<'g> FontArt<'g> {
pub const fn has_glyph(&self, c: char) -> bool {
let idx = (c as u32).wrapping_sub(self.first_glyph as u32);
let max_idx = self.last_glyph as u32 - self.first_glyph as u32;
idx <= max_idx
}
#[must_use]
pub const fn get_digit(&self, digit: u8) -> &'g [&'g str] {
assert!(digit <= 9, "Digit must be between 0 and 9");
let char_code = self.first_glyph as u8 + digit;
self.get_glyph_from_scalar_unchecked(char_code as u32)
}
#[must_use]
pub const fn get_digit_base(&self, digit: u8, base: u8) -> &'g [&'g str] {
assert!(base >= 2 && base <= 36, "Base must be between 2 and 36");
assert!(digit < base, "The given digit is not valid in the given base");
let offset = TextLut::ASCII_BASE36_OFFSET[digit as usize];
let char_code = self.first_glyph as u8 + digit + offset;
self.get_glyph_from_scalar_unchecked(char_code as u32)
}
#[must_use]
pub const fn get_glyph(&self, c: char) -> Option<&'g [&'g str]> {
let idx = (c as usize).wrapping_sub(self.first_glyph as usize);
let max_idx = self.last_glyph as usize - self.first_glyph as usize;
is![idx <= max_idx, Some(self.glyphs[idx]), None]
}
pub const fn get_glyph_from_scalar(&self, c: u32) -> Option<&'g [&'g str]> {
let idx = c.wrapping_sub(self.first_glyph as u32) as usize;
let max_idx = self.last_glyph as usize - self.first_glyph as usize;
is![idx <= max_idx, Some(self.glyphs[idx]), None]
}
pub const fn get_glyph_from_scalar_unchecked(&self, c: u32) -> &'g [&'g str] {
let idx = c.wrapping_sub(self.first_glyph as u32) as usize;
self.glyphs[idx]
}
pub const fn get_glyph_unchecked(&self, c: char) -> &'g [&'g str] {
let idx = (c as usize) - (self.first_glyph as usize);
self.glyphs[idx]
}
pub const fn get_glyph_or(&self, c: char, default: &'g [&'g str]) -> &'g [&'g str] {
unwrap![some_or self.get_glyph(c), default]
}
pub const fn get_glyph_or_first(&self, c: char) -> &'g [&'g str] {
unwrap![some_or self.get_glyph(c), self.glyphs[0]]
}
}
#[cfg(test)]
mod tests {
#[test]
fn get() {}
}