1use crate::BoundingBox;
2use bit_vec::BitVec;
3use thiserror::Error;
4
5#[derive(Debug, Error)]
6#[error("Index ({0}, {1}) Out Of Bounds")]
7#[non_exhaustive]
8pub struct OutOfBounds(usize, usize);
9
10#[derive(Clone, Copy, Debug)]
11pub struct Bitmap<'a> {
12 pub(crate) data: &'a Vec<BitVec>,
13 pub(crate) bbox: BoundingBox
14}
15
16impl Bitmap<'_> {
17 pub fn width(self) -> usize {
18 self.bbox.width as usize
19 }
20
21 pub fn height(self) -> usize {
22 self.bbox.height as usize
23 }
24
25 pub fn baseline(self) -> usize {
26 (self.bbox.height as i32 - 1 + self.bbox.offset_y) as usize
27 }
28
29 pub fn get(self, x: usize, y: usize) -> Result<bool, OutOfBounds> {
30 let row = self.data.get(y).ok_or(OutOfBounds(x, y))?;
31 row.get(x).ok_or(OutOfBounds(x, y))
32 }
33
34 pub fn ascii_art(self) -> String {
35 let mut buf = String::new();
36 for y in 0 .. self.height() {
37 for x in 0 .. self.width() {
38 if self.get(x, y).unwrap() {
39 buf += "##";
40 } else {
41 buf += "..";
42 }
43 }
44 buf += "\n";
45
46 if y == self.baseline() {
47 for _ in 0 .. self.width() {
48 buf += "--";
49 }
50 buf += "\n";
51 }
52 }
53 buf
54 }
55}