mirror of
https://github.com/ghostty-org/ghostty.git
synced 2025-08-02 14:57:31 +03:00
pkg/macos: font initializes, get glyphs
This commit is contained in:
@ -1,4 +1,5 @@
|
|||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
const assert = std.debug.assert;
|
||||||
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");
|
const c = @import("c.zig");
|
||||||
@ -100,6 +101,14 @@ pub const StringEncoding = enum(u32) {
|
|||||||
utf32_le = 0x1c000100,
|
utf32_le = 0x1c000100,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
pub fn stringGetSurrogatePairForLongCharacter(
|
||||||
|
ch: u32,
|
||||||
|
surrogates: []u16,
|
||||||
|
) bool {
|
||||||
|
assert(surrogates.len >= 2);
|
||||||
|
return c.CFStringGetSurrogatePairForLongCharacter(ch, surrogates.ptr) == 1;
|
||||||
|
}
|
||||||
|
|
||||||
test "string" {
|
test "string" {
|
||||||
const testing = std.testing;
|
const testing = std.testing;
|
||||||
|
|
||||||
@ -119,3 +128,10 @@ test "string" {
|
|||||||
try testing.expectEqualStrings("hello world", cstr);
|
try testing.expectEqualStrings("hello world", cstr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
test "unichar" {
|
||||||
|
const testing = std.testing;
|
||||||
|
|
||||||
|
var unichars: [2]u16 = undefined;
|
||||||
|
try testing.expect(!stringGetSurrogatePairForLongCharacter('A', &unichars));
|
||||||
|
}
|
||||||
|
6
pkg/macos/graphics.zig
Normal file
6
pkg/macos/graphics.zig
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
pub const c = @import("graphics/c.zig");
|
||||||
|
pub usingnamespace @import("graphics/font.zig");
|
||||||
|
|
||||||
|
test {
|
||||||
|
@import("std").testing.refAllDecls(@This());
|
||||||
|
}
|
3
pkg/macos/graphics/c.zig
Normal file
3
pkg/macos/graphics/c.zig
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
pub usingnamespace @cImport({
|
||||||
|
@cInclude("CoreGraphics/CoreGraphics.h");
|
||||||
|
});
|
3
pkg/macos/graphics/font.zig
Normal file
3
pkg/macos/graphics/font.zig
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
const c = @import("c.zig");
|
||||||
|
|
||||||
|
pub const Glyph = c.CGGlyph;
|
@ -1,4 +1,5 @@
|
|||||||
pub const foundation = @import("foundation.zig");
|
pub const foundation = @import("foundation.zig");
|
||||||
|
pub const graphics = @import("graphics.zig");
|
||||||
pub const text = @import("text.zig");
|
pub const text = @import("text.zig");
|
||||||
|
|
||||||
test {
|
test {
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
pub usingnamespace @import("text/font.zig");
|
||||||
pub usingnamespace @import("text/font_collection.zig");
|
pub usingnamespace @import("text/font_collection.zig");
|
||||||
pub usingnamespace @import("text/font_descriptor.zig");
|
pub usingnamespace @import("text/font_descriptor.zig");
|
||||||
|
|
||||||
|
53
pkg/macos/text/font.zig
Normal file
53
pkg/macos/text/font.zig
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
const std = @import("std");
|
||||||
|
const assert = std.debug.assert;
|
||||||
|
const Allocator = std.mem.Allocator;
|
||||||
|
const foundation = @import("../foundation.zig");
|
||||||
|
const graphics = @import("../graphics.zig");
|
||||||
|
const text = @import("../text.zig");
|
||||||
|
const c = @import("c.zig");
|
||||||
|
|
||||||
|
pub const Font = opaque {
|
||||||
|
pub fn createWithFontDescriptor(desc: *text.FontDescriptor, size: f32) Allocator.Error!*Font {
|
||||||
|
return @intToPtr(
|
||||||
|
?*Font,
|
||||||
|
@ptrToInt(c.CTFontCreateWithFontDescriptor(
|
||||||
|
@ptrCast(c.CTFontDescriptorRef, desc),
|
||||||
|
size,
|
||||||
|
null,
|
||||||
|
)),
|
||||||
|
) orelse Allocator.Error.OutOfMemory;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn release(self: *Font) void {
|
||||||
|
c.CFRelease(self);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn getGlyphsForCharacters(self: *Font, chars: []const u16, glyphs: []graphics.Glyph) bool {
|
||||||
|
assert(chars.len == glyphs.len);
|
||||||
|
return c.CTFontGetGlyphsForCharacters(
|
||||||
|
@ptrCast(c.CTFontRef, self),
|
||||||
|
chars.ptr,
|
||||||
|
glyphs.ptr,
|
||||||
|
@intCast(c_long, chars.len),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
test {
|
||||||
|
const testing = std.testing;
|
||||||
|
|
||||||
|
const name = try foundation.String.createWithBytes("Monaco", .utf8, false);
|
||||||
|
defer name.release();
|
||||||
|
const desc = try text.FontDescriptor.createWithNameAndSize(name, 12);
|
||||||
|
defer desc.release();
|
||||||
|
|
||||||
|
const font = try Font.createWithFontDescriptor(desc, 12);
|
||||||
|
defer font.release();
|
||||||
|
|
||||||
|
var glyphs = [1]graphics.Glyph{0};
|
||||||
|
try testing.expect(font.getGlyphsForCharacters(
|
||||||
|
&[_]u16{'A'},
|
||||||
|
&glyphs,
|
||||||
|
));
|
||||||
|
try testing.expect(glyphs[0] > 0);
|
||||||
|
}
|
Reference in New Issue
Block a user