From a789230cf9f87c68fcced647d0a1e8edc516f1c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Wed, 16 Oct 2019 21:52:12 +0200 Subject: [PATCH] font: font_from_name() returns an allocated font struct --- font.c | 10 +++++++--- font.h | 2 +- main.c | 20 ++++++++++---------- render.c | 4 ++-- terminal.h | 2 +- 5 files changed, 21 insertions(+), 17 deletions(-) diff --git a/font.c b/font.c index 049a3be8..b68e341c 100644 --- a/font.c +++ b/font.c @@ -291,8 +291,8 @@ from_name(const char *base_name, const font_list_t *fallbacks, const char *attri return true; } -bool -font_from_name(font_list_t names, const char *attributes, struct font *font) +struct font * +font_from_name(font_list_t names, const char *attributes) { if (tll_length(names) == 0) return false; @@ -308,10 +308,14 @@ font_from_name(font_list_t names, const char *attributes, struct font *font) tll_push_back(fallbacks, it->item); } + struct font *font = malloc(sizeof(*font)); bool ret = from_name(tll_front(names), &fallbacks, attributes, font, false); + if (!ret) + free(font); + tll_free(fallbacks); - return ret; + return ret ? font : NULL; } static size_t diff --git a/font.h b/font.h index 594ae6ec..1d5ad4dc 100644 --- a/font.h +++ b/font.h @@ -58,6 +58,6 @@ struct font { mtx_t lock; }; -bool font_from_name(font_list_t names, const char *attributes, struct font *result); +struct font *font_from_name(font_list_t names, const char *attributes); const struct glyph *font_glyph_for_wc(struct font *font, wchar_t wc); void font_destroy(struct font *font); diff --git a/main.c b/main.c index 0f93e188..e81d2395 100644 --- a/main.c +++ b/main.c @@ -623,28 +623,28 @@ main(int argc, char *const *argv) tll_foreach(conf.fonts, it) tll_push_back(font_names, it->item); - if (!font_from_name(font_names, "", &term.fonts[0])) { + if ((term.fonts[0] = font_from_name(font_names, "")) == NULL) { tll_free(font_names); goto out; } - font_from_name(font_names, "style=bold", &term.fonts[1]); - font_from_name(font_names, "style=italic", &term.fonts[2]); - font_from_name(font_names, "style=bold italic", &term.fonts[3]); + term.fonts[1] = font_from_name(font_names, "style=bold"); + term.fonts[2] = font_from_name(font_names, "style=italic"); + term.fonts[3] = font_from_name(font_names, "style=bold italic"); tll_free(font_names); { - FT_Face ft_face = term.fonts[0].face; + FT_Face ft_face = term.fonts[0]->face; int max_x_advance = ft_face->size->metrics.max_advance / 64; int height = ft_face->size->metrics.height / 64; int descent = ft_face->size->metrics.descender / 64; int ascent = ft_face->size->metrics.ascender / 64; - term.fextents.height = height * term.fonts[0].pixel_size_fixup; - term.fextents.descent = -descent * term.fonts[0].pixel_size_fixup; - term.fextents.ascent = ascent * term.fonts[0].pixel_size_fixup; - term.fextents.max_x_advance = max_x_advance * term.fonts[0].pixel_size_fixup; + term.fextents.height = height * term.fonts[0]->pixel_size_fixup; + term.fextents.descent = -descent * term.fonts[0]->pixel_size_fixup; + term.fextents.ascent = ascent * term.fonts[0]->pixel_size_fixup; + term.fextents.max_x_advance = max_x_advance * term.fonts[0]->pixel_size_fixup; LOG_DBG("metrics: height: %d, descent: %d, ascent: %d, x-advance: %d", height, descent, ascent, max_x_advance); @@ -1185,7 +1185,7 @@ out: tll_free_and_free(term.window_title_stack, free); for (size_t i = 0; i < sizeof(term.fonts) / sizeof(term.fonts[0]); i++) - font_destroy(&term.fonts[i]); + font_destroy(term.fonts[i]); free(term.search.buf); diff --git a/render.c b/render.c index bfd27faa..ff05d501 100644 --- a/render.c +++ b/render.c @@ -24,7 +24,7 @@ struct font * attrs_to_font(struct terminal *term, const struct attributes *attrs) { int idx = attrs->italic << 1 | attrs->bold; - return &term->fonts[idx]; + return term->fonts[idx]; } static inline struct rgb @@ -750,7 +750,7 @@ render_search_box(struct terminal *term) PIXMAN_OP_SRC, buf->pix, &color, 1, &(pixman_rectangle16_t){0, 0, width, height}); - struct font *font = &term->fonts[0]; + struct font *font = term->fonts[0]; int x = margin; int y = margin; pixman_color_t fg = color_hex_to_pixman(term->colors.table[0]); diff --git a/terminal.h b/terminal.h index 72a41fcf..6f6e1d53 100644 --- a/terminal.h +++ b/terminal.h @@ -366,7 +366,7 @@ struct terminal { struct grid alt; struct grid *grid; - struct font fonts[4]; + struct font *fonts[4]; struct { int height; int descent;