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/base.zig");
pub usingnamespace @import("foundation/dictionary.zig");

View File

@ -1,6 +1,7 @@
const std = @import("std");
const Allocator = std.mem.Allocator;
const foundation = @import("../foundation.zig");
const c = @import("c.zig");
pub const String = opaque {
pub fn createWithBytes(
@ -8,21 +9,24 @@ pub const String = opaque {
encoding: StringEncoding,
external: bool,
) Allocator.Error!*String {
return CFStringCreateWithBytes(
return @intToPtr(?*String, @ptrToInt(c.CFStringCreateWithBytes(
null,
bs.ptr,
bs.len,
@intCast(c_long, bs.len),
@enumToInt(encoding),
external,
) orelse Allocator.Error.OutOfMemory;
@boolToInt(external),
))) orelse Allocator.Error.OutOfMemory;
}
pub fn release(self: *String) void {
foundation.CFRelease(self);
c.CFRelease(self);
}
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(
@ -32,37 +36,32 @@ pub const String = opaque {
) foundation.ComparisonResult {
return @intToEnum(
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 {
if (CFStringGetCString(
self,
if (c.CFStringGetCString(
@ptrCast(c.CFStringRef, self),
buf.ptr,
buf.len,
@intCast(c_long, buf.len),
@enumToInt(encoding),
) == 0) return null;
return std.mem.sliceTo(buf, 0);
}
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;
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 {

View File

@ -2,26 +2,28 @@ const std = @import("std");
const Allocator = std.mem.Allocator;
const foundation = @import("../foundation.zig");
const text = @import("../text.zig");
const c = @import("c.zig");
pub const FontCollection = opaque {
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 {
foundation.CFRelease(self);
c.CFRelease(self);
}
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" {

View File

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