4 * i3 - an improved dynamic tiling window manager
6 * © 2009 Michael Stapelberg and contributors
8 * See file LICENSE for license information.
10 * font.c: Handles font loading (with caching, with height information)
21 TAILQ_HEAD(cached_fonts_head, Font) cached_fonts = TAILQ_HEAD_INITIALIZER(cached_fonts);
24 * Loads a font for usage, getting its height. This function is used very often, so it
28 i3Font *load_font(xcb_connection_t *connection, const char *pattern) {
29 /* Check if we got the font cached */
31 TAILQ_FOREACH(font, &cached_fonts, fonts)
32 if (strcmp(font->pattern, pattern) == 0)
35 i3Font *new = smalloc(sizeof(i3Font));
36 xcb_void_cookie_t font_cookie;
37 xcb_list_fonts_with_info_cookie_t info_cookie;
39 /* Send all our requests first */
40 new->id = xcb_generate_id(connection);
41 font_cookie = xcb_open_font_checked(connection, new->id, strlen(pattern), pattern);
42 info_cookie = xcb_list_fonts_with_info(connection, 1, strlen(pattern), pattern);
44 check_error(connection, font_cookie, "Could not open font");
46 /* Get information (height/name) for this font */
47 xcb_list_fonts_with_info_reply_t *reply = xcb_list_fonts_with_info_reply(connection, info_cookie, NULL);
48 exit_if_null(reply, "Could not load font \"%s\"\n", pattern);
50 if (asprintf(&(new->name), "%.*s", xcb_list_fonts_with_info_name_length(reply),
51 xcb_list_fonts_with_info_name(reply)) == -1)
52 die("asprintf() failed\n");
53 new->pattern = sstrdup(pattern);
54 new->height = reply->font_ascent + reply->font_descent;
56 /* Insert into cache */
57 TAILQ_INSERT_TAIL(&cached_fonts, new, fonts);