getOrCreate
Parameters
Source
Implementation
pub fn getOrCreate(self: *Cache, gpa: std.mem.Allocator, font: Font) std.mem.Allocator.Error!*Entry {
const entry = try self.cache.getOrPut(gpa, font.hash());
if (entry.found_existing) return entry.value_ptr;
const fname = font.name(gpa);
defer gpa.free(fname);
const exact, const second = self.findSource(font);
const source = exact orelse blk: {
if (second) |s| {
const sname = s.name(gpa);
defer gpa.free(sname);
dvui.log.warn("Font {s} not loaded in dvui, using second best {s}", .{ fname, sname });
break :blk s;
} else {
dvui.log.warn("Font {s} not loaded in dvui, using fallback", .{fname});
break :blk Source.fallback;
}
};
//log.debug("FontCacheGet creating font hash {x} ptr {*} size {d} name \"{s}\"", .{ fontHash, bytes.ptr, font.size, font.name });
entry.value_ptr.* = Entry.init(gpa, source.bytes, font) catch |err| {
dvui.log.err("Font {s} init got {any}, using fallback", .{ fname, err });
// Remove the invalid font cache entry, something went wrong reading the ttf_bytes
self.cache.map.removeByPtr(entry.key_ptr);
// Substitute the known good fallback font
return self.getOrCreate(gpa, Source.fallback.font());
};
//log.debug("- size {d} ascent {d} height {d}", .{ font.size, entry.ascent, entry.height });
return entry.value_ptr;
}