diff --git a/pkg/macos/graphics.zig b/pkg/macos/graphics.zig index d11870d95..e901c0ab5 100644 --- a/pkg/macos/graphics.zig +++ b/pkg/macos/graphics.zig @@ -3,7 +3,7 @@ pub usingnamespace @import("graphics/affine_transform.zig"); pub usingnamespace @import("graphics/bitmap_context.zig"); pub usingnamespace @import("graphics/color_space.zig"); pub usingnamespace @import("graphics/font.zig"); -pub usingnamespace @import("graphics/point.zig"); +pub usingnamespace @import("graphics/geometry.zig"); test { @import("std").testing.refAllDecls(@This()); diff --git a/pkg/macos/graphics/geometry.zig b/pkg/macos/graphics/geometry.zig new file mode 100644 index 000000000..34345d5bc --- /dev/null +++ b/pkg/macos/graphics/geometry.zig @@ -0,0 +1,28 @@ +const c = @import("c.zig"); + +pub const Point = extern struct { + x: c.CGFloat, + y: c.CGFloat, + + pub fn cval(self: Point) c.struct_CGPoint { + return @bitCast(c.struct_CGPoint, self); + } +}; + +pub const Rect = extern struct { + origin: Point, + size: Size, + + pub fn cval(self: Rect) c.struct_CGRect { + return @bitCast(c.struct_CGRect, self); + } +}; + +pub const Size = extern struct { + width: c.CGFloat, + height: c.CGFloat, + + pub fn cval(self: Size) c.struct_CGSize { + return @bitCast(c.struct_CGSize, self); + } +}; diff --git a/pkg/macos/graphics/point.zig b/pkg/macos/graphics/point.zig deleted file mode 100644 index 8a440ac28..000000000 --- a/pkg/macos/graphics/point.zig +++ /dev/null @@ -1,10 +0,0 @@ -const c = @import("c.zig"); - -pub const Point = extern struct { - x: c.CGFloat, - y: c.CGFloat, - - pub fn cval(self: Point) c.struct_CGPoint { - return @bitCast(c.struct_CGPoint, self); - } -}; diff --git a/pkg/macos/text/font.zig b/pkg/macos/text/font.zig index d0e75e478..ca4405594 100644 --- a/pkg/macos/text/font.zig +++ b/pkg/macos/text/font.zig @@ -48,6 +48,22 @@ pub const Font = opaque { ); } + pub fn getBoundingRectForGlyphs( + self: *Font, + orientation: FontOrientation, + glyphs: []const graphics.Glyph, + rects: ?[]graphics.Rect, + ) graphics.Rect { + if (rects) |s| assert(glyphs.len == s.len); + return @bitCast(graphics.Rect, c.CTFontGetBoundingRectsForGlyphs( + @ptrCast(c.CTFontRef, self), + @enumToInt(orientation), + glyphs.ptr, + @ptrCast(?[*]c.struct_CGRect, if (rects) |s| s.ptr else null), + @intCast(c_long, glyphs.len), + )); + } + pub fn copyAttribute(self: *Font, comptime attr: text.FontAttribute) attr.Value() { return @intToPtr(attr.Value(), @ptrToInt(c.CTFontCopyAttribute( @ptrCast(c.CTFontRef, self), @@ -63,6 +79,12 @@ pub const Font = opaque { } }; +pub const FontOrientation = enum(c_uint) { + default = c.kCTFontOrientationDefault, + horizontal = c.kCTFontOrientationHorizontal, + vertical = c.kCTFontOrientationVertical, +}; + test { const testing = std.testing; @@ -81,6 +103,17 @@ test { )); try testing.expect(glyphs[0] > 0); + // Bounding rect + { + var rect = font.getBoundingRectForGlyphs(.horizontal, &glyphs, null); + try testing.expect(rect.size.width > 0); + + var singles: [1]graphics.Rect = undefined; + rect = font.getBoundingRectForGlyphs(.horizontal, &glyphs, &singles); + try testing.expect(rect.size.width > 0); + try testing.expect(singles[0].size.width > 0); + } + // Draw { const cs = try graphics.ColorSpace.createDeviceGray();