Skip to content

Commit e5fe9d6

Browse files
feat(scene): use utf-8 view for text node
1 parent 6418611 commit e5fe9d6

File tree

2 files changed

+6
-13
lines changed

2 files changed

+6
-13
lines changed

src/phantom/scene/backends/fb.zig

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,7 @@ pub const NodeText = @import("../nodes/text.zig").NodeText(struct {
7272
pub fn frame(self: *NodeText, scene: *Self.Scene, subscene: ?BaseScene.Sub) anyerror!void {
7373
const startPos: vizops.vector.UsizeVector2 = if (subscene) |sub| sub.pos else .{};
7474

75-
const view = try std.unicode.Utf8View.init(self.options.text);
76-
var viewIter = view.iterator();
75+
var viewIter = self.options.view.iterator();
7776

7877
var origin = vizops.vector.UsizeVector2.zero();
7978

src/phantom/scene/nodes/text.zig

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const Node = @import("../node.zig");
77

88
pub const Options = struct {
99
font: *fonts.Font,
10-
text: []const u21,
10+
view: std.unicode.Utf8View,
1111
};
1212

1313
pub fn NodeText(comptime Impl: type) type {
@@ -18,7 +18,7 @@ pub fn NodeText(comptime Impl: type) type {
1818

1919
const State = struct {
2020
font: *fonts.Font,
21-
text: []const u21,
21+
view: std.unicode.Utf8View,
2222
implState: ImplState,
2323

2424
pub fn init(alloc: Allocator, options: Options) Allocator.Error!*State {
@@ -34,14 +34,13 @@ pub fn NodeText(comptime Impl: type) type {
3434
pub fn equal(self: *State, other: *State) bool {
3535
return std.simd.countTrues(@Vector(2, bool){
3636
self.font == other.font,
37-
std.mem.eql(u8, self.text, other.text),
37+
std.mem.eql(u8, self.view.bytes, other.view.bytes),
3838
if (ImplState != void) self.implState.equal(other.implState) else true,
3939
}) == 2;
4040
}
4141

4242
pub fn deinit(self: *State, alloc: Allocator) void {
4343
if (ImplState != void) self.implState.deinit(alloc);
44-
alloc.free(self.text);
4544
alloc.destroy(self);
4645
}
4746
};
@@ -53,10 +52,7 @@ pub fn NodeText(comptime Impl: type) type {
5352
pub fn new(alloc: Allocator, id: ?usize, options: Options) Allocator.Error!*Node {
5453
const self = try alloc.create(Self);
5554
self.* = .{
56-
.options = .{
57-
.font = options.font,
58-
.text = try alloc.dupe(u21, options.text),
59-
},
55+
.options = options,
6056
.node = .{
6157
.allocator = alloc,
6258
.ptr = self,
@@ -92,8 +88,7 @@ pub fn NodeText(comptime Impl: type) type {
9288
}
9389

9490
fn calcSize(self: *Self) !vizops.vector.UsizeVector2 {
95-
const view = try std.unicode.Utf8View.init(self.options.text);
96-
var viewIter = view.iterator();
91+
var viewIter = self.option.view.iterator();
9792

9893
var width: usize = 0;
9994
var yMaxMin = vizops.vector.UsizeVector2.zero();
@@ -160,7 +155,6 @@ pub fn NodeText(comptime Impl: type) type {
160155
Impl.deinit(self);
161156
}
162157

163-
self.node.allocator.free(self.options.text);
164158
self.node.allocator.destroy(self);
165159
}
166160

0 commit comments

Comments
 (0)