From b51ef704f49e260802c0597a8927bb5780f4d886 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 25 Aug 2023 14:40:59 -0700 Subject: [PATCH] font: DeferredFace.name takes a buffer --- src/Surface.zig | 14 +++++++++----- src/font/DeferredFace.zig | 18 ++++++------------ src/font/Group.zig | 3 ++- 3 files changed, 17 insertions(+), 18 deletions(-) diff --git a/src/Surface.zig b/src/Surface.zig index 066e5464a..8bacd4886 100644 --- a/src/Surface.zig +++ b/src/Surface.zig @@ -222,6 +222,9 @@ pub fn init( }; group.discover = disco; + // A buffer we use to store the font names for logging. + var name_buf: [256]u8 = undefined; + if (config.@"font-family") |family| { var disco_it = try disco.discover(.{ .family = family, @@ -229,7 +232,7 @@ pub fn init( }); defer disco_it.deinit(); if (try disco_it.next()) |face| { - log.info("font regular: {s}", .{try face.name()}); + log.info("font regular: {s}", .{try face.name(&name_buf)}); try group.addFace(.regular, .{ .deferred = face }); } else log.warn("font-family not found: {s}", .{family}); } @@ -241,7 +244,7 @@ pub fn init( }); defer disco_it.deinit(); if (try disco_it.next()) |face| { - log.info("font bold: {s}", .{try face.name()}); + log.info("font bold: {s}", .{try face.name(&name_buf)}); try group.addFace(.bold, .{ .deferred = face }); } else log.warn("font-family-bold not found: {s}", .{family}); } @@ -253,7 +256,7 @@ pub fn init( }); defer disco_it.deinit(); if (try disco_it.next()) |face| { - log.info("font italic: {s}", .{try face.name()}); + log.info("font italic: {s}", .{try face.name(&name_buf)}); try group.addFace(.italic, .{ .deferred = face }); } else log.warn("font-family-italic not found: {s}", .{family}); } @@ -266,7 +269,7 @@ pub fn init( }); defer disco_it.deinit(); if (try disco_it.next()) |face| { - log.info("font bold+italic: {s}", .{try face.name()}); + log.info("font bold+italic: {s}", .{try face.name(&name_buf)}); try group.addFace(.bold_italic, .{ .deferred = face }); } else log.warn("font-family-bold-italic not found: {s}", .{family}); } @@ -307,7 +310,8 @@ pub fn init( }); defer disco_it.deinit(); if (try disco_it.next()) |face| { - log.info("font emoji: {s}", .{try face.name()}); + var name_buf: [256]u8 = undefined; + log.info("font emoji: {s}", .{try face.name(&name_buf)}); try group.addFace(.regular, .{ .deferred = face }); } } diff --git a/src/font/DeferredFace.zig b/src/font/DeferredFace.zig index 27e0c040b..60ac41458 100644 --- a/src/font/DeferredFace.zig +++ b/src/font/DeferredFace.zig @@ -100,7 +100,7 @@ pub fn deinit(self: *DeferredFace) void { /// Returns the name of this face. The memory is always owned by the /// face so it doesn't have to be freed. -pub fn name(self: DeferredFace) ![:0]const u8 { +pub fn name(self: DeferredFace, buf: []u8) ![]const u8 { switch (options.backend) { .freetype => {}, @@ -114,22 +114,15 @@ pub fn name(self: DeferredFace) ![:0]const u8 { // this to be returned efficiently." In this case, we need // to allocate. But we can't return an allocated string because // we don't have an allocator. Let's use the stack and log it. - var buf: [1024]u8 = undefined; - const buf_name = display_name.cstring(&buf, .utf8) orelse - ""; - - log.info( - "CoreText font required too much space to copy, value = {s}", - .{buf_name}, - ); - break :unsupported ""; + break :unsupported display_name.cstring(buf, .utf8) orelse + return error.OutOfMemory; }; }, .web_canvas => if (self.wc) |wc| return wc.font_str, } - return "TODO: built-in font names"; + return ""; } /// Load the deferred font face. This does nothing if the face is loaded. @@ -397,7 +390,8 @@ test "coretext" { try testing.expect(def.hasCodepoint(' ', null)); // Verify we can get the name - const n = try def.name(); + var buf: [1024]u8 = undefined; + const n = try def.name(&buf); try testing.expect(n.len > 0); // Load it and verify it works diff --git a/src/font/Group.zig b/src/font/Group.zig index a80fc016e..b2fc99189 100644 --- a/src/font/Group.zig +++ b/src/font/Group.zig @@ -259,9 +259,10 @@ pub fn indexForCodepoint( defer disco_it.deinit(); if (disco_it.next() catch break :discover) |face| { + var buf: [256]u8 = undefined; log.info("found codepoint 0x{x} in fallback face={s}", .{ cp, - face.name() catch "", + face.name(&buf) catch "", }); self.addFace(style, .{ .deferred = face }) catch break :discover; if (self.indexForCodepointExact(cp, style, p)) |value| return value;