From 4d7993b36fa3f97e26c527c37b36eb9321b79791 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Fri, 2 Aug 2019 18:19:07 +0200 Subject: cell: pack more efficiently and store glyph as a wchar The 'attributes' struct is now 8 bytes and naturally packed (used to be 9 bytes, artificially packed). 'cell' struct is now 12 bytes, naturally packed (used to be 13 bytes, artificially packed). Furthermore, the glyph is stored as a wchar instead of a char*. This makes it easier (faster) to do glyph lookup when rendering. --- font.c | 23 +++++++---------------- 1 file changed, 7 insertions(+), 16 deletions(-) (limited to 'font.c') diff --git a/font.c b/font.c index 9c4a9b50..8deb2a3a 100644 --- a/font.c +++ b/font.c @@ -381,29 +381,23 @@ glyph_for_wchar(struct font *font, wchar_t wc, struct glyph *glyph) .left = font->face->glyph->bitmap_left, .top = font->face->glyph->bitmap_top, .pixel_size_fixup = font->pixel_size_fixup, + .valid = true, }; return true; err: + *glyph = (struct glyph){ + .valid = false, + }; return false; } const struct glyph * -font_glyph_for_utf8(struct font *font, const char *utf8) +font_glyph_for_wc(struct font *font, wchar_t wc) { mtx_lock(&font->lock); - mbstate_t ps = {0}; - wchar_t wc; - if (mbrtowc(&wc, utf8, 4, &ps) < 0) { - LOG_DBG("failed to convert utf-8 sequence %02x %02x %02x %02x to unicode", - (unsigned char)utf8[0], (unsigned char)utf8[1], - (unsigned char)utf8[2], (unsigned char)utf8[3]); - mtx_unlock(&font->lock); - return NULL; - } - assert(font->cache != NULL); size_t hash_idx = hash_index(wc); hash_entry_t *hash_entry = font->cache[hash_idx]; @@ -418,10 +412,7 @@ font_glyph_for_utf8(struct font *font, const char *utf8) } struct glyph glyph; - if (!glyph_for_wchar(font, wc, &glyph)) { - mtx_unlock(&font->lock); - return NULL; - } + bool got_glyph = glyph_for_wchar(font, wc, &glyph); if (hash_entry == NULL) { hash_entry = calloc(1, sizeof(*hash_entry)); @@ -434,7 +425,7 @@ font_glyph_for_utf8(struct font *font, const char *utf8) tll_push_back(*hash_entry, glyph); mtx_unlock(&font->lock); - return &tll_back(*hash_entry); + return got_glyph ? &tll_back(*hash_entry) : NULL; } void -- cgit v1.3