pkg/macos: add mutable dictionaries, flesh out font descriptor

This commit is contained in:
Mitchell Hashimoto
2022-10-01 20:50:51 -07:00
parent 20f80c4fb7
commit decbaafc6a
3 changed files with 52 additions and 0 deletions

View File

@ -41,6 +41,29 @@ pub const Dictionary = opaque {
}
};
pub const MutableDictionary = opaque {
pub fn create(cap: usize) Allocator.Error!*MutableDictionary {
return @intToPtr(?*MutableDictionary, @ptrToInt(c.CFDictionaryCreateMutable(
null,
@intCast(c.CFIndex, cap),
&c.kCFTypeDictionaryKeyCallBacks,
&c.kCFTypeDictionaryValueCallBacks,
))) orelse Allocator.Error.OutOfMemory;
}
pub fn release(self: *MutableDictionary) void {
foundation.CFRelease(self);
}
pub fn setValue(self: *MutableDictionary, key: ?*const anyopaque, value: ?*const anyopaque) void {
c.CFDictionarySetValue(
@ptrCast(c.CFMutableDictionaryRef, self),
key,
value,
);
}
};
test "dictionary" {
const testing = std.testing;
@ -56,3 +79,22 @@ test "dictionary" {
try testing.expect(dict.getValue(foundation.String, c.kCFURLIsPurgeableKey) != null);
try testing.expect(dict.getValue(foundation.String, c.kCFURLIsVolumeKey) == null);
}
test "mutable dictionary" {
const testing = std.testing;
const dict = try MutableDictionary.create(0);
defer dict.release();
const str = try foundation.String.createWithBytes("hello", .unicode, false);
defer str.release();
dict.setValue(c.kCFURLIsPurgeableKey, str);
{
const imm = @ptrCast(*Dictionary, dict);
try testing.expectEqual(@as(usize, 1), imm.getCount());
try testing.expect(imm.getValue(foundation.String, c.kCFURLIsPurgeableKey) != null);
try testing.expect(imm.getValue(foundation.String, c.kCFURLIsVolumeKey) == null);
}
}

3
pkg/macos/text/c.zig Normal file
View File

@ -0,0 +1,3 @@
pub usingnamespace @cImport({
@cInclude("CoreText/CoreText.h");
});

View File

@ -11,6 +11,13 @@ pub const FontDescriptor = opaque {
) orelse Allocator.Error.OutOfMemory;
}
pub fn createWithAttributes(dict: *foundation.Dictionary) Allocator.Error!*FontDescriptor {
return @intToPtr(
?*FontDescriptor,
@ptrToInt(c.CTFontDescriptorCreateWithAttributes(@ptrCast(c.CFDictionaryRef, dict))),
) orelse Allocator.Error.OutOfMemory;
}
pub fn release(self: *FontDescriptor) void {
c.CFRelease(self);
}