From 061f7f6a302174cfd4ebc89a3b55d2a446fdd501 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 8 Oct 2022 17:29:50 -0700 Subject: [PATCH] pkg/macos: text framesetter --- pkg/macos/foundation/attributed_string.zig | 2 ++ pkg/macos/text.zig | 1 + pkg/macos/text/framesetter.zig | 35 ++++++++++++++++++++++ 3 files changed, 38 insertions(+) create mode 100644 pkg/macos/text/framesetter.zig diff --git a/pkg/macos/foundation/attributed_string.zig b/pkg/macos/foundation/attributed_string.zig index 1194924af..fbe972acf 100644 --- a/pkg/macos/foundation/attributed_string.zig +++ b/pkg/macos/foundation/attributed_string.zig @@ -5,6 +5,8 @@ const foundation = @import("../foundation.zig"); const text = @import("../text.zig"); const c = @import("c.zig"); +pub const AttributedString = opaque {}; + pub const MutableAttributedString = opaque { pub fn create(cap: usize) Allocator.Error!*MutableAttributedString { return @intToPtr( diff --git a/pkg/macos/text.zig b/pkg/macos/text.zig index efdcf1568..5875441ec 100644 --- a/pkg/macos/text.zig +++ b/pkg/macos/text.zig @@ -2,6 +2,7 @@ pub usingnamespace @import("text/font.zig"); pub usingnamespace @import("text/font_collection.zig"); pub usingnamespace @import("text/font_descriptor.zig"); pub usingnamespace @import("text/font_manager.zig"); +pub usingnamespace @import("text/framesetter.zig"); test { @import("std").testing.refAllDecls(@This()); diff --git a/pkg/macos/text/framesetter.zig b/pkg/macos/text/framesetter.zig new file mode 100644 index 000000000..e62ff125b --- /dev/null +++ b/pkg/macos/text/framesetter.zig @@ -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(); +}