pkg/macos: text framesetter

This commit is contained in:
Mitchell Hashimoto
2022-10-08 17:29:50 -07:00
parent 26280d4fe5
commit 061f7f6a30
3 changed files with 38 additions and 0 deletions

View File

@ -5,6 +5,8 @@ const foundation = @import("../foundation.zig");
const text = @import("../text.zig"); const text = @import("../text.zig");
const c = @import("c.zig"); const c = @import("c.zig");
pub const AttributedString = opaque {};
pub const MutableAttributedString = opaque { pub const MutableAttributedString = opaque {
pub fn create(cap: usize) Allocator.Error!*MutableAttributedString { pub fn create(cap: usize) Allocator.Error!*MutableAttributedString {
return @intToPtr( return @intToPtr(

View File

@ -2,6 +2,7 @@ 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");
pub usingnamespace @import("text/font_manager.zig"); pub usingnamespace @import("text/font_manager.zig");
pub usingnamespace @import("text/framesetter.zig");
test { test {
@import("std").testing.refAllDecls(@This()); @import("std").testing.refAllDecls(@This());

View File

@ -0,0 +1,35 @@
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 Framesetter = opaque {
pub fn createWithAttributedString(str: *foundation.AttributedString) Allocator.Error!*Framesetter {
return @intToPtr(
?*Framesetter,
@ptrToInt(c.CTFramesetterCreateWithAttributedString(
@ptrCast(c.CFAttributedStringRef, str),
)),
) orelse Allocator.Error.OutOfMemory;
}
pub fn release(self: *Framesetter) void {
foundation.CFRelease(self);
}
};
test {
const str = try foundation.MutableAttributedString.create(0);
defer str.release();
{
const rep = try foundation.String.createWithBytes("hello", .utf8, false);
defer rep.release();
str.replaceString(foundation.Range.init(0, 0), rep);
}
const fs = try Framesetter.createWithAttributedString(@ptrCast(*foundation.AttributedString, str));
defer fs.release();
}