pkg/macos: add many more text APIs

This commit is contained in:
Mitchell Hashimoto
2023-12-11 11:06:52 -08:00
parent 22a0869525
commit 0137f5b0d3
4 changed files with 71 additions and 4 deletions

View File

@ -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);
}

View File

@ -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,

View File

@ -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))));
}
};

View File

@ -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;
}
};