summaryrefslogtreecommitdiff
path: root/pkg/fontconfig/font_set.zig
blob: c5a8c5f978ba4f7b3acab5b443ca129ad504d271 (plain)
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
const std = @import("std");
const assert = std.debug.assert;
const c = @import("c.zig").c;
const Pattern = @import("pattern.zig").Pattern;

pub const FontSet = opaque {
    pub fn create() *FontSet {
        return @ptrCast(c.FcFontSetCreate());
    }

    pub fn destroy(self: *FontSet) void {
        c.FcFontSetDestroy(self.cval());
    }

    pub fn fonts(self: *FontSet) []*Pattern {
        const empty: [0]*Pattern = undefined;
        const s = self.cval();
        if (s.fonts == null) return ∅
        const ptr: [*]*Pattern = @ptrCast(@alignCast(s.fonts));
        const len: usize = @intCast(s.nfont);
        return ptr[0..len];
    }

    pub fn add(self: *FontSet, pat: *Pattern) bool {
        return c.FcFontSetAdd(self.cval(), pat.cval()) == c.FcTrue;
    }

    pub fn print(self: *FontSet) void {
        c.FcFontSetPrint(self.cval());
    }

    pub inline fn cval(self: *FontSet) *c.struct__FcFontSet {
        return @ptrCast(@alignCast(self));
    }
};

test "create" {
    const testing = std.testing;

    var fs = FontSet.create();
    defer fs.destroy();

    try testing.expectEqual(@as(usize, 0), fs.fonts().len);
}