font/coretext: face doesn't need harfbuzz font if we're not using it

This commit is contained in:
Mitchell Hashimoto
2024-04-04 12:18:28 -07:00
parent c5bbbdb5ee
commit e41e45e1ad
3 changed files with 17 additions and 10 deletions

View File

@ -13,8 +13,9 @@ pub const Face = struct {
/// Our font face
font: *macos.text.Font,
/// Harfbuzz font corresponding to this face.
hb_font: harfbuzz.Font,
/// Harfbuzz font corresponding to this face. We only use this
/// if we're using Harfbuzz.
hb_font: if (harfbuzz_shaper) harfbuzz.Font else void,
/// The presentation for this font.
presentation: font.Presentation,
@ -25,6 +26,10 @@ pub const Face = struct {
/// Set quirks.disableDefaultFontFeatures
quirks_disable_default_font_features: bool = false,
/// True if our build is using Harfbuzz. If we're not, we can avoid
/// some Harfbuzz-specific code paths.
const harfbuzz_shaper = font.Shaper == font.shape.harfbuzz.Shaper;
/// The matrix applied to a regular font to auto-italicize it.
pub const italic_skew = macos.graphics.AffineTransform{
.a = 1,
@ -75,10 +80,6 @@ pub const Face = struct {
/// Initialize a face with a CTFont. This will take ownership over
/// the CTFont. This does NOT copy or retain the CTFont.
pub fn initFont(ct_font: *macos.text.Font, opts: font.face.Options) !Face {
var hb_font = try harfbuzz.coretext.createFont(ct_font);
errdefer hb_font.destroy();
hb_font.setScale(opts.size.pixels(), opts.size.pixels());
const traits = ct_font.getSymbolicTraits();
const metrics = metrics: {
var metrics = try calcMetrics(ct_font);
@ -86,6 +87,13 @@ pub const Face = struct {
break :metrics metrics;
};
var hb_font = if (comptime harfbuzz_shaper) font: {
var hb_font = try harfbuzz.coretext.createFont(ct_font);
hb_font.setScale(opts.size.pixels(), opts.size.pixels());
break :font hb_font;
} else {};
errdefer if (comptime harfbuzz_shaper) hb_font.destroy();
var result: Face = .{
.font = ct_font,
.hb_font = hb_font,
@ -144,7 +152,7 @@ pub const Face = struct {
pub fn deinit(self: *Face) void {
self.font.release();
self.hb_font.destroy();
if (comptime harfbuzz_shaper) self.hb_font.destroy();
self.* = undefined;
}

View File

@ -1,7 +1,7 @@
const builtin = @import("builtin");
const options = @import("main.zig").options;
const harfbuzz = @import("shaper/harfbuzz.zig");
const coretext = @import("shaper/coretext.zig");
pub const harfbuzz = @import("shaper/harfbuzz.zig");
pub const coretext = @import("shaper/coretext.zig");
pub const web_canvas = @import("shaper/web_canvas.zig");
pub usingnamespace @import("shaper/run.zig");

View File

@ -1,7 +1,6 @@
const std = @import("std");
const assert = std.debug.assert;
const Allocator = std.mem.Allocator;
const harfbuzz = @import("harfbuzz");
const macos = @import("macos");
const trace = @import("tracy").trace;
const font = @import("../main.zig");