Skip to content

Commit

Permalink
Fix font width calculation when loading fonts monospaced
Browse files Browse the repository at this point in the history
  • Loading branch information
a740g committed Apr 30, 2024
1 parent 5308331 commit dbe028e
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions internal/c/parts/video/font/font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

// 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");
}
}

fontManager.fonts[h]->monospaceWidth = fontManager.fonts[h]->GetStringPixelWidth(&testCP, 1);
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);
}
Expand Down

0 comments on commit dbe028e

Please sign in to comment.