pkg/macos: import headers instead of doing externs manually

This commit is contained in:
Mitchell Hashimoto
2022-10-01 15:24:22 -07:00
parent 57c1d8d329
commit 3eeef74315
4 changed files with 71 additions and 98 deletions

View File

@ -1,3 +1,4 @@
pub const c = @import("foundation/c.zig");
pub usingnamespace @import("foundation/array.zig"); pub usingnamespace @import("foundation/array.zig");
pub usingnamespace @import("foundation/base.zig"); pub usingnamespace @import("foundation/base.zig");
pub usingnamespace @import("foundation/dictionary.zig"); pub usingnamespace @import("foundation/dictionary.zig");

View File

@ -1,6 +1,7 @@
const std = @import("std"); const std = @import("std");
const Allocator = std.mem.Allocator; const Allocator = std.mem.Allocator;
const foundation = @import("../foundation.zig"); const foundation = @import("../foundation.zig");
const c = @import("c.zig");
pub const String = opaque { pub const String = opaque {
pub fn createWithBytes( pub fn createWithBytes(
@ -8,21 +9,24 @@ pub const String = opaque {
encoding: StringEncoding, encoding: StringEncoding,
external: bool, external: bool,
) Allocator.Error!*String { ) Allocator.Error!*String {
return CFStringCreateWithBytes( return @intToPtr(?*String, @ptrToInt(c.CFStringCreateWithBytes(
null, null,
bs.ptr, bs.ptr,
bs.len, @intCast(c_long, bs.len),
@enumToInt(encoding), @enumToInt(encoding),
external, @boolToInt(external),
) orelse Allocator.Error.OutOfMemory; ))) orelse Allocator.Error.OutOfMemory;
} }
pub fn release(self: *String) void { pub fn release(self: *String) void {
foundation.CFRelease(self); c.CFRelease(self);
} }
pub fn hasPrefix(self: *String, prefix: *String) bool { pub fn hasPrefix(self: *String, prefix: *String) bool {
return CFStringHasPrefix(self, prefix) == 1; return c.CFStringHasPrefix(
@ptrCast(c.CFStringRef, self),
@ptrCast(c.CFStringRef, prefix),
) == 1;
} }
pub fn compare( pub fn compare(
@ -32,37 +36,32 @@ pub const String = opaque {
) foundation.ComparisonResult { ) foundation.ComparisonResult {
return @intToEnum( return @intToEnum(
foundation.ComparisonResult, foundation.ComparisonResult,
CFStringCompare(self, other, @bitCast(c_int, options)), c.CFStringCompare(
@ptrCast(c.CFStringRef, self),
@ptrCast(c.CFStringRef, other),
@intCast(c_ulong, @bitCast(c_int, options)),
),
); );
} }
pub fn cstring(self: *String, buf: []u8, encoding: StringEncoding) ?[]const u8 { pub fn cstring(self: *String, buf: []u8, encoding: StringEncoding) ?[]const u8 {
if (CFStringGetCString( if (c.CFStringGetCString(
self, @ptrCast(c.CFStringRef, self),
buf.ptr, buf.ptr,
buf.len, @intCast(c_long, buf.len),
@enumToInt(encoding), @enumToInt(encoding),
) == 0) return null; ) == 0) return null;
return std.mem.sliceTo(buf, 0); return std.mem.sliceTo(buf, 0);
} }
pub fn cstringPtr(self: *String, encoding: StringEncoding) ?[:0]const u8 { pub fn cstringPtr(self: *String, encoding: StringEncoding) ?[:0]const u8 {
const ptr = CFStringGetCStringPtr(self, @enumToInt(encoding)); const ptr = c.CFStringGetCStringPtr(
@ptrCast(c.CFStringRef, self),
@enumToInt(encoding),
);
if (ptr == null) return null; if (ptr == null) return null;
return std.mem.sliceTo(ptr, 0); return std.mem.sliceTo(ptr, 0);
} }
pub extern "c" fn CFStringCreateWithBytes(
allocator: ?*anyopaque,
bytes: [*]const u8,
numBytes: usize,
encooding: u32,
is_external: bool,
) ?*String;
pub extern "c" fn CFStringHasPrefix(*String, *String) u8;
pub extern "c" fn CFStringCompare(*String, *String, c_int) c_int;
pub extern "c" fn CFStringGetCString(*String, [*]u8, usize, u32) u8;
pub extern "c" fn CFStringGetCStringPtr(*String, u32) [*c]const u8;
}; };
pub const StringComparison = packed struct { pub const StringComparison = packed struct {

View File

@ -2,26 +2,28 @@ const std = @import("std");
const Allocator = std.mem.Allocator; const Allocator = std.mem.Allocator;
const foundation = @import("../foundation.zig"); const foundation = @import("../foundation.zig");
const text = @import("../text.zig"); const text = @import("../text.zig");
const c = @import("c.zig");
pub const FontCollection = opaque { pub const FontCollection = opaque {
pub fn createFromAvailableFonts() Allocator.Error!*FontCollection { pub fn createFromAvailableFonts() Allocator.Error!*FontCollection {
return CTFontCollectionCreateFromAvailableFonts(null) orelse Allocator.Error.OutOfMemory; return @intToPtr(
?*FontCollection,
@ptrToInt(c.CTFontCollectionCreateFromAvailableFonts(null)),
) orelse Allocator.Error.OutOfMemory;
} }
pub fn release(self: *FontCollection) void { pub fn release(self: *FontCollection) void {
foundation.CFRelease(self); c.CFRelease(self);
} }
pub fn createMatchingFontDescriptors(self: *FontCollection) *foundation.Array { pub fn createMatchingFontDescriptors(self: *FontCollection) *foundation.Array {
return CTFontCollectionCreateMatchingFontDescriptors(self); return @intToPtr(
*foundation.Array,
@ptrToInt(c.CTFontCollectionCreateMatchingFontDescriptors(
@ptrCast(c.CTFontCollectionRef, self),
)),
);
} }
pub extern "c" fn CTFontCollectionCreateFromAvailableFonts(
options: ?*foundation.Dictionary,
) ?*FontCollection;
pub extern "c" fn CTFontCollectionCreateMatchingFontDescriptors(
collection: *FontCollection,
) *foundation.Array;
}; };
test "collection" { test "collection" {

View File

@ -1,29 +1,26 @@
const std = @import("std"); const std = @import("std");
const Allocator = std.mem.Allocator; const Allocator = std.mem.Allocator;
const foundation = @import("../foundation.zig"); const foundation = @import("../foundation.zig");
const c = @import("c.zig");
pub const FontDescriptor = opaque { pub const FontDescriptor = opaque {
pub fn createWithNameAndSize(name: *foundation.String, size: f64) Allocator.Error!*FontDescriptor { pub fn createWithNameAndSize(name: *foundation.String, size: f64) Allocator.Error!*FontDescriptor {
return CTFontDescriptorCreateWithNameAndSize(name, size) orelse Allocator.Error.OutOfMemory; return @intToPtr(
?*FontDescriptor,
@ptrToInt(c.CTFontDescriptorCreateWithNameAndSize(@ptrCast(c.CFStringRef, name), size)),
) orelse Allocator.Error.OutOfMemory;
} }
pub fn release(self: *FontDescriptor) void { pub fn release(self: *FontDescriptor) void {
foundation.CFRelease(self); c.CFRelease(self);
} }
pub fn copyAttribute(self: *FontDescriptor, comptime attr: FontAttribute) attr.Value() { pub fn copyAttribute(self: *FontDescriptor, comptime attr: FontAttribute) attr.Value() {
const T = attr.Value(); return @intToPtr(attr.Value(), @ptrToInt(c.CTFontDescriptorCopyAttribute(
return @ptrCast(T, CTFontDescriptorCopyAttribute(self, attr.key())); @ptrCast(c.CTFontDescriptorRef, self),
@ptrCast(c.CFStringRef, attr.key()),
)));
} }
pub extern "c" fn CTFontDescriptorCreateWithNameAndSize(
name: *foundation.String,
size: f64,
) ?*FontDescriptor;
pub extern "c" fn CTFontDescriptorCopyAttribute(
*FontDescriptor,
*foundation.String,
) ?*anyopaque;
}; };
pub const FontAttribute = enum { pub const FontAttribute = enum {
@ -53,32 +50,32 @@ pub const FontAttribute = enum {
downloaded, downloaded,
pub fn key(self: FontAttribute) *foundation.String { pub fn key(self: FontAttribute) *foundation.String {
return switch (self) { return @intToPtr(*foundation.String, @ptrToInt(switch (self) {
.url => kCTFontURLAttribute, .url => c.kCTFontURLAttribute,
.name => kCTFontNameAttribute, .name => c.kCTFontNameAttribute,
.display_name => kCTFontDisplayNameAttribute, .display_name => c.kCTFontDisplayNameAttribute,
.family_name => kCTFontFamilyNameAttribute, .family_name => c.kCTFontFamilyNameAttribute,
.style_name => kCTFontStyleNameAttribute, .style_name => c.kCTFontStyleNameAttribute,
.traits => kCTFontTraitsAttribute, .traits => c.kCTFontTraitsAttribute,
.variation => kCTFontVariationAttribute, .variation => c.kCTFontVariationAttribute,
.size => kCTFontSizeAttribute, .size => c.kCTFontSizeAttribute,
.matrix => kCTFontMatrixAttribute, .matrix => c.kCTFontMatrixAttribute,
.cascade_list => kCTFontCascadeListAttribute, .cascade_list => c.kCTFontCascadeListAttribute,
.character_set => kCTFontCharacterSetAttribute, .character_set => c.kCTFontCharacterSetAttribute,
.languages => kCTFontLanguagesAttribute, .languages => c.kCTFontLanguagesAttribute,
.baseline_adjust => kCTFontBaselineAdjustAttribute, .baseline_adjust => c.kCTFontBaselineAdjustAttribute,
.macintosh_encodings => kCTFontMacintoshEncodingsAttribute, .macintosh_encodings => c.kCTFontMacintoshEncodingsAttribute,
.features => kCTFontFeaturesAttribute, .features => c.kCTFontFeaturesAttribute,
.feature_settings => kCTFontFeatureSettingsAttribute, .feature_settings => c.kCTFontFeatureSettingsAttribute,
.fixed_advance => kCTFontFixedAdvanceAttribute, .fixed_advance => c.kCTFontFixedAdvanceAttribute,
.orientation => kCTFontOrientationAttribute, .orientation => c.kCTFontOrientationAttribute,
.format => kCTFontFormatAttribute, .format => c.kCTFontFormatAttribute,
.registration_scope => kCTFontRegistrationScopeAttribute, .registration_scope => c.kCTFontRegistrationScopeAttribute,
.priority => kCTFontPriorityAttribute, .priority => c.kCTFontPriorityAttribute,
.enabled => kCTFontEnabledAttribute, .enabled => c.kCTFontEnabledAttribute,
.downloadable => kCTFontDownloadableAttribute, .downloadable => c.kCTFontDownloadableAttribute,
.downloaded => kCTFontDownloadedAttribute, .downloaded => c.kCTFontDownloadedAttribute,
}; }));
} }
pub fn Value(self: FontAttribute) type { pub fn Value(self: FontAttribute) type {
@ -109,32 +106,6 @@ pub const FontAttribute = enum {
.downloaded => *anyopaque, // CFBoolean .downloaded => *anyopaque, // CFBoolean
}; };
} }
extern "c" const kCTFontURLAttribute: *foundation.String;
extern "c" const kCTFontNameAttribute: *foundation.String;
extern "c" const kCTFontDisplayNameAttribute: *foundation.String;
extern "c" const kCTFontFamilyNameAttribute: *foundation.String;
extern "c" const kCTFontStyleNameAttribute: *foundation.String;
extern "c" const kCTFontTraitsAttribute: *foundation.String;
extern "c" const kCTFontVariationAttribute: *foundation.String;
extern "c" const kCTFontVariationAxesAttribute: *foundation.String;
extern "c" const kCTFontSizeAttribute: *foundation.String;
extern "c" const kCTFontMatrixAttribute: *foundation.String;
extern "c" const kCTFontCascadeListAttribute: *foundation.String;
extern "c" const kCTFontCharacterSetAttribute: *foundation.String;
extern "c" const kCTFontLanguagesAttribute: *foundation.String;
extern "c" const kCTFontBaselineAdjustAttribute: *foundation.String;
extern "c" const kCTFontMacintoshEncodingsAttribute: *foundation.String;
extern "c" const kCTFontFeaturesAttribute: *foundation.String;
extern "c" const kCTFontFeatureSettingsAttribute: *foundation.String;
extern "c" const kCTFontFixedAdvanceAttribute: *foundation.String;
extern "c" const kCTFontOrientationAttribute: *foundation.String;
extern "c" const kCTFontFormatAttribute: *foundation.String;
extern "c" const kCTFontRegistrationScopeAttribute: *foundation.String;
extern "c" const kCTFontPriorityAttribute: *foundation.String;
extern "c" const kCTFontEnabledAttribute: *foundation.String;
extern "c" const kCTFontDownloadableAttribute: *foundation.String;
extern "c" const kCTFontDownloadedAttribute: *foundation.String;
}; };
test "descriptor" { test "descriptor" {