pkg/macos: font draw glyphs

This commit is contained in:
Mitchell Hashimoto
2022-10-07 21:04:30 -07:00
parent 61955c5d8d
commit c48ddcecd7
3 changed files with 42 additions and 0 deletions

View File

@ -3,6 +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");
test {
@import("std").testing.refAllDecls(@This());

View File

@ -0,0 +1,10 @@
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

@ -32,6 +32,22 @@ pub const Font = opaque {
);
}
pub fn drawGlyphs(
self: *Font,
glyphs: []const graphics.Glyph,
positions: []const graphics.Point,
context: anytype, // Must be some context type from graphics
) void {
assert(positions.len == glyphs.len);
c.CTFontDrawGlyphs(
@ptrCast(c.CTFontRef, self),
glyphs.ptr,
@ptrCast([*]const c.struct_CGPoint, positions.ptr),
glyphs.len,
@ptrCast(c.CGContextRef, context),
);
}
pub fn copyAttribute(self: *Font, comptime attr: text.FontAttribute) attr.Value() {
return @intToPtr(attr.Value(), @ptrToInt(c.CTFontCopyAttribute(
@ptrCast(c.CTFontRef, self),
@ -64,4 +80,19 @@ test {
&glyphs,
));
try testing.expect(glyphs[0] > 0);
// Draw
{
const cs = try graphics.ColorSpace.createDeviceGray();
defer cs.release();
const ctx = try graphics.BitmapContext.create(null, 80, 80, 8, 80, cs);
defer ctx.release();
var pos = [_]graphics.Point{.{ .x = 0, .y = 0 }};
font.drawGlyphs(
&glyphs,
&pos,
ctx,
);
}
}