Skip to main content

strat9_kernel/arch/x86_64/vga/
font.rs

1/*
2 * This file is part of strat9-os.
3 *
4 * strat9-os is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * strat9-os is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with strat9-os. If not, see <https://www.gnu.org/licenses/>.
16 *
17 * Website: https://www.strat9-os.org
18 * Repository: https://github.com/strat9-os/strat9-os
19 *
20 * Copyright (C) 2023-26 Guillaume Gielly
21 */
22
23//! PSF (PC Screen Font) font parsing.
24//!
25//! Supports PSF1 and PSF2 font formats including unicode map decoding.
26
27use 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    // PSF1
42    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    // PSF2
58    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
88/// Performs the decode utf8 at operation.
89fn 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
127/// Parses psf2 unicode map.
128pub(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                // PSF2 sequence marker; skip marker and continue parsing bytes until glyph separator.
146                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}