use unicode_width::UnicodeWidthChar;
use crate::{Ink, Size};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Glyph {
pub unicode: char,
pub ascii: char,
}
impl Glyph {
pub const fn new(unicode: char, ascii: char) -> Self {
Self { unicode, ascii }
}
pub const fn ascii(ch: char) -> Self {
Self::new(ch, ch)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum GlyphMode {
#[default]
Unicode,
Ascii,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Cell {
pub ch: char,
pub ink: Ink,
pub bg: Ink,
}
impl Cell {
pub const BLANK: Cell = Cell {
ch: ' ',
ink: Ink::BLANK,
bg: Ink::BLANK,
};
}
#[derive(Debug, Clone)]
pub struct Canvas {
size: Size,
mode: GlyphMode,
cells: Vec<Cell>,
}
impl Canvas {
pub fn new(size: Size, mode: GlyphMode) -> Self {
Self {
size,
mode,
cells: vec![Cell::BLANK; size.area()],
}
}
pub fn size(&self) -> Size {
self.size
}
pub fn mode(&self) -> GlyphMode {
self.mode
}
pub fn set_mode(&mut self, mode: GlyphMode) {
self.mode = mode;
}
pub fn cells(&self) -> &[Cell] {
&self.cells
}
pub fn resize(&mut self, size: Size) {
if size != self.size {
self.size = size;
self.cells = vec![Cell::BLANK; size.area()];
} else {
self.clear();
}
}
pub fn clear(&mut self) {
self.cells.fill(Cell::BLANK);
}
fn index(&self, x: i32, y: i32) -> Option<usize> {
let (w, h) = (i32::from(self.size.width()), i32::from(self.size.height()));
((0..w).contains(&x) && (0..h).contains(&y))
.then(|| y as usize * usize::from(self.size.width()) + x as usize)
}
pub fn get(&self, x: i32, y: i32) -> Option<&Cell> {
self.index(x, y).map(|i| &self.cells[i])
}
pub fn put(&mut self, x: i32, y: i32, glyph: Glyph, ink: Ink) {
self.put_bg(x, y, glyph, ink, Ink::BLANK);
}
pub fn put_bg(&mut self, x: i32, y: i32, glyph: Glyph, fg: Ink, bg: Ink) {
let Some(i) = self.index(x, y) else { return };
let ch = match self.mode {
GlyphMode::Ascii => glyph.ascii,
GlyphMode::Unicode if glyph.unicode.width() == Some(1) => glyph.unicode,
GlyphMode::Unicode => glyph.ascii,
};
self.cells[i] = Cell { ch, ink: fg, bg };
}
pub fn text(&mut self, x: i32, y: i32, s: &str, ink: Ink) {
for (i, ch) in s.chars().enumerate() {
self.put(x.saturating_add(i as i32), y, Glyph::ascii(ch), ink);
}
}
pub fn to_lines(&self) -> Vec<String> {
self.cells
.chunks(usize::from(self.size.width()))
.map(|row| row.iter().map(|c| c.ch).collect())
.collect()
}
}