strat9_kernel/arch/x86_64/vga/
font.rs1use alloc::vec::Vec;
28
29pub(crate) const FONT_PSF: &[u8] = include_bytes!("../fonts/zap-ext-light20.psf");
30
31pub(crate) struct FontInfo {
32 pub(crate) glyph_count: usize,
33 pub(crate) bytes_per_glyph: usize,
34 pub(crate) glyph_w: usize,
35 pub(crate) glyph_h: usize,
36 pub(crate) data_offset: usize,
37 pub(crate) unicode_table_offset: Option<usize>,
38}
39
40pub(crate) fn parse_psf(font: &[u8]) -> Option<FontInfo> {
41 if font.len() >= 4 && font[0] == 0x36 && font[1] == 0x04 {
43 let mode = font[2];
44 let glyph_count = if (mode & 0x01) != 0 { 512 } else { 256 };
45 let glyph_h = font[3] as usize;
46 let bytes_per_glyph = glyph_h;
47 return Some(FontInfo {
48 glyph_count,
49 bytes_per_glyph,
50 glyph_w: 8,
51 glyph_h,
52 data_offset: 4,
53 unicode_table_offset: None,
54 });
55 }
56
57 if font.len() >= 32 && font[0] == 0x72 && font[1] == 0xB5 && font[2] == 0x4A && font[3] == 0x86
59 {
60 let rd_u32 = |off: usize| -> u32 {
61 u32::from_le_bytes([font[off], font[off + 1], font[off + 2], font[off + 3]])
62 };
63 let headersize = rd_u32(8) as usize;
64 let flags = rd_u32(12);
65 let glyph_count = rd_u32(16) as usize;
66 let bytes_per_glyph = rd_u32(20) as usize;
67 let glyph_h = rd_u32(24) as usize;
68 let glyph_w = rd_u32(28) as usize;
69 let glyph_bytes = glyph_count.saturating_mul(bytes_per_glyph);
70 let unicode_table_offset = if (flags & 1) != 0 {
71 Some(headersize.saturating_add(glyph_bytes))
72 } else {
73 None
74 };
75 return Some(FontInfo {
76 glyph_count,
77 bytes_per_glyph,
78 glyph_w,
79 glyph_h,
80 data_offset: headersize,
81 unicode_table_offset,
82 });
83 }
84
85 None
86}
87
88fn decode_utf8_at(bytes: &[u8], pos: usize) -> Option<(u32, usize)> {
90 let b0 = *bytes.get(pos)?;
91 if b0 < 0x80 {
92 return Some((b0 as u32, 1));
93 }
94 if (b0 & 0xE0) == 0xC0 {
95 let b1 = *bytes.get(pos + 1)?;
96 if (b1 & 0xC0) != 0x80 {
97 return None;
98 }
99 let cp = (((b0 & 0x1F) as u32) << 6) | ((b1 & 0x3F) as u32);
100 return Some((cp, 2));
101 }
102 if (b0 & 0xF0) == 0xE0 {
103 let b1 = *bytes.get(pos + 1)?;
104 let b2 = *bytes.get(pos + 2)?;
105 if (b1 & 0xC0) != 0x80 || (b2 & 0xC0) != 0x80 {
106 return None;
107 }
108 let cp = (((b0 & 0x0F) as u32) << 12) | (((b1 & 0x3F) as u32) << 6) | ((b2 & 0x3F) as u32);
109 return Some((cp, 3));
110 }
111 if (b0 & 0xF8) == 0xF0 {
112 let b1 = *bytes.get(pos + 1)?;
113 let b2 = *bytes.get(pos + 2)?;
114 let b3 = *bytes.get(pos + 3)?;
115 if (b1 & 0xC0) != 0x80 || (b2 & 0xC0) != 0x80 || (b3 & 0xC0) != 0x80 {
116 return None;
117 }
118 let cp = (((b0 & 0x07) as u32) << 18)
119 | (((b1 & 0x3F) as u32) << 12)
120 | (((b2 & 0x3F) as u32) << 6)
121 | ((b3 & 0x3F) as u32);
122 return Some((cp, 4));
123 }
124 None
125}
126
127pub(crate) fn parse_psf2_unicode_map(font: &[u8], info: &FontInfo) -> Vec<(u32, usize)> {
129 let Some(mut i) = info.unicode_table_offset else {
130 return Vec::new();
131 };
132 if i >= font.len() {
133 return Vec::new();
134 }
135
136 let mut map = Vec::new();
137 for glyph in 0..info.glyph_count {
138 while i < font.len() {
139 let b = font[i];
140 if b == 0xFF {
141 i += 1;
142 break;
143 }
144 if b == 0xFE {
145 i += 1;
147 continue;
148 }
149 if let Some((cp, adv)) = decode_utf8_at(font, i) {
150 if !map.iter().any(|(u, _)| *u == cp) {
151 map.push((cp, glyph));
152 }
153 i += adv;
154 } else {
155 i += 1;
156 }
157 }
158 }
159
160 map
161}