use crate::buffer::Cell;
use crate::canvas::Canvas;
use crossterm::style::{Attribute, Color};
pub const SAMPLES_PER_CELL: usize = 2;
pub const FULL: char = '█';
pub const UPPER: char = '▀';
pub const LOWER: char = '▄';
pub const LEFT: char = '▌';
pub const RIGHT: char = '▐';
pub const UPPER_LEFT: char = '▛';
pub const UPPER_RIGHT: char = '▜';
pub const LOWER_LEFT: char = '▙';
pub const LOWER_RIGHT: char = '▟';
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Quadrants {
pub upper_left: bool,
pub upper_right: bool,
pub lower_left: bool,
pub lower_right: bool,
}
impl Quadrants {
pub fn glyph(self) -> char {
let Self {
upper_left,
upper_right,
lower_left,
lower_right,
} = self;
match (upper_left, upper_right, lower_left, lower_right) {
(false, false, false, false) => ' ',
(true, true, true, true) => FULL,
(true, true, false, false) => UPPER,
(false, false, true, true) => LOWER,
(true, false, true, false) => LEFT,
(false, true, false, true) => RIGHT,
(true, false, false, false) => UPPER_LEFT,
(false, true, false, false) => UPPER_RIGHT,
(false, false, true, false) => LOWER_LEFT,
(false, false, false, true) => LOWER_RIGHT,
_ => FULL,
}
}
pub fn needs_background(self) -> bool {
self.glyph() != FULL && self.glyph() != ' '
}
pub fn from_samples(
samples: [[bool; SAMPLES_PER_CELL]; SAMPLES_PER_CELL],
) -> Self {
Self {
upper_left: samples[0][0],
upper_right: samples[0][1],
lower_left: samples[1][0],
lower_right: samples[1][1],
}
}
}
#[derive(Clone)]
pub struct QuadrantMask {
width: usize,
height: usize,
occupied: Vec<bool>,
}
impl QuadrantMask {
pub fn new(width: usize, height: usize) -> Self {
let (width, height) = (width.max(1), height.max(1));
Self {
width,
height,
occupied: vec![
false;
width * SAMPLES_PER_CELL * height * SAMPLES_PER_CELL
],
}
}
pub fn width(&self) -> usize {
self.width
}
pub fn height(&self) -> usize {
self.height
}
pub fn sample_width(&self) -> usize {
self.width * SAMPLES_PER_CELL
}
pub fn sample_height(&self) -> usize {
self.height * SAMPLES_PER_CELL
}
pub fn clear(&mut self) {
self.occupied.fill(false);
}
pub fn resize(&mut self, width: usize, height: usize) {
let (width, height) = (width.max(1), height.max(1));
if (width, height) == (self.width, self.height) {
return;
}
*self = Self::new(width, height);
}
#[inline]
pub fn set(&mut self, x: usize, y: usize, on: bool) {
if x >= self.sample_width() || y >= self.sample_height() {
return;
}
let stride = self.sample_width();
self.occupied[y * stride + x] = on;
}
#[inline]
pub fn get(&self, x: usize, y: usize) -> bool {
if x >= self.sample_width() || y >= self.sample_height() {
return false;
}
self.occupied[y * self.sample_width() + x]
}
pub fn cell(&self, cell_x: usize, cell_y: usize) -> Quadrants {
let x = cell_x * SAMPLES_PER_CELL;
let y = cell_y * SAMPLES_PER_CELL;
Quadrants::from_samples([
[self.get(x, y), self.get(x + 1, y)],
[self.get(x, y + 1), self.get(x + 1, y + 1)],
])
}
pub fn count(&self) -> usize {
self.occupied.iter().filter(|on| **on).count()
}
pub fn write_to(
&self,
canvas: &mut Canvas,
ink: Color,
background: Color,
attr: Attribute,
) {
for cell_y in 0..self.height.min(canvas.height()) {
for cell_x in 0..self.width.min(canvas.width()) {
let quadrants = self.cell(cell_x, cell_y);
if quadrants == Quadrants::from_samples([[false; 2]; 2]) {
continue;
}
let cell = if quadrants.needs_background() {
Cell::with_bg(quadrants.glyph(), ink, background, attr)
} else {
Cell::new(quadrants.glyph(), ink, attr)
};
canvas.set(cell_x, cell_y, cell);
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn quadrants(ul: bool, ur: bool, ll: bool, lr: bool) -> Quadrants {
Quadrants {
upper_left: ul,
upper_right: ur,
lower_left: ll,
lower_right: lr,
}
}
#[test]
fn every_arrangement_produces_a_glyph() {
for bits in 0..16u8 {
let q = quadrants(
bits & 1 != 0,
bits & 2 != 0,
bits & 4 != 0,
bits & 8 != 0,
);
let glyph = q.glyph();
assert!(!glyph.is_control(), "{bits:04b} produced {glyph:?}");
}
}
#[test]
fn the_single_quadrants_use_the_quadrant_glyphs() {
assert_eq!(quadrants(true, false, false, false).glyph(), '▛');
assert_eq!(quadrants(false, true, false, false).glyph(), '▜');
assert_eq!(quadrants(false, false, true, false).glyph(), '▙');
assert_eq!(quadrants(false, false, false, true).glyph(), '▟');
}
#[test]
fn the_halves_and_the_full_block_use_the_half_block_glyphs() {
assert_eq!(quadrants(true, true, false, false).glyph(), '▀');
assert_eq!(quadrants(false, false, true, true).glyph(), '▄');
assert_eq!(quadrants(true, false, true, false).glyph(), '▌');
assert_eq!(quadrants(false, true, false, true).glyph(), '▐');
assert_eq!(quadrants(true, true, true, true).glyph(), '█');
assert_eq!(quadrants(false, false, false, false).glyph(), ' ');
}
#[test]
fn only_split_cells_need_a_background() {
assert!(!quadrants(true, true, true, true).needs_background());
assert!(!quadrants(false, false, false, false).needs_background());
assert!(quadrants(true, true, false, false).needs_background());
assert!(quadrants(true, false, false, false).needs_background());
}
#[test]
fn the_field_is_two_samples_per_cell_on_both_axes() {
let mask = QuadrantMask::new(10, 5);
assert_eq!(mask.width(), 10);
assert_eq!(mask.height(), 5);
assert_eq!(mask.sample_width(), 20);
assert_eq!(mask.sample_height(), 10);
}
#[test]
fn one_sample_lights_exactly_one_quadrant() {
for (x, y, expected) in
[(0usize, 0usize, '▛'), (1, 0, '▜'), (0, 1, '▙'), (1, 1, '▟')]
{
let mut mask = QuadrantMask::new(4, 4);
mask.set(x, y, true);
assert_eq!(mask.cell(0, 0).glyph(), expected, "sample ({x},{y})");
assert_eq!(mask.count(), 1);
}
}
#[test]
fn out_of_range_samples_are_dropped_rather_than_clamped() {
let mut mask = QuadrantMask::new(4, 4);
mask.set(99, 99, true);
assert_eq!(
mask.count(),
0,
"an out-of-range write leaked into the mask"
);
assert!(!mask.get(99, 99));
}
#[test]
fn a_solid_region_renders_as_one_full_block_glyph() {
let mut mask = QuadrantMask::new(6, 4);
for y in 0..mask.sample_height() {
for x in 0..mask.sample_width() {
mask.set(x, y, true);
}
}
for cell_y in 0..4 {
for cell_x in 0..6 {
assert_eq!(
mask.cell(cell_x, cell_y).glyph(),
FULL,
"cell ({cell_x},{cell_y}) of a solid region is not a full block"
);
}
}
}
#[test]
fn writing_lands_in_the_right_cells() {
let mut mask = QuadrantMask::new(4, 2);
mask.set(0, 0, true); let mut canvas = Canvas::new(4, 2);
let ink = Color::Rgb {
r: 10,
g: 20,
b: 30,
};
let black = Color::Rgb { r: 0, g: 0, b: 0 };
mask.write_to(&mut canvas, ink, black, Attribute::Reset);
assert_eq!(canvas.get(0, 0).symbol, '▛');
assert_eq!(canvas.get(0, 0).color, ink);
assert_eq!(canvas.get(1, 0).symbol, ' ');
assert_eq!(canvas.get(0, 1).symbol, ' ');
}
#[test]
fn writing_is_clipped_to_the_canvas() {
let mut mask = QuadrantMask::new(10, 6);
for y in 0..mask.sample_height() {
for x in 0..mask.sample_width() {
mask.set(x, y, true);
}
}
let mut canvas = Canvas::new(4, 3);
mask.write_to(&mut canvas, Color::White, Color::Black, Attribute::Reset);
for y in 0..3 {
for x in 0..4 {
assert_eq!(canvas.get(x, y).symbol, FULL, "cell ({x},{y})");
}
}
}
}