From c48ddcecd780f9a92d1123353ed6cc6520fded29 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 7 Oct 2022 21:04:30 -0700 Subject: [PATCH] pkg/macos: font draw glyphs --- pkg/macos/graphics.zig | 1 + pkg/macos/graphics/point.zig | 10 ++++++++++ pkg/macos/text/font.zig | 31 +++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+) create mode 100644 pkg/macos/graphics/point.zig diff --git a/pkg/macos/graphics.zig b/pkg/macos/graphics.zig index 690d45e5b..d11870d95 100644 --- a/pkg/macos/graphics.zig +++ b/pkg/macos/graphics.zig @@ -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()); diff --git a/pkg/macos/graphics/point.zig b/pkg/macos/graphics/point.zig new file mode 100644 index 000000000..8a440ac28 --- /dev/null +++ b/pkg/macos/graphics/point.zig @@ -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); + } +}; diff --git a/pkg/macos/text/font.zig b/pkg/macos/text/font.zig index 0ffc37015..d0e75e478 100644 --- a/pkg/macos/text/font.zig +++ b/pkg/macos/text/font.zig @@ -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, + ); + } }