Move static variable for finalize font config to be global scope

This commit is contained in:
Filip Sykala 2022-03-30 16:11:27 +02:00
parent 6610bf2eb1
commit 0bf90ae0af
3 changed files with 18 additions and 35 deletions

View File

@ -8,14 +8,18 @@
using namespace Slic3r::GUI; using namespace Slic3r::GUI;
FontConfigHelp::FontConfigHelp() { FcInit(); } // @Vojta suggest to make static variable global
FontConfigHelp::~FontConfigHelp() { // Guard for finalize Font Config
// fccache.c:795: FcCacheFini: Assertion `fcCacheChains[i] == NULL' failed. // Will be finalized on application exit
// FcFini(); static std::optional<Slic3r::ScopeGuard> finalize_guard;
}
std::string FontConfigHelp::get_font_path(const wxFont &font) std::string Slic3r::GUI::get_font_path(const wxFont &font)
{ {
if (!finalize_guard.has_value()) {
FcInit();
finalize_guard.emplace([]() { FcFini(); });
}
FcConfig *fc = FcInitLoadConfigAndFonts(); FcConfig *fc = FcInitLoadConfigAndFonts();
if (fc == nullptr) return ""; if (fc == nullptr) return "";
ScopeGuard sg_fc([fc]() { FcConfigDestroy(fc); }); ScopeGuard sg_fc([fc]() { FcConfigDestroy(fc); });

View File

@ -7,33 +7,15 @@
#ifdef EXIST_FONT_CONFIG_INCLUDE #ifdef EXIST_FONT_CONFIG_INCLUDE
#include <wx/font.h> #include <wx/font.h>
namespace Slic3r::GUI { namespace Slic3r::GUI {
/// <summary> /// <summary>
/// helper object for RAII access to font config /// initialize font config
/// initialize & finalize FontConfig /// Convert wx widget font to file path
/// inspired by wxpdfdoc -
/// https://github.com/utelle/wxpdfdoc/blob/5bdcdb9953327d06dc50ec312685ccd9bc8400e0/src/pdffontmanager.cpp
/// </summary> /// </summary>
class FontConfigHelp std::string get_font_path(const wxFont &font);
{
public:
/// <summary>
/// initialize font config
/// </summary>
FontConfigHelp();
/// <summary>
/// free font config resources
/// </summary>
~FontConfigHelp();
/// <summary>
/// initialize font config
/// Convert wx widget font to file path
/// inspired by wxpdfdoc -
/// https://github.com/utelle/wxpdfdoc/blob/5bdcdb9953327d06dc50ec312685ccd9bc8400e0/src/pdffontmanager.cpp
/// </summary>
std::string get_font_path(const wxFont &font);
};
} // namespace Slic3r } // namespace Slic3r
#endif // EXIST_FONT_CONFIG_INCLUDE #endif // EXIST_FONT_CONFIG_INCLUDE

View File

@ -59,9 +59,7 @@ bool WxFontUtils::can_load(const wxFont &font)
return false; return false;
return is_valid_ttf(file_path); return is_valid_ttf(file_path);
#elif defined(__linux__) #elif defined(__linux__)
// TODO: find better way std::string font_path = Slic3r::GUI::get_font_path(font);
static FontConfigHelp help;
std::string font_path = help.get_font_path(font);
return !font_path.empty(); return !font_path.empty();
#endif #endif
return false; return false;
@ -88,8 +86,7 @@ std::unique_ptr<Emboss::FontFile> WxFontUtils::create_font_file(const wxFont &fo
file_path = file_path.substr(start, file_path.size() - start); file_path = file_path.substr(start, file_path.size() - start);
return Emboss::create_font_file(file_path.c_str()); return Emboss::create_font_file(file_path.c_str());
#elif defined(__linux__) #elif defined(__linux__)
static FontConfigHelp help; std::string font_path = Slic3r::GUI::get_font_path(font);
std::string font_path = help.get_font_path(font);
if (font_path.empty()) return nullptr; if (font_path.empty()) return nullptr;
return Emboss::create_font_file(font_path.c_str()); return Emboss::create_font_file(font_path.c_str());
#else #else