Initialize Qt application fonts from cached versions on filesystem

This commit is contained in:
Dan Paulat 2023-10-08 23:33:27 -05:00
parent 7af2edd3ec
commit fe1acb32cf
6 changed files with 28 additions and 26 deletions

View file

@ -8,6 +8,7 @@
#include <QFile> #include <QFile>
#include <QFileInfo> #include <QFileInfo>
#include <QFontDatabase>
#include <QStandardPaths> #include <QStandardPaths>
#include <boost/container_hash/hash.hpp> #include <boost/container_hash/hash.hpp>
#include <boost/unordered/unordered_flat_map.hpp> #include <boost/unordered/unordered_flat_map.hpp>
@ -92,6 +93,8 @@ public:
boost::unordered_flat_set<types::FontCategory> dirtyFonts_ {}; boost::unordered_flat_set<types::FontCategory> dirtyFonts_ {};
std::mutex dirtyFontsMutex_ {}; std::mutex dirtyFontsMutex_ {};
boost::unordered_flat_map<types::Font, int> fontIds_ {};
}; };
FontManager::FontManager() : p(std::make_unique<Impl>(this)) {} FontManager::FontManager() : p(std::make_unique<Impl>(this)) {}
@ -175,6 +178,16 @@ std::uint64_t FontManager::imgui_fonts_build_count() const
return p->imguiFontsBuildCount_; return p->imguiFontsBuildCount_;
} }
int FontManager::GetFontId(types::Font font) const
{
auto it = p->fontIds_.find(font);
if (it != p->fontIds_.cend())
{
return it->second;
}
return -1;
}
std::shared_ptr<types::ImGuiFont> std::shared_ptr<types::ImGuiFont>
FontManager::GetImGuiFont(types::FontCategory fontCategory) FontManager::GetImGuiFont(types::FontCategory fontCategory)
{ {
@ -322,7 +335,8 @@ FontManager::Impl::GetRawFontData(const std::string& filename)
return result.first->second; return result.first->second;
} }
void FontManager::LoadApplicationFont(const std::string& filename) void FontManager::LoadApplicationFont(types::Font font,
const std::string& filename)
{ {
// If the font cache failed to create, don't attempt to cache any fonts // If the font cache failed to create, don't attempt to cache any fonts
if (p->fontCachePath_.empty()) if (p->fontCachePath_.empty())
@ -358,6 +372,11 @@ void FontManager::LoadApplicationFont(const std::string& filename)
return; return;
} }
// Load the file into the Qt Font Database
int fontId =
QFontDatabase::addApplicationFont(QString::fromStdString(cacheFilename));
p->fontIds_.emplace(font, fontId);
// Load the file into fontconfig // Load the file into fontconfig
FcBool result = FcConfigAppFontAddFile( FcBool result = FcConfigAppFontAddFile(
nullptr, reinterpret_cast<const FcChar8*>(cacheFilename.c_str())); nullptr, reinterpret_cast<const FcChar8*>(cacheFilename.c_str()));

View file

@ -1,6 +1,7 @@
#pragma once #pragma once
#include <scwx/qt/types/imgui_font.hpp> #include <scwx/qt/types/imgui_font.hpp>
#include <scwx/qt/types/font_types.hpp>
#include <scwx/qt/types/text_types.hpp> #include <scwx/qt/types/text_types.hpp>
#include <shared_mutex> #include <shared_mutex>
@ -27,6 +28,7 @@ public:
std::shared_mutex& imgui_font_atlas_mutex(); std::shared_mutex& imgui_font_atlas_mutex();
std::uint64_t imgui_fonts_build_count() const; std::uint64_t imgui_fonts_build_count() const;
int GetFontId(types::Font font) const;
std::shared_ptr<types::ImGuiFont> std::shared_ptr<types::ImGuiFont>
GetImGuiFont(types::FontCategory fontCategory); GetImGuiFont(types::FontCategory fontCategory);
std::shared_ptr<types::ImGuiFont> std::shared_ptr<types::ImGuiFont>
@ -35,7 +37,7 @@ public:
units::font_size::points<double> size, units::font_size::points<double> size,
bool loadIfNotFound = true); bool loadIfNotFound = true);
void LoadApplicationFont(const std::string& filename); void LoadApplicationFont(types::Font font, const std::string& filename);
void InitializeFonts(); void InitializeFonts();
static QFont GetQFont(types::FontCategory fontCategory); static QFont GetQFont(types::FontCategory fontCategory);

View file

@ -8,7 +8,6 @@
#include <execution> #include <execution>
#include <mutex> #include <mutex>
#include <QFontDatabase>
#include <imgui.h> #include <imgui.h>
namespace scwx namespace scwx
@ -31,8 +30,6 @@ static const std::vector<std::pair<types::Font, std::string>> fontNames_ {
{types::Font::din1451alt_g, ":/res/fonts/din1451alt_g.ttf"}, {types::Font::din1451alt_g, ":/res/fonts/din1451alt_g.ttf"},
{types::Font::Inconsolata_Regular, ":/res/fonts/Inconsolata-Regular.ttf"}}; {types::Font::Inconsolata_Regular, ":/res/fonts/Inconsolata-Regular.ttf"}};
static std::unordered_map<types::Font, int> fontIds_ {};
void Initialize() void Initialize()
{ {
config::CountyDatabase::Initialize(); config::CountyDatabase::Initialize();
@ -43,16 +40,6 @@ void Initialize()
void Shutdown() {} void Shutdown() {}
int FontId(types::Font font)
{
auto it = fontIds_.find(font);
if (it != fontIds_.cend())
{
return it->second;
}
return -1;
}
std::shared_ptr<boost::gil::rgba8_image_t> std::shared_ptr<boost::gil::rgba8_image_t>
LoadImageResource(const std::string& urlString) LoadImageResource(const std::string& urlString)
{ {
@ -95,11 +82,7 @@ static void LoadFonts()
for (auto& fontName : fontNames_) for (auto& fontName : fontNames_)
{ {
int fontId = QFontDatabase::addApplicationFont( fontManager.LoadApplicationFont(fontName.first, fontName.second);
QString::fromStdString(fontName.second));
fontIds_.emplace(fontName.first, fontId);
fontManager.LoadApplicationFont(fontName.second);
} }
fontManager.InitializeFonts(); fontManager.InitializeFonts();

View file

@ -18,8 +18,6 @@ namespace ResourceManager
void Initialize(); void Initialize();
void Shutdown(); void Shutdown();
int FontId(types::Font font);
std::shared_ptr<boost::gil::rgba8_image_t> std::shared_ptr<boost::gil::rgba8_image_t>
LoadImageResource(const std::string& urlString); LoadImageResource(const std::string& urlString);
std::vector<std::shared_ptr<boost::gil::rgba8_image_t>> std::vector<std::shared_ptr<boost::gil::rgba8_image_t>>

View file

@ -1,7 +1,7 @@
#include "about_dialog.hpp" #include "about_dialog.hpp"
#include "ui_about_dialog.h" #include "ui_about_dialog.h"
#include <scwx/qt/main/versions.hpp> #include <scwx/qt/main/versions.hpp>
#include <scwx/qt/manager/resource_manager.hpp> #include <scwx/qt/manager/font_manager.hpp>
#include <QFontDatabase> #include <QFontDatabase>
@ -27,7 +27,7 @@ AboutDialog::AboutDialog(QWidget* parent) :
ui->setupUi(this); ui->setupUi(this);
int titleFontId = int titleFontId =
manager::ResourceManager::FontId(types::Font::din1451alt_g); manager::FontManager::Instance().GetFontId(types::Font::din1451alt_g);
QString titleFontFamily = QString titleFontFamily =
QFontDatabase::applicationFontFamilies(titleFontId).at(0); QFontDatabase::applicationFontFamilies(titleFontId).at(0);
QFont titleFont(titleFontFamily, 14); QFont titleFont(titleFontFamily, 14);

View file

@ -1,7 +1,7 @@
#include "update_dialog.hpp" #include "update_dialog.hpp"
#include "ui_update_dialog.h" #include "ui_update_dialog.h"
#include <scwx/qt/main/versions.hpp> #include <scwx/qt/main/versions.hpp>
#include <scwx/qt/manager/resource_manager.hpp> #include <scwx/qt/manager/font_manager.hpp>
#include <QDesktopServices> #include <QDesktopServices>
#include <QFontDatabase> #include <QFontDatabase>
@ -30,7 +30,7 @@ UpdateDialog::UpdateDialog(QWidget* parent) :
ui->setupUi(this); ui->setupUi(this);
int titleFontId = int titleFontId =
manager::ResourceManager::FontId(types::Font::din1451alt_g); manager::FontManager::Instance().GetFontId(types::Font::din1451alt_g);
QString titleFontFamily = QString titleFontFamily =
QFontDatabase::applicationFontFamilies(titleFontId).at(0); QFontDatabase::applicationFontFamilies(titleFontId).at(0);
QFont titleFont(titleFontFamily, 12); QFont titleFont(titleFontFamily, 12);