pkg/macos: fix zig C ABI issue

This commit is contained in:
Mitchell Hashimoto
2022-10-10 09:18:57 -07:00
parent 1e23779337
commit a6c3ee3bc2
3 changed files with 34 additions and 2 deletions

View File

@ -18,7 +18,11 @@ pub fn link(
opt: Options, opt: Options,
) !*std.build.LibExeObjStep { ) !*std.build.LibExeObjStep {
_ = opt; _ = opt;
var flags = std.ArrayList([]const u8).init(b.allocator);
defer flags.deinit();
const lib = b.addStaticLibrary("macos", null); const lib = b.addStaticLibrary("macos", null);
step.addCSourceFile(thisDir() ++ "/text/ext.c", flags.items);
step.linkFramework("CoreFoundation"); step.linkFramework("CoreFoundation");
step.linkFramework("CoreText"); step.linkFramework("CoreText");
return lib; return lib;

10
pkg/macos/text/ext.c Normal file
View File

@ -0,0 +1,10 @@
#include <CoreText/CoreText.h>
// A wrapper to fix a Zig C ABI issue.
void zig_cabi_CTLineGetBoundsWithOptions(
CTLineRef line,
CTLineBoundsOptions options,
CGRect *result
) {
*result = CTLineGetBoundsWithOptions(line, options);
}

View File

@ -30,12 +30,30 @@ pub const Line = opaque {
self: *Line, self: *Line,
opts: LineBoundsOptions, opts: LineBoundsOptions,
) graphics.Rect { ) 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), @ptrCast(c.CTLineRef, self),
opts.cval(), 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( pub fn getTypographicBounds(
self: *Line, self: *Line,
ascent: ?*f64, ascent: ?*f64,