refactor font caching to just save the ID instead of mainting a cache with pattern→id-mapping
This commit is contained in:
parent
6e4a2b0b96
commit
f162e7efaa
@ -86,7 +86,7 @@ struct Mode {
|
|||||||
*/
|
*/
|
||||||
struct Config {
|
struct Config {
|
||||||
const char *terminal;
|
const char *terminal;
|
||||||
const char *font;
|
i3Font font;
|
||||||
|
|
||||||
const char *ipc_socket_path;
|
const char *ipc_socket_path;
|
||||||
const char *restart_state_path;
|
const char *restart_state_path;
|
||||||
|
@ -173,10 +173,6 @@ struct Autostart {
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
struct Font {
|
struct Font {
|
||||||
/** The name of the font, that is what the pattern resolves to */
|
|
||||||
char *name;
|
|
||||||
/** A copy of the pattern to build a cache */
|
|
||||||
char *pattern;
|
|
||||||
/** The height of the font, built from font_ascent + font_descent */
|
/** The height of the font, built from font_ascent + font_descent */
|
||||||
int height;
|
int height;
|
||||||
/** The xcb-id for the font */
|
/** The xcb-id for the font */
|
||||||
|
@ -71,11 +71,12 @@ enum { _NET_SUPPORTED = 0,
|
|||||||
extern unsigned int xcb_numlock_mask;
|
extern unsigned int xcb_numlock_mask;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Loads a font for usage, getting its height. This function is used very
|
* Loads a font for usage, also getting its height. If fallback is true,
|
||||||
* often, so it maintains a cache.
|
* i3 loads 'fixed' or '-misc-*' if the font cannot be found instead of
|
||||||
|
* exiting.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
i3Font *load_font(xcb_connection_t *conn, const char *pattern);
|
i3Font load_font(const char *pattern, bool fallback);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the colorpixel to use for the given hex color (think of HTML).
|
* Returns the colorpixel to use for the given hex color (think of HTML).
|
||||||
@ -162,8 +163,7 @@ void cached_pixmap_prepare(xcb_connection_t *conn, struct Cached_Pixmap *pixmap)
|
|||||||
* real length (amount of glyphs) using the given font.
|
* real length (amount of glyphs) using the given font.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
int predict_text_width(xcb_connection_t *conn, const char *font_pattern, char *text,
|
int predict_text_width(char *text, int length);
|
||||||
int length);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Configures the given window to have the size/position specified by given rect
|
* Configures the given window to have the size/position specified by given rect
|
||||||
|
@ -597,8 +597,8 @@ terminal:
|
|||||||
font:
|
font:
|
||||||
TOKFONT WHITESPACE STR
|
TOKFONT WHITESPACE STR
|
||||||
{
|
{
|
||||||
config.font = $<string>3;
|
config.font = load_font($<string>3, true);
|
||||||
printf("font %s\n", config.font);
|
printf("font %s\n", $<string>3);
|
||||||
}
|
}
|
||||||
;
|
;
|
||||||
|
|
||||||
|
@ -332,7 +332,10 @@ void load_configuration(xcb_connection_t *conn, const char *override_configpath,
|
|||||||
grab_all_keys(conn, false);
|
grab_all_keys(conn, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
REQUIRED_OPTION(font);
|
if (config.font.id == 0) {
|
||||||
|
ELOG("You did not specify required configuration option \"font\"\n");
|
||||||
|
config.font = load_font("fixed", true);
|
||||||
|
}
|
||||||
|
|
||||||
#if 0
|
#if 0
|
||||||
/* Set an empty name for every workspace which got no name */
|
/* Set an empty name for every workspace which got no name */
|
||||||
|
@ -94,8 +94,7 @@ void floating_enable(Con *con, bool automatic) {
|
|||||||
free(name);
|
free(name);
|
||||||
|
|
||||||
/* find the height for the decorations */
|
/* find the height for the decorations */
|
||||||
i3Font *font = load_font(conn, config.font);
|
int deco_height = config.font.height + 5;
|
||||||
int deco_height = font->height + 5;
|
|
||||||
|
|
||||||
DLOG("Original rect: (%d, %d) with %d x %d\n", con->rect.x, con->rect.y, con->rect.width, con->rect.height);
|
DLOG("Original rect: (%d, %d) with %d x %d\n", con->rect.x, con->rect.y, con->rect.width, con->rect.height);
|
||||||
Rect zero = { 0, 0, 0, 0 };
|
Rect zero = { 0, 0, 0, 0 };
|
||||||
|
@ -336,8 +336,7 @@ int handle_configure_request(void *prophs, xcb_connection_t *conn, xcb_configure
|
|||||||
DLOG("Configure request!\n");
|
DLOG("Configure request!\n");
|
||||||
if (con_is_floating(con) && con_is_leaf(con)) {
|
if (con_is_floating(con) && con_is_leaf(con)) {
|
||||||
/* find the height for the decorations */
|
/* find the height for the decorations */
|
||||||
i3Font *font = load_font(conn, config.font);
|
int deco_height = config.font.height + 5;
|
||||||
int deco_height = font->height + 5;
|
|
||||||
/* we actually need to apply the size/position changes to the *parent*
|
/* we actually need to apply the size/position changes to the *parent*
|
||||||
* container */
|
* container */
|
||||||
Rect bsr = con_border_style_rect(con);
|
Rect bsr = con_border_style_rect(con);
|
||||||
|
@ -194,8 +194,7 @@ void render_con(Con *con, bool render_fullscreen) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* find the height for the decorations */
|
/* find the height for the decorations */
|
||||||
i3Font *font = load_font(conn, config.font);
|
int deco_height = config.font.height + 5;
|
||||||
int deco_height = font->height + 5;
|
|
||||||
|
|
||||||
/* precalculate the sizes to be able to correct rounding errors */
|
/* precalculate the sizes to be able to correct rounding errors */
|
||||||
int sizes[children];
|
int sizes[children];
|
||||||
|
@ -165,16 +165,14 @@ void handle_signal(int sig, siginfo_t *info, void *data) {
|
|||||||
xcb_event_handlers_init(conn, &sig_evenths);
|
xcb_event_handlers_init(conn, &sig_evenths);
|
||||||
xcb_event_set_key_press_handler(&sig_evenths, sig_handle_key_press, NULL);
|
xcb_event_set_key_press_handler(&sig_evenths, sig_handle_key_press, NULL);
|
||||||
|
|
||||||
i3Font *font = load_font(conn, config.font);
|
|
||||||
|
|
||||||
/* width and height of the popup window, so that the text fits in */
|
/* width and height of the popup window, so that the text fits in */
|
||||||
int crash_text_num = sizeof(crash_text) / sizeof(char*);
|
int crash_text_num = sizeof(crash_text) / sizeof(char*);
|
||||||
int height = 13 + (crash_text_num * font->height);
|
int height = 13 + (crash_text_num * config.font.height);
|
||||||
|
|
||||||
/* calculate width for longest text */
|
/* calculate width for longest text */
|
||||||
int text_len = strlen(crash_text[crash_text_longest]);
|
int text_len = strlen(crash_text[crash_text_longest]);
|
||||||
char *longest_text = convert_utf8_to_ucs2(crash_text[crash_text_longest], &text_len);
|
char *longest_text = convert_utf8_to_ucs2(crash_text[crash_text_longest], &text_len);
|
||||||
int font_width = predict_text_width(conn, config.font, longest_text, text_len);
|
int font_width = predict_text_width(longest_text, text_len);
|
||||||
int width = font_width + 20;
|
int width = font_width + 20;
|
||||||
|
|
||||||
/* Open a popup window on each virtual screen */
|
/* Open a popup window on each virtual screen */
|
||||||
@ -192,7 +190,7 @@ void handle_signal(int sig, siginfo_t *info, void *data) {
|
|||||||
xcb_create_gc(conn, pixmap_gc, pixmap, 0, 0);
|
xcb_create_gc(conn, pixmap_gc, pixmap, 0, 0);
|
||||||
|
|
||||||
/* Create graphics context */
|
/* Create graphics context */
|
||||||
xcb_change_gc_single(conn, pixmap_gc, XCB_GC_FONT, font->id);
|
xcb_change_gc_single(conn, pixmap_gc, XCB_GC_FONT, config.font.id);
|
||||||
|
|
||||||
/* Grab the keyboard to get all input */
|
/* Grab the keyboard to get all input */
|
||||||
xcb_grab_keyboard(conn, false, win, XCB_CURRENT_TIME, XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC);
|
xcb_grab_keyboard(conn, false, win, XCB_CURRENT_TIME, XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC);
|
||||||
@ -201,7 +199,7 @@ void handle_signal(int sig, siginfo_t *info, void *data) {
|
|||||||
xcb_grab_pointer(conn, false, win, XCB_NONE, XCB_GRAB_MODE_ASYNC,
|
xcb_grab_pointer(conn, false, win, XCB_NONE, XCB_GRAB_MODE_ASYNC,
|
||||||
XCB_GRAB_MODE_ASYNC, win, XCB_NONE, XCB_CURRENT_TIME);
|
XCB_GRAB_MODE_ASYNC, win, XCB_NONE, XCB_CURRENT_TIME);
|
||||||
|
|
||||||
sig_draw_window(win, width, height, font->height);
|
sig_draw_window(win, width, height, config.font.height);
|
||||||
xcb_flush(conn);
|
xcb_flush(conn);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
5
src/x.c
5
src/x.c
@ -311,11 +311,10 @@ void x_draw_decoration(Con *con) {
|
|||||||
con->deco_rect.y + con->deco_rect.height - 1); /* to_y */
|
con->deco_rect.y + con->deco_rect.height - 1); /* to_y */
|
||||||
|
|
||||||
/* 5: draw the title */
|
/* 5: draw the title */
|
||||||
i3Font *font = load_font(conn, config.font);
|
|
||||||
uint32_t mask = XCB_GC_FOREGROUND | XCB_GC_BACKGROUND | XCB_GC_FONT;
|
uint32_t mask = XCB_GC_FOREGROUND | XCB_GC_BACKGROUND | XCB_GC_FONT;
|
||||||
uint32_t values[] = { color->text, color->background, font->id };
|
uint32_t values[] = { color->text, color->background, config.font.id };
|
||||||
xcb_change_gc(conn, parent->gc, mask, values);
|
xcb_change_gc(conn, parent->gc, mask, values);
|
||||||
int text_offset_y = font->height + (con->deco_rect.height - font->height) / 2 - 1;
|
int text_offset_y = config.font.height + (con->deco_rect.height - config.font.height) / 2 - 1;
|
||||||
|
|
||||||
struct Window *win = con->window;
|
struct Window *win = con->window;
|
||||||
if (win == NULL || win->name_x == NULL) {
|
if (win == NULL || win->name_x == NULL) {
|
||||||
|
42
src/xcb.c
42
src/xcb.c
@ -17,24 +17,19 @@ TAILQ_HEAD(cached_fonts_head, Font) cached_fonts = TAILQ_HEAD_INITIALIZER(cached
|
|||||||
unsigned int xcb_numlock_mask;
|
unsigned int xcb_numlock_mask;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Loads a font for usage, getting its height. This function is used very often, so it
|
* Loads a font for usage, also getting its height. If fallback is true,
|
||||||
* maintains a cache.
|
* i3 loads 'fixed' or '-misc-*' if the font cannot be found instead of
|
||||||
|
* exiting.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
i3Font *load_font(xcb_connection_t *conn, const char *pattern) {
|
i3Font load_font(const char *pattern, bool fallback) {
|
||||||
/* Check if we got the font cached */
|
i3Font new;
|
||||||
i3Font *font;
|
|
||||||
TAILQ_FOREACH(font, &cached_fonts, fonts)
|
|
||||||
if (strcmp(font->pattern, pattern) == 0)
|
|
||||||
return font;
|
|
||||||
|
|
||||||
i3Font *new = smalloc(sizeof(i3Font));
|
|
||||||
xcb_void_cookie_t font_cookie;
|
xcb_void_cookie_t font_cookie;
|
||||||
xcb_list_fonts_with_info_cookie_t info_cookie;
|
xcb_list_fonts_with_info_cookie_t info_cookie;
|
||||||
|
|
||||||
/* Send all our requests first */
|
/* Send all our requests first */
|
||||||
new->id = xcb_generate_id(conn);
|
new.id = xcb_generate_id(conn);
|
||||||
font_cookie = xcb_open_font_checked(conn, new->id, strlen(pattern), pattern);
|
font_cookie = xcb_open_font_checked(conn, new.id, strlen(pattern), pattern);
|
||||||
info_cookie = xcb_list_fonts_with_info(conn, 1, strlen(pattern), pattern);
|
info_cookie = xcb_list_fonts_with_info(conn, 1, strlen(pattern), pattern);
|
||||||
|
|
||||||
/* Check for errors. If errors, fall back to default font. */
|
/* Check for errors. If errors, fall back to default font. */
|
||||||
@ -44,7 +39,7 @@ i3Font *load_font(xcb_connection_t *conn, const char *pattern) {
|
|||||||
if (error != NULL) {
|
if (error != NULL) {
|
||||||
ELOG("Could not open font %s (X error %d). Reverting to backup font.\n", pattern, error->error_code);
|
ELOG("Could not open font %s (X error %d). Reverting to backup font.\n", pattern, error->error_code);
|
||||||
pattern = "fixed";
|
pattern = "fixed";
|
||||||
font_cookie = xcb_open_font_checked(conn, new->id, strlen(pattern), pattern);
|
font_cookie = xcb_open_font_checked(conn, new.id, strlen(pattern), pattern);
|
||||||
info_cookie = xcb_list_fonts_with_info(conn, 1, strlen(pattern), pattern);
|
info_cookie = xcb_list_fonts_with_info(conn, 1, strlen(pattern), pattern);
|
||||||
|
|
||||||
/* Check if we managed to open 'fixed' */
|
/* Check if we managed to open 'fixed' */
|
||||||
@ -54,7 +49,7 @@ i3Font *load_font(xcb_connection_t *conn, const char *pattern) {
|
|||||||
if (error != NULL) {
|
if (error != NULL) {
|
||||||
ELOG("Could not open fallback font '%s', trying with '-misc-*'\n",pattern);
|
ELOG("Could not open fallback font '%s', trying with '-misc-*'\n",pattern);
|
||||||
pattern = "-misc-*";
|
pattern = "-misc-*";
|
||||||
font_cookie = xcb_open_font_checked(conn, new->id, strlen(pattern), pattern);
|
font_cookie = xcb_open_font_checked(conn, new.id, strlen(pattern), pattern);
|
||||||
info_cookie = xcb_list_fonts_with_info(conn, 1, strlen(pattern), pattern);
|
info_cookie = xcb_list_fonts_with_info(conn, 1, strlen(pattern), pattern);
|
||||||
|
|
||||||
check_error(conn, font_cookie, "Could open neither requested font nor fallback (fixed or -misc-*");
|
check_error(conn, font_cookie, "Could open neither requested font nor fallback (fixed or -misc-*");
|
||||||
@ -65,14 +60,7 @@ i3Font *load_font(xcb_connection_t *conn, const char *pattern) {
|
|||||||
xcb_list_fonts_with_info_reply_t *reply = xcb_list_fonts_with_info_reply(conn, info_cookie, NULL);
|
xcb_list_fonts_with_info_reply_t *reply = xcb_list_fonts_with_info_reply(conn, info_cookie, NULL);
|
||||||
exit_if_null(reply, "Could not load font \"%s\"\n", pattern);
|
exit_if_null(reply, "Could not load font \"%s\"\n", pattern);
|
||||||
|
|
||||||
if (asprintf(&(new->name), "%.*s", xcb_list_fonts_with_info_name_length(reply),
|
new.height = reply->font_ascent + reply->font_descent;
|
||||||
xcb_list_fonts_with_info_name(reply)) == -1)
|
|
||||||
die("asprintf() failed\n");
|
|
||||||
new->pattern = sstrdup(pattern);
|
|
||||||
new->height = reply->font_ascent + reply->font_descent;
|
|
||||||
|
|
||||||
/* Insert into cache */
|
|
||||||
TAILQ_INSERT_TAIL(&cached_fonts, new, fonts);
|
|
||||||
|
|
||||||
return new;
|
return new;
|
||||||
}
|
}
|
||||||
@ -127,9 +115,9 @@ xcb_window_t create_window(xcb_connection_t *conn, Rect dims, uint16_t window_cl
|
|||||||
values[0] = xcursor_get_cursor(cursor);
|
values[0] = xcursor_get_cursor(cursor);
|
||||||
xcb_change_window_attributes(conn, result, mask, values);
|
xcb_change_window_attributes(conn, result, mask, values);
|
||||||
} else {
|
} else {
|
||||||
i3Font *cursor_font = load_font(conn, "cursor");
|
i3Font cursor_font = load_font("cursor", false);
|
||||||
int xcb_cursor = xcursor_get_xcb_cursor(cursor);
|
int xcb_cursor = xcursor_get_xcb_cursor(cursor);
|
||||||
xcb_create_glyph_cursor(conn, cursor_id, cursor_font->id, cursor_font->id,
|
xcb_create_glyph_cursor(conn, cursor_id, cursor_font.id, cursor_font.id,
|
||||||
xcb_cursor, xcb_cursor + 1, 0, 0, 0, 65535, 65535, 65535);
|
xcb_cursor, xcb_cursor + 1, 0, 0, 0, 65535, 65535, 65535);
|
||||||
xcb_change_window_attributes(conn, result, XCB_CW_CURSOR, &cursor_id);
|
xcb_change_window_attributes(conn, result, XCB_CW_CURSOR, &cursor_id);
|
||||||
xcb_free_cursor(conn, cursor_id);
|
xcb_free_cursor(conn, cursor_id);
|
||||||
@ -318,15 +306,13 @@ void cached_pixmap_prepare(xcb_connection_t *conn, struct Cached_Pixmap *pixmap)
|
|||||||
* length (amount of glyphs) using the given font.
|
* length (amount of glyphs) using the given font.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
int predict_text_width(xcb_connection_t *conn, const char *font_pattern, char *text, int length) {
|
int predict_text_width(char *text, int length) {
|
||||||
i3Font *font = load_font(conn, font_pattern);
|
|
||||||
|
|
||||||
xcb_query_text_extents_cookie_t cookie;
|
xcb_query_text_extents_cookie_t cookie;
|
||||||
xcb_query_text_extents_reply_t *reply;
|
xcb_query_text_extents_reply_t *reply;
|
||||||
xcb_generic_error_t *error;
|
xcb_generic_error_t *error;
|
||||||
int width;
|
int width;
|
||||||
|
|
||||||
cookie = xcb_query_text_extents(conn, font->id, length, (xcb_char2b_t*)text);
|
cookie = xcb_query_text_extents(conn, config.font.id, length, (xcb_char2b_t*)text);
|
||||||
if ((reply = xcb_query_text_extents_reply(conn, cookie, &error)) == NULL) {
|
if ((reply = xcb_query_text_extents_reply(conn, cookie, &error)) == NULL) {
|
||||||
ELOG("Could not get text extents (X error code %d)\n",
|
ELOG("Could not get text extents (X error code %d)\n",
|
||||||
error->error_code);
|
error->error_code);
|
||||||
|
Loading…
Reference in New Issue
Block a user