From dbe028e79954007aa27dab6de47440079d6215fc Mon Sep 17 00:00:00 2001 From: Samuel Gomes Date: Tue, 30 Apr 2024 12:35:11 +0530 Subject: [PATCH 1/2] Fix font width calculation when loading fonts monospaced --- internal/c/parts/video/font/font.cpp | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/internal/c/parts/video/font/font.cpp b/internal/c/parts/video/font/font.cpp index 8d4193871..18cdfe7ad 100644 --- a/internal/c/parts/video/font/font.cpp +++ b/internal/c/parts/video/font/font.cpp @@ -822,9 +822,23 @@ int32_t FontLoad(const uint8_t *content_original, int32_t content_bytes, int32_t fontManager.fonts[h]->options = options; // save the options for use later if ((options & FONT_LOAD_MONOSPACE) || FT_IS_FIXED_WIDTH(fontManager.fonts[h]->face)) { - const uint32_t testCP = 'W'; // since W is usually the widest + const FT_ULong testCP = 'W'; // since W is usually the widest - fontManager.fonts[h]->monospaceWidth = fontManager.fonts[h]->GetStringPixelWidth(&testCP, 1); + // Load using monochrome rendering + if (FT_Load_Char(fontManager.fonts[h]->face, testCP, FT_LOAD_RENDER | FT_LOAD_MONOCHROME | FT_LOAD_TARGET_MONO)) { + FONT_DEBUG_PRINT("FT_Load_Char() (monochrome) failed"); + // Retry using gray-level rendering + if (FT_Load_Char(fontManager.fonts[h]->face, testCP, FT_LOAD_RENDER)) { + FONT_DEBUG_PRINT("FT_Load_Char() (gray) failed"); + } + } + + if (fontManager.fonts[h]->face->glyph) { + fontManager.fonts[h]->monospaceWidth = + std::max(fontManager.fonts[h]->face->glyph->advance.x / 64, (FT_Pos)fontManager.fonts[h]->face->glyph->bitmap.width); // save the max width + + FONT_DEBUG_PRINT("Monospace font (width = %li) requested", fontManager.fonts[h]->monospaceWidth); + } FONT_DEBUG_PRINT("Monospace font (width = %li) requested", fontManager.fonts[h]->monospaceWidth); } From b3a247c97e7cfff2d751dea1c3ac9b842f11ee81 Mon Sep 17 00:00:00 2001 From: Samuel Gomes Date: Tue, 30 Apr 2024 12:55:31 +0530 Subject: [PATCH 2/2] Remove extra log line --- internal/c/parts/video/font/font.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/internal/c/parts/video/font/font.cpp b/internal/c/parts/video/font/font.cpp index 18cdfe7ad..1671590be 100644 --- a/internal/c/parts/video/font/font.cpp +++ b/internal/c/parts/video/font/font.cpp @@ -839,8 +839,6 @@ int32_t FontLoad(const uint8_t *content_original, int32_t content_bytes, int32_t FONT_DEBUG_PRINT("Monospace font (width = %li) requested", fontManager.fonts[h]->monospaceWidth); } - - FONT_DEBUG_PRINT("Monospace font (width = %li) requested", fontManager.fonts[h]->monospaceWidth); } FONT_DEBUG_PRINT("Font (height = %i, index = %i) successfully initialized", default_pixel_height, which_font);