From dafc99746dbe9d3d7957541976a623bd7f69a218 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 29 Aug 2022 16:46:53 -0700 Subject: [PATCH] font: Face initializes a hb_font alongside every face --- pkg/harfbuzz/build.zig | 2 ++ pkg/harfbuzz/main.zig | 2 +- src/font/Face.zig | 10 +++++++++- src/font/Shaper.zig | 1 - 4 files changed, 12 insertions(+), 3 deletions(-) diff --git a/pkg/harfbuzz/build.zig b/pkg/harfbuzz/build.zig index 5b7d63e2c..b32792609 100644 --- a/pkg/harfbuzz/build.zig +++ b/pkg/harfbuzz/build.zig @@ -1,4 +1,5 @@ const std = @import("std"); +const freetypepkg = @import("../freetype/build.zig"); /// Directories with our includes. const root = thisDir() ++ "../../../vendor/harfbuzz/"; @@ -9,6 +10,7 @@ pub const include_paths = .{include_path}; pub const pkg = std.build.Pkg{ .name = "harfbuzz", .source = .{ .path = thisDir() ++ "/main.zig" }, + .dependencies = &.{freetypepkg.pkg}, }; fn thisDir() []const u8 { diff --git a/pkg/harfbuzz/main.zig b/pkg/harfbuzz/main.zig index 85df31b30..27195090f 100644 --- a/pkg/harfbuzz/main.zig +++ b/pkg/harfbuzz/main.zig @@ -7,7 +7,7 @@ pub usingnamespace @import("face.zig"); pub usingnamespace @import("font.zig"); pub usingnamespace @import("shape.zig"); pub usingnamespace @import("version.zig"); -pub const Freetype = @import("freetype.zig"); +pub const freetype = @import("freetype.zig"); test { @import("std").testing.refAllDecls(@This()); diff --git a/src/font/Face.zig b/src/font/Face.zig index 4aee12b30..c90ae41dc 100644 --- a/src/font/Face.zig +++ b/src/font/Face.zig @@ -8,6 +8,7 @@ const Face = @This(); const std = @import("std"); const builtin = @import("builtin"); const freetype = @import("freetype"); +const harfbuzz = @import("harfbuzz"); const assert = std.debug.assert; const testing = std.testing; const Allocator = std.mem.Allocator; @@ -20,6 +21,9 @@ const log = std.log.scoped(.font_face); /// Our font 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 /// wrong on modern devices so it is highly recommended you get the DPI /// 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 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 { self.face.deinit(); + self.hb_font.destroy(); self.* = undefined; } diff --git a/src/font/Shaper.zig b/src/font/Shaper.zig index cd41ea6aa..9ef3b56fc 100644 --- a/src/font/Shaper.zig +++ b/src/font/Shaper.zig @@ -128,7 +128,6 @@ test "run iterator" { count += 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(usize, 3), count);