1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, Default)]
pub enum PixelSize {
    #[default]
    /// A pixel from the 8x8 font is represented by a full character cell in the terminal.
    Full,
    /// A pixel from the 8x8 font is represented by a half (upper/lower) character cell in the
    /// terminal.
    HalfHeight,
    /// A pixel from the 8x8 font is represented by a half (left/right) character cell in the
    /// terminal.
    HalfWidth,
    /// A pixel from the 8x8 font is represented by a quadrant of a character cell in the
    /// terminal.
    Quadrant,
    /// A pixel from the 8x8 font is represented by a third (top/middle/bottom) of a character
    /// cell in the terminal.  
    /// *Note: depending on how the used terminal renders characters, the generated text with
    /// this PixelSize might look very strange.*
    ThirdHeight,
    /// A pixel from the 8x8 font is represented by a sextant of a character cell in the
    /// terminal.  
    /// *Note: depending on how the used terminal renders characters, the generated text with
    /// this PixelSize might look very strange.*
    Sextant,
}

impl PixelSize {
    /// The number of pixels that can be displayed in a single character cell for the given
    /// pixel size.
    ///
    /// The first value is the number of pixels in the horizontal direction, the second value is
    /// the number of pixels in the vertical direction.
    pub(crate) fn pixels_per_cell(self) -> (u16, u16) {
        match self {
            PixelSize::Full => (1, 1),
            PixelSize::HalfHeight => (1, 2),
            PixelSize::HalfWidth => (2, 1),
            PixelSize::Quadrant => (2, 2),
            PixelSize::ThirdHeight => (1, 3),
            PixelSize::Sextant => (2, 3),
        }
    }

    /// Get a symbol/char that represents the pixels at the given position with the given pixel size
    pub(crate) fn symbol_for_position(self, glyph: &[u8; 8], row: usize, col: i32) -> char {
        match self {
            PixelSize::Full => match glyph[row] & (1 << col) {
                0 => ' ',
                _ => '█',
            },
            PixelSize::HalfHeight => {
                let top = glyph[row] & (1 << col);
                let bottom = glyph[row + 1] & (1 << col);
                get_symbol_half_height(top, bottom)
            }
            PixelSize::HalfWidth => {
                let left = glyph[row] & (1 << col);
                let right = glyph[row] & (1 << (col + 1));
                get_symbol_half_width(left, right)
            }
            PixelSize::Quadrant => {
                let top_left = glyph[row] & (1 << col);
                let top_right = glyph[row] & (1 << (col + 1));
                let bottom_left = glyph[row + 1] & (1 << col);
                let bottom_right = glyph[row + 1] & (1 << (col + 1));
                get_symbol_quadrant_size(top_left, top_right, bottom_left, bottom_right)
            }
            PixelSize::ThirdHeight => {
                let top = glyph[row] & (1 << col);
                let is_middle_available = (row + 1) < glyph.len();
                let middle = if is_middle_available {
                    glyph[row + 1] & (1 << col)
                } else {
                    0
                };
                let is_bottom_available = (row + 2) < glyph.len();
                let bottom = if is_bottom_available {
                    glyph[row + 2] & (1 << col)
                } else {
                    0
                };
                get_symbol_third_height(top, middle, bottom)
            }
            PixelSize::Sextant => {
                let top_left = glyph[row] & (1 << col);
                let top_right = glyph[row] & (1 << (col + 1));
                let is_middle_available = (row + 1) < glyph.len();
                let (middle_left, middle_right) = if is_middle_available {
                    (
                        glyph[row + 1] & (1 << col),
                        glyph[row + 1] & (1 << (col + 1)),
                    )
                } else {
                    (0, 0)
                };
                let is_bottom_available = (row + 2) < glyph.len();
                let (bottom_left, bottom_right) = if is_bottom_available {
                    (
                        glyph[row + 2] & (1 << col),
                        glyph[row + 2] & (1 << (col + 1)),
                    )
                } else {
                    (0, 0)
                };
                get_symbol_sextant_size(
                    top_left,
                    top_right,
                    middle_left,
                    middle_right,
                    bottom_left,
                    bottom_right,
                )
            }
        }
    }
}

/// Get the correct unicode symbol for two vertical "pixels"
fn get_symbol_half_height(top: u8, bottom: u8) -> char {
    match top {
        0 => match bottom {
            0 => ' ',
            _ => '▄',
        },
        _ => match bottom {
            0 => '▀',
            _ => '█',
        },
    }
}

/// Get the correct unicode symbol for two horizontal "pixels"
fn get_symbol_half_width(left: u8, right: u8) -> char {
    match left {
        0 => match right {
            0 => ' ',
            _ => '▐',
        },
        _ => match right {
            0 => '▌',
            _ => '█',
        },
    }
}

/// Get the correct unicode symbol for 2x2 "pixels"
fn get_symbol_quadrant_size(
    top_left: u8,
    top_right: u8,
    bottom_left: u8,
    bottom_right: u8,
) -> char {
    let top_left = if top_left > 0 { 1 } else { 0 };
    let top_right = if top_right > 0 { 1 } else { 0 };
    let bottom_left = if bottom_left > 0 { 1 } else { 0 };
    let bottom_right = if bottom_right > 0 { 1 } else { 0 };

    // We use an array here instead of directlu indexing into the unicode symbols, because although
    // most symbols are in order in unicode, some of them are already part of another character set
    // and missing in this character set.
    const QUADRANT_SYMBOLS: [char; 16] = [
        ' ', '▘', '▝', '▀', '▖', '▌', '▞', '▛', '▗', '▚', '▐', '▜', '▄', '▙', '▟', '█',
    ];
    let character_index = top_left + (top_right << 1) + (bottom_left << 2) + (bottom_right << 3);

    QUADRANT_SYMBOLS[character_index]
}

/// Get the correct unicode symbol for 1x3 "pixels"
fn get_symbol_third_height(top: u8, middle: u8, bottom: u8) -> char {
    get_symbol_sextant_size(top, top, middle, middle, bottom, bottom)
}

/// Get the correct unicode symbol for 2x3 "pixels"
fn get_symbol_sextant_size(
    top_left: u8,
    top_right: u8,
    middle_left: u8,
    middle_right: u8,
    bottom_left: u8,
    bottom_right: u8,
) -> char {
    let top_left = if top_left > 0 { 1 } else { 0 };
    let top_right = if top_right > 0 { 1 } else { 0 };
    let middle_left = if middle_left > 0 { 1 } else { 0 };
    let middle_right = if middle_right > 0 { 1 } else { 0 };
    let bottom_left = if bottom_left > 0 { 1 } else { 0 };
    let bottom_right = if bottom_right > 0 { 1 } else { 0 };

    // We use an array here instead of directlu indexing into the unicode symbols, because although
    // most symbols are in order in unicode, some of them are already part of another character set
    // and missing in this character set.
    const SEXANT_SYMBOLS: [char; 64] = [
        ' ', '🬀', '🬁', '🬂', '🬃', '🬄', '🬅', '🬆', '🬇', '🬈', '🬉', '🬊', '🬋', '🬌', '🬍', '🬎', '🬏', '🬐',
        '🬑', '🬒', '🬓', '▌', '🬔', '🬕', '🬖', '🬗', '🬘', '🬙', '🬚', '🬛', '🬜', '🬝', '🬞', '🬟', '🬠', '🬡',
        '🬢', '🬣', '🬤', '🬥', '🬦', '🬧', '▐', '🬨', '🬩', '🬪', '🬫', '🬬', '🬭', '🬮', '🬯', '🬰', '🬱', '🬲',
        '🬳', '🬴', '🬵', '🬶', '🬷', '🬸', '🬹', '🬺', '🬻', '█',
    ];
    let character_index = top_left
        + (top_right << 1)
        + (middle_left << 2)
        + (middle_right << 3)
        + (bottom_left << 4)
        + (bottom_right << 5);

    SEXANT_SYMBOLS[character_index]
}

#[cfg(test)]
mod tests {
    use super::*;

    type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;

    #[test]
    fn check_quadrant_size_symbols() -> Result<()> {
        assert_eq!(get_symbol_quadrant_size(0, 0, 0, 0), ' ');
        assert_eq!(get_symbol_quadrant_size(1, 0, 0, 0), '▘');
        assert_eq!(get_symbol_quadrant_size(0, 1, 0, 0), '▝');
        assert_eq!(get_symbol_quadrant_size(1, 1, 0, 0), '▀');
        assert_eq!(get_symbol_quadrant_size(0, 0, 1, 0), '▖');
        assert_eq!(get_symbol_quadrant_size(1, 0, 1, 0), '▌');
        assert_eq!(get_symbol_quadrant_size(0, 1, 1, 0), '▞');
        assert_eq!(get_symbol_quadrant_size(1, 1, 1, 0), '▛');
        assert_eq!(get_symbol_quadrant_size(0, 0, 0, 1), '▗');
        assert_eq!(get_symbol_quadrant_size(1, 0, 0, 1), '▚');
        assert_eq!(get_symbol_quadrant_size(0, 1, 0, 1), '▐');
        assert_eq!(get_symbol_quadrant_size(1, 1, 0, 1), '▜');
        assert_eq!(get_symbol_quadrant_size(0, 0, 1, 1), '▄');
        assert_eq!(get_symbol_quadrant_size(1, 0, 1, 1), '▙');
        assert_eq!(get_symbol_quadrant_size(0, 1, 1, 1), '▟');
        assert_eq!(get_symbol_quadrant_size(1, 1, 1, 1), '█');
        Ok(())
    }

    #[test]
    fn check_sextant_size_symbols() -> Result<()> {
        assert_eq!(get_symbol_sextant_size(0, 0, 0, 0, 0, 0), ' ');
        assert_eq!(get_symbol_sextant_size(1, 0, 0, 0, 0, 0), '🬀');
        assert_eq!(get_symbol_sextant_size(0, 1, 0, 0, 0, 0), '🬁');
        assert_eq!(get_symbol_sextant_size(1, 1, 0, 0, 0, 0), '🬂');
        assert_eq!(get_symbol_sextant_size(0, 0, 1, 0, 0, 0), '🬃');
        assert_eq!(get_symbol_sextant_size(1, 0, 1, 0, 0, 0), '🬄');
        assert_eq!(get_symbol_sextant_size(0, 1, 1, 0, 0, 0), '🬅');
        assert_eq!(get_symbol_sextant_size(1, 1, 1, 0, 0, 0), '🬆');
        assert_eq!(get_symbol_sextant_size(0, 0, 0, 1, 0, 0), '🬇');
        assert_eq!(get_symbol_sextant_size(1, 0, 0, 1, 0, 0), '🬈');
        assert_eq!(get_symbol_sextant_size(0, 1, 0, 1, 0, 0), '🬉');
        assert_eq!(get_symbol_sextant_size(1, 1, 0, 1, 0, 0), '🬊');
        assert_eq!(get_symbol_sextant_size(0, 0, 1, 1, 0, 0), '🬋');
        assert_eq!(get_symbol_sextant_size(1, 0, 1, 1, 0, 0), '🬌');
        assert_eq!(get_symbol_sextant_size(0, 1, 1, 1, 0, 0), '🬍');
        assert_eq!(get_symbol_sextant_size(1, 1, 1, 1, 0, 0), '🬎');
        assert_eq!(get_symbol_sextant_size(0, 0, 0, 0, 1, 0), '🬏');
        assert_eq!(get_symbol_sextant_size(1, 0, 0, 0, 1, 0), '🬐');
        assert_eq!(get_symbol_sextant_size(0, 1, 0, 0, 1, 0), '🬑');
        assert_eq!(get_symbol_sextant_size(1, 1, 0, 0, 1, 0), '🬒');
        assert_eq!(get_symbol_sextant_size(0, 0, 1, 0, 1, 0), '🬓');
        assert_eq!(get_symbol_sextant_size(1, 0, 1, 0, 1, 0), '▌');
        assert_eq!(get_symbol_sextant_size(0, 1, 1, 0, 1, 0), '🬔');
        assert_eq!(get_symbol_sextant_size(1, 1, 1, 0, 1, 0), '🬕');
        assert_eq!(get_symbol_sextant_size(0, 0, 0, 1, 1, 0), '🬖');
        assert_eq!(get_symbol_sextant_size(1, 0, 0, 1, 1, 0), '🬗');
        assert_eq!(get_symbol_sextant_size(0, 1, 0, 1, 1, 0), '🬘');
        assert_eq!(get_symbol_sextant_size(1, 1, 0, 1, 1, 0), '🬙');
        assert_eq!(get_symbol_sextant_size(0, 0, 1, 1, 1, 0), '🬚');
        assert_eq!(get_symbol_sextant_size(1, 0, 1, 1, 1, 0), '🬛');
        assert_eq!(get_symbol_sextant_size(0, 1, 1, 1, 1, 0), '🬜');
        assert_eq!(get_symbol_sextant_size(1, 1, 1, 1, 1, 0), '🬝');
        assert_eq!(get_symbol_sextant_size(0, 0, 0, 0, 0, 1), '🬞');
        assert_eq!(get_symbol_sextant_size(1, 0, 0, 0, 0, 1), '🬟');
        assert_eq!(get_symbol_sextant_size(0, 1, 0, 0, 0, 1), '🬠');
        assert_eq!(get_symbol_sextant_size(1, 1, 0, 0, 0, 1), '🬡');
        assert_eq!(get_symbol_sextant_size(0, 0, 1, 0, 0, 1), '🬢');
        assert_eq!(get_symbol_sextant_size(1, 0, 1, 0, 0, 1), '🬣');
        assert_eq!(get_symbol_sextant_size(0, 1, 1, 0, 0, 1), '🬤');
        assert_eq!(get_symbol_sextant_size(1, 1, 1, 0, 0, 1), '🬥');
        assert_eq!(get_symbol_sextant_size(0, 0, 0, 1, 0, 1), '🬦');
        assert_eq!(get_symbol_sextant_size(1, 0, 0, 1, 0, 1), '🬧');
        assert_eq!(get_symbol_sextant_size(0, 1, 0, 1, 0, 1), '▐');
        assert_eq!(get_symbol_sextant_size(1, 1, 0, 1, 0, 1), '🬨');
        assert_eq!(get_symbol_sextant_size(0, 0, 1, 1, 0, 1), '🬩');
        assert_eq!(get_symbol_sextant_size(1, 0, 1, 1, 0, 1), '🬪');
        assert_eq!(get_symbol_sextant_size(0, 1, 1, 1, 0, 1), '🬫');
        assert_eq!(get_symbol_sextant_size(1, 1, 1, 1, 0, 1), '🬬');
        assert_eq!(get_symbol_sextant_size(0, 0, 0, 0, 1, 1), '🬭');
        assert_eq!(get_symbol_sextant_size(1, 0, 0, 0, 1, 1), '🬮');
        assert_eq!(get_symbol_sextant_size(0, 1, 0, 0, 1, 1), '🬯');
        assert_eq!(get_symbol_sextant_size(1, 1, 0, 0, 1, 1), '🬰');
        assert_eq!(get_symbol_sextant_size(0, 0, 1, 0, 1, 1), '🬱');
        assert_eq!(get_symbol_sextant_size(1, 0, 1, 0, 1, 1), '🬲');
        assert_eq!(get_symbol_sextant_size(0, 1, 1, 0, 1, 1), '🬳');
        assert_eq!(get_symbol_sextant_size(1, 1, 1, 0, 1, 1), '🬴');
        assert_eq!(get_symbol_sextant_size(0, 0, 0, 1, 1, 1), '🬵');
        assert_eq!(get_symbol_sextant_size(1, 0, 0, 1, 1, 1), '🬶');
        assert_eq!(get_symbol_sextant_size(0, 1, 0, 1, 1, 1), '🬷');
        assert_eq!(get_symbol_sextant_size(1, 1, 0, 1, 1, 1), '🬸');
        assert_eq!(get_symbol_sextant_size(0, 0, 1, 1, 1, 1), '🬹');
        assert_eq!(get_symbol_sextant_size(1, 0, 1, 1, 1, 1), '🬺');
        assert_eq!(get_symbol_sextant_size(0, 1, 1, 1, 1, 1), '🬻');
        assert_eq!(get_symbol_sextant_size(1, 1, 1, 1, 1, 1), '█');
        Ok(())
    }

    #[test]
    fn check_third_height_symbols() -> Result<()> {
        assert_eq!(get_symbol_third_height(0, 0, 0), ' ');
        assert_eq!(get_symbol_third_height(1, 0, 0), '🬂');
        assert_eq!(get_symbol_third_height(0, 1, 0), '🬋');
        assert_eq!(get_symbol_third_height(1, 1, 0), '🬎');
        assert_eq!(get_symbol_third_height(0, 0, 1), '🬭');
        assert_eq!(get_symbol_third_height(1, 0, 1), '🬰');
        assert_eq!(get_symbol_third_height(0, 1, 1), '🬹');
        assert_eq!(get_symbol_third_height(1, 1, 1), '█');
        Ok(())
    }

    #[test]
    fn check_get_symbol_for_position_in_glyph_third_height_defensive_middle() -> Result<()> {
        // In this test, we set all pixels of the glyph to 1 (all bytes are u8-max)
        // We expect that pixels out of the glyph-bounds are not set
        // Returned character is upper third filled only

        let glyph = [0xFFu8; 8];
        assert_eq!(
            PixelSize::ThirdHeight.symbol_for_position(&glyph, 7, 0),
            '🬂'
        );
        Ok(())
    }

    #[test]
    fn check_get_symbol_for_position_in_glyph_sextant_size_defensive_middle() -> Result<()> {
        // In this test, we set all pixels of the glyph to 1 (all bytes are u8-max)
        // We expect that pixels out of the glyph-bounds are not set
        // Returned character is upper third filled only

        let glyph = [0xFFu8; 8];
        assert_eq!(PixelSize::Sextant.symbol_for_position(&glyph, 7, 0), '🬂');
        Ok(())
    }
}