]> git.sur5r.net Git - i3/i3/blob - src/font.c
803ac0a6a778c3a06a0446299cab8350f320c087
[i3/i3] / src / font.c
1 /*
2  * Handles font loading
3  *
4  */
5 #include <string.h>
6 #include <stdlib.h>
7 #include <stdio.h>
8 #include <xcb/xcb.h>
9
10 #include "data.h"
11 #include "util.h"
12
13 i3Font *load_font(xcb_connection_t *c, const char *pattern) {
14         /* TODO: this function should be caching */
15         i3Font *new = malloc(sizeof(i3Font));
16
17         xcb_list_fonts_with_info_cookie_t cookie = xcb_list_fonts_with_info(c, 1, strlen(pattern), pattern);
18         xcb_list_fonts_with_info_reply_t *reply = xcb_list_fonts_with_info_reply(c, cookie, NULL);
19         if (!reply) {
20                 printf("Could not load font\n");
21                 exit(1);
22         }
23
24         /* Oh my, this is so ugly :-(. Why can’t they just return a null-terminated
25          * string? That’s what abstraction layers are for. */
26         char buffer[xcb_list_fonts_with_info_name_length(reply)+1];
27         memset(buffer, 0, sizeof(buffer));
28         memcpy(buffer, xcb_list_fonts_with_info_name(reply), sizeof(buffer)-1);
29         new->name = strdup(buffer);
30         new->pattern = strdup(pattern);
31         new->height = reply->font_ascent + reply->font_descent;
32
33         /* Actually load the font */
34         new->id = xcb_generate_id(c);
35         xcb_void_cookie_t font_cookie = xcb_open_font_checked(c, new->id, strlen(pattern), pattern);
36         check_error(c, font_cookie, "Could not open font");
37
38         return new;
39 }