summaryrefslogtreecommitdiff
path: root/pkg/fontconfig/common.zig
blob: 6ad6f5685c1d1940ecd4a1760031a3975d93fb5d (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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
const std = @import("std");
const c = @import("c.zig").c;
const Error = @import("main.zig").Error;

pub const Weight = enum(c_uint) {
    thin = c.FC_WEIGHT_THIN,
    extralight = c.FC_WEIGHT_EXTRALIGHT,
    light = c.FC_WEIGHT_LIGHT,
    demilight = c.FC_WEIGHT_DEMILIGHT,
    book = c.FC_WEIGHT_BOOK,
    regular = c.FC_WEIGHT_REGULAR,
    medium = c.FC_WEIGHT_MEDIUM,
    demibold = c.FC_WEIGHT_DEMIBOLD,
    bold = c.FC_WEIGHT_BOLD,
    extrabold = c.FC_WEIGHT_EXTRABOLD,
    black = c.FC_WEIGHT_BLACK,
    extrablack = c.FC_WEIGHT_EXTRABLACK,
};

pub const Slant = enum(c_uint) {
    roman = c.FC_SLANT_ROMAN,
    italic = c.FC_SLANT_ITALIC,
    oblique = c.FC_SLANT_OBLIQUE,
};

pub const Spacing = enum(c_uint) {
    proportional = c.FC_PROPORTIONAL,
    dual = c.FC_DUAL,
    mono = c.FC_MONO,
    charcell = c.FC_CHARCELL,
};

pub const Property = enum {
    family,
    style,
    slant,
    weight,
    size,
    aspect,
    pixel_size,
    spacing,
    foundry,
    antialias,
    hinting,
    hint_style,
    vertical_layout,
    autohint,
    global_advance,
    width,
    file,
    index,
    ft_face,
    rasterizer,
    outline,
    scalable,
    color,
    variable,
    scale,
    symbol,
    dpi,
    rgba,
    minspace,
    source,
    charset,
    lang,
    fontversion,
    fullname,
    familylang,
    stylelang,
    fullnamelang,
    capability,
    embolden,
    embedded_bitmap,
    decorative,
    lcd_filter,
    font_features,
    font_variations,
    namelang,
    prgname,
    hash,
    postscript_name,
    font_has_hint,
    order,

    pub fn cval(self: Property) [:0]const u8 {
        @setEvalBranchQuota(10_000);
        inline for (@typeInfo(Property).@"enum".fields) |field| {
            if (self == @field(Property, field.name)) {
                // Build our string in a comptime context so it is a binary
                // constant and not stack allocated.
                return comptime name: {
                    // Replace _ with ""
                    var buf: [field.name.len]u8 = undefined;
                    const count = std.mem.replace(u8, field.name, "_", "", &buf);
                    const replaced = buf[0 .. field.name.len - count];

                    // Build our string
                    var name: [replaced.len:0]u8 = undefined;
                    @memcpy(&name, replaced);
                    name[replaced.len] = 0;
                    const final = name;
                    break :name &final;
                };
            }
        }

        unreachable;
    }

    test "cval" {
        const testing = std.testing;
        try testing.expectEqualStrings("family", Property.family.cval());
        try testing.expectEqualStrings("pixelsize", Property.pixel_size.cval());
    }
};

pub const Result = enum(c_uint) {
    match = c.FcResultMatch,
    no_match = c.FcResultNoMatch,
    type_mismatch = c.FcResultTypeMismatch,
    no_id = c.FcResultNoId,
    out_of_memory = c.FcResultOutOfMemory,

    pub fn toError(self: Result) Error!void {
        return switch (self) {
            .match => {},
            .no_match => Error.FontconfigNoMatch,
            .type_mismatch => Error.FontconfigTypeMismatch,
            .no_id => Error.FontconfigNoId,
            .out_of_memory => Error.OutOfMemory,
        };
    }
};

pub const MatchKind = enum(c_uint) {
    pattern = c.FcMatchPattern,
    font = c.FcMatchFont,
    scan = c.FcMatchScan,
};