aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/libghostty-vt/pkg/macos/graphics/path.zig
blob: 7f549709dd9bb062daab1db444a0a39d22623229 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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").c;

pub const Path = opaque {
    pub fn createWithRect(
        rect: graphics.Rect,
        transform: ?*const graphics.AffineTransform,
    ) Allocator.Error!*Path {
        return @as(
            ?*Path,
            @ptrFromInt(@intFromPtr(c.CGPathCreateWithRect(
                @bitCast(rect),
                @ptrCast(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 @as(
            ?*MutablePath,
            @ptrFromInt(@intFromPtr(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(self),
            @ptrCast(transform),
            @bitCast(rect),
        );
    }

    pub fn getBoundingBox(self: *MutablePath) graphics.Rect {
        return @bitCast(c.CGPathGetBoundingBox(@ptrCast(self)));
    }
};

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