Files
wehub-resource-sync bd44b5aa08
Test / build-libghostty-vt (x86_64-windows-gnu) (push) Has been cancelled
Test / build-libghostty-vt-macos (aarch64-ios) (push) Has been cancelled
Test / build-libghostty-vt-macos (aarch64-macos) (push) Has been cancelled
Test / build-libghostty-vt-macos (x86_64-macos) (push) Has been cancelled
Test / build-nix (namespace-profile-ghostty-md-arm64) (push) Has been cancelled
Test / build-dist (push) Has been cancelled
Test / GTK x11=true wayland=true (push) Has been cancelled
Test / Build -Dsimd=false (push) Has been cancelled
Test / Build -Dsimd=true (push) Has been cancelled
Test / Build -Dsentry=false (push) Has been cancelled
Test / Build -Dsentry=true (push) Has been cancelled
Test / zig-fmt (push) Has been cancelled
Test / GitHub Actions Pins (push) Has been cancelled
Test / prettier (push) Has been cancelled
Test / swiftlint (push) Has been cancelled
Test / alejandra (push) Has been cancelled
Test / typos (push) Has been cancelled
Nix / Required Checks: Nix (push) Has been cancelled
Nix / check-zig-cache-hash (push) Has been cancelled
Test / skip (push) Has been cancelled
Test / Required Checks: Test (push) Has been cancelled
Test / build-bench (push) Has been cancelled
Test / list-examples (push) Has been cancelled
Test / Example ${{ matrix.dir }} (Windows) (push) Has been cancelled
Test / build-cmake (push) Has been cancelled
Test / test-lib-vt-pkgconfig (push) Has been cancelled
Test / build-flatpak (push) Has been cancelled
Test / build-snap (push) Has been cancelled
Test / build-libghostty-vt (aarch64-linux) (push) Has been cancelled
Test / build-libghostty-vt (aarch64-macos) (push) Has been cancelled
Test / build-libghostty-vt (wasm32-freestanding) (push) Has been cancelled
Test / build-libghostty-vt (x86_64-linux) (push) Has been cancelled
Test / build-libghostty-vt (x86_64-linux-musl) (push) Has been cancelled
Test / build-libghostty-vt (x86_64-macos) (push) Has been cancelled
Test / build-libghostty-vt-android (aarch64-linux-android) (push) Has been cancelled
Test / build-libghostty-vt-android (arm-linux-androideabi) (push) Has been cancelled
Test / build-libghostty-vt-android (x86_64-linux-android) (push) Has been cancelled
Test / build-libghostty-vt-windows (push) Has been cancelled
Test / build-libghostty-windows-gnu (push) Has been cancelled
Test / build-linux (namespace-profile-ghostty-md) (push) Has been cancelled
Test / build-linux (namespace-profile-ghostty-md-arm64) (push) Has been cancelled
Test / build-linux-libghostty (push) Has been cancelled
Test / build-nix (namespace-profile-ghostty-md) (push) Has been cancelled
Test / build-dist-lib-vt (push) Has been cancelled
Test / trigger-snap (push) Has been cancelled
Test / trigger-flatpak (push) Has been cancelled
Test / build-macos (push) Has been cancelled
Test / build-macos-freetype (push) Has been cancelled
Test / test (push) Has been cancelled
Test / test-lib-vt (push) Has been cancelled
Test / GTK x11=false wayland=false (push) Has been cancelled
Test / GTK x11=true wayland=false (push) Has been cancelled
Test / GTK x11=false wayland=true (push) Has been cancelled
Test / test-macos (push) Has been cancelled
Test / test-windows (push) Has been cancelled
Test / Build -Di18n=false (push) Has been cancelled
Test / Build -Di18n=true (push) Has been cancelled
Test / Build test/fuzz-libghostty (push) Has been cancelled
Test / shellcheck (push) Has been cancelled
Test / translations (push) Has been cancelled
Test / blueprint-compiler (push) Has been cancelled
Test / Test pkg/wuffs (push) Has been cancelled
Test / Test build on Debian 13 (push) Has been cancelled
Test / valgrind (push) Has been cancelled
Test / Example ${{ matrix.dir }} (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:02:48 +08:00

140 lines
4.5 KiB
Zig

const std = @import("std");
const Allocator = std.mem.Allocator;
const foundation = @import("../foundation.zig");
const text = @import("../text.zig");
const c = @import("c.zig").c;
pub const FontCollection = opaque {
pub fn createFromAvailableFonts() Allocator.Error!*FontCollection {
return @as(
?*FontCollection,
@ptrFromInt(@intFromPtr(c.CTFontCollectionCreateFromAvailableFonts(null))),
) orelse Allocator.Error.OutOfMemory;
}
pub fn createWithFontDescriptors(descs: *foundation.Array) Allocator.Error!*FontCollection {
return @as(
?*FontCollection,
@ptrFromInt(@intFromPtr(c.CTFontCollectionCreateWithFontDescriptors(
@ptrCast(descs),
null,
))),
) orelse Allocator.Error.OutOfMemory;
}
pub fn release(self: *FontCollection) void {
c.CFRelease(self);
}
pub fn createMatchingFontDescriptors(self: *FontCollection) *foundation.Array {
const result = c.CTFontCollectionCreateMatchingFontDescriptors(@ptrCast(self));
if (result) |ptr| return @ptrFromInt(@intFromPtr(ptr));
// If we have no results, we create an empty array. This is not
// exactly matching the Mac API. We can fix this later if we want
// but I chose this to make it slightly more Zig-like at the cost
// of some memory in the rare case.
return foundation.Array.create(anyopaque, &[_]*const anyopaque{}) catch unreachable;
}
};
fn debugDumpList(list: *foundation.Array) !void {
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).?;
var family_buf: [128]u8 = undefined;
const family = desc.copyAttribute(.family_name);
defer family.release();
const family_cstr = family.cstring(&family_buf, .utf8).?;
var buf2: [128]u8 = undefined;
const url = desc.copyAttribute(.url);
defer url.release();
const path = path: {
const blank = try foundation.String.createWithBytes("", .utf8, false);
defer blank.release();
const path = url.copyPath() orelse break :path "<no path>";
defer path.release();
const decoded = try foundation.URL.createStringByReplacingPercentEscapes(
path,
blank,
);
defer decoded.release();
break :path decoded.cstring(&buf2, .utf8) orelse
"<path cannot be converted to string>";
};
std.log.warn("i={d} name={s} family={s} path={s}", .{ i, cstr, family_cstr, path });
}
}
}
test "collection" {
const testing = std.testing;
const v = try FontCollection.createFromAvailableFonts();
defer v.release();
const list = v.createMatchingFontDescriptors();
defer list.release();
try testing.expect(list.getCount() > 0);
}
test "from descriptors" {
const testing = std.testing;
const name = try foundation.String.createWithBytes("AppleColorEmoji", .utf8, false);
defer name.release();
const desc = try text.FontDescriptor.createWithNameAndSize(name, 12);
defer desc.release();
var desc_arr = [_]*const text.FontDescriptor{desc};
const arr = try foundation.Array.create(text.FontDescriptor, &desc_arr);
defer arr.release();
const v = try FontCollection.createWithFontDescriptors(arr);
defer v.release();
const list = v.createMatchingFontDescriptors();
defer list.release();
try testing.expect(list.getCount() > 0);
// try debugDumpList(list);
}
test "from descriptors no match" {
const testing = std.testing;
const name = try foundation.String.createWithBytes("ThisShouldNeverExist", .utf8, false);
defer name.release();
const desc = try text.FontDescriptor.createWithNameAndSize(name, 12);
defer desc.release();
var desc_arr = [_]*const text.FontDescriptor{desc};
const arr = try foundation.Array.create(text.FontDescriptor, &desc_arr);
defer arr.release();
const v = try FontCollection.createWithFontDescriptors(arr);
defer v.release();
const list = v.createMatchingFontDescriptors();
defer list.release();
try testing.expect(list.getCount() == 0);
//try debugDumpList(list);
}