2 * vim:ts=4:sw=4:expandtab
4 * i3 - an improved dynamic tiling window manager
5 * © 2009-2011 Michael Stapelberg and contributors (see also: LICENSE)
16 extern xcb_connection_t *conn;
19 * Loads a font for usage, also getting its height. If fallback is true,
20 * the fonts 'fixed' or '-misc-*' will be loaded instead of exiting.
23 i3Font load_font(const char *pattern, bool fallback) {
25 xcb_void_cookie_t font_cookie;
26 xcb_list_fonts_with_info_cookie_t info_cookie;
27 xcb_list_fonts_with_info_reply_t *info_reply;
28 xcb_generic_error_t *error;
30 /* Send all our requests first */
31 font.id = xcb_generate_id(conn);
32 font_cookie = xcb_open_font_checked(conn, font.id, strlen(pattern), pattern);
33 info_cookie = xcb_list_fonts_with_info(conn, 1, strlen(pattern), pattern);
35 /* Check for errors. If errors, fall back to default font. */
36 error = xcb_request_check(conn, font_cookie);
38 /* If we fail to open font, fall back to 'fixed' */
39 if (fallback && error != NULL) {
40 ELOG("Could not open font %s (X error %d). Trying fallback to 'fixed'.\n",
41 pattern, error->error_code);
43 font_cookie = xcb_open_font_checked(conn, font.id, strlen(pattern), pattern);
44 info_cookie = xcb_list_fonts_with_info(conn, 1, strlen(pattern), pattern);
46 /* Check if we managed to open 'fixed' */
47 error = xcb_request_check(conn, font_cookie);
49 /* Fall back to '-misc-*' if opening 'fixed' fails. */
51 ELOG("Could not open fallback font 'fixed', trying with '-misc-*'.\n");
53 font_cookie = xcb_open_font_checked(conn, font.id, strlen(pattern), pattern);
54 info_cookie = xcb_list_fonts_with_info(conn, 1, strlen(pattern), pattern);
56 if ((error = xcb_request_check(conn, font_cookie)) != NULL)
57 errx(EXIT_FAILURE, "Could open neither requested font nor fallbacks "
58 "(fixed or -misc-*): X11 error %d", error->error_code);
62 /* Get information (height/name) for this font */
63 if (!(info_reply = xcb_list_fonts_with_info_reply(conn, info_cookie, NULL)))
64 errx(EXIT_FAILURE, "Could not load font \"%s\"", pattern);
66 font.height = info_reply->font_ascent + info_reply->font_descent;