From 32129083bdb26df3257d4777bd499cb3c31046ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Sat, 2 Nov 2019 13:50:40 +0100 Subject: [PATCH] main: pre-allocate fonts Since fonts are cached, this adds no additional memory. However, it makes the first terminal window in --server mode start much faster, since the (primary) fonts have already been loaded. Fallback fonts are still loaded on-demand. --- main.c | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/main.c b/main.c index 26a8f300..1865b37a 100644 --- a/main.c +++ b/main.c @@ -59,6 +59,26 @@ term_shutdown_cb(void *data, int exit_code) ctx->exit_code = exit_code; } +static bool +initialize_fonts(const struct config *conf, struct font *fonts[4]) +{ + font_list_t font_names = tll_init(); + tll_foreach(conf->fonts, it) + tll_push_back(font_names, it->item); + + if ((fonts[0] = font_from_name(font_names, "")) == NULL || + (fonts[1] = font_from_name(font_names, "style=bold")) == NULL || + (fonts[2] = font_from_name(font_names, "style=italic")) == NULL || + (fonts[3] = font_from_name(font_names, "style=bold italic")) == NULL) + { + tll_free(font_names); + return false; + } + + tll_free(font_names); + return true; +} + int main(int argc, char *const *argv) { @@ -162,12 +182,16 @@ main(int argc, char *const *argv) setlocale(LC_ALL, ""); + struct font *fonts[4] = {NULL}; struct fdm *fdm = NULL; struct wayland *wayl = NULL; struct terminal *term = NULL; struct server *server = NULL; struct shutdown_context shutdown_ctx = {.term = &term, .exit_code = EXIT_FAILURE}; + if (!initialize_fonts(&conf, fonts)) + goto out; + if ((fdm = fdm_init()) == NULL) goto out; @@ -204,7 +228,10 @@ out: term_destroy(term); wayl_destroy(wayl); fdm_destroy(fdm); - config_free(conf); + for (size_t i = 0; i < sizeof(fonts) / sizeof(fonts[0]); i++) + font_destroy(fonts[i]); + + config_free(conf); return ret == EXIT_SUCCESS && !as_server ? shutdown_ctx.exit_code : ret; }