pkg/macos: some font metrics functions

This commit is contained in:
Mitchell Hashimoto
2022-10-08 11:34:45 -07:00
parent f393049988
commit c9a335646e

View File

@ -76,6 +76,22 @@ pub const Font = opaque {
)); ));
} }
pub fn getAdvancesForGlyphs(
self: *Font,
orientation: FontOrientation,
glyphs: []const graphics.Glyph,
advances: ?[]graphics.Size,
) f64 {
if (advances) |s| assert(glyphs.len == s.len);
return c.CTFontGetAdvancesForGlyphs(
@ptrCast(c.CTFontRef, self),
@enumToInt(orientation),
glyphs.ptr,
@ptrCast(?[*]c.struct_CGSize, if (advances) |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),
@ -95,6 +111,14 @@ pub const Font = opaque {
@ptrCast(c.CTFontRef, self), @ptrCast(c.CTFontRef, self),
)); ));
} }
pub fn getUnderlinePosition(self: *Font) f64 {
return c.CTFontGetUnderlinePosition(@ptrCast(c.CTFontRef, self));
}
pub fn getUnderlineThickness(self: *Font) f64 {
return c.CTFontGetUnderlineThickness(@ptrCast(c.CTFontRef, self));
}
}; };
pub const FontOrientation = enum(c_uint) { pub const FontOrientation = enum(c_uint) {
@ -138,6 +162,17 @@ test {
try testing.expect(singles[0].size.width > 0); try testing.expect(singles[0].size.width > 0);
} }
// Advances
{
var advance = font.getAdvancesForGlyphs(.horizontal, &glyphs, null);
try testing.expect(advance > 0);
var singles: [1]graphics.Size = undefined;
advance = font.getAdvancesForGlyphs(.horizontal, &glyphs, &singles);
try testing.expect(advance > 0);
try testing.expect(singles[0].width > 0);
}
// Draw // Draw
{ {
const cs = try graphics.ColorSpace.createDeviceGray(); const cs = try graphics.ColorSpace.createDeviceGray();