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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
|
#ifndef _BM_CAIRO_H_
#define _BM_CAIRO_H_
#include "internal.h"
#include <string.h>
#include <assert.h>
#include <math.h>
#include <cairo/cairo.h>
#include <pango/pangocairo.h>
struct cairo {
cairo_t *cr;
cairo_surface_t *surface;
PangoContext *pango;
};
struct cairo_color {
float r, g, b, a;
};
struct cairo_paint {
struct cairo_color fg;
struct cairo_color bg;
const char *font;
int32_t baseline;
struct box {
int32_t lx, rx; // left/right offset (pos.x - lx, box.w + rx)
int32_t ty, by; // top/bottom offset (pos.y - ty, box.h + by)
int32_t w, h; // 0 for text width/height
} box;
struct pos {
int32_t x, y;
} pos;
};
struct cairo_result {
uint32_t x_advance;
uint32_t height;
uint32_t baseline;
};
struct cairo_paint_result {
uint32_t displayed;
uint32_t height;
};
static size_t blen = 0;
static char *buffer = NULL;
__attribute__((unused)) static bool
bm_cairo_create_for_surface(struct cairo *cairo, cairo_surface_t *surface)
{
assert(cairo && surface);
if (!(cairo->cr = cairo_create(surface)))
goto fail;
if (!(cairo->pango = pango_cairo_create_context(cairo->cr)))
goto fail;
cairo->surface = surface;
return true;
fail:
if (cairo->cr)
cairo_destroy(cairo->cr);
return false;
}
__attribute__((unused)) static void
bm_cairo_destroy(struct cairo *cairo)
{
if (cairo->cr)
cairo_destroy(cairo->cr);
if (cairo->surface)
cairo_surface_destroy(cairo->surface);
}
__attribute__((unused)) static PangoLayout*
bm_pango_get_layout(struct cairo *cairo, struct cairo_paint *paint, const char *buffer)
{
PangoLayout *layout = pango_cairo_create_layout(cairo->cr);
pango_layout_set_text(layout, buffer, -1);
PangoFontDescription *desc = pango_font_description_from_string(paint->font);
pango_layout_set_font_description(layout, desc);
pango_layout_set_single_paragraph_mode(layout, 1);
pango_font_description_free(desc);
return layout;
}
__attribute__((unused)) BM_LOG_ATTR(4, 5) static bool
bm_pango_get_text_extents(struct cairo *cairo, struct cairo_paint *paint, struct cairo_result *result, const char *fmt, ...)
{
assert(cairo && paint && result && fmt);
memset(result, 0, sizeof(struct cairo_result));
va_list args;
va_start(args, fmt);
bool ret = bm_vrprintf(&buffer, &blen, fmt, args);
va_end(args);
if (!ret)
return false;
PangoRectangle rect;
PangoLayout *layout = bm_pango_get_layout(cairo, paint, buffer);
pango_layout_get_pixel_extents(layout, NULL, &rect);
int baseline = pango_layout_get_baseline(layout) / PANGO_SCALE;
g_object_unref(layout);
result->x_advance = rect.x + rect.width;
result->height = rect.height;
result->baseline = baseline;
return true;
}
__attribute__((unused)) BM_LOG_ATTR(4, 5) static bool
bm_cairo_draw_line(struct cairo *cairo, struct cairo_paint *paint, struct cairo_result *result, const char *fmt, ...)
{
assert(cairo && paint && result && fmt);
memset(result, 0, sizeof(struct cairo_result));
va_list args;
va_start(args, fmt);
bool ret = bm_vrprintf(&buffer, &blen, fmt, args);
va_end(args);
if (!ret)
return false;
PangoLayout *layout = bm_pango_get_layout(cairo, paint, buffer);
pango_cairo_update_layout(cairo->cr, layout);
int width, height;
pango_layout_get_pixel_size(layout, &width, &height);
height = paint->box.h > 0 ? paint->box.h : height;
int base = pango_layout_get_baseline(layout) / PANGO_SCALE;
cairo_set_source_rgba(cairo->cr, paint->bg.r, paint->bg.b, paint->bg.g, paint->bg.a);
cairo_rectangle(cairo->cr,
paint->pos.x - paint->box.lx, paint->pos.y - paint->box.ty,
(paint->box.w > 0 ? paint->box.w : width) + paint->box.rx + paint->box.lx,
height + paint->box.by + paint->box.ty);
cairo_fill(cairo->cr);
cairo_set_source_rgba(cairo->cr, paint->fg.r, paint->fg.b, paint->fg.g, paint->fg.a);
cairo_move_to(cairo->cr, paint->box.lx + paint->pos.x, paint->pos.y - base + paint->baseline);
pango_cairo_show_layout(cairo->cr, layout);
g_object_unref(layout);
result->x_advance = width + paint->box.rx;
result->height = height + paint->box.by + paint->box.ty;
return true;
}
__attribute__((unused)) static void
bm_cairo_color_from_menu_color(const struct bm_menu *menu, enum bm_color color, struct cairo_color *c)
{
assert(menu);
c->r = (float)menu->colors[color].r / 255.0f;
c->g = (float)menu->colors[color].g / 255.0f;
c->b = (float)menu->colors[color].b / 255.0f;
c->a = 1.0f;
}
__attribute__((unused)) static void
bm_cairo_paint(struct cairo *cairo, uint32_t width, uint32_t max_height, const struct bm_menu *menu, struct cairo_paint_result *out_result)
{
assert(cairo && menu && out_result);
uint32_t height = fmin(menu->line_height, max_height);
memset(out_result, 0, sizeof(struct cairo_paint_result));
out_result->displayed = 1;
cairo_set_source_rgb(cairo->cr, 0, 0, 0);
cairo_rectangle(cairo->cr, 0, 0, width, height);
cairo_fill(cairo->cr);
struct cairo_paint paint;
memset(&paint, 0, sizeof(paint));
paint.font = menu->font.name;
struct cairo_result result;
memset(&result, 0, sizeof(result));
int ascii_height;
bm_pango_get_text_extents(cairo, &paint, &result, "!\"#$%%&'()*+,-./0123456789:;<=>?@ABCD"
"EFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~");
ascii_height = result.height;
paint.baseline = result.baseline;
int32_t vpadding = height == 0 ? 2 : (height - ascii_height) / 2;
memset(&result, 0, sizeof(result));
uint32_t title_x = 0;
if (menu->title) {
bm_cairo_color_from_menu_color(menu, BM_COLOR_TITLE_FG, &paint.fg);
bm_cairo_color_from_menu_color(menu, BM_COLOR_TITLE_BG, &paint.bg);
paint.pos = (struct pos){ result.x_advance, vpadding };
paint.box = (struct box){ 4, 8, vpadding, vpadding, 0, ascii_height };
bm_cairo_draw_line(cairo, &paint, &result, "%s", menu->title);
title_x = result.x_advance;
}
bm_cairo_color_from_menu_color(menu, BM_COLOR_FILTER_FG, &paint.fg);
bm_cairo_color_from_menu_color(menu, BM_COLOR_FILTER_BG, &paint.bg);
paint.pos = (struct pos){ (menu->title ? 2 : 0) + result.x_advance, vpadding };
paint.box = (struct box){ (menu->title ? 2 : 4), 0, vpadding, vpadding, width - paint.pos.x, ascii_height };
bm_cairo_draw_line(cairo, &paint, &result, "%s", (menu->filter ? menu->filter : ""));
const uint32_t titleh = result.height;
out_result->height = titleh;
uint32_t count;
struct bm_item **items = bm_menu_get_filtered_items(menu, &count);
uint32_t lines = (menu->lines > 0 ? menu->lines : 1);
if (menu->lines > 0) {
/* vertical mode */
const bool scrollbar = (menu->scrollbar > BM_SCROLLBAR_NONE && (menu->scrollbar != BM_SCROLLBAR_AUTOHIDE || count > lines) ? true : false);
const uint32_t spacing_x = (scrollbar ? 4 : 0);
uint32_t spacing_y = 0; // 0 == variable width spacing
if (lines > max_height / titleh) {
/* there is more lines than screen can fit, enter fixed spacing mode */
lines = max_height / titleh - 1;
spacing_y = titleh;
}
uint32_t prefix_x = 0;
if (menu->prefix) {
bm_pango_get_text_extents(cairo, &paint, &result, "%s ", menu->prefix);
prefix_x += result.x_advance;
}
uint32_t posy = titleh;
const uint32_t page = (menu->index / lines) * lines;
for (uint32_t l = 0, i = page; l < lines && i < count && posy < max_height; ++i, ++l) {
bool highlighted = (items[i] == bm_menu_get_highlighted_item(menu));
if (highlighted) {
bm_cairo_color_from_menu_color(menu, BM_COLOR_HIGHLIGHTED_FG, &paint.fg);
bm_cairo_color_from_menu_color(menu, BM_COLOR_HIGHLIGHTED_BG, &paint.bg);
} else if (bm_menu_item_is_selected(menu, items[i])) {
bm_cairo_color_from_menu_color(menu, BM_COLOR_SELECTED_FG, &paint.fg);
bm_cairo_color_from_menu_color(menu, BM_COLOR_SELECTED_BG, &paint.bg);
} else {
bm_cairo_color_from_menu_color(menu, BM_COLOR_ITEM_FG, &paint.fg);
bm_cairo_color_from_menu_color(menu, BM_COLOR_ITEM_BG, &paint.bg);
}
if (menu->prefix && highlighted) {
paint.pos = (struct pos){ 0, vpadding + posy };
paint.box = (struct box){ 4 + spacing_x, 0, vpadding, vpadding, width - paint.pos.x, ascii_height };
bm_cairo_draw_line(cairo, &paint, &result, "%s %s", menu->prefix, (items[i]->text ? items[i]->text : ""));
} else {
paint.pos = (struct pos){ 0, vpadding + posy };
paint.box = (struct box){ 4 + spacing_x + prefix_x, 0, vpadding, vpadding, width - paint.pos.x, ascii_height };
bm_cairo_draw_line(cairo, &paint, &result, "%s", (items[i]->text ? items[i]->text : ""));
}
posy += (spacing_y ? spacing_y : result.height);
out_result->height = posy;
out_result->displayed++;
}
if (scrollbar && count > 0) {
bm_cairo_color_from_menu_color(menu, BM_COLOR_SCROLLBAR_BG, &paint.bg);
bm_cairo_color_from_menu_color(menu, BM_COLOR_SCROLLBAR_FG, &paint.fg);
const uint32_t sheight = out_result->height - titleh;
cairo_set_source_rgba(cairo->cr, paint.bg.r, paint.bg.b, paint.bg.g, paint.bg.a);
cairo_rectangle(cairo->cr, 0, titleh, 2, sheight);
cairo_fill(cairo->cr);
const float percent = fmin(((float)page / (count - lines)), 1.0f);
const uint32_t size = fmax(sheight * ((float)lines / count), 2.0f);
const uint32_t posy = percent * (sheight - size);
cairo_set_source_rgba(cairo->cr, paint.fg.r, paint.fg.b, paint.fg.g, paint.fg.a);
cairo_rectangle(cairo->cr, 0, titleh + posy, 2, size);
cairo_fill(cairo->cr);
}
} else {
/* single-line mode */
bm_pango_get_text_extents(cairo, &paint, &result, "lorem ipsum lorem ipsum lorem ipsum lorem");
uint32_t cl = fmin(title_x + result.x_advance, width / 4);
if (menu->wrap || menu->index > 0) {
paint.pos = (struct pos){ cl, vpadding };
paint.box = (struct box){ 1, 2, vpadding, vpadding, 0, ascii_height };
bm_cairo_draw_line(cairo, &paint, &result, "<");
cl += result.x_advance + 1;
}
for (uint32_t i = menu->index; i < count && cl < width; ++i) {
bool highlighted = (items[i] == bm_menu_get_highlighted_item(menu));
if (highlighted) {
bm_cairo_color_from_menu_color(menu, BM_COLOR_HIGHLIGHTED_FG, &paint.fg);
bm_cairo_color_from_menu_color(menu, BM_COLOR_HIGHLIGHTED_BG, &paint.bg);
} else if (bm_menu_item_is_selected(menu, items[i])) {
bm_cairo_color_from_menu_color(menu, BM_COLOR_SELECTED_FG, &paint.fg);
bm_cairo_color_from_menu_color(menu, BM_COLOR_SELECTED_BG, &paint.bg);
} else {
bm_cairo_color_from_menu_color(menu, BM_COLOR_ITEM_FG, &paint.fg);
bm_cairo_color_from_menu_color(menu, BM_COLOR_ITEM_BG, &paint.bg);
}
paint.pos = (struct pos){ cl, vpadding };
paint.box = (struct box){ 2, 4, vpadding, vpadding, 0, ascii_height };
bm_cairo_draw_line(cairo, &paint, &result, "%s", (items[i]->text ? items[i]->text : ""));
cl += result.x_advance + 2;
out_result->displayed += (cl < width);
out_result->height = fmax(out_result->height, result.height);
}
if (menu->wrap || menu->index + 1 < count) {
bm_cairo_color_from_menu_color(menu, BM_COLOR_FILTER_FG, &paint.fg);
bm_cairo_color_from_menu_color(menu, BM_COLOR_FILTER_BG, &paint.bg);
bm_pango_get_text_extents(cairo, &paint, &result, ">");
paint.pos = (struct pos){ width - result.x_advance - 2, vpadding };
paint.box = (struct box){ 1, 2, vpadding, vpadding, 0, ascii_height };
bm_cairo_draw_line(cairo, &paint, &result, ">");
}
}
}
#endif /* _BM_CAIRO_H */
/* vim: set ts=8 sw=4 tw=0 :*/
|