aboutsummaryrefslogtreecommitdiff
path: root/src/pangocairo.zig
blob: 24bbfd6c804a14e0a2ab09eeef660d61cc3a2c31 (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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
const c = @cImport({
    @cInclude("pango/pangocairo.h");
});

pub const pango = struct {
    pub const scale = c.PANGO_SCALE;

    pub const Layout = opaque {
        extern fn pango_cairo_create_layout(s: *cairo.Context) ?*Layout;
        pub fn createForCairo(s: *cairo.Context) !*Layout {
            return pango_cairo_create_layout(s) orelse return error.OutOfMemory;
        }

        extern fn pango_layout_set_font_description(l: *Layout, f: *FontDescription) void;
        pub const setFontDescription = pango_layout_set_font_description;

        extern fn pango_layout_set_text(l: *Layout, text: [*:0]const u8, len: i32) void;
        pub const setText = pango_layout_set_text;

        pub fn unref(l: *Layout) void {
            c.g_object_unref(l);
        }
    };

    pub const FontDescription = opaque {
        extern fn pango_font_description_new() ?*FontDescription;
        pub fn create() !*FontDescription {
            return pango_font_description_new() orelse return error.OutOfMemory;
        }

        extern fn pango_font_description_from_string(str: [*:0]const u8) ?*FontDescription;
        pub fn fromString(str: [*:0]const u8) !*FontDescription {
            return c.pango_font_description_from_string(str) orelse return error.OutOfMemory;
        }

        extern fn pango_font_description_set_family(
            f: *FontDescription,
            name: [*:0]const u8,
        ) void;
        pub const setFamily = pango_font_description_set_family;

        extern fn pango_font_description_set_weight(f: *FontDescription, weight: u32) void;
        pub const setWeight = pango_font_description_set_weight;

        extern fn pango_font_description_set_size(f: *FontDescription, size: i32) void;
        pub const setSize = pango_font_description_set_size;

        pub fn setSizePt(f: *FontDescription, pt: i32) void {
            f.setSize(pt * scale);
        }

        extern fn pango_font_description_free(f: *FontDescription) void;
        pub const destroy = pango_font_description_free;
    };
};

pub const cairo = struct {
    pub const Error = error{
        Unknown,
        NullPtr,
        NoMemory,
        ReadError,
        InvalidContent,
        InvalidFormat,
        InvalidVisual,
    };
    fn statusToError(status: c_int) Error!void {
        switch (status) {
            c.CAIRO_STATUS_SUCCESS => return,
            c.CAIRO_STATUS_NULL_POINTER => return Error.NullPtr,
            c.CAIRO_STATUS_NO_MEMORY => return Error.NoMemory,
            c.CAIRO_STATUS_READ_ERROR => return Error.ReadError,
            c.CAIRO_STATUS_INVALID_CONTENT => return Error.InvalidContent,
            c.CAIRO_STATUS_INVALID_FORMAT => return Error.InvalidFormat,
            c.CAIRO_STATUS_INVALID_VISUAL => return Error.InvalidVisual,
            else => return Error.Unknown,
        }
    }

    pub const Format = enum(c_int) {
        invalid = -1,
        argb32,
        rgb24,

        extern fn cairo_format_stride_for_width(format: Format, width: i32) i32;
        pub const strideForWidth = cairo_format_stride_for_width;
    };

    pub const Context = opaque {
        extern fn cairo_status(ctx: ?*Context) c_int;
        fn checkStatus(ctx: ?*Context) Error!void {
            return statusToError(cairo_status(ctx));
        }

        extern fn cairo_create(surface: *Surface) ?*Context;
        pub fn create(surface: *Surface) !*Context {
            const ctx = cairo_create(surface);
            try checkStatus(ctx);
            return ctx orelse unreachable;
        }

        extern fn cairo_move_to(ctx: *Context, x: f64, y: f64) void;
        pub const moveTo = cairo_move_to;

        extern fn cairo_set_source_rgba(ctx: *Context, green: f64, blue: f64, red: f64, alpha: f64) void;
        pub const setSourceRgba = cairo_set_source_rgba;

        extern fn cairo_paint(ctx: *Context) void;
        pub const paint = cairo_paint;

        extern fn pango_cairo_show_layout(ctx: *Context, layout: *pango.Layout) void;
        pub const showPangoLayout = pango_cairo_show_layout;

        extern fn cairo_destroy(ctx: *Context) void;
        pub const destroy = cairo_destroy;
    };

    pub const Surface = opaque {
        extern fn cairo_surface_status(surface: ?*Surface) c_int;
        fn checkStatus(surface: ?*Surface) Error!void {
            return statusToError(cairo_surface_status(surface));
        }

        extern fn cairo_image_surface_create(
            format: Format,
            width: i32,
            height: i32,
        ) ?*Surface;
        pub fn createImage(format: Format, width: i32, height: i32) Error!*Surface {
            const surface = cairo_image_surface_create(
                format,
                width,
                height,
            );
            try checkStatus(surface);
            return surface orelse unreachable;
        }

        extern fn cairo_image_surface_create_for_data(
            data: [*c]u8,
            format: Format,
            width: i32,
            height: i32,
            stride: i32,
        ) ?*Surface;
        pub fn createImageForData(
            data: []u8,
            format: Format,
            width: i32,
            height: i32,
            stride: i32,
        ) Error!*Surface {
            const s = cairo_image_surface_create_for_data(
                data.ptr,
                format,
                width,
                height,
                stride,
            );
            try checkStatus(s);
            return s orelse unreachable;
        }

        extern fn cairo_surface_destroy(s: *Surface) void;
        pub const destroy = cairo_surface_destroy;
    };
};