Skip to content

Commit 404741b

Browse files
feat: add font management
1 parent e7fb500 commit 404741b

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

src/phantom/fonts.zig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,11 @@
1+
const std = @import("std");
2+
13
pub const Font = @import("fonts/font.zig");
4+
pub const Manager = @import("fonts/manager.zig");
5+
6+
pub const backends = @import("fonts/backends.zig");
7+
pub const BackendType = std.meta.DeclEnum(backends);
8+
9+
pub fn Backend(comptime T: BackendType) type {
10+
return @field(backends, @tagName(T));
11+
}

src/phantom/fonts/backends.zig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const root = @import("root");
2+
3+
pub usingnamespace if (@hasDecl(root, "phantomOptions")) if (@hasDecl(root.phantomOptions, "fontBackends")) root.phantomOptions.fontBackends else struct {} else struct {};

src/phantom/fonts/manager.zig

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const std = @import("std");
2+
const metap = @import("meta+");
3+
const Font = @import("font.zig");
4+
const Self = @This();
5+
6+
const BaseVTable = struct {
7+
loadBuffer: *const fn (*anyopaque, []const u8) anyerror!*Font,
8+
deinit: ?*const fn (*anyopaque) void = null,
9+
};
10+
11+
const FsVTable = struct {
12+
loadFile: ?*const fn (*anyopaque, std.fs.File) anyerror!*Font = null,
13+
};
14+
15+
pub const VTable = metap.structs.fields.mix(BaseVTable, if (@hasDecl(std.os.system, "fd_t")) FsVTable else struct {});
16+
17+
ptr: *anyopaque,
18+
vtable: *const VTable,
19+
20+
pub inline fn loadBuffer(self: *Self, buff: []const u8) !*Font {
21+
return self.vtable.loadBuffer(self.ptr, buff);
22+
}
23+
24+
pub inline fn loadFile(self: *Self, file: std.fs.File) !*Font {
25+
if (@hasDecl(std.os.system, "fd_t")) {
26+
if (self.vtable.loadFile) |f| return f(self.ptr, file);
27+
return error.NotImplemented;
28+
}
29+
return error.NotSupported;
30+
}
31+
32+
pub inline fn deinit(self: *Self) void {
33+
return if (self.vtable.deinit) |f| f(self.ptr) else {};
34+
}

0 commit comments

Comments
 (0)