pkg/harfbuzz: add coretext compilation on macOS

This commit is contained in:
Mitchell Hashimoto
2022-10-02 11:05:40 -07:00
parent f41cbf228b
commit 939e6e7a65
6 changed files with 57 additions and 2 deletions

View File

@ -258,13 +258,18 @@ fn addDeps(
}); });
// Harfbuzz // Harfbuzz
_ = try harfbuzz.link(b, step, .{ const harfbuzz_step = try harfbuzz.link(b, step, .{
.freetype = .{ .freetype = .{
.enabled = true, .enabled = true,
.step = freetype_step, .step = freetype_step,
.include = &freetype.include_paths, .include = &freetype.include_paths,
}, },
.coretext = .{
.enabled = enable_coretext,
},
}); });
system_sdk.include(b, harfbuzz_step, .{});
// Libuv // Libuv
const libuv_step = try libuv.link(b, step); const libuv_step = try libuv.link(b, step);

View File

@ -1,5 +1,6 @@
const std = @import("std"); const std = @import("std");
const freetypepkg = @import("../freetype/build.zig"); const freetypepkg = @import("../freetype/build.zig");
const macospkg = @import("../macos/build.zig");
/// Directories with our includes. /// Directories with our includes.
const root = thisDir() ++ "../../../vendor/harfbuzz/"; const root = thisDir() ++ "../../../vendor/harfbuzz/";
@ -10,7 +11,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}, .dependencies = &.{ freetypepkg.pkg, macospkg.pkg },
}; };
fn thisDir() []const u8 { fn thisDir() []const u8 {
@ -19,12 +20,17 @@ fn thisDir() []const u8 {
pub const Options = struct { pub const Options = struct {
freetype: Freetype = .{}, freetype: Freetype = .{},
coretext: CoreText = .{},
pub const Freetype = struct { pub const Freetype = struct {
enabled: bool = false, enabled: bool = false,
step: ?*std.build.LibExeObjStep = null, step: ?*std.build.LibExeObjStep = null,
include: ?[]const []const u8 = null, include: ?[]const []const u8 = null,
}; };
pub const CoreText = struct {
enabled: bool = false,
};
}; };
pub fn link( pub fn link(
@ -85,6 +91,14 @@ pub fn buildHarfbuzz(
"-DHAVE_FT_GET_TRANSFORM=1", "-DHAVE_FT_GET_TRANSFORM=1",
}); });
if (opt.coretext.enabled) {
try flags.appendSlice(&.{
"-DHAVE_CORETEXT=1",
});
lib.linkFramework("ApplicationServices");
}
// C files // C files
lib.addCSourceFiles(srcs, flags.items); lib.addCSourceFiles(srcs, flags.items);

View File

@ -1,4 +1,7 @@
const builtin = @import("builtin");
pub usingnamespace @cImport({ pub usingnamespace @cImport({
@cInclude("hb.h"); @cInclude("hb.h");
@cInclude("hb-ft.h"); @cInclude("hb-ft.h");
if (builtin.os.tag == .macos) @cInclude("hb-coretext.h");
}); });

30
pkg/harfbuzz/coretext.zig Normal file
View File

@ -0,0 +1,30 @@
const macos = @import("macos");
const std = @import("std");
const c = @import("c.zig");
const Face = @import("face.zig").Face;
const Font = @import("font.zig").Font;
const Error = @import("errors.zig").Error;
// Use custom extern functions so that the proper CoreText structs are used
// without a ptrcast.
extern fn hb_coretext_font_create(ct_face: *macos.text.Font) ?*c.hb_font_t;
/// Creates an hb_font_t font object from the specified CTFontRef.
pub fn createFont(face: *macos.text.Font) Error!Font {
const handle = hb_coretext_font_create(face) orelse return Error.HarfbuzzFailed;
return Font{ .handle = handle };
}
test {
if (!@hasDecl(c, "hb_coretext_font_create")) return error.SkipZigTest;
const name = try macos.foundation.String.createWithBytes("Monaco", .utf8, false);
defer name.release();
const desc = try macos.text.FontDescriptor.createWithNameAndSize(name, 12);
defer desc.release();
const font = try macos.text.Font.createWithFontDescriptor(desc, 12);
defer font.release();
var hb_font = try createFont(font);
defer hb_font.destroy();
}

View File

@ -47,6 +47,8 @@ pub fn setFontFuncs(font: Font) void {
} }
test { test {
if (!@hasDecl(c, "hb_ft_font_create_referenced")) return error.SkipZigTest;
const testing = std.testing; const testing = std.testing;
const testFont = freetype.testing.font_regular; const testFont = freetype.testing.font_regular;
const ftc = freetype.c; const ftc = freetype.c;

View File

@ -8,6 +8,7 @@ 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");
pub const coretext = @import("coretext.zig");
test { test {
@import("std").testing.refAllDecls(@This()); @import("std").testing.refAllDecls(@This());