font: Face initializes a hb_font alongside every face

This commit is contained in:
Mitchell Hashimoto
2022-08-29 16:46:53 -07:00
parent 0505018186
commit dafc99746d
4 changed files with 12 additions and 3 deletions

View File

@ -1,4 +1,5 @@
const std = @import("std"); const std = @import("std");
const freetypepkg = @import("../freetype/build.zig");
/// Directories with our includes. /// Directories with our includes.
const root = thisDir() ++ "../../../vendor/harfbuzz/"; const root = thisDir() ++ "../../../vendor/harfbuzz/";
@ -9,6 +10,7 @@ pub const include_paths = .{include_path};
pub const pkg = std.build.Pkg{ pub const pkg = std.build.Pkg{
.name = "harfbuzz", .name = "harfbuzz",
.source = .{ .path = thisDir() ++ "/main.zig" }, .source = .{ .path = thisDir() ++ "/main.zig" },
.dependencies = &.{freetypepkg.pkg},
}; };
fn thisDir() []const u8 { fn thisDir() []const u8 {

View File

@ -7,7 +7,7 @@ pub usingnamespace @import("face.zig");
pub usingnamespace @import("font.zig"); pub usingnamespace @import("font.zig");
pub usingnamespace @import("shape.zig"); pub usingnamespace @import("shape.zig");
pub usingnamespace @import("version.zig"); pub usingnamespace @import("version.zig");
pub const Freetype = @import("freetype.zig"); pub const freetype = @import("freetype.zig");
test { test {
@import("std").testing.refAllDecls(@This()); @import("std").testing.refAllDecls(@This());

View File

@ -8,6 +8,7 @@ const Face = @This();
const std = @import("std"); const std = @import("std");
const builtin = @import("builtin"); const builtin = @import("builtin");
const freetype = @import("freetype"); const freetype = @import("freetype");
const harfbuzz = @import("harfbuzz");
const assert = std.debug.assert; const assert = std.debug.assert;
const testing = std.testing; const testing = std.testing;
const Allocator = std.mem.Allocator; const Allocator = std.mem.Allocator;
@ -20,6 +21,9 @@ const log = std.log.scoped(.font_face);
/// Our font face. /// Our font face.
face: freetype.Face, face: freetype.Face,
/// Harfbuzz font corresponding to this face.
hb_font: harfbuzz.Font,
/// If a DPI can't be calculated, this DPI is used. This is probably /// If a DPI can't be calculated, this DPI is used. This is probably
/// wrong on modern devices so it is highly recommended you get the DPI /// wrong on modern devices so it is highly recommended you get the DPI
/// using whatever platform method you can. /// using whatever platform method you can.
@ -48,11 +52,15 @@ pub fn init(lib: Library, source: [:0]const u8, size: DesiredSize) !Face {
try face.selectCharmap(.unicode); try face.selectCharmap(.unicode);
try setSize_(face, size); try setSize_(face, size);
return Face{ .face = face }; const hb_font = try harfbuzz.freetype.createFont(face.handle);
errdefer hb_font.destroy();
return Face{ .face = face, .hb_font = hb_font };
} }
pub fn deinit(self: *Face) void { pub fn deinit(self: *Face) void {
self.face.deinit(); self.face.deinit();
self.hb_font.destroy();
self.* = undefined; self.* = undefined;
} }

View File

@ -128,7 +128,6 @@ test "run iterator" {
count += 1; count += 1;
// All runs should be exactly length 1 // All runs should be exactly length 1
std.log.warn("YES", .{});
try testing.expectEqual(@as(u32, 1), shaper.hb_buf.getLength()); try testing.expectEqual(@as(u32, 1), shaper.hb_buf.getLength());
} }
try testing.expectEqual(@as(usize, 3), count); try testing.expectEqual(@as(usize, 3), count);