src/font: working on initiaizing deferred fonts from fc

This commit is contained in:
Mitchell Hashimoto
2022-09-23 12:09:40 -07:00
parent bc9a0a36a8
commit 88a4cb65f3
3 changed files with 54 additions and 0 deletions

View File

@ -32,6 +32,18 @@ pub fn version(self: Library) Version {
return v; return v;
} }
/// Call FT_New_Face to open a font from a file.
pub fn initFace(self: Library, path: [:0]const u8, index: i32) Error!Face {
var face: Face = undefined;
try intToError(c.FT_New_Face(
self.handle,
path.ptr,
index,
&face.handle,
));
return face;
}
/// Call FT_Open_Face to open a font that has been loaded into memory. /// Call FT_Open_Face to open a font that has been loaded into memory.
pub fn initMemoryFace(self: Library, data: []const u8, index: i32) Error!Face { pub fn initMemoryFace(self: Library, data: []const u8, index: i32) Error!Face {
var face: Face = undefined; var face: Face = undefined;

View File

@ -61,6 +61,31 @@ pub inline fn loaded(self: DeferredFace) bool {
return self.face != null; return self.face != null;
} }
pub fn load(self: *DeferredFace) !void {
// No-op if we already loaded
if (self.face != null) return;
if (options.fontconfig) {
try self.loadFontconfig();
return;
}
unreachable;
}
fn loadFontconfig(self: *DeferredFace) !void {
assert(self.face == null);
const fc = self.fc.?;
// Filename and index for our face so we can load it
const filename = (try fc.pattern.get(.file, 0)).string;
const face_index = (try fc.pattern.get(.index, 0)).integer;
self.face = try Face.initFile(filename, face_index, .{
.points = fc.req_size,
});
}
/// Returns true if this face can satisfy the given codepoint and /// Returns true if this face can satisfy the given codepoint and
/// presentation. If presentation is null, then it just checks if the /// presentation. If presentation is null, then it just checks if the
/// codepoint is present at all. /// codepoint is present at all.

View File

@ -50,6 +50,23 @@ pub const DesiredSize = struct {
} }
}; };
/// Initialize a new font face with the given source in-memory.
pub fn initFile(lib: Library, path: [:0]const u8, index: i32, size: 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,
};
}
/// Initialize a new font face with the given source in-memory. /// Initialize a new font face with the given source in-memory.
pub fn init(lib: Library, source: [:0]const u8, size: DesiredSize) !Face { pub fn init(lib: Library, source: [:0]const u8, size: DesiredSize) !Face {
const face = try lib.lib.initMemoryFace(source, 0); const face = try lib.lib.initMemoryFace(source, 0);