font: DeferredFace.name takes a buffer

This commit is contained in:
Mitchell Hashimoto
2023-08-25 14:40:59 -07:00
parent 167bf6f098
commit b51ef704f4
3 changed files with 17 additions and 18 deletions

View File

@ -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 });
}
}

View File

@ -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
"<not enough internal storage space>";
log.info(
"CoreText font required too much space to copy, value = {s}",
.{buf_name},
);
break :unsupported "<CoreText internal storage limited, see logs>";
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

View File

@ -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 "<error>",
face.name(&buf) catch "<error>",
});
self.addFace(style, .{ .deferred = face }) catch break :discover;
if (self.indexForCodepointExact(cp, style, p)) |value| return value;