#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Charset {
Ascii,
HalfBlocks,
Quadrants,
Braille,
}
const QUADRANTS: [char; 16] = [
' ', '\u{2598}', '\u{259D}', '\u{2580}', '\u{2596}', '\u{258C}', '\u{259E}', '\u{259B}',
'\u{2597}', '\u{259A}', '\u{2590}', '\u{259C}', '\u{2584}', '\u{2599}', '\u{259F}', '\u{2588}',
];
const BRAILLE_DOTS: [u8; 8] = [0x01, 0x08, 0x02, 0x10, 0x04, 0x20, 0x40, 0x80];
impl Charset {
pub fn pixels_per_cell(self) -> (usize, usize) {
match self {
Charset::Ascii => (1, 1),
Charset::HalfBlocks => (1, 2),
Charset::Quadrants => (2, 2),
Charset::Braille => (2, 4),
}
}
pub(crate) fn bit(self, column: usize, row: usize) -> u8 {
match self {
Charset::Ascii => 1,
Charset::HalfBlocks => 1 << row,
Charset::Quadrants => 1 << (row * 2 + column),
Charset::Braille => BRAILLE_DOTS[row * 2 + column],
}
}
pub(crate) fn fill_ramp(self) -> &'static [char] {
match self {
Charset::Ascii => &['#'],
_ => &[
'\u{2581}', '\u{2582}', '\u{2583}', '\u{2584}', '\u{2585}', '\u{2586}', '\u{2587}',
'\u{2588}',
],
}
}
pub(crate) fn glyph(self, bits: u8) -> char {
if bits == 0 {
return ' ';
}
match self {
Charset::Ascii => '*',
Charset::HalfBlocks => match bits {
1 => '\u{2580}',
2 => '\u{2584}',
_ => '\u{2588}',
},
Charset::Quadrants => QUADRANTS[usize::from(bits & 0x0F)],
Charset::Braille => {
char::from_u32(0x2800 + u32::from(bits)).expect("braille block is contiguous")
}
}
}
}
#[cfg(test)]
#[path = "tests/charset_tests.rs"]
mod tests;