From 431564c8bcafaec283fafc94fb39dcdd92665795 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 27 Mar 2018 12:04:38 +0530 Subject: [PATCH] Linux: When calculating cell widths use the actual rendered bitmap width in addition to the horizontal advance reported by FreeType. The horizontal advance is insufficient for some fonts at some point sizes. So although rendering the characters is slower, we do it to ensure cell width is correct. Fixes #352 --- kitty/freetype.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/kitty/freetype.c b/kitty/freetype.c index 45f2ce9ba..31d2b111d 100644 --- a/kitty/freetype.c +++ b/kitty/freetype.c @@ -278,8 +278,12 @@ calc_cell_width(Face *self) { unsigned int ans = 0; for (char_type i = 32; i < 128; i++) { int glyph_index = FT_Get_Char_Index(self->face, i); - if (load_glyph(self, glyph_index, FT_LOAD_DEFAULT)) { - ans = MAX(ans, (unsigned long)ceilf((float)self->face->glyph->metrics.horiAdvance / 64.f)); + // We actually render the bitmap and get its width even though this is very slow, because + // there are fonts for which the horizontal advance is incorrect, see https://github.com/kovidgoyal/kitty/issues/352#issuecomment-376262576 + if (load_glyph(self, glyph_index, FT_LOAD_RENDER)) { + unsigned int horizontal_advance = (unsigned int)ceilf((float)self->face->glyph->metrics.horiAdvance / 64.f); + ans = MAX(ans, self->face->glyph->bitmap.width); + ans = MAX(ans, horizontal_advance); } } return ans;