Files
RockOS/kernel/lib/font.c

48 lines
1.1 KiB
C
Raw Normal View History

2026-07-16 14:44:08 -05:00
#include "drivers/serial/serial.h"
2026-07-14 17:40:30 -05:00
#include "vfs.h"
#include <lib/font.h>
#include <lib/memory.h>
2026-07-16 14:44:08 -05:00
#include <lib/print.h>
2026-07-14 17:40:30 -05:00
#include <stddef.h>
psf_font_t* load_font(const char* path) {
int fd = vfs_open(path, 0);
if (fd == -1) {
2026-07-16 14:44:08 -05:00
kprintf("failed to open font file %s\n", path);
2026-07-14 17:40:30 -05:00
return NULL;
}
psf_hdr_t* header = kalloc(sizeof(psf_hdr_t));
int rd = vfs_read(fd, header, sizeof(psf_hdr_t));
if (rd <= 0) {
2026-07-16 14:44:08 -05:00
kprintf("failed to read font header\n");
2026-07-14 17:40:30 -05:00
kfree(header);
return NULL;
}
uint32_t num_glyphs = (header->mode & (1 << 0)) ? 512 : 256;
uint32_t glyph_sz = header->charsz;
uint32_t glyph_buf_sz = num_glyphs * glyph_sz;
void* glyphs = kalloc(glyph_buf_sz);
rd = 0;
rd = vfs_read(fd, glyphs, glyph_buf_sz);
if (rd <= 0) {
kfree(header);
kfree(glyphs);
return NULL;
}
psf_font_t* font = kalloc(sizeof(psf_font_t));
font->hdr = header;
font->glyphs = glyphs;
return font;
}
void free_font(psf_font_t* font) {
kfree(font->glyphs);
kfree(font->hdr);
kfree(font);
}