Microkernel thing OS experiment (Zig ⚡)
1const std = @import("std"); 2 3pub const Psf2Header = extern struct { 4 magic: u32 = 0x864ab572, 5 version: u32, 6 header_size: u32, 7 flags: u32, 8 numglyph: u32, 9 bytes_per_glyph: u32, 10 height: u32, 11 width: u32, 12 13 pub fn bytesPerLine(self: *const Psf2Header) u32 { 14 return (self.width + 7) / 8; 15 } 16}; 17 18pub const Font = struct { 19 const Self = @This(); 20 fontdata: []const u8 align(4), 21 pub fn new(fontdata: []const u8) Self { 22 return .{ 23 .fontdata = fontdata, 24 }; 25 } 26 27 pub fn getHdr(self: *const Self) *const Psf2Header { 28 return @ptrCast(@alignCast(self.fontdata)); 29 } 30 31 pub fn getGlyph(self: *const Self, ch: u8) ![]const u8 { 32 const hdr = self.getHdr(); 33 const startpos: u64 = @as(u64, hdr.header_size) + @as(u64, ch) * @as(u64, hdr.bytes_per_glyph); 34 35 if (self.fontdata.len < startpos + @as(u64, hdr.bytes_per_glyph)) return error.InvalidCharacter; 36 37 return self.fontdata[startpos..][0..hdr.bytes_per_glyph]; 38 } 39};