diff --git a/Common/Render/Text/draw_text_sdl.cpp b/Common/Render/Text/draw_text_sdl.cpp index d3fff97878..e38efba8ec 100644 --- a/Common/Render/Text/draw_text_sdl.cpp +++ b/Common/Render/Text/draw_text_sdl.cpp @@ -192,7 +192,7 @@ uint32_t TextDrawerSDL::CheckMissingGlyph(std::string_view text) { return 0; } - TTF_Font *font = iter->second; + TTF_Font *font = iter->second.first; if (!font) { return 0; } @@ -256,24 +256,34 @@ void TextDrawerSDL::SetOrCreateFont(const FontStyle &style) { } TTF_Font *font = nullptr; + uint8_t *fileData = nullptr; if (fontMap_.find(style) == fontMap_.end()) { - std::string useFont = "assets/" + GetFilenameForFontStyle(style) + ".ttf"; + std::string useFont = GetFilenameForFontStyle(style) + ".ttf"; const int ptSize = static_cast(style.sizePts / dpiScale_ * 1.25f); - INFO_LOG(Log::G3D, "Loading SDL font %s at size %d pts", useFont.c_str(), ptSize); - font = TTF_OpenFont(useFont.c_str(), ptSize); - if (!font) { - bool exists = File::Exists(Path(useFont)); - ERROR_LOG(Log::G3D, "Failed to load font file %s: exists=%d", useFont.c_str(), (int)exists); + INFO_LOG(Log::G3D, "Loading SDL font '%s' from VFS at size %d pts", useFont.c_str(), ptSize); + + size_t fileSz; + uint8_t *fileData = g_VFS.ReadFile(useFont.c_str(), &fileSz); + if (fileData) { + SDL_RWops *rw = SDL_RWFromMem(fileData, static_cast(fileSz)); + INFO_LOG(Log::G3D, "Opened font from RW: '%p' '%d'", fileData, (int)fileSz); + font = TTF_OpenFontRW(rw, 1, ptSize); + if (!font) { + ERROR_LOG(Log::G3D, "Failed to load font from asset file: '%s'", useFont.c_str()); + } + } else { + ERROR_LOG(Log::G3D, "Failed to load font file %s from VFS", useFont.c_str()); } + // Still, even if it failed and font is nullptr, mark it in the map to avoid retrying. } - fontMap_[style] = font; + fontMap_[style] = std::make_pair(font, fileData); fontStyle_ = style; } void TextDrawerSDL::MeasureStringInternal(std::string_view str, float *w, float *h) { - TTF_Font *font = fontMap_.find(fontStyle_)->second; + TTF_Font *font = fontMap_.find(fontStyle_)->second.first; if (!font) { *w = 1.0f; @@ -326,7 +336,7 @@ bool TextDrawerSDL::DrawStringBitmap(std::vector &bitmapData, TextStrin return false; } - TTF_Font *font = fontIter->second; + TTF_Font *font = fontIter->second.first; if (!font) { return false; } @@ -394,8 +404,9 @@ bool TextDrawerSDL::DrawStringBitmap(std::vector &bitmapData, TextStrin void TextDrawerSDL::ClearFonts() { for (auto iter : fontMap_) { - if (iter.second) { - TTF_CloseFont(iter.second); + if (iter.second.first) { + TTF_CloseFont(iter.second.first); + delete[] iter.second.second; } } for (auto iter : fallbackFonts_) { diff --git a/Common/Render/Text/draw_text_sdl.h b/Common/Render/Text/draw_text_sdl.h index 13fa67d49c..fc0d3bf19f 100644 --- a/Common/Render/Text/draw_text_sdl.h +++ b/Common/Render/Text/draw_text_sdl.h @@ -33,7 +33,7 @@ private: uint32_t CheckMissingGlyph(std::string_view text); int FindFallbackFonts(uint32_t missingGlyph, int ptSize); - std::map fontMap_; + std::map> fontMap_; std::vector fallbackFonts_; std::vector> fallbackFontPaths_; // path and font face index