From 49c9c21d523f01ccd4c6493cb8e590f72295cf54 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 1 Oct 2022 11:06:07 -0700 Subject: [PATCH] macos: test listing fonts --- pkg/macos/foundation/array.zig | 9 ++++++--- pkg/macos/text/font_collection.zig | 13 +++++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/pkg/macos/foundation/array.zig b/pkg/macos/foundation/array.zig index 927269627..9ab5fa293 100644 --- a/pkg/macos/foundation/array.zig +++ b/pkg/macos/foundation/array.zig @@ -20,8 +20,11 @@ pub const Array = opaque { return CFArrayGetCount(self); } - pub fn getValueAtIndex(self: *Array, comptime T: type, idx: usize) *const T { - return @ptrCast(*const T, CFArrayGetValueAtIndex(self, idx)); + /// Note the return type is actually a `*const T` but we strip the + /// constness so that further API calls work correctly. The Foundation + /// API doesn't properly mark things const/non-const. + pub fn getValueAtIndex(self: *Array, comptime T: type, idx: usize) *T { + return @ptrCast(*T, CFArrayGetValueAtIndex(self, idx)); } pub extern "c" fn CFArrayCreate( @@ -31,7 +34,7 @@ pub const Array = opaque { callbacks: ?*const anyopaque, ) ?*Array; pub extern "c" fn CFArrayGetCount(*Array) usize; - pub extern "c" fn CFArrayGetValueAtIndex(*Array, usize) *const anyopaque; + pub extern "c" fn CFArrayGetValueAtIndex(*Array, usize) *anyopaque; extern "c" var kCFTypeArrayCallBacks: anyopaque; }; diff --git a/pkg/macos/text/font_collection.zig b/pkg/macos/text/font_collection.zig index 9335c7658..1b6168719 100644 --- a/pkg/macos/text/font_collection.zig +++ b/pkg/macos/text/font_collection.zig @@ -1,6 +1,7 @@ const std = @import("std"); const Allocator = std.mem.Allocator; const foundation = @import("../foundation.zig"); +const text = @import("../text.zig"); pub const FontCollection = opaque { pub fn createFromAvailableFonts() Allocator.Error!*FontCollection { @@ -33,4 +34,16 @@ test "collection" { defer list.release(); try testing.expect(list.getCount() > 0); + + // var i: usize = 0; + // while (i < list.getCount()) : (i += 1) { + // const desc = list.getValueAtIndex(text.FontDescriptor, i); + // { + // var buf: [128]u8 = undefined; + // const name = desc.copyAttribute(.name); + // defer name.release(); + // const cstr = name.cstring(&buf, .utf8).?; + // std.log.warn("i={d} v={s}", .{ i, cstr }); + // } + // } }