Skip to content

Commit b22aedf

Browse files
refactor: example font rendering
1 parent 99fe366 commit b22aedf

File tree

4 files changed

+47765
-36
lines changed

4 files changed

+47765
-36
lines changed

build.zig.zon

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,24 @@
44
.paths = .{"."},
55
.dependencies = .{
66
.@"phantom.display.uefi" = .{
7-
.url = "https://github.com/PhantomUIx/display-uefi/archive/723a5f8b4c751a488eeaae781e67ec60cdef0525.tar.gz",
8-
.hash = "1220c5b8911538cc2aa1009f1f6dccd54ae5dc11bc0f5ee6c84c07fa7ddfaa02fa3f",
9-
},
10-
.@"phantom.compat.zigimg" = .{
11-
.url = "https://github.com/PhantomUIx/compat-zigimg/archive/6f81da19d29aa88f73d5ac27d7a4d4540e5b67a7.tar.gz",
12-
.hash = "1220a71bc11122abbd2c27ced9ca77a496c2b426c78b7be0838fd2bf95f9c2a4a632",
7+
.url = "https://github.com/PhantomUIx/display-uefi/archive/a97eb8f55adca2883b9b0619b76ddb3f30da0fbd.tar.gz",
8+
.hash = "1220a18ff3a2fee1b60d0eef7070e1a93532a059c82bfcb2655a6cc7570172056ed5",
139
},
1410
.phantom = .{
15-
.url = "https://github.com/PhantomUIx/core/archive/13cb70007519475065743509e3a908c7105ded1d.tar.gz",
16-
.hash = "12203b3735b8056b34491d5aa777ca5689d1031b1be03690f2cb5b18f6bacd42424f",
11+
.url = "https://github.com/PhantomUIx/core/archive/db46e7bfc2d3158f8b2c3bff1338ee5eafe08aba.tar.gz",
12+
.hash = "122034f36829dc85e51afdc6450259c3b3659b4d934d08096f213aaf0e528c654dfa",
1713
},
1814
.vizops = .{
1915
.url = "https://github.com/MidstallSoftware/vizops/archive/626d6bbf78fe7d192d0dea77abb7d99d272f7039.tar.gz",
2016
.hash = "1220f1f74b130449d782e38792cbe5d6b519ff706dd4f260f8cb0ced10a1f03f8eab",
2117
},
18+
.@"phantom.display.fbdev" = .{
19+
.url = "https://github.com/PhantomUIx/display-fbdev/archive/d91033076cf37346bd6e66ad0a6d7aa385a263af.tar.gz",
20+
.hash = "122034cb13e03603cc7e8ece3234c1b3361504a2624cf5d18fa374e8b95f48cc9e22",
21+
},
22+
.@"phantom.font.bdf" = .{
23+
.url = "https://github.com/PhantomUIx/font-bdf/archive/b08385f3379158f30cb02f66296b15353c6e902e.tar.gz",
24+
.hash = "1220102f5282de7ff2215cd4f37dbfb32fe9e04c73d3297a1f1fd87736c9a4a9cdb3",
25+
},
2226
},
2327
}

src/compositor.zig

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -47,47 +47,59 @@ pub fn main() void {
4747
}
4848

4949
const output = outputs.items[0];
50+
const outputInfo = output.info() catch |e| @panic(@errorName(e));
5051
const surface = output.createSurface(.output, .{
51-
.size = (output.info() catch |e| @panic(@errorName(e))).size.res,
52+
.size = outputInfo.size.res,
5253
}) catch |e| @panic(@errorName(e));
5354
defer {
5455
surface.destroy() catch {};
5556
surface.deinit();
5657
}
5758

59+
const fontFormat = phantom.fonts.backends.bdf.create(alloc) catch |e| @panic(@errorName(e));
60+
defer fontFormat.deinit();
61+
62+
const font = fontFormat.loadBuffer(@embedFile("example.bdf"), .{
63+
.colorspace = .sRGB,
64+
.colorFormat = .{ .rgba = @splat(8) },
65+
.foregroundColor = .{
66+
.uint8 = .{
67+
.sRGB = .{
68+
.value = @splat(0),
69+
},
70+
},
71+
},
72+
.backgroundColor = .{
73+
.uint8 = .{
74+
.sRGB = .{
75+
.value = @splat(255),
76+
},
77+
},
78+
},
79+
}) catch |e| @panic(@errorName(e));
80+
defer font.deinit();
81+
82+
const utfView = std.unicode.Utf8View.initComptime("Hello, world!");
83+
var iter = utfView.iterator();
84+
85+
var nodes = std.ArrayList(*phantom.scene.Node).init(alloc);
86+
5887
const scene = surface.createScene(@enumFromInt(@intFromEnum(sceneBackendType))) catch |e| @panic(@errorName(e));
5988

60-
const format = phantom.painting.image.formats.zigimg.create(alloc) catch |e| @panic(@errorName(e));
61-
defer format.deinit();
89+
while (iter.nextCodepoint()) |codepoint| {
90+
const glyph = font.lookupGlyph(codepoint) catch |e| @panic(@errorName(e));
6291

63-
const image = format.readBuffer(@embedFile("example.gif")) catch |e| @panic(@errorName(e));
64-
defer image.deinit();
92+
nodes.append(scene.createNode(.NodeFrameBuffer, .{
93+
.source = glyph.fb,
94+
}) catch |e| @panic(@errorName(e))) catch |e| @panic(@errorName(e));
95+
}
6596

66-
const fb = scene.createNode(.NodeFrameBuffer, .{
67-
.source = image.buffer(0) catch |e| @panic(@errorName(e)),
97+
const flex = scene.createNode(.NodeFlex, .{
98+
.direction = .horizontal,
99+
.children = nodes.items,
68100
}) catch |e| @panic(@errorName(e));
69101

70-
const stderr = if (builtin.os.tag == .uefi) SimpleTextOutputWriter{
71-
.context = std.os.uefi.system_table.std_err.?,
72-
} else std.io.getStdErr().writer();
73-
74-
var prevTime = std.time.milliTimestamp();
75102
while (true) {
76-
const currTime = std.time.milliTimestamp();
77-
const deltaTime = currTime - prevTime;
78-
_ = stderr.print("FPS: {} (Delta time: {}, Prev: {}, Curr: {})\n", .{
79-
60 / @max(deltaTime, 1),
80-
deltaTime,
81-
prevTime,
82-
currTime,
83-
}) catch {};
84-
85-
_ = scene.frame(fb) catch |e| @panic(@errorName(e));
86-
87-
fb.setProperties(.{
88-
.source = image.buffer(scene.seq % image.info().seqCount) catch |e| @panic(@errorName(e)),
89-
}) catch |e| @panic(@errorName(e));
90-
91-
prevTime = currTime;
103+
_ = scene.frame(flex) catch |e| @panic(@errorName(e));
92104
}
93105
}

0 commit comments

Comments
 (0)