aboutsummaryrefslogtreecommitdiff
path: root/src/pangocairo.zig
blob: 3995ba7606cbf216cc0726ed0c8e9f9a2e003a6b (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
const c = @cImport({
    @cInclude("pango/pangocairo.h");
});

pub const pango = struct {
    pub const Layout = struct {
        layout: *c.PangoLayout,

        pub fn createForCairo(s: *cairo.Surface) !Layout {
            const layout = c.pango_cairo_create_layout(
                s.cairo,
            ) orelse return error.PangoCairoError;
            return .{ .layout = layout };
        }

        pub fn setFont(l: Layout, f: FontDescription) void {
            c.pango_layout_set_font_description(l.layout, f.font);
        }

        pub fn setFontFromString(l: Layout, font: [*:0]const u8) !void {
            const pfont = c.pango_font_description_from_string(
                font,
            ) orelse return error.BadFont;
            defer c.pango_font_description_free(pfont);
            c.pango_layout_set_font_description(l.layout, pfont);
        }

        pub fn setText(l: Layout, text: [*:0]const u8, len: i32) void {
            c.pango_layout_set_text(l.layout, text, len);
        }

        pub fn show(l: Layout, s: *cairo.Surface) void {
            c.pango_cairo_show_layout(s.cairo, l.layout);
        }

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

    pub const FontDescription = struct {
        font: *c.PangoFontDescription,

        pub fn create() !FontDescription {
            const font = c.pango_font_description_new() orelse return error.PangoError;
            return .{ .font = font };
        }

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

        pub fn setFamily(f: FontDescription, name: [*:0]const u8) void {
            c.pango_font_description_set_family(f.font, name);
        }

        pub fn setWeight(f: FontDescription, weight: u32) void {
            c.pango_font_description_set_weight(f.font, weight);
        }

        pub fn setSize(f: FontDescription, size: i32) void {
            c.pango_font_description_set_size(f.font, size);
        }

        pub fn setSizePt(f: FontDescription, pt: i32) void {
            c.pango_font_description_set_size(f.font, pt * c.PANGO_SCALE);
        }

        pub fn destroy(f: FontDescription) void {
            c.pango_font_description_free(f.font);
        }
    };
};

pub const cairo = struct {
    pub const Format = enum(c_int) {
        invalid = -1,
        argb32,
        rgb24,
    };

    pub const Surface = struct {
        cairo: *c.cairo_t,
        surface: *c.cairo_surface_t,

        pub fn formatStrideForWidth(format: Format, width: i32) i32 {
            return c.cairo_format_stride_for_width(@intFromEnum(format), width);
        }

        pub fn createImage(format: Format, width: i32, height: i32) !Surface {
            const surface = c.cairo_image_surface_create(
                @intFromEnum(format),
                width,
                height,
            ) orelse return error.CairoError;
            const cr = c.cairo_create(surface) orelse return error.CairoError;
            return .{ .cairo = cr, .surface = surface };
        }

        pub fn createImageForData(
            data: []u8,
            format: Format,
            width: i32,
            height: i32,
            stride: i32,
        ) !Surface {
            const surface = c.cairo_image_surface_create_for_data(
                data.ptr,
                @intFromEnum(format),
                width,
                height,
                stride,
            ) orelse return error.CairoError;
            const cr = c.cairo_create(surface) orelse return error.CairoError;
            return .{ .cairo = cr, .surface = surface };
        }

        pub fn moveTo(s: Surface, x: f64, y: f64) void {
            c.cairo_move_to(s.cairo, x, y);
        }

        pub fn setSourceRgba(
            s: Surface,
            red: f64,
            green: f64,
            blue: f64,
            alpha: f64,
        ) void {
            c.cairo_set_source_rgba(s.cairo, red, green, blue, alpha);
        }

        pub fn paint(s: Surface) void {
            c.cairo_paint(s.cairo);
        }

        pub fn destroy(s: Surface) void {
            c.cairo_destroy(s.cairo);
            c.cairo_surface_destroy(s.surface);
        }
    };
};