diff --git a/pkg/macos/graphics.zig b/pkg/macos/graphics.zig index e901c0ab5..ba8be1657 100644 --- a/pkg/macos/graphics.zig +++ b/pkg/macos/graphics.zig @@ -4,6 +4,7 @@ pub usingnamespace @import("graphics/bitmap_context.zig"); pub usingnamespace @import("graphics/color_space.zig"); pub usingnamespace @import("graphics/font.zig"); pub usingnamespace @import("graphics/geometry.zig"); +pub usingnamespace @import("graphics/path.zig"); test { @import("std").testing.refAllDecls(@This()); diff --git a/pkg/macos/graphics/geometry.zig b/pkg/macos/graphics/geometry.zig index 34345d5bc..bd3caf0db 100644 --- a/pkg/macos/graphics/geometry.zig +++ b/pkg/macos/graphics/geometry.zig @@ -13,6 +13,10 @@ pub const Rect = extern struct { origin: Point, size: Size, + pub fn init(x: f64, y: f64, width: f64, height: f64) Rect { + return @bitCast(Rect, c.CGRectMake(x, y, width, height)); + } + pub fn cval(self: Rect) c.struct_CGRect { return @bitCast(c.struct_CGRect, self); } diff --git a/pkg/macos/graphics/path.zig b/pkg/macos/graphics/path.zig new file mode 100644 index 000000000..8b3280572 --- /dev/null +++ b/pkg/macos/graphics/path.zig @@ -0,0 +1,64 @@ +const std = @import("std"); +const assert = std.debug.assert; +const Allocator = std.mem.Allocator; +const foundation = @import("../foundation.zig"); +const graphics = @import("../graphics.zig"); +const c = @import("c.zig"); + +pub const Path = opaque { + pub fn createWithRect( + rect: graphics.Rect, + transform: ?*const graphics.AffineTransform, + ) Allocator.Error!*Path { + return @intToPtr( + ?*Path, + @ptrToInt(c.CGPathCreateWithRect( + rect.cval(), + @ptrCast(?[*]const c.struct_CGAffineTransform, transform), + )), + ) orelse Allocator.Error.OutOfMemory; + } + + pub fn release(self: *Path) void { + foundation.CFRelease(self); + } +}; + +pub const MutablePath = opaque { + pub fn create() Allocator.Error!*MutablePath { + return @intToPtr( + ?*MutablePath, + @ptrToInt(c.CGPathCreateMutable()), + ) orelse Allocator.Error.OutOfMemory; + } + + pub fn release(self: *MutablePath) void { + foundation.CFRelease(self); + } + + pub fn addRect( + self: *MutablePath, + transform: ?*const graphics.AffineTransform, + rect: graphics.Rect, + ) void { + c.CGPathAddRect( + @ptrCast(c.CGMutablePathRef, self), + @ptrCast(?[*]const c.struct_CGAffineTransform, transform), + rect.cval(), + ); + } +}; + +test "mutable path" { + //const testing = std.testing; + + const path = try MutablePath.create(); + defer path.release(); + + path.addRect(null, graphics.Rect.init(0, 0, 100, 200)); +} + +test "path from rect" { + const path = try Path.createWithRect(graphics.Rect.init(0, 0, 100, 200), null); + defer path.release(); +}