xref: /DragonOS/kernel/src/libs/lib_ui/font/mod.rs (revision 2755467c790d6510fa97cbf052ce8e91ad1372c6)
1abe3a6eaShanjiezhou use super::textui::GlyphMapping;
2abe3a6eaShanjiezhou 
3abe3a6eaShanjiezhou pub mod spleen_font;
4abe3a6eaShanjiezhou 
5abe3a6eaShanjiezhou pub use spleen_font::SPLEEN_FONT_8x16 as FONT_8x16;
6abe3a6eaShanjiezhou 
7abe3a6eaShanjiezhou /// Stores the font bitmap and some additional info for each font.
8abe3a6eaShanjiezhou #[derive(Clone, Copy)]
9abe3a6eaShanjiezhou pub struct BitmapFont<'a> {
10abe3a6eaShanjiezhou     /// The raw bitmap data for the font.
11abe3a6eaShanjiezhou     bitmap: RawBitMap<'a>,
12abe3a6eaShanjiezhou 
13abe3a6eaShanjiezhou     /// The char to glyph mapping.
14abe3a6eaShanjiezhou     glyph_mapping: &'a dyn GlyphMapping,
15abe3a6eaShanjiezhou 
16abe3a6eaShanjiezhou     /// The size of each character in the raw bitmap data.
17abe3a6eaShanjiezhou     size: Size,
18abe3a6eaShanjiezhou 
19abe3a6eaShanjiezhou     bytes_per_char: usize,
20abe3a6eaShanjiezhou }
21abe3a6eaShanjiezhou 
22abe3a6eaShanjiezhou #[allow(dead_code)]
23abe3a6eaShanjiezhou impl<'a> BitmapFont<'a> {
new( bitmap: RawBitMap<'a>, glyph_mapping: &'a dyn GlyphMapping, size: Size, ) -> Self24abe3a6eaShanjiezhou     pub const fn new(
25abe3a6eaShanjiezhou         bitmap: RawBitMap<'a>,
26abe3a6eaShanjiezhou         glyph_mapping: &'a dyn GlyphMapping,
27abe3a6eaShanjiezhou         size: Size,
28abe3a6eaShanjiezhou     ) -> Self {
29abe3a6eaShanjiezhou         Self {
30abe3a6eaShanjiezhou             bitmap,
31abe3a6eaShanjiezhou             glyph_mapping,
32abe3a6eaShanjiezhou             size,
33abe3a6eaShanjiezhou             bytes_per_char: (size.width + 7) / 8 * size.height,
34abe3a6eaShanjiezhou         }
35abe3a6eaShanjiezhou     }
36abe3a6eaShanjiezhou     /// Return the width of each character.
width(&self) -> u3237abe3a6eaShanjiezhou     pub const fn width(&self) -> u32 {
38abe3a6eaShanjiezhou         self.size.width as u32
39abe3a6eaShanjiezhou     }
40abe3a6eaShanjiezhou 
41abe3a6eaShanjiezhou     /// Return the height of each character.
height(&self) -> u3242abe3a6eaShanjiezhou     pub const fn height(&self) -> u32 {
43abe3a6eaShanjiezhou         self.size.height as u32
44abe3a6eaShanjiezhou     }
45abe3a6eaShanjiezhou 
46abe3a6eaShanjiezhou     #[inline(always)]
char_map(&self, character: char) -> &'a [u8]47abe3a6eaShanjiezhou     pub fn char_map(&self, character: char) -> &'a [u8] {
48*2755467cS曾俊         //获得ASCII的index
49abe3a6eaShanjiezhou         let index = self.glyph_mapping.index(character);
50abe3a6eaShanjiezhou         let pos = index * self.bytes_per_char;
51abe3a6eaShanjiezhou 
52abe3a6eaShanjiezhou         return &self.bitmap.data[pos..pos + self.bytes_per_char];
53abe3a6eaShanjiezhou     }
54abe3a6eaShanjiezhou }
55abe3a6eaShanjiezhou 
56abe3a6eaShanjiezhou #[derive(Clone, Copy)]
57abe3a6eaShanjiezhou pub struct Size {
58abe3a6eaShanjiezhou     pub width: usize,
59abe3a6eaShanjiezhou     pub height: usize,
60abe3a6eaShanjiezhou }
61abe3a6eaShanjiezhou 
62abe3a6eaShanjiezhou impl Size {
new(width: usize, height: usize) -> Self63abe3a6eaShanjiezhou     pub const fn new(width: usize, height: usize) -> Self {
64abe3a6eaShanjiezhou         Self { width, height }
65abe3a6eaShanjiezhou     }
66abe3a6eaShanjiezhou }
67abe3a6eaShanjiezhou 
68abe3a6eaShanjiezhou #[derive(Clone, Copy)]
69abe3a6eaShanjiezhou pub struct RawBitMap<'a> {
70abe3a6eaShanjiezhou     pub data: &'a [u8],
71abe3a6eaShanjiezhou     pub size: Size,
72abe3a6eaShanjiezhou }
73abe3a6eaShanjiezhou 
74abe3a6eaShanjiezhou #[allow(dead_code)]
75abe3a6eaShanjiezhou impl RawBitMap<'_> {
new(data: &'static [u8], width: usize) -> Self76abe3a6eaShanjiezhou     pub const fn new(data: &'static [u8], width: usize) -> Self {
77abe3a6eaShanjiezhou         let size = Size {
78abe3a6eaShanjiezhou             width: 128,
79abe3a6eaShanjiezhou             height: data.len() / width / 8,
80abe3a6eaShanjiezhou         };
81abe3a6eaShanjiezhou         Self { data, size }
82abe3a6eaShanjiezhou     }
83abe3a6eaShanjiezhou 
size(&self) -> Size84abe3a6eaShanjiezhou     pub const fn size(&self) -> Size {
85abe3a6eaShanjiezhou         self.size
86abe3a6eaShanjiezhou     }
87abe3a6eaShanjiezhou 
len(&self) -> usize88abe3a6eaShanjiezhou     pub const fn len(&self) -> usize {
89abe3a6eaShanjiezhou         self.data.len()
90abe3a6eaShanjiezhou     }
91abe3a6eaShanjiezhou }
92