font: freetype eliminate some duplicated work on init

This commit is contained in:
Mitchell Hashimoto
2022-11-15 15:23:28 -08:00
parent e24dabd67a
commit 1c4552de3b

View File

@@ -40,24 +40,17 @@ pub const Face = struct {
pub fn initFile(lib: Library, path: [:0]const u8, index: i32, size: font.face.DesiredSize) !Face {
const face = try lib.lib.initFace(path, index);
errdefer face.deinit();
try face.selectCharmap(.unicode);
try setSize_(face, size);
const hb_font = try harfbuzz.freetype.createFont(face.handle);
errdefer hb_font.destroy();
return Face{
.face = face,
.hb_font = hb_font,
.presentation = if (face.hasColor()) .emoji else .text,
.metrics = calcMetrics(face),
};
return try initFace(face, size);
}
/// Initialize a new font face with the given source in-memory.
pub fn init(lib: Library, source: [:0]const u8, size: font.face.DesiredSize) !Face {
const face = try lib.lib.initMemoryFace(source, 0);
errdefer face.deinit();
return try initFace(face, size);
}
fn initFace(face: freetype.Face, size: font.face.DesiredSize) !Face {
try face.selectCharmap(.unicode);
try setSize_(face, size);