pkg/macos: bounding rects for text

This commit is contained in:
Mitchell Hashimoto
2022-10-07 21:23:34 -07:00
parent 67bb68b39f
commit 833fd0e49a
4 changed files with 62 additions and 11 deletions

View File

@ -3,7 +3,7 @@ pub usingnamespace @import("graphics/affine_transform.zig");
pub usingnamespace @import("graphics/bitmap_context.zig"); pub usingnamespace @import("graphics/bitmap_context.zig");
pub usingnamespace @import("graphics/color_space.zig"); pub usingnamespace @import("graphics/color_space.zig");
pub usingnamespace @import("graphics/font.zig"); pub usingnamespace @import("graphics/font.zig");
pub usingnamespace @import("graphics/point.zig"); pub usingnamespace @import("graphics/geometry.zig");
test { test {
@import("std").testing.refAllDecls(@This()); @import("std").testing.refAllDecls(@This());

View File

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

View File

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

View File

@ -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() { pub fn copyAttribute(self: *Font, comptime attr: text.FontAttribute) attr.Value() {
return @intToPtr(attr.Value(), @ptrToInt(c.CTFontCopyAttribute( return @intToPtr(attr.Value(), @ptrToInt(c.CTFontCopyAttribute(
@ptrCast(c.CTFontRef, self), @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 { test {
const testing = std.testing; const testing = std.testing;
@ -81,6 +103,17 @@ test {
)); ));
try testing.expect(glyphs[0] > 0); 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 // Draw
{ {
const cs = try graphics.ColorSpace.createDeviceGray(); const cs = try graphics.ColorSpace.createDeviceGray();