SDL: Load font from VFS instead of using a file path.

Hopefully helps #21014
This commit is contained in:
Henrik Rydgård committed 2025-11-24 00:55:12 +01:00
1 parent 666808fb8c
commit acd8cf70d0
2 files changed
+24 -13

No files matched your search

+23 -12
View File
@@ -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<int>(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<int>(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<uint8_t> &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<uint8_t> &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_) {
+1 -1
View File
@@ -33,7 +33,7 @@ private:
uint32_t CheckMissingGlyph(std::string_view text);
int FindFallbackFonts(uint32_t missingGlyph, int ptSize);
std::map<FontStyle, TTF_Font *> fontMap_;
std::map<FontStyle, std::pair<TTF_Font *, uint8_t *>> fontMap_;
std::vector<TTF_Font *> fallbackFonts_;
std::vector<std::pair<std::string, int>> fallbackFontPaths_; // path and font face index