os_terminal/font/
bitmap.rs

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
use noto_sans_mono_bitmap::{get_raster, get_raster_width};
use noto_sans_mono_bitmap::{FontWeight, RasterHeight};

use super::{ContentInfo, FontManager, Rasterized};

const FONT_WIDTH: usize = get_raster_width(FontWeight::Regular, FONT_HEIGHT);
const FONT_HEIGHT: RasterHeight = RasterHeight::Size20;

pub struct BitmapFont;

impl FontManager for BitmapFont {
    fn size(&self) -> (usize, usize) {
        (FONT_WIDTH, FONT_HEIGHT as usize)
    }

    fn rasterize(&mut self, info: ContentInfo) -> Rasterized {
        let font_weight = if info.bold {
            FontWeight::Bold
        } else {
            FontWeight::Regular
        };

        let char_raster = get_raster(info.content, font_weight, FONT_HEIGHT)
            .unwrap_or_else(|| get_raster('\u{fffd}', font_weight, FONT_HEIGHT).unwrap());

        Rasterized::Slice(char_raster.raster())
    }
}