From 0757156caff45d6fcc86ef43f8fb51664ee60e15 Mon Sep 17 00:00:00 2001 From: Leon Styhre Date: Sun, 9 Oct 2022 18:01:30 +0200 Subject: [PATCH] Improved error handling for unloadable font files. --- es-core/src/resources/Font.cpp | 22 ++++++++++------------ es-core/src/resources/Font.h | 8 ++++---- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/es-core/src/resources/Font.cpp b/es-core/src/resources/Font.cpp index 02140261a..d19d57bde 100644 --- a/es-core/src/resources/Font.cpp +++ b/es-core/src/resources/Font.cpp @@ -4,7 +4,7 @@ // Font.h // // 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" @@ -12,12 +12,9 @@ #include "Log.h" #include "renderers/Renderer.h" #include "utils/FileSystemUtil.h" +#include "utils/PlatformUtil.h" #include "utils/StringUtil.h" -FT_Library Font::sLibrary {nullptr}; - -std::map, std::weak_ptr> Font::sFontMap; - Font::Font(int size, const std::string& path) : mRenderer {Renderer::getInstance()} , 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} { - int err { - FT_New_Memory_Face(sLibrary, data.ptr.get(), static_cast(data.length), 0, &face)}; - assert(!err); + if (FT_New_Memory_Face(sLibrary, d.ptr.get(), static_cast(d.length), 0, &face) != 0) { + LOG(LogError) << "Couldn't load font file \"" << path << "\""; + Utils::Platform::emergencyShutdown(); + } - if (!err) - FT_Set_Pixel_Sizes(face, 0, size); + FT_Set_Pixel_Sizes(face, 0, size); } Font::FontFace::~FontFace() @@ -708,7 +705,8 @@ FT_Face Font::getFaceForChar(unsigned int id) // Otherwise, take from fallbackFonts. const std::string& path {i == 0 ? mPath : fallbackFonts.at(i - 1)}; ResourceData data {ResourceManager::getInstance().getFileData(path)}; - mFaceCache[i] = std::unique_ptr(new FontFace(std::move(data), mFontSize)); + mFaceCache[i] = + std::unique_ptr(new FontFace(std::move(data), mFontSize, mPath)); fit = mFaceCache.find(i); } diff --git a/es-core/src/resources/Font.h b/es-core/src/resources/Font.h index ef6b11302..602c763a5 100644 --- a/es-core/src/resources/Font.h +++ b/es-core/src/resources/Font.h @@ -4,7 +4,7 @@ // Font.h // // 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 @@ -105,8 +105,8 @@ public: private: Renderer* mRenderer; - static FT_Library sLibrary; - static std::map, std::weak_ptr> sFontMap; + static inline FT_Library sLibrary {nullptr}; + static inline std::map, std::weak_ptr> sFontMap; Font(int size, const std::string& path); @@ -135,7 +135,7 @@ private: const ResourceData data; FT_Face face; - FontFace(ResourceData&& d, int size); + FontFace(ResourceData&& d, int size, const std::string& path); virtual ~FontFace(); };