FIXED THE APIC HELL YEAH

This commit is contained in:
2025-03-04 12:44:42 +00:00
parent 2186b829aa
commit 8704b5d249
5 changed files with 59 additions and 77 deletions
+31 -46
View File
@@ -40,13 +40,13 @@ pub fn include_font(item: TokenStream) -> TokenStream {
filename.value()
);
let font_bytes = match load_file(file_path) {
let font_bytes = match std::fs::read(file_path) {
Ok(bytes) => bytes,
Err(why) => panic!("{}", why),
};
let font_data = match Font::new(font_bytes) {
Ok(font) => font.0,
let font_data = match FontBuilder::load(font_bytes) {
Ok(font) => font.data,
Err(why) => panic!("{}", why),
};
@@ -66,40 +66,39 @@ struct FontData {
width: u8,
height: u8,
length: u16,
data: Vec<Glyph>,
unicode_table: [[u8; 2]; 512],
pub data: [[u8; 16]; 512],
}
struct Glyph {
bytes: Vec<u8>,
}
enum FontBuilder {
Psf1,
Psf2,
Psf1(FontData),
Psf2(FontData),
}
impl FontBuilder {
const PSF1_MAGIC: u16 = 0x3604;
const PSF2_MAGIC: u32 = 0x72b54a86;
pub fn load() -> Option<Self> {
None
fn revision(data: &[u8]) -> u8 {
if (data[0] as u16) << 8 | data[1] as u16 == Self::PSF1_MAGIC {
1
} else if (data[0] as u32) << 24 | (data[1] as u32) << 16 | (data[2] as u32) << 8 | data[3] as u32 == Self::PSF2_MAGIC {
2
} else {
0
}
}
}
impl Font {
const MAGIC: u16 = 0x3604;
pub fn load(data: Vec<u8>) -> Result<FontData, &'static str> {
match Self::revision(&data[0..4]) {
1 => Self::parse_psf1(&data),
2 => Self::parse_psf2(&data),
_ => panic!("invalid font revision result"),
}
}
pub fn new(data: [u8; (32 + 2) * 512 + 4]) -> Result<Self, &'static str> {
let magic: u16 = (data[0] as u16) << 8 | data[1] as u16;
fn parse_psf1(data: &[u8]) -> Result<FontData, &'static str> {
let mode = data[2];
let size = data[3];
if magic != Self::MAGIC {
return Err("Magic value is invalid!");
}
let has_512_glyphs = (mode & 0x01) != 0;
let mut glyphs = [[0; 16]; 512];
let glyph_count = if has_512_glyphs { 512 } else { 256 };
@@ -112,29 +111,15 @@ impl Font {
glyphs[i] = buff;
}
Ok(Self(glyphs))
}
}
type FileContents = [u8; (32 + 2) * 512 + 4];
fn load_file(filename: String) -> Result<FileContents, std::io::Error> {
let mut buf = [0; (32 + 2) * 512 + 4];
let mut f = File::open(filename).unwrap();
f.seek(SeekFrom::Start(0)).unwrap();
loop {
match f.read(&mut buf) {
Ok(read) => {
if read == 0 {
break;
}
}
Err(why) => {
eprintln!("Failed to read PS1 font file: {}", why);
return Err(why);
}
}
Ok(FontData {
width: 8,
height: size,
length: glyph_count,
data: glyphs,
})
}
Ok(buf)
fn parse_psf2(data: &[u8]) -> Result<FontData, &'static str> {
Err("PSF2 support is not implemented yet!")
}
}