From c2aa0072285378f16a657cd987aa36a2cb3d6482 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=9A=93?= Date: Thu, 27 Mar 2025 14:59:28 -0400 Subject: [PATCH] Fix text size calculation in libretro builds Since I got rid of SDL in libretro builds, I can't use SDL_ttf, so I have to implement text rendering somewhat more manually. I'm still getting the hang of it. --- src/display/bitmap.cpp | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/display/bitmap.cpp b/src/display/bitmap.cpp index 70bdf8b2..45c676f7 100644 --- a/src/display/bitmap.cpp +++ b/src/display/bitmap.cpp @@ -2172,8 +2172,8 @@ IntRect Bitmap::textRect(const char *str) // TODO: handle kerning int bitmap_left = 0; int bitmap_right = 0; - int bitmap_top = 0; - int bitmap_bottom = 0; + int bitmap_top = -font->size->metrics.ascender / 64; + int bitmap_bottom = -font->size->metrics.descender / 64; int glyph_x = 0; int glyph_y = 0; @@ -2187,13 +2187,13 @@ IntRect Bitmap::textRect(const char *str) int glyph_top = glyph_y - font->glyph->bitmap_top; int glyph_bottom = glyph_top + font->glyph->bitmap.rows; - bitmap_left = std::min(bitmap_left, glyph_left); - bitmap_right = std::max(bitmap_right, glyph_right); - bitmap_top = std::min(bitmap_top, glyph_top); - bitmap_bottom = std::max(bitmap_bottom, glyph_bottom); - glyph_x += font->glyph->advance.x / 64; glyph_y += font->glyph->advance.y / 64; + + bitmap_left = std::min(bitmap_left, std::min(glyph_left, glyph_x)); + bitmap_right = std::max(bitmap_right, std::max(glyph_right, glyph_x)); + bitmap_top = std::min(bitmap_top, std::min(glyph_top, glyph_y)); + bitmap_bottom = std::max(bitmap_bottom, std::max(glyph_bottom, glyph_y)); } return IntRect(bitmap_left, bitmap_top, bitmap_right - bitmap_left, bitmap_bottom - bitmap_top); @@ -2435,15 +2435,15 @@ IntRect Bitmap::textSize(const char *str) // TODO: High-res Bitmap textSize not implemented, but I think it's the same as low-res? // Need to double-check this. + std::string fixed = fixupString(str); + str = fixed.c_str(); + #ifdef MKXPZ_RETRO IntRect rect = textRect(str); return IntRect(0, 0, rect.w, rect.h); #else TTF_Font *font = p->font->getSdlFont(); - std::string fixed = fixupString(str); - str = fixed.c_str(); - int w, h; TTF_SizeUTF8(font, str, &w, &h);