font: font_from_name() returns an allocated font struct

This commit is contained in:
Daniel Eklöf 2019-10-16 21:52:12 +02:00
parent 3ca6f9fe0b
commit a789230cf9
No known key found for this signature in database
GPG key ID: 5BBD4992C116573F
5 changed files with 21 additions and 17 deletions

10
font.c
View file

@ -291,8 +291,8 @@ from_name(const char *base_name, const font_list_t *fallbacks, const char *attri
return true; return true;
} }
bool struct font *
font_from_name(font_list_t names, const char *attributes, struct font *font) font_from_name(font_list_t names, const char *attributes)
{ {
if (tll_length(names) == 0) if (tll_length(names) == 0)
return false; 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); tll_push_back(fallbacks, it->item);
} }
struct font *font = malloc(sizeof(*font));
bool ret = from_name(tll_front(names), &fallbacks, attributes, font, false); bool ret = from_name(tll_front(names), &fallbacks, attributes, font, false);
if (!ret)
free(font);
tll_free(fallbacks); tll_free(fallbacks);
return ret; return ret ? font : NULL;
} }
static size_t static size_t

2
font.h
View file

@ -58,6 +58,6 @@ struct font {
mtx_t lock; 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); const struct glyph *font_glyph_for_wc(struct font *font, wchar_t wc);
void font_destroy(struct font *font); void font_destroy(struct font *font);

20
main.c
View file

@ -623,28 +623,28 @@ main(int argc, char *const *argv)
tll_foreach(conf.fonts, it) tll_foreach(conf.fonts, it)
tll_push_back(font_names, it->item); 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); tll_free(font_names);
goto out; goto out;
} }
font_from_name(font_names, "style=bold", &term.fonts[1]); term.fonts[1] = font_from_name(font_names, "style=bold");
font_from_name(font_names, "style=italic", &term.fonts[2]); term.fonts[2] = font_from_name(font_names, "style=italic");
font_from_name(font_names, "style=bold italic", &term.fonts[3]); term.fonts[3] = font_from_name(font_names, "style=bold italic");
tll_free(font_names); 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 max_x_advance = ft_face->size->metrics.max_advance / 64;
int height = ft_face->size->metrics.height / 64; int height = ft_face->size->metrics.height / 64;
int descent = ft_face->size->metrics.descender / 64; int descent = ft_face->size->metrics.descender / 64;
int ascent = ft_face->size->metrics.ascender / 64; int ascent = ft_face->size->metrics.ascender / 64;
term.fextents.height = height * 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.descent = -descent * term.fonts[0]->pixel_size_fixup;
term.fextents.ascent = ascent * 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.max_x_advance = max_x_advance * term.fonts[0]->pixel_size_fixup;
LOG_DBG("metrics: height: %d, descent: %d, ascent: %d, x-advance: %d", LOG_DBG("metrics: height: %d, descent: %d, ascent: %d, x-advance: %d",
height, descent, ascent, max_x_advance); height, descent, ascent, max_x_advance);
@ -1185,7 +1185,7 @@ out:
tll_free_and_free(term.window_title_stack, free); tll_free_and_free(term.window_title_stack, free);
for (size_t i = 0; i < sizeof(term.fonts) / sizeof(term.fonts[0]); i++) 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); free(term.search.buf);

View file

@ -24,7 +24,7 @@ struct font *
attrs_to_font(struct terminal *term, const struct attributes *attrs) attrs_to_font(struct terminal *term, const struct attributes *attrs)
{ {
int idx = attrs->italic << 1 | attrs->bold; int idx = attrs->italic << 1 | attrs->bold;
return &term->fonts[idx]; return term->fonts[idx];
} }
static inline struct rgb static inline struct rgb
@ -750,7 +750,7 @@ render_search_box(struct terminal *term)
PIXMAN_OP_SRC, buf->pix, &color, PIXMAN_OP_SRC, buf->pix, &color,
1, &(pixman_rectangle16_t){0, 0, width, height}); 1, &(pixman_rectangle16_t){0, 0, width, height});
struct font *font = &term->fonts[0]; struct font *font = term->fonts[0];
int x = margin; int x = margin;
int y = margin; int y = margin;
pixman_color_t fg = color_hex_to_pixman(term->colors.table[0]); pixman_color_t fg = color_hex_to_pixman(term->colors.table[0]);

View file

@ -366,7 +366,7 @@ struct terminal {
struct grid alt; struct grid alt;
struct grid *grid; struct grid *grid;
struct font fonts[4]; struct font *fonts[4];
struct { struct {
int height; int height;
int descent; int descent;