const std = @import("std"); pub const Psf2Header = extern struct { magic: u32 = 0x864ab572, version: u32, header_size: u32, flags: u32, numglyph: u32, bytes_per_glyph: u32, height: u32, width: u32, pub fn bytesPerLine(self: *const Psf2Header) u32 { return (self.width + 7) / 8; } }; pub const Font = struct { const Self = @This(); fontdata: []const u8 align(4), pub fn new(fontdata: []const u8) Self { return .{ .fontdata = fontdata, }; } pub fn getHdr(self: *const Self) *const Psf2Header { return @ptrCast(@alignCast(self.fontdata)); } pub fn getGlyph(self: *const Self, ch: u8) ![]const u8 { const hdr = self.getHdr(); const startpos: u64 = @as(u64, hdr.header_size) + @as(u64, ch) * @as(u64, hdr.bytes_per_glyph); if (self.fontdata.len < startpos + @as(u64, hdr.bytes_per_glyph)) return error.InvalidCharacter; return self.fontdata[startpos..][0..hdr.bytes_per_glyph]; } };