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
use alloc::collections::btree_map::BTreeMap;
use core::mem::swap;

use crate::cell::{Cell, Flags};
use crate::color::Rgb888;
use crate::config::CONFIG;
use crate::font::{FontWeight, Rasterized};

pub trait DrawTarget {
    fn size(&self) -> (usize, usize);
    fn draw_pixel(&mut self, x: usize, y: usize, color: Rgb888);
}

type FgBgPair = (Rgb888, Rgb888);

pub struct TextOnGraphic<D: DrawTarget> {
    graphic: D,
    color_cache: BTreeMap<FgBgPair, ColorCache>,
}

impl<D: DrawTarget> TextOnGraphic<D> {
    #[inline]
    pub fn width(&self) -> usize {
        self.graphic.size().0
    }

    #[inline]
    pub fn height(&self) -> usize {
        self.graphic.size().1
    }
}

impl<D: DrawTarget> TextOnGraphic<D> {
    pub fn new(graphic: D) -> Self {
        Self {
            graphic,
            color_cache: BTreeMap::new(),
        }
    }

    pub fn clear(&mut self, cell: Cell) {
        let (width, height) = self.graphic.size();
        for y in 0..height {
            for x in 0..width {
                self.graphic.draw_pixel(x, y, cell.background.to_rgb());
            }
        }
    }

    pub fn write(&mut self, row: usize, col: usize, cell: Cell) {
        let mut foreground = cell.foreground.to_rgb();
        let mut background = cell.background.to_rgb();

        if cell.flags.contains(Flags::INVERSE) || cell.flags.contains(Flags::CURSOR_BLOCK) {
            swap(&mut foreground, &mut background);
        }

        if cell.flags.contains(Flags::HIDDEN) {
            foreground = background;
        }

        let color_cache = self
            .color_cache
            .entry((foreground, background))
            .or_insert_with(|| ColorCache::new(foreground, background));

        if let Some(font_manager) = CONFIG.lock().font_manager.as_mut() {
            let (font_width, font_height) = font_manager.size();
            let (x_start, y_start) = (col * font_width, row * font_height);

            let font_weight = if cell.flags.contains(Flags::BOLD) {
                FontWeight::Bold
            } else {
                FontWeight::Regular
            };

            macro_rules! draw_raster {
                ($raster:ident) => {
                    for (y, lines) in $raster.iter().enumerate() {
                        for (x, &intensity) in lines.iter().enumerate() {
                            let (r, g, b) = color_cache.colors[intensity as usize];
                            self.graphic.draw_pixel(x_start + x, y_start + y, (r, g, b));
                        }
                    }
                };
            }

            match font_manager.rasterize(cell.content, font_weight) {
                Rasterized::Borrowed(raster) => draw_raster!(raster),
                Rasterized::Owned(raster) => draw_raster!(raster),
            }

            if cell.flags.contains(Flags::CURSOR_BEAM) {
                for y in 0..font_height as usize {
                    let (r, g, b) = color_cache.colors[0xff as usize];
                    self.graphic.draw_pixel(x_start, y_start + y, (r, g, b));
                }
            }

            if cell.flags.contains(Flags::UNDERLINE) || cell.flags.contains(Flags::CURSOR_UNDERLINE)
            {
                for x in 0..font_width {
                    let (r, g, b) = color_cache.colors[0xff as usize];
                    self.graphic.draw_pixel(
                        x_start + x,
                        y_start + font_height as usize - 1,
                        (r, g, b),
                    );
                }
            }
        }
    }
}

struct ColorCache {
    colors: [Rgb888; 256],
}

impl ColorCache {
    fn new(foreground: Rgb888, background: Rgb888) -> Self {
        let r_diff = foreground.0 as i32 - background.0 as i32;
        let g_diff = foreground.1 as i32 - background.1 as i32;
        let b_diff = foreground.2 as i32 - background.2 as i32;

        let mut colors = [(0u8, 0u8, 0u8); 256];

        for intensity in 0..256 {
            let weight = intensity as i32;
            let r = ((background.0 as i32 + (r_diff * weight / 0xff)).clamp(0, 255)) as u8;
            let g = ((background.1 as i32 + (g_diff * weight / 0xff)).clamp(0, 255)) as u8;
            let b = ((background.2 as i32 + (b_diff * weight / 0xff)).clamp(0, 255)) as u8;
            colors[intensity] = (r, g, b);
        }

        ColorCache { colors }
    }
}