clock_tui/clock_text/font/
mod.rs

1pub mod bricks;
2
3use ratatui::{buffer::Buffer, layout::Rect, style::Style};
4
5use super::point::Point;
6
7pub trait Font {
8    fn get_char(&self, c: char) -> Option<&[Point]>;
9    fn get_char_width(&self) -> u16;
10    fn get_char_height(&self) -> u16;
11
12    fn draw_char(&self, c: char, x: u16, y: u16, style: Style, buf: &mut Buffer) {
13        if let Some(points) = self.get_char(c) {
14            for point in points {
15                let x = x + point.0;
16                let y = y + point.1;
17                if x < buf.area.right() && y < buf.area.bottom() {
18                    buf.get_mut(x, y).set_style(style);
19                }
20            }
21        }
22    }
23
24    fn draw_str(&self, s: &str, area: Rect, style: Style, buf: &mut Buffer) {
25        let mut x = area.x;
26        let y = area.y;
27        let spacing = 2;
28        for c in s.chars() {
29            if x + self.get_char_width() > area.right() {
30                break;
31            }
32            self.draw_char(c, x, y, style, buf);
33            x += self.get_char_width() + spacing;
34        }
35    }
36}