use std::fmt::Display;
use figfont::{FIGcharacter, SubCharacter};
#[derive(Debug, Clone)]
pub struct FIGchar {
width: usize,
matrix: Vec<Vec<SubCharacter>>,
}
impl FIGchar {
pub fn lines(&self) -> &Vec<Vec<SubCharacter>> {
&self.matrix
}
pub fn width(&self) -> usize {
self.width
}
pub fn is_whitespace(&self) -> bool {
self.matrix.iter().all(|line| {
line.iter().all(|subchar| match subchar {
SubCharacter::Blank => true,
SubCharacter::Symbol(s) => s.chars().all(|c| c.is_whitespace()),
})
})
}
pub fn leading_spaces(&self) -> Vec<(usize, SubCharacter)> {
let mut spaces = vec![(self.width, SubCharacter::Blank); self.matrix.len()];
for (i, line) in self.matrix.iter().enumerate() {
for (j, c) in line.iter().enumerate() {
if *c != SubCharacter::Symbol(" ".to_string()) {
spaces[i] = (j, c.clone());
break;
}
}
}
spaces
}
}
impl Display for FIGchar {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
for line in &self.matrix {
writeln!(f, "{}", line.join(""))?;
}
Ok(())
}
}
impl From<&FIGcharacter> for FIGchar {
fn from(value: &FIGcharacter) -> Self {
FIGchar {
width: value.width(),
matrix: value.lines().to_vec(),
}
}
}