Cleanup load_font(), make it caching

This commit is contained in:
Michael Stapelberg 2009-02-15 02:40:03 +01:00
parent 26944bea99
commit 0917cdda36
2 changed files with 27 additions and 12 deletions

View File

@ -106,6 +106,8 @@ struct Font {
int height; int height;
/* The xcb-id for the font */ /* The xcb-id for the font */
xcb_font_t id; xcb_font_t id;
TAILQ_ENTRY(Font) fonts;
}; };
/* /*

View File

@ -20,30 +20,43 @@
#include "data.h" #include "data.h"
#include "util.h" #include "util.h"
TAILQ_HEAD(cached_fonts_head, Font) cached_fonts = TAILQ_HEAD_INITIALIZER(cached_fonts);
/*
* Loads a font for usage, getting its height. This function is used very often, so it
* maintains a cache.
*
*/
i3Font *load_font(xcb_connection_t *c, const char *pattern) { i3Font *load_font(xcb_connection_t *c, const char *pattern) {
/* TODO: this function should be caching */ /* Check if we got the font cached */
i3Font *font;
TAILQ_FOREACH(font, &cached_fonts, fonts)
if (strcmp(font->pattern, pattern) == 0)
return font;
i3Font *new = malloc(sizeof(i3Font)); i3Font *new = malloc(sizeof(i3Font));
/* Send all our requests first */
new->id = xcb_generate_id(c);
xcb_void_cookie_t font_cookie = xcb_open_font_checked(c, new->id, strlen(pattern), pattern);
xcb_list_fonts_with_info_cookie_t cookie = xcb_list_fonts_with_info(c, 1, strlen(pattern), pattern); xcb_list_fonts_with_info_cookie_t cookie = xcb_list_fonts_with_info(c, 1, strlen(pattern), pattern);
check_error(c, font_cookie, "Could not open font");
/* Get information (height/name) for this font */
xcb_list_fonts_with_info_reply_t *reply = xcb_list_fonts_with_info_reply(c, cookie, NULL); xcb_list_fonts_with_info_reply_t *reply = xcb_list_fonts_with_info_reply(c, cookie, NULL);
if (!reply) { if (reply == NULL) {
printf("Could not load font\n"); printf("Could not load font\n");
exit(1); exit(1);
} }
/* Oh my, this is so ugly :-(. Why cant they just return a null-terminated asprintf(&(new->name), "%.*s", xcb_list_fonts_with_info_name_length(reply),
* string? Thats what abstraction layers are for. */ xcb_list_fonts_with_info_name(reply));
char buffer[xcb_list_fonts_with_info_name_length(reply)+1];
memset(buffer, 0, sizeof(buffer));
memcpy(buffer, xcb_list_fonts_with_info_name(reply), sizeof(buffer)-1);
new->name = strdup(buffer);
new->pattern = strdup(pattern); new->pattern = strdup(pattern);
new->height = reply->font_ascent + reply->font_descent; new->height = reply->font_ascent + reply->font_descent;
/* Actually load the font */ /* Insert into cache */
new->id = xcb_generate_id(c); TAILQ_INSERT_TAIL(&cached_fonts, new, fonts);
xcb_void_cookie_t font_cookie = xcb_open_font_checked(c, new->id, strlen(pattern), pattern);
check_error(c, font_cookie, "Could not open font");
return new; return new;
} }