Improved error handling for unloadable font files.

This commit is contained in:
Leon Styhre 2022-10-09 18:01:30 +02:00
parent 50832a5b64
commit 0757156caf
2 changed files with 14 additions and 16 deletions

View file

@ -4,7 +4,7 @@
// Font.h // Font.h
// //
// Loading, unloading, caching and rendering of fonts. // Loading, unloading, caching and rendering of fonts.
// Also functions for word wrapping and similar. // Also functions for text wrapping and similar.
// //
#include "resources/Font.h" #include "resources/Font.h"
@ -12,12 +12,9 @@
#include "Log.h" #include "Log.h"
#include "renderers/Renderer.h" #include "renderers/Renderer.h"
#include "utils/FileSystemUtil.h" #include "utils/FileSystemUtil.h"
#include "utils/PlatformUtil.h"
#include "utils/StringUtil.h" #include "utils/StringUtil.h"
FT_Library Font::sLibrary {nullptr};
std::map<std::pair<std::string, int>, std::weak_ptr<Font>> Font::sFontMap;
Font::Font(int size, const std::string& path) Font::Font(int size, const std::string& path)
: mRenderer {Renderer::getInstance()} : mRenderer {Renderer::getInstance()}
, mFontSize(size) , mFontSize(size)
@ -607,15 +604,15 @@ void Font::FontTexture::deinitTexture()
} }
} }
Font::FontFace::FontFace(ResourceData&& d, int size) Font::FontFace::FontFace(ResourceData&& d, int size, const std::string& path)
: data {d} : data {d}
{ {
int err { if (FT_New_Memory_Face(sLibrary, d.ptr.get(), static_cast<FT_Long>(d.length), 0, &face) != 0) {
FT_New_Memory_Face(sLibrary, data.ptr.get(), static_cast<FT_Long>(data.length), 0, &face)}; LOG(LogError) << "Couldn't load font file \"" << path << "\"";
assert(!err); Utils::Platform::emergencyShutdown();
}
if (!err) FT_Set_Pixel_Sizes(face, 0, size);
FT_Set_Pixel_Sizes(face, 0, size);
} }
Font::FontFace::~FontFace() Font::FontFace::~FontFace()
@ -708,7 +705,8 @@ FT_Face Font::getFaceForChar(unsigned int id)
// Otherwise, take from fallbackFonts. // Otherwise, take from fallbackFonts.
const std::string& path {i == 0 ? mPath : fallbackFonts.at(i - 1)}; const std::string& path {i == 0 ? mPath : fallbackFonts.at(i - 1)};
ResourceData data {ResourceManager::getInstance().getFileData(path)}; ResourceData data {ResourceManager::getInstance().getFileData(path)};
mFaceCache[i] = std::unique_ptr<FontFace>(new FontFace(std::move(data), mFontSize)); mFaceCache[i] =
std::unique_ptr<FontFace>(new FontFace(std::move(data), mFontSize, mPath));
fit = mFaceCache.find(i); fit = mFaceCache.find(i);
} }

View file

@ -4,7 +4,7 @@
// Font.h // Font.h
// //
// Loading, unloading, caching and rendering of fonts. // Loading, unloading, caching and rendering of fonts.
// Also functions for word wrapping and similar. // Also functions for text wrapping and similar.
// //
#ifndef ES_CORE_RESOURCES_FONT_H #ifndef ES_CORE_RESOURCES_FONT_H
@ -105,8 +105,8 @@ public:
private: private:
Renderer* mRenderer; Renderer* mRenderer;
static FT_Library sLibrary; static inline FT_Library sLibrary {nullptr};
static std::map<std::pair<std::string, int>, std::weak_ptr<Font>> sFontMap; static inline std::map<std::pair<std::string, int>, std::weak_ptr<Font>> sFontMap;
Font(int size, const std::string& path); Font(int size, const std::string& path);
@ -135,7 +135,7 @@ private:
const ResourceData data; const ResourceData data;
FT_Face face; FT_Face face;
FontFace(ResourceData&& d, int size); FontFace(ResourceData&& d, int size, const std::string& path);
virtual ~FontFace(); virtual ~FontFace();
}; };