Skip to content

Commit 42f1d54

Browse files
fix(scene): make font rendering work
1 parent c6b99c9 commit 42f1d54

File tree

3 files changed

+36
-16
lines changed

3 files changed

+36
-16
lines changed

src/phantom/math.zig

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,16 @@
1+
const std = @import("std");
2+
13
pub usingnamespace @import("math/units.zig");
4+
5+
pub fn OneBiggerInt(comptime T: type) type {
6+
var info = @typeInfo(T);
7+
info.Int.bits += 1;
8+
return @Type(info);
9+
}
10+
11+
pub fn add(a: anytype, b: anytype) @TypeOf(a) {
12+
if (b >= 0) {
13+
return a + @as(@TypeOf(a), @intCast(b));
14+
}
15+
return a - @as(@TypeOf(a), @intCast(-@as(OneBiggerInt(@TypeOf(b)), b)));
16+
}

src/phantom/scene/backends/fb.zig

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,27 +70,27 @@ pub const NodeText = @import("../nodes/text.zig").NodeText(struct {
7070
pub const Scene = @import("fb/scene.zig");
7171

7272
pub fn frame(self: *NodeText, scene: *Self.Scene, subscene: ?BaseScene.Sub) anyerror!void {
73+
const size = (try self.node.state(scene.base.frameInfo())).size;
7374
const startPos: vizops.vector.UsizeVector2 = if (subscene) |sub| sub.pos else .{};
7475

7576
var viewIter = self.options.view.iterator();
7677

77-
var origin = vizops.vector.UsizeVector2.zero();
78+
var pos = vizops.vector.UsizeVector2.zero();
7879

7980
while (viewIter.nextCodepoint()) |cp| {
8081
const glyph = try self.options.font.lookupGlyph(cp);
8182

83+
var destOffset = pos.add(startPos);
84+
85+
destOffset.value[0] = math.add(destOffset.value[0], glyph.bearing.value[0]);
86+
destOffset.value[1] = math.add(destOffset.value[1], size.value[1] - @as(u8, @intCast(glyph.bearing.value[1])));
87+
8288
try glyph.fb.blt(.to, scene.buffer, .{
83-
.destOffset = origin.add(startPos).add(.{
84-
glyph.bearing.value[0],
85-
-glyph.bearing.value[1],
86-
}).cast(usize),
87-
.size = glyph.size,
89+
.destOffset = destOffset,
90+
.size = glyph.size.cast(usize),
8891
});
8992

90-
origin = origin.add(.{
91-
glyph.advance.value[0],
92-
-glyph.advance.value[1],
93-
});
93+
pos.value[0] += @intCast(glyph.advance.value[0]);
9494
}
9595
}
9696
});

src/phantom/scene/nodes/text.zig

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@ pub fn NodeText(comptime Impl: type) type {
2525
const self = try alloc.create(State);
2626
self.* = .{
2727
.font = options.font,
28-
.text = try alloc.dupe(u21, options.text),
28+
.view = options.view,
2929
.implState = if (ImplState != void) ImplState.init(alloc, options) else {},
3030
};
3131
return self;
3232
}
3333

3434
pub fn equal(self: *State, other: *State) bool {
35-
return std.simd.countTrues(@Vector(2, bool){
35+
return std.simd.countTrues(@Vector(3, bool){
3636
self.font == other.font,
3737
std.mem.eql(u8, self.view.bytes, other.view.bytes),
3838
if (ImplState != void) self.implState.equal(other.implState) else true,
@@ -96,9 +96,14 @@ pub fn NodeText(comptime Impl: type) type {
9696
while (viewIter.nextCodepoint()) |cp| {
9797
const glyph = try self.options.font.lookupGlyph(cp);
9898

99-
width += glyph.advance.value[0];
100-
yMaxMin.value[0] = @max(yMaxMin.value[0], glyph.bearing.value[1]);
101-
yMaxMin.value[1] = @min(yMaxMin.value[1], glyph.bearing.value[1] - glyph.size.value[1]);
99+
width += @intCast(glyph.advance.value[0]);
100+
yMaxMin.value[0] = @max(yMaxMin.value[0], @as(usize, @intCast(glyph.bearing.value[1])));
101+
102+
if (glyph.bearing.value[1] >= glyph.size.value[1]) {
103+
yMaxMin.value[1] = @min(yMaxMin.value[1], @as(usize, @intCast(glyph.bearing.value[1] - @as(i8, @intCast(glyph.size.value[1])))));
104+
} else {
105+
yMaxMin.value[1] = @min(yMaxMin.value[1], @as(usize, @intCast(glyph.size.value[1] - glyph.size.value[1])));
106+
}
102107
}
103108

104109
return .{ .value = .{ width, yMaxMin.value[0] - yMaxMin.value[1] } };
@@ -164,7 +169,7 @@ pub fn NodeText(comptime Impl: type) type {
164169
var output = std.ArrayList(u8).init(self.node.allocator);
165170
errdefer output.deinit();
166171

167-
try output.writer().print("{{ .font = {}, .text = \"{}\" }}", .{ self.options.font, std.unicode.fmtUtf8(self.options.view.bytes) });
172+
try output.writer().print("{{ .font = {}, .text = \"{s}\" }}", .{ self.options.font, self.options.view.bytes });
168173
return output;
169174
}
170175
};

0 commit comments

Comments
 (0)