Fix QFont styling

This commit is contained in:
Dan Paulat 2023-10-10 19:18:20 -05:00
parent 8326b2f2bf
commit 73adeda604
3 changed files with 40 additions and 19 deletions

View file

@ -10,6 +10,7 @@
#include <QFile> #include <QFile>
#include <QFileInfo> #include <QFileInfo>
#include <QFontDatabase> #include <QFontDatabase>
#include <QGuiApplication>
#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>
@ -64,6 +65,7 @@ public:
void InitializeFontCache(); void InitializeFontCache();
void InitializeFontconfig(); void InitializeFontconfig();
void UpdateImGuiFont(types::FontCategory fontCategory); void UpdateImGuiFont(types::FontCategory fontCategory);
void UpdateQFont(types::FontCategory fontCategory);
const std::vector<char>& GetRawFontData(const std::string& filename); const std::vector<char>& GetRawFontData(const std::string& filename);
@ -90,7 +92,9 @@ public:
std::shared_ptr<types::ImGuiFont> defaultFont_ {}; std::shared_ptr<types::ImGuiFont> defaultFont_ {};
boost::unordered_flat_map<types::FontCategory, boost::unordered_flat_map<types::FontCategory,
std::shared_ptr<types::ImGuiFont>> std::shared_ptr<types::ImGuiFont>>
fontCategoryMap_ {}; fontCategoryImguiFontMap_ {};
boost::unordered_flat_map<types::FontCategory, QFont>
fontCategoryQFontMap_ {};
std::mutex fontCategoryMutex_ {}; std::mutex fontCategoryMutex_ {};
boost::unordered_flat_set<types::FontCategory> dirtyFonts_ {}; boost::unordered_flat_set<types::FontCategory> dirtyFonts_ {};
@ -143,6 +147,7 @@ void FontManager::Impl::ConnectSignals()
for (auto fontCategory : dirtyFonts_) for (auto fontCategory : dirtyFonts_)
{ {
UpdateImGuiFont(fontCategory); UpdateImGuiFont(fontCategory);
UpdateQFont(fontCategory);
} }
dirtyFonts_.clear(); dirtyFonts_.clear();
@ -154,6 +159,7 @@ void FontManager::InitializeFonts()
for (auto fontCategory : types::FontCategoryIterator()) for (auto fontCategory : types::FontCategoryIterator())
{ {
p->UpdateImGuiFont(fontCategory); p->UpdateImGuiFont(fontCategory);
p->UpdateQFont(fontCategory);
} }
} }
@ -166,10 +172,27 @@ void FontManager::Impl::UpdateImGuiFont(types::FontCategory fontCategory)
units::font_size::points<double> size { units::font_size::points<double> size {
textSettings.font_point_size(fontCategory).GetValue()}; textSettings.font_point_size(fontCategory).GetValue()};
fontCategoryMap_.insert_or_assign( fontCategoryImguiFontMap_.insert_or_assign(
fontCategory, self_->LoadImGuiFont(family, {styles}, size)); fontCategory, self_->LoadImGuiFont(family, {styles}, size));
} }
void FontManager::Impl::UpdateQFont(types::FontCategory fontCategory)
{
auto& textSettings = settings::TextSettings::Instance();
auto family = textSettings.font_family(fontCategory).GetValue();
auto styles = textSettings.font_style(fontCategory).GetValue();
units::font_size::points<double> size {
textSettings.font_point_size(fontCategory).GetValue()};
QFont font = QFontDatabase::font(QString::fromStdString(family),
QString::fromStdString(styles),
static_cast<int>(size.value()));
font.setPointSizeF(size.value());
fontCategoryQFontMap_.insert_or_assign(fontCategory, font);
}
std::shared_mutex& FontManager::imgui_font_atlas_mutex() std::shared_mutex& FontManager::imgui_font_atlas_mutex()
{ {
return p->imguiFontAtlasMutex_; return p->imguiFontAtlasMutex_;
@ -195,8 +218,8 @@ FontManager::GetImGuiFont(types::FontCategory fontCategory)
{ {
std::unique_lock lock {p->fontCategoryMutex_}; std::unique_lock lock {p->fontCategoryMutex_};
auto it = p->fontCategoryMap_.find(fontCategory); auto it = p->fontCategoryImguiFontMap_.find(fontCategory);
if (it != p->fontCategoryMap_.cend()) if (it != p->fontCategoryImguiFontMap_.cend())
{ {
return it->second; return it->second;
} }
@ -206,18 +229,15 @@ FontManager::GetImGuiFont(types::FontCategory fontCategory)
QFont FontManager::GetQFont(types::FontCategory fontCategory) QFont FontManager::GetQFont(types::FontCategory fontCategory)
{ {
auto& textSettings = settings::TextSettings::Instance(); std::unique_lock lock {p->fontCategoryMutex_};
auto family = textSettings.font_family(fontCategory).GetValue(); auto it = p->fontCategoryQFontMap_.find(fontCategory);
auto styles = textSettings.font_style(fontCategory).GetValue(); if (it != p->fontCategoryQFontMap_.cend())
units::font_size::points<double> size { {
textSettings.font_point_size(fontCategory).GetValue()}; return it->second;
}
QFont font(QString::fromStdString(family)); return QGuiApplication::font();
font.setStyleName(QString::fromStdString(styles));
font.setPointSizeF(size.value());
return font;
} }
std::shared_ptr<types::ImGuiFont> std::shared_ptr<types::ImGuiFont>

View file

@ -31,6 +31,7 @@ public:
int GetFontId(types::Font font) 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);
QFont GetQFont(types::FontCategory fontCategory);
std::shared_ptr<types::ImGuiFont> std::shared_ptr<types::ImGuiFont>
LoadImGuiFont(const std::string& family, LoadImGuiFont(const std::string& family,
const std::vector<std::string>& styles, const std::vector<std::string>& styles,
@ -40,8 +41,6 @@ public:
void LoadApplicationFont(types::Font font, const std::string& filename); void LoadApplicationFont(types::Font font, const std::string& filename);
void InitializeFonts(); void InitializeFonts();
static QFont GetQFont(types::FontCategory fontCategory);
static FontManager& Instance(); static FontManager& Instance();
private: private:

View file

@ -24,6 +24,7 @@
#include <fmt/format.h> #include <fmt/format.h>
#include <QColorDialog> #include <QColorDialog>
#include <QFileDialog> #include <QFileDialog>
#include <QFontDatabase>
#include <QFontDialog> #include <QFontDialog>
#include <QStandardItemModel> #include <QStandardItemModel>
#include <QToolButton> #include <QToolButton>
@ -949,8 +950,9 @@ QFont SettingsDialogImpl::GetSelectedFont()
.GetSettingsVariable() .GetSettingsVariable()
->GetStagedOrValue()}; ->GetStagedOrValue()};
QFont font(QString::fromStdString(fontFamily)); QFont font = QFontDatabase::font(QString::fromStdString(fontFamily),
font.setStyleName(QString::fromStdString(fontStyle)); QString::fromStdString(fontStyle),
static_cast<int>(fontSize.value()));
font.setPointSizeF(fontSize.value()); font.setPointSizeF(fontSize.value());
return font; return font;