~novakane/agertu

agertu/src/decorations.zig -rw-r--r-- 5.5 KiB
9ea83d8fHugo Machet build: Bump to version 2.0.0-dev 4 months ago
                                                                                
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// This file is part of agertu
//
// Copyright (C) 2022 Hugo Machet
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 3
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

const std = @import("std");
const fmt = std.fmt;
const mem = std.mem;

const fcft = @import("fcft");
const pixman = @import("pixman");

const util = @import("util.zig");

const log = std.log.scoped(.decorations);

pub fn get_fcft_fonts(fonts: []const u8) !*fcft.Font {
    var arena = std.heap.ArenaAllocator.init(util.gpa);
    defer arena.deinit();
    const alloc = arena.allocator();

    var list = try std.ArrayListUnmanaged([*:0]const u8).initCapacity(alloc, 2);
    errdefer list.deinit(alloc);

    var it = mem.tokenize(u8, fonts, ",");
    while (it.next()) |font| {
        try list.append(alloc, try alloc.dupeZ(u8, font));
    }

    const fcft_fonts = try fcft.Font.fromName(list.items[0..], null);
    errdefer fcft_fonts.destroy();

    return fcft_fonts;
}

/// This is a really simple function without allocation as we only need to
/// render tag number.
pub fn draw_bytes(
    bytes: []const u8,
    font: *fcft.Font,
    image: *pixman.Image,
    fg_color: []const u8,
    x: *i32,
    y: i32,
) !void {
    for (bytes) |c| {
        const glyph = try font.rasterizeCharUtf32(c, .none);

        var x_kern: c_long = 0;
        _ = font.kerning(c - 1, c, &x_kern, null);

        const fg = try parse_rgba(fg_color);

        switch (pixman.Image.getFormat(glyph.pix)) {
            // Glyph is a pre-rendered image. (e.g. a color emoji)
            .a8r8g8b8 => {
                pixman.Image.composite32(
                    .over,
                    glyph.pix,
                    null,
                    image,
                    0,
                    0,
                    0,
                    0,
                    x.* + @intCast(i32, glyph.x),
                    y - @intCast(i32, glyph.y),
                    glyph.width,
                    glyph.height,
                );
            },
            // Glyph is an alpha mask.
            else => {
                const color = pixman.Image.createSolidFill(&fg).?;
                defer _ = color.unref();

                pixman.Image.composite32(
                    .over,
                    color,
                    glyph.pix,
                    image,
                    0,
                    0,
                    0,
                    0,
                    x.* + @intCast(i32, glyph.x),
                    y - @intCast(i32, glyph.y),
                    glyph.width,
                    glyph.height,
                );
            },
        }

        x.* += glyph.advance.x;
    }
}

/// Render a rectangle with a colored background and colored borders
/// inside the rectangle.
pub fn draw_bordered_rectangle(
    image: *pixman.Image,
    x: i16,
    y: i16,
    width: u16,
    height: u16,
    borders_size: u16,
    background_color: []const u8,
    borders_color: []const u8,
) !void {
    const bg_color = try parse_rgba(background_color);
    const bd_color = try parse_rgba(borders_color);

    // Render background
    _ = pixman.Image.fillRectangles(
        .src,
        image,
        &bg_color,
        1,
        &[1]pixman.Rectangle16{
            .{ .x = x, .y = y, .width = width, .height = height },
        },
    );

    // Render borders
    const borders = [4]pixman.Rectangle16{
        // Top
        .{
            .x = x,
            .y = y,
            .width = width,
            .height = borders_size,
        },
        // Bottom
        .{
            .x = x,
            .y = y + @intCast(i16, height - borders_size),
            .width = width,
            .height = borders_size,
        },
        // Left
        .{
            .x = x,
            .y = y + @intCast(i16, borders_size),
            .width = borders_size,
            .height = height - 2 * borders_size,
        },
        // Right
        .{
            .x = x + @intCast(i16, width - borders_size),
            .y = y + @intCast(i16, borders_size),
            .width = borders_size,
            .height = height - 2 * borders_size,
        },
    };
    _ = pixman.Image.fillRectangles(
        .src,
        image,
        &bd_color,
        4,
        &borders,
    );
}

/// Parse a color in the format "0xRRGGBB" or "0xRRGGBBAA" and convert it to a
/// pixman.Color.
pub fn parse_rgba(s: []const u8) !pixman.Color {
    check_rgba(.decorations, s);

    var color = try fmt.parseUnsigned(u32, s[2..], 16);
    if (s.len == 8) {
        color <<= 8;
        color |= 0xff;
    }

    const bytes = @bitCast([4]u8, color);
    return pixman.Color{
        .red = @as(u16, bytes[3]) * 0x101,
        .green = @as(u16, bytes[2]) * 0x101,
        .blue = @as(u16, bytes[1]) * 0x101,
        .alpha = @as(u16, bytes[0]) * 0x101,
    };
}

pub fn check_rgba(comptime scope: @Type(.EnumLiteral), s: []const u8) void {
    if (s.len != 8 and s.len != 10) {
        util.fatal(scope, "color must be '0xRRGGBB' or '0xRRGGBBAA'", .{});
    }
    if (s[0] != '0' or s[1] != 'x') {
        util.fatal(scope, "color must be '0xRRGGBB' or '0xRRGGBBAA'", .{});
    }
}