pkg/macos: path

This commit is contained in:
Mitchell Hashimoto
2022-10-08 17:39:25 -07:00
parent 061f7f6a30
commit cc14344bf8
3 changed files with 69 additions and 0 deletions

View File

@ -4,6 +4,7 @@ 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/geometry.zig"); pub usingnamespace @import("graphics/geometry.zig");
pub usingnamespace @import("graphics/path.zig");
test { test {
@import("std").testing.refAllDecls(@This()); @import("std").testing.refAllDecls(@This());

View File

@ -13,6 +13,10 @@ pub const Rect = extern struct {
origin: Point, origin: Point,
size: Size, 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 { pub fn cval(self: Rect) c.struct_CGRect {
return @bitCast(c.struct_CGRect, self); return @bitCast(c.struct_CGRect, self);
} }

View File

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