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 <QFileInfo>
#include <QFontDatabase>
#include <QStandardPaths>
#include <boost/container_hash/hash.hpp>
#include <boost/unordered/unordered_flat_map.hpp>
@ -92,6 +93,8 @@ public:
boost::unordered_flat_set<types::FontCategory> dirtyFonts_ {};
std::mutex dirtyFontsMutex_ {};
boost::unordered_flat_map<types::Font, int> fontIds_ {};
};
FontManager::FontManager() : p(std::make_unique<Impl>(this)) {}
@ -175,6 +178,16 @@ std::uint64_t FontManager::imgui_fonts_build_count() const
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>
FontManager::GetImGuiFont(types::FontCategory fontCategory)
{
@ -322,7 +335,8 @@ FontManager::Impl::GetRawFontData(const std::string& filename)
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 (p->fontCachePath_.empty())
@ -358,6 +372,11 @@ void FontManager::LoadApplicationFont(const std::string& filename)
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
FcBool result = FcConfigAppFontAddFile(
nullptr, reinterpret_cast<const FcChar8*>(cacheFilename.c_str()));

View file

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

View file

@ -8,7 +8,6 @@
#include <execution>
#include <mutex>
#include <QFontDatabase>
#include <imgui.h>
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::Inconsolata_Regular, ":/res/fonts/Inconsolata-Regular.ttf"}};
static std::unordered_map<types::Font, int> fontIds_ {};
void Initialize()
{
config::CountyDatabase::Initialize();
@ -43,16 +40,6 @@ void Initialize()
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>
LoadImageResource(const std::string& urlString)
{
@ -95,11 +82,7 @@ static void LoadFonts()
for (auto& fontName : fontNames_)
{
int fontId = QFontDatabase::addApplicationFont(
QString::fromStdString(fontName.second));
fontIds_.emplace(fontName.first, fontId);
fontManager.LoadApplicationFont(fontName.second);
fontManager.LoadApplicationFont(fontName.first, fontName.second);
}
fontManager.InitializeFonts();

View file

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

View file

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

View file

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