ableos/sysdata/libraries/render/src/text.hb

75 lines
1.2 KiB
Plaintext
Raw Normal View History

2024-10-26 03:23:28 -05:00
.{log} := @use("../../stn/src/lib.hb")
PSF1Header := packed struct {
magic: u16,
font_mode: u8,
character_size: u8,
}
PSF2Header := packed struct {
magic: u32,
version: u32,
header_size: u32,
flags: u32,
num_glyph: u32,
bytes_per_glyph: u32,
height: u32,
width: u32,
}
Font := struct {
data: ^u8,
width: uint,
height: uint,
num_glyphs: uint,
bytes_per_glyph: uint,
has_unicode_table: bool,
line_gap: uint,
char_gap: uint,
}
2024-11-03 16:31:53 -06:00
font_from_psf1 := fn(psf: ^u8): ?Font {
2024-10-26 03:23:28 -05:00
header := @as(^PSF1Header, @bitcast(psf))
if header.magic != 0x436 {
log.error("failed to load psf font: not a psf1 font, idiot\0")
2024-11-03 16:31:53 -06:00
return null
2024-10-26 03:23:28 -05:00
}
psf += @sizeof(PSF1Header)
return .(
psf,
8,
2024-11-03 16:31:53 -06:00
header.character_size,
2024-10-26 03:23:28 -05:00
256,
2024-11-03 16:31:53 -06:00
header.character_size,
2024-10-26 03:23:28 -05:00
false,
0,
0,
)
}
2024-11-03 16:31:53 -06:00
font_from_psf2 := fn(psf: ^u8): ?Font {
2024-10-26 03:23:28 -05:00
header := @as(^PSF2Header, @bitcast(psf))
if header.magic != 0x864AB572 {
log.error("failed to load psf font: not a psf2 font, idiot\0")
2024-11-03 16:31:53 -06:00
return null
2024-10-26 03:23:28 -05:00
}
psf += header.header_size
return .(
psf,
2024-11-03 16:31:53 -06:00
header.width,
header.height,
header.num_glyph,
header.bytes_per_glyph,
2024-10-26 03:23:28 -05:00
(header.flags & 1) != 0,
0,
0,
)
}
get_glyph := fn(font: Font, index: uint): ^u8 {
return font.data + index * font.bytes_per_glyph
}