Get ImGui font by font category

This commit is contained in:
Dan Paulat 2023-10-05 21:18:58 -05:00
parent e4ab1e8c19
commit acc782b2bc
4 changed files with 60 additions and 10 deletions

View file

@ -64,6 +64,8 @@ public:
std::shared_mutex imguiFontAtlasMutex_ {}; std::shared_mutex imguiFontAtlasMutex_ {};
std::uint64_t imguiFontsBuildCount_ {};
boost::unordered_flat_map<FontRecordPair, boost::unordered_flat_map<FontRecordPair,
std::shared_ptr<types::ImGuiFont>, std::shared_ptr<types::ImGuiFont>,
FontRecordHash<FontRecordPair>> FontRecordHash<FontRecordPair>>
@ -73,6 +75,12 @@ public:
boost::unordered_flat_map<std::string, std::vector<std::uint8_t>> boost::unordered_flat_map<std::string, std::vector<std::uint8_t>>
rawFontData_ {}; rawFontData_ {};
std::mutex rawFontDataMutex_ {}; std::mutex rawFontDataMutex_ {};
std::shared_ptr<types::ImGuiFont> defaultFont_ {};
boost::unordered_flat_map<types::FontCategory,
std::shared_ptr<types::ImGuiFont>>
fontCategoryMap_ {};
std::mutex fontCategoryMutex_ {};
}; };
FontManager::FontManager() : p(std::make_unique<Impl>()) {} FontManager::FontManager() : p(std::make_unique<Impl>()) {}
@ -84,11 +92,30 @@ std::shared_mutex& FontManager::imgui_font_atlas_mutex()
return p->imguiFontAtlasMutex_; return p->imguiFontAtlasMutex_;
} }
std::uint64_t FontManager::imgui_fonts_build_count() const
{
return p->imguiFontsBuildCount_;
}
std::shared_ptr<types::ImGuiFont> std::shared_ptr<types::ImGuiFont>
FontManager::GetImGuiFont(const std::string& family, FontManager::GetImGuiFont(types::FontCategory fontCategory)
const std::vector<std::string>& styles, {
units::font_size::points<double> size, std::unique_lock lock {p->fontCategoryMutex_};
bool loadIfNotFound)
auto it = p->fontCategoryMap_.find(fontCategory);
if (it != p->fontCategoryMap_.cend())
{
return it->second;
}
return p->defaultFont_;
}
std::shared_ptr<types::ImGuiFont>
FontManager::LoadImGuiFont(const std::string& family,
const std::vector<std::string>& styles,
units::font_size::points<double> size,
bool loadIfNotFound)
{ {
const std::string styleString = fmt::format("{}", fmt::join(styles, " ")); const std::string styleString = fmt::format("{}", fmt::join(styles, " "));
const std::string fontString = const std::string fontString =
@ -148,6 +175,9 @@ FontManager::GetImGuiFont(const std::string& family,
// Store the ImGui font // Store the ImGui font
p->imguiFonts_.insert_or_assign(imguiFontKey, imguiFont); p->imguiFonts_.insert_or_assign(imguiFontKey, imguiFont);
// Increment ImGui font build count
++p->imguiFontsBuildCount_;
// Return the ImGui font // Return the ImGui font
return imguiFont; return imguiFont;
} }
@ -181,7 +211,7 @@ FontManager::Impl::GetRawFontData(const std::string& filename)
auto result = rawFontData_.emplace(filename, std::move(buffer)); auto result = rawFontData_.emplace(filename, std::move(buffer));
// Return the cached buffer // Return the cached buffer
return it->second; return result.first->second;
} }
void FontManager::LoadApplicationFont(const std::string& filename) void FontManager::LoadApplicationFont(const std::string& filename)

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/text_types.hpp>
#include <shared_mutex> #include <shared_mutex>
@ -22,12 +23,15 @@ public:
~FontManager(); ~FontManager();
std::shared_mutex& imgui_font_atlas_mutex(); std::shared_mutex& imgui_font_atlas_mutex();
std::uint64_t imgui_fonts_build_count() const;
std::shared_ptr<types::ImGuiFont> std::shared_ptr<types::ImGuiFont>
GetImGuiFont(const std::string& family, GetImGuiFont(types::FontCategory fontCategory);
const std::vector<std::string>& styles, std::shared_ptr<types::ImGuiFont>
units::font_size::points<double> size, LoadImGuiFont(const std::string& family,
bool loadIfNotFound = false); const std::vector<std::string>& styles,
units::font_size::points<double> size,
bool loadIfNotFound = true);
void LoadApplicationFont(const std::string& filename); void LoadApplicationFont(const std::string& filename);

View file

@ -707,7 +707,7 @@ void PlacefileManager::Impl::LoadFontResources(
styles.push_back("italic"); styles.push_back("italic");
} }
FontManager::Instance().GetImGuiFont(font.second->face_, styles, size); FontManager::Instance().LoadImGuiFont(font.second->face_, styles, size);
} }
} }

View file

@ -155,6 +155,7 @@ public:
ImGuiContext* imGuiContext_; ImGuiContext* imGuiContext_;
std::string imGuiContextName_; std::string imGuiContextName_;
bool imGuiRendererInitialized_; bool imGuiRendererInitialized_;
std::uint64_t imGuiFontsBuildCount_ {};
std::shared_ptr<manager::PlacefileManager> placefileManager_ { std::shared_ptr<manager::PlacefileManager> placefileManager_ {
manager::PlacefileManager::Instance()}; manager::PlacefileManager::Instance()};
@ -981,9 +982,15 @@ void MapWidget::initializeGL()
makeCurrent(); makeCurrent();
p->context_->gl().initializeOpenGLFunctions(); p->context_->gl().initializeOpenGLFunctions();
// Lock ImGui font atlas prior to new ImGui frame
std::shared_lock imguiFontAtlasLock {
manager::FontManager::Instance().imgui_font_atlas_mutex()};
// Initialize ImGui OpenGL3 backend // Initialize ImGui OpenGL3 backend
ImGui::SetCurrentContext(p->imGuiContext_); ImGui::SetCurrentContext(p->imGuiContext_);
ImGui_ImplOpenGL3_Init(); ImGui_ImplOpenGL3_Init();
p->imGuiFontsBuildCount_ =
manager::FontManager::Instance().imgui_fonts_build_count();
p->imGuiRendererInitialized_ = true; p->imGuiRendererInitialized_ = true;
p->map_.reset( p->map_.reset(
@ -1038,6 +1045,15 @@ void MapWidget::paintGL()
ImGui_ImplOpenGL3_NewFrame(); ImGui_ImplOpenGL3_NewFrame();
ImGui::NewFrame(); ImGui::NewFrame();
// Update ImGui Fonts if required
std::uint64_t currentImGuiFontsBuildCount =
manager::FontManager::Instance().imgui_fonts_build_count();
if (p->imGuiFontsBuildCount_ != currentImGuiFontsBuildCount)
{
ImGui_ImplOpenGL3_DestroyFontsTexture();
ImGui_ImplOpenGL3_CreateFontsTexture();
}
// Update pixel ratio // Update pixel ratio
p->context_->set_pixel_ratio(pixelRatio()); p->context_->set_pixel_ratio(pixelRatio());