mirror of
				https://github.com/ciphervance/supercell-wx.git
				synced 2025-10-31 06:30:05 +00:00 
			
		
		
		
	Load ImGui fonts on initialization and settings changes
This commit is contained in:
		
							parent
							
								
									67881d31d5
								
							
						
					
					
						commit
						3a4a32b97a
					
				
					 4 changed files with 87 additions and 2 deletions
				
			
		|  | @ -1,4 +1,6 @@ | ||||||
| #include <scwx/qt/manager/font_manager.hpp> | #include <scwx/qt/manager/font_manager.hpp> | ||||||
|  | #include <scwx/qt/manager/settings_manager.hpp> | ||||||
|  | #include <scwx/qt/settings/text_settings.hpp> | ||||||
| #include <scwx/util/logger.hpp> | #include <scwx/util/logger.hpp> | ||||||
| 
 | 
 | ||||||
| #include <filesystem> | #include <filesystem> | ||||||
|  | @ -9,6 +11,7 @@ | ||||||
| #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> | ||||||
|  | #include <boost/unordered/unordered_flat_set.hpp> | ||||||
| #include <fontconfig/fontconfig.h> | #include <fontconfig/fontconfig.h> | ||||||
| 
 | 
 | ||||||
| namespace scwx | namespace scwx | ||||||
|  | @ -44,22 +47,27 @@ struct FontRecordHash<FontRecordPair> | ||||||
| class FontManager::Impl | class FontManager::Impl | ||||||
| { | { | ||||||
| public: | public: | ||||||
|    explicit Impl() |    explicit Impl(FontManager* self) : self_ {self} | ||||||
|    { |    { | ||||||
|       InitializeFontCache(); |       InitializeFontCache(); | ||||||
|       InitializeFontconfig(); |       InitializeFontconfig(); | ||||||
|  |       ConnectSignals(); | ||||||
|    } |    } | ||||||
|    ~Impl() { FinalizeFontconfig(); } |    ~Impl() { FinalizeFontconfig(); } | ||||||
| 
 | 
 | ||||||
|  |    void ConnectSignals(); | ||||||
|    void FinalizeFontconfig(); |    void FinalizeFontconfig(); | ||||||
|    void InitializeFontCache(); |    void InitializeFontCache(); | ||||||
|    void InitializeFontconfig(); |    void InitializeFontconfig(); | ||||||
|  |    void UpdateImGuiFont(types::FontCategory fontCategory); | ||||||
| 
 | 
 | ||||||
|    const std::vector<std::uint8_t>& GetRawFontData(const std::string& filename); |    const std::vector<std::uint8_t>& GetRawFontData(const std::string& filename); | ||||||
| 
 | 
 | ||||||
|    static FontRecord MatchFontFile(const std::string&              family, |    static FontRecord MatchFontFile(const std::string&              family, | ||||||
|                                    const std::vector<std::string>& styles); |                                    const std::vector<std::string>& styles); | ||||||
| 
 | 
 | ||||||
|  |    FontManager* self_; | ||||||
|  | 
 | ||||||
|    std::string fontCachePath_ {}; |    std::string fontCachePath_ {}; | ||||||
| 
 | 
 | ||||||
|    std::shared_mutex imguiFontAtlasMutex_ {}; |    std::shared_mutex imguiFontAtlasMutex_ {}; | ||||||
|  | @ -81,12 +89,82 @@ public: | ||||||
|                              std::shared_ptr<types::ImGuiFont>> |                              std::shared_ptr<types::ImGuiFont>> | ||||||
|               fontCategoryMap_ {}; |               fontCategoryMap_ {}; | ||||||
|    std::mutex fontCategoryMutex_ {}; |    std::mutex fontCategoryMutex_ {}; | ||||||
|  | 
 | ||||||
|  |    boost::unordered_flat_set<types::FontCategory> dirtyFonts_ {}; | ||||||
|  |    std::mutex                                     dirtyFontsMutex_ {}; | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| FontManager::FontManager() : p(std::make_unique<Impl>()) {} | FontManager::FontManager() : p(std::make_unique<Impl>(this)) {} | ||||||
| 
 | 
 | ||||||
| FontManager::~FontManager() {}; | FontManager::~FontManager() {}; | ||||||
| 
 | 
 | ||||||
|  | void FontManager::Impl::ConnectSignals() | ||||||
|  | { | ||||||
|  |    auto& textSettings = settings::TextSettings::Instance(); | ||||||
|  | 
 | ||||||
|  |    for (auto fontCategory : types::FontCategoryIterator()) | ||||||
|  |    { | ||||||
|  |       textSettings.font_family(fontCategory) | ||||||
|  |          .RegisterValueChangedCallback( | ||||||
|  |             [this, fontCategory](const auto&) | ||||||
|  |             { | ||||||
|  |                std::unique_lock lock {dirtyFontsMutex_}; | ||||||
|  |                dirtyFonts_.insert(fontCategory); | ||||||
|  |             }); | ||||||
|  |       textSettings.font_style(fontCategory) | ||||||
|  |          .RegisterValueChangedCallback( | ||||||
|  |             [this, fontCategory](const auto&) | ||||||
|  |             { | ||||||
|  |                std::unique_lock lock {dirtyFontsMutex_}; | ||||||
|  |                dirtyFonts_.insert(fontCategory); | ||||||
|  |             }); | ||||||
|  |       textSettings.font_point_size(fontCategory) | ||||||
|  |          .RegisterValueChangedCallback( | ||||||
|  |             [this, fontCategory](const auto&) | ||||||
|  |             { | ||||||
|  |                std::unique_lock lock {dirtyFontsMutex_}; | ||||||
|  |                dirtyFonts_.insert(fontCategory); | ||||||
|  |             }); | ||||||
|  |    } | ||||||
|  | 
 | ||||||
|  |    QObject::connect( | ||||||
|  |       &SettingsManager::Instance(), | ||||||
|  |       &SettingsManager::SettingsSaved, | ||||||
|  |       self_, | ||||||
|  |       [this]() | ||||||
|  |       { | ||||||
|  |          std::scoped_lock lock {dirtyFontsMutex_, fontCategoryMutex_}; | ||||||
|  | 
 | ||||||
|  |          for (auto fontCategory : dirtyFonts_) | ||||||
|  |          { | ||||||
|  |             UpdateImGuiFont(fontCategory); | ||||||
|  |          } | ||||||
|  | 
 | ||||||
|  |          dirtyFonts_.clear(); | ||||||
|  |       }); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | void FontManager::InitializeFonts() | ||||||
|  | { | ||||||
|  |    for (auto fontCategory : types::FontCategoryIterator()) | ||||||
|  |    { | ||||||
|  |       p->UpdateImGuiFont(fontCategory); | ||||||
|  |    } | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | void FontManager::Impl::UpdateImGuiFont(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()}; | ||||||
|  | 
 | ||||||
|  |    fontCategoryMap_.insert_or_assign( | ||||||
|  |       fontCategory, self_->LoadImGuiFont(family, {styles}, size)); | ||||||
|  | } | ||||||
|  | 
 | ||||||
| std::shared_mutex& FontManager::imgui_font_atlas_mutex() | std::shared_mutex& FontManager::imgui_font_atlas_mutex() | ||||||
| { | { | ||||||
|    return p->imguiFontAtlasMutex_; |    return p->imguiFontAtlasMutex_; | ||||||
|  |  | ||||||
|  | @ -17,6 +17,7 @@ namespace manager | ||||||
| class FontManager : public QObject | class FontManager : public QObject | ||||||
| { | { | ||||||
|    Q_OBJECT |    Q_OBJECT | ||||||
|  |    Q_DISABLE_COPY_MOVE(FontManager) | ||||||
| 
 | 
 | ||||||
| public: | public: | ||||||
|    explicit FontManager(); |    explicit FontManager(); | ||||||
|  | @ -34,6 +35,7 @@ public: | ||||||
|                  bool                             loadIfNotFound = true); |                  bool                             loadIfNotFound = true); | ||||||
| 
 | 
 | ||||||
|    void LoadApplicationFont(const std::string& filename); |    void LoadApplicationFont(const std::string& filename); | ||||||
|  |    void InitializeFonts(); | ||||||
| 
 | 
 | ||||||
|    static FontManager& Instance(); |    static FontManager& Instance(); | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -104,6 +104,8 @@ void SettingsManager::SaveSettings() | ||||||
| 
 | 
 | ||||||
|       boost::json::value settingsJson = Impl::ConvertSettingsToJson(); |       boost::json::value settingsJson = Impl::ConvertSettingsToJson(); | ||||||
|       util::json::WriteJsonFile(p->settingsPath_, settingsJson); |       util::json::WriteJsonFile(p->settingsPath_, settingsJson); | ||||||
|  | 
 | ||||||
|  |       Q_EMIT SettingsSaved(); | ||||||
|    } |    } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -28,6 +28,9 @@ public: | ||||||
| 
 | 
 | ||||||
|    static SettingsManager& Instance(); |    static SettingsManager& Instance(); | ||||||
| 
 | 
 | ||||||
|  | signals: | ||||||
|  |    void SettingsSaved(); | ||||||
|  | 
 | ||||||
| private: | private: | ||||||
|    class Impl; |    class Impl; | ||||||
|    std::unique_ptr<Impl> p; |    std::unique_ptr<Impl> p; | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Dan Paulat
						Dan Paulat