From 0137f5b0d3953b1f17f362f25f896bf98789f9b4 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 11 Dec 2023 11:06:52 -0800 Subject: [PATCH] pkg/macos: add many more text APIs --- pkg/macos/foundation/attributed_string.zig | 11 +++++++ pkg/macos/foundation/string.zig | 25 +++++++++++++++ pkg/macos/text/line.zig | 2 +- pkg/macos/text/run.zig | 37 ++++++++++++++++++++-- 4 files changed, 71 insertions(+), 4 deletions(-) diff --git a/pkg/macos/foundation/attributed_string.zig b/pkg/macos/foundation/attributed_string.zig index 0c7bb13c3..b25cdef76 100644 --- a/pkg/macos/foundation/attributed_string.zig +++ b/pkg/macos/foundation/attributed_string.zig @@ -6,6 +6,17 @@ const text = @import("../text.zig"); const c = @import("c.zig"); pub const AttributedString = opaque { + pub fn create( + str: *foundation.String, + attributes: *foundation.Dictionary, + ) Allocator.Error!*AttributedString { + return @constCast(@ptrCast(c.CFAttributedStringCreate( + null, + @ptrCast(str), + @ptrCast(attributes), + ) orelse return Allocator.Error.OutOfMemory)); + } + pub fn release(self: *AttributedString) void { foundation.CFRelease(self); } diff --git a/pkg/macos/foundation/string.zig b/pkg/macos/foundation/string.zig index 5f794ae66..b642201de 100644 --- a/pkg/macos/foundation/string.zig +++ b/pkg/macos/foundation/string.zig @@ -66,6 +66,31 @@ pub const String = opaque { } }; +pub const MutableString = opaque { + pub fn create(cap: usize) !*MutableString { + return @ptrCast(c.CFStringCreateMutable( + null, + @intCast(cap), + ) orelse return Allocator.Error.OutOfMemory); + } + + pub fn release(self: *MutableString) void { + foundation.CFRelease(self); + } + + pub fn string(self: *MutableString) *String { + return @ptrCast(self); + } + + pub fn appendCharacters(self: *MutableString, chars: []const u16) void { + c.CFStringAppendCharacters( + @ptrCast(self), + chars.ptr, + @intCast(chars.len), + ); + } +}; + pub const StringComparison = packed struct { case_insensitive: bool = false, _unused_2: bool = false, diff --git a/pkg/macos/text/line.zig b/pkg/macos/text/line.zig index a5d1b2888..c13220c8b 100644 --- a/pkg/macos/text/line.zig +++ b/pkg/macos/text/line.zig @@ -51,7 +51,7 @@ pub const Line = opaque { } pub fn getGlyphRuns(self: *Line) *foundation.Array { - return @ptrCast(c.CTLineGetGlyphRuns(@ptrCast(self))); + return @constCast(@ptrCast(c.CTLineGetGlyphRuns(@ptrCast(self)))); } }; diff --git a/pkg/macos/text/run.zig b/pkg/macos/text/run.zig index 83e3641ae..a774ad6f3 100644 --- a/pkg/macos/text/run.zig +++ b/pkg/macos/text/run.zig @@ -26,7 +26,11 @@ pub const Run = opaque { const len = self.getGlyphCount(); const ptr = try alloc.alloc(graphics.Glyph, len); errdefer alloc.free(ptr); - c.CTRunGetGlyphs(@ptrCast(self), .{ .location = 0, .len = 0 }, ptr); + c.CTRunGetGlyphs( + @ptrCast(self), + .{ .location = 0, .length = 0 }, + @ptrCast(ptr.ptr), + ); return ptr; } @@ -41,7 +45,11 @@ pub const Run = opaque { const len = self.getGlyphCount(); const ptr = try alloc.alloc(graphics.Point, len); errdefer alloc.free(ptr); - c.CTRunGetPositions(@ptrCast(self), .{ .location = 0, .len = 0 }, ptr); + c.CTRunGetPositions( + @ptrCast(self), + .{ .location = 0, .length = 0 }, + @ptrCast(ptr.ptr), + ); return ptr; } @@ -56,7 +64,30 @@ pub const Run = opaque { const len = self.getGlyphCount(); const ptr = try alloc.alloc(graphics.Size, len); errdefer alloc.free(ptr); - c.CTRunGetAdvances(@ptrCast(self), .{ .location = 0, .len = 0 }, ptr); + c.CTRunGetAdvances( + @ptrCast(self), + .{ .location = 0, .length = 0 }, + @ptrCast(ptr.ptr), + ); + return ptr; + } + + pub fn getStringIndicesPtr(self: *Run) []const usize { + const len = self.getGlyphCount(); + if (len == 0) return &.{}; + const ptr = c.CTRunGetStringIndicesPtr(@ptrCast(self)) orelse &.{}; + return ptr[0..len]; + } + + pub fn getStringIndices(self: *Run, alloc: Allocator) ![]const usize { + const len = self.getGlyphCount(); + const ptr = try alloc.alloc(usize, len); + errdefer alloc.free(ptr); + c.CTRunGetStringIndices( + @ptrCast(self), + .{ .location = 0, .length = 0 }, + @ptrCast(ptr.ptr), + ); return ptr; } };