From a6c3ee3bc2311d53a096b36a79755749a75bdff0 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 10 Oct 2022 09:18:57 -0700 Subject: [PATCH] pkg/macos: fix zig C ABI issue --- pkg/macos/build.zig | 4 ++++ pkg/macos/text/ext.c | 10 ++++++++++ pkg/macos/text/line.zig | 22 ++++++++++++++++++++-- 3 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 pkg/macos/text/ext.c diff --git a/pkg/macos/build.zig b/pkg/macos/build.zig index 78df21ee8..b2f778b0c 100644 --- a/pkg/macos/build.zig +++ b/pkg/macos/build.zig @@ -18,7 +18,11 @@ pub fn link( opt: Options, ) !*std.build.LibExeObjStep { _ = opt; + var flags = std.ArrayList([]const u8).init(b.allocator); + defer flags.deinit(); + const lib = b.addStaticLibrary("macos", null); + step.addCSourceFile(thisDir() ++ "/text/ext.c", flags.items); step.linkFramework("CoreFoundation"); step.linkFramework("CoreText"); return lib; diff --git a/pkg/macos/text/ext.c b/pkg/macos/text/ext.c new file mode 100644 index 000000000..55bff8a61 --- /dev/null +++ b/pkg/macos/text/ext.c @@ -0,0 +1,10 @@ +#include + +// A wrapper to fix a Zig C ABI issue. +void zig_cabi_CTLineGetBoundsWithOptions( + CTLineRef line, + CTLineBoundsOptions options, + CGRect *result +) { + *result = CTLineGetBoundsWithOptions(line, options); +} diff --git a/pkg/macos/text/line.zig b/pkg/macos/text/line.zig index 014d64cb9..abe9b114b 100644 --- a/pkg/macos/text/line.zig +++ b/pkg/macos/text/line.zig @@ -30,12 +30,30 @@ pub const Line = opaque { self: *Line, opts: LineBoundsOptions, ) graphics.Rect { - return @bitCast(c.CGRect, c.CTLineGetBoundsWithOptions( + // return @bitCast(c.CGRect, c.CTLineGetBoundsWithOptions( + // @ptrCast(c.CTLineRef, self), + // opts.cval(), + // )); + + // We have to use a custom C wrapper here because there is some + // C ABI issue happening. + var result: graphics.Rect = undefined; + zig_cabi_CTLineGetBoundsWithOptions( @ptrCast(c.CTLineRef, self), opts.cval(), - )); + @ptrCast(*c.CGRect, &result), + ); + + return result; } + // See getBoundsWithOptions + extern "c" fn zig_cabi_CTLineGetBoundsWithOptions( + c.CTLineRef, + c.CTLineBoundsOptions, + *c.CGRect, + ) void; + pub fn getTypographicBounds( self: *Line, ascent: ?*f64,