diff --git a/scwx-qt/source/scwx/qt/gl/draw/placefile_text.cpp b/scwx-qt/source/scwx/qt/gl/draw/placefile_text.cpp index aee2d884..cd1ca34e 100644 --- a/scwx-qt/source/scwx/qt/gl/draw/placefile_text.cpp +++ b/scwx-qt/source/scwx/qt/gl/draw/placefile_text.cpp @@ -1,6 +1,5 @@ #include #include -#include #include #include #include @@ -62,8 +61,12 @@ public: std::vector> textList_ {}; std::vector> newList_ {}; - std::vector> fonts_ {}; - std::vector> newFonts_ {}; + std::vector, + units::font_size::pixels>> + fonts_ {}; + std::vector, + units::font_size::pixels>> + newFonts_ {}; }; PlacefileText::PlacefileText(const std::string& placefileName) : @@ -159,7 +162,8 @@ void PlacefileText::Impl::RenderTextDrawItem( std::size_t fontNumber = std::clamp(di->fontNumber_, 0, 8); // Set the font for the drop shadow and text - ImGui::PushFont(fonts_[fontNumber]->font(), 0.0f); + ImGui::PushFont(fonts_[fontNumber].first->font(), + fonts_[fontNumber].second.value()); if (settings::TextSettings::Instance() .placefile_text_drop_shadow_enabled() @@ -261,9 +265,7 @@ void PlacefileText::StartText() p->newList_.clear(); } -void PlacefileText::SetFonts( - const boost::unordered_flat_map>& fonts) +void PlacefileText::SetFonts(const manager::PlacefileManager::FontMap& fonts) { auto defaultFont = manager::FontManager::Instance().GetImGuiFont( types::FontCategory::Default); diff --git a/scwx-qt/source/scwx/qt/gl/draw/placefile_text.hpp b/scwx-qt/source/scwx/qt/gl/draw/placefile_text.hpp index e36be5a6..e4e46755 100644 --- a/scwx-qt/source/scwx/qt/gl/draw/placefile_text.hpp +++ b/scwx-qt/source/scwx/qt/gl/draw/placefile_text.hpp @@ -2,6 +2,7 @@ #include #include +#include #include #include @@ -54,10 +55,7 @@ public: * * @param [in] fonts A map of ImGui fonts */ - void - SetFonts(const boost::unordered_flat_map>& - fonts); + void SetFonts(const manager::PlacefileManager::FontMap& fonts); /** * Adds placefile text to the internal draw list. diff --git a/scwx-qt/source/scwx/qt/manager/font_manager.cpp b/scwx-qt/source/scwx/qt/manager/font_manager.cpp index d492a5be..587c4e60 100644 --- a/scwx-qt/source/scwx/qt/manager/font_manager.cpp +++ b/scwx-qt/source/scwx/qt/manager/font_manager.cpp @@ -34,15 +34,13 @@ struct FontRecord std::string filename_ {}; }; -typedef std::pair> FontRecordPair; - template struct FontRecordHash; template<> -struct FontRecordHash +struct FontRecordHash { - size_t operator()(const FontRecordPair& x) const; + size_t operator()(const FontRecord& x) const; }; class FontManager::Impl @@ -77,18 +75,20 @@ public: std::shared_mutex imguiFontAtlasMutex_ {}; - boost::unordered_flat_map, - FontRecordHash> + FontRecordHash> imguiFonts_ {}; std::shared_mutex imguiFontsMutex_ {}; boost::unordered_flat_map> rawFontData_ {}; std::mutex rawFontDataMutex_ {}; - std::shared_ptr defaultFont_ {}; + std::pair, units::font_size::pixels> + defaultFont_ {}; boost::unordered_flat_map> + std::pair, + units::font_size::pixels>> fontCategoryImguiFontMap_ {}; boost::unordered_flat_map fontCategoryQFontMap_ {}; @@ -160,6 +160,17 @@ void FontManager::InitializeFonts() } } +units::font_size::pixels +FontManager::ImFontSize(units::font_size::pixels size) +{ + // Only allow whole pixels, and clamp to 6-72 pt + units::font_size::pixels pixels {size}; + units::font_size::pixels imFontSize { + std::clamp(static_cast(pixels.value()), 8, 96)}; + + return imFontSize; +} + void FontManager::Impl::UpdateImGuiFont(types::FontCategory fontCategory) { auto& textSettings = settings::TextSettings::Instance(); @@ -170,7 +181,8 @@ void FontManager::Impl::UpdateImGuiFont(types::FontCategory fontCategory) textSettings.font_point_size(fontCategory).GetValue()}; fontCategoryImguiFontMap_.insert_or_assign( - fontCategory, self_->LoadImGuiFont(family, {styles}, size)); + fontCategory, + std::make_pair(self_->LoadImGuiFont(family, {styles}), ImFontSize(size))); } void FontManager::Impl::UpdateQFont(types::FontCategory fontCategory) @@ -211,7 +223,7 @@ int FontManager::GetFontId(types::Font font) const return -1; } -std::shared_ptr +std::pair, units::font_size::pixels> FontManager::GetImGuiFont(types::FontCategory fontCategory) { std::unique_lock lock {p->fontCategoryMutex_}; @@ -239,31 +251,23 @@ QFont FontManager::GetQFont(types::FontCategory fontCategory) } std::shared_ptr -FontManager::LoadImGuiFont(const std::string& family, - const std::vector& styles, - units::font_size::points size, - bool loadIfNotFound) +FontManager::LoadImGuiFont(const std::string& family, + const std::vector& styles, + bool loadIfNotFound) { const std::string styleString = fmt::format("{}", fmt::join(styles, " ")); - const std::string fontString = - fmt::format("{}-{}:{}", family, size.value(), styleString); + const std::string fontString = fmt::format("{}:{}", family, styleString); logger_->debug("LoadFontResource: {}", fontString); FontRecord fontRecord = Impl::MatchFontFile(family, styles); - // Only allow whole pixels, and clamp to 6-72 pt - units::font_size::pixels pixels {size}; - units::font_size::pixels imFontSize { - std::clamp(static_cast(pixels.value()), 8, 96)}; - auto imguiFontKey = std::make_pair(fontRecord, imFontSize); - // Search for a loaded ImGui font { std::shared_lock imguiFontLock {p->imguiFontsMutex_}; // Search for the associated ImGui font - auto it = p->imguiFonts_.find(imguiFontKey); + auto it = p->imguiFonts_.find(fontRecord); if (it != p->imguiFonts_.end()) { return it->second; @@ -288,7 +292,7 @@ FontManager::LoadImGuiFont(const std::string& family, // Search for the associated ImGui font again, to prevent loading the same // font twice - auto it = p->imguiFonts_.find(imguiFontKey); + auto it = p->imguiFonts_.find(fontRecord); if (it != p->imguiFonts_.end()) { return it->second; @@ -299,22 +303,20 @@ FontManager::LoadImGuiFont(const std::string& family, try { fontName = fmt::format( - "{}:{}", - std::filesystem::path(fontRecord.filename_).filename().string(), - imFontSize.value()); + "{}", std::filesystem::path(fontRecord.filename_).filename().string()); } catch (const std::exception& ex) { logger_->warn(ex.what()); - fontName = fmt::format("{}:{}", fontRecord.filename_, imFontSize.value()); + fontName = fmt::format("{}", fontRecord.filename_); } // Create an ImGui font std::shared_ptr imguiFont = - std::make_shared(fontName, rawFontData, imFontSize); + std::make_shared(fontName, rawFontData); // Store the ImGui font - p->imguiFonts_.insert_or_assign(imguiFontKey, imguiFont); + p->imguiFonts_.insert_or_assign(fontRecord, imguiFont); // Return the ImGui font return imguiFont; @@ -554,13 +556,12 @@ FontManager& FontManager::Instance() return instance_; } -size_t FontRecordHash::operator()(const FontRecordPair& x) const +size_t FontRecordHash::operator()(const FontRecord& x) const { size_t seed = 0; - boost::hash_combine(seed, x.first.family_); - boost::hash_combine(seed, x.first.style_); - boost::hash_combine(seed, x.first.filename_); - boost::hash_combine(seed, x.second.value()); + boost::hash_combine(seed, x.family_); + boost::hash_combine(seed, x.style_); + boost::hash_combine(seed, x.filename_); return seed; } diff --git a/scwx-qt/source/scwx/qt/manager/font_manager.hpp b/scwx-qt/source/scwx/qt/manager/font_manager.hpp index 26d5484b..04c254ea 100644 --- a/scwx-qt/source/scwx/qt/manager/font_manager.hpp +++ b/scwx-qt/source/scwx/qt/manager/font_manager.hpp @@ -28,18 +28,20 @@ public: std::shared_mutex& imgui_font_atlas_mutex(); int GetFontId(types::Font font) const; - std::shared_ptr + std::pair, units::font_size::pixels> GetImGuiFont(types::FontCategory fontCategory); QFont GetQFont(types::FontCategory fontCategory); std::shared_ptr - LoadImGuiFont(const std::string& family, - const std::vector& styles, - units::font_size::points size, - bool loadIfNotFound = true); + LoadImGuiFont(const std::string& family, + const std::vector& styles, + bool loadIfNotFound = true); void LoadApplicationFont(types::Font font, const std::string& filename); void InitializeFonts(); + static units::font_size::pixels + ImFontSize(units::font_size::pixels size); + static FontManager& Instance(); private: diff --git a/scwx-qt/source/scwx/qt/manager/placefile_manager.cpp b/scwx-qt/source/scwx/qt/manager/placefile_manager.cpp index d85f9e40..9685f33f 100644 --- a/scwx-qt/source/scwx/qt/manager/placefile_manager.cpp +++ b/scwx-qt/source/scwx/qt/manager/placefile_manager.cpp @@ -52,8 +52,7 @@ public: void ReadPlacefileSettings(); void WritePlacefileSettings(); - static boost::unordered_flat_map> + static FontMap LoadFontResources(const std::shared_ptr& placefile); static std::vector> LoadImageResources(const std::shared_ptr& placefile); @@ -147,8 +146,7 @@ public: std::mutex refreshMutex_ {}; std::mutex timerMutex_ {}; - boost::unordered_flat_map> - fonts_ {}; + FontMap fonts_ {}; std::mutex fontsMutex_ {}; std::vector> images_ {}; @@ -235,7 +233,7 @@ PlacefileManager::placefile(const std::string& name) return nullptr; } -boost::unordered_flat_map> +PlacefileManager::FontMap PlacefileManager::placefile_fonts(const std::string& name) { std::shared_lock lock(p->placefileRecordLock_); @@ -775,13 +773,11 @@ std::shared_ptr PlacefileManager::Instance() return placefileManager; } -boost::unordered_flat_map> -PlacefileManager::Impl::LoadFontResources( +PlacefileManager::FontMap PlacefileManager::Impl::LoadFontResources( const std::shared_ptr& placefile) { - boost::unordered_flat_map> - imGuiFonts {}; - auto fonts = placefile->fonts(); + FontMap imGuiFonts {}; + auto fonts = placefile->fonts(); for (auto& font : fonts) { @@ -797,9 +793,11 @@ PlacefileManager::Impl::LoadFontResources( styles.push_back("italic"); } - auto imGuiFont = FontManager::Instance().LoadImGuiFont( - font.second->face_, styles, size); - imGuiFonts.emplace(font.first, std::move(imGuiFont)); + auto imGuiFont = + FontManager::Instance().LoadImGuiFont(font.second->face_, styles); + imGuiFonts.emplace( + font.first, + std::make_pair(std::move(imGuiFont), FontManager::ImFontSize(size))); } return imGuiFonts; diff --git a/scwx-qt/source/scwx/qt/manager/placefile_manager.hpp b/scwx-qt/source/scwx/qt/manager/placefile_manager.hpp index 17b39b2f..0a667c16 100644 --- a/scwx-qt/source/scwx/qt/manager/placefile_manager.hpp +++ b/scwx-qt/source/scwx/qt/manager/placefile_manager.hpp @@ -2,6 +2,7 @@ #include #include +#include #include #include @@ -22,12 +23,16 @@ public: explicit PlacefileManager(); ~PlacefileManager(); + using FontMap = + boost::unordered_flat_map, + units::font_size::pixels>>; + bool placefile_enabled(const std::string& name); bool placefile_thresholded(const std::string& name); std::string placefile_title(const std::string& name); std::shared_ptr placefile(const std::string& name); - boost::unordered_flat_map> - placefile_fonts(const std::string& name); + FontMap placefile_fonts(const std::string& name); void set_placefile_enabled(const std::string& name, bool enabled); void set_placefile_thresholded(const std::string& name, bool thresholded); diff --git a/scwx-qt/source/scwx/qt/map/draw_layer.cpp b/scwx-qt/source/scwx/qt/map/draw_layer.cpp index c59ed79f..f07cb2ac 100644 --- a/scwx-qt/source/scwx/qt/map/draw_layer.cpp +++ b/scwx-qt/source/scwx/qt/map/draw_layer.cpp @@ -102,7 +102,7 @@ void DrawLayer::ImGuiFrameStart(const std::shared_ptr& mapContext) ImGui_ImplQt_NewFrame(mapContext->widget()); ImGui_ImplOpenGL3_NewFrame(); ImGui::NewFrame(); - ImGui::PushFont(defaultFont->font(), 0.0f); + ImGui::PushFont(defaultFont.first->font(), defaultFont.second.value()); } void DrawLayer::ImGuiFrameEnd() diff --git a/scwx-qt/source/scwx/qt/map/map_widget.cpp b/scwx-qt/source/scwx/qt/map/map_widget.cpp index 5426734c..537318f0 100644 --- a/scwx-qt/source/scwx/qt/map/map_widget.cpp +++ b/scwx-qt/source/scwx/qt/map/map_widget.cpp @@ -1644,7 +1644,7 @@ void MapWidget::paintGL() ImGui::NewFrame(); // Set default font - ImGui::PushFont(defaultFont->font(), 0.0f); + ImGui::PushFont(defaultFont.first->font(), defaultFont.second.value()); // Perform mouse picking if (p->hasMouse_) diff --git a/scwx-qt/source/scwx/qt/map/overlay_layer.cpp b/scwx-qt/source/scwx/qt/map/overlay_layer.cpp index 8c60bc33..04b0890e 100644 --- a/scwx-qt/source/scwx/qt/map/overlay_layer.cpp +++ b/scwx-qt/source/scwx/qt/map/overlay_layer.cpp @@ -539,7 +539,8 @@ void OverlayLayer::Render(const std::shared_ptr& mapContext, ImVec2 {1.0f, 1.0f}); ImGui::SetNextWindowBgAlpha(0.5f); ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2 {3.0f, 2.0f}); - ImGui::PushFont(attributionFont->font(), 0.0f); + ImGui::PushFont(attributionFont.first->font(), + attributionFont.second.value()); ImGui::Begin("Attribution", nullptr, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | diff --git a/scwx-qt/source/scwx/qt/types/imgui_font.cpp b/scwx-qt/source/scwx/qt/types/imgui_font.cpp index e6f22ad1..647c584b 100644 --- a/scwx-qt/source/scwx/qt/types/imgui_font.cpp +++ b/scwx-qt/source/scwx/qt/types/imgui_font.cpp @@ -23,10 +23,9 @@ static const auto logger_ = scwx::util::Logger::Create(logPrefix_); class ImGuiFont::Impl { public: - explicit Impl(const std::string& fontName, - const std::vector& fontData, - units::font_size::pixels size) : - fontName_ {fontName}, size_ {size} + explicit Impl(const std::string& fontName, + const std::vector& fontData) : + fontName_ {fontName} { CreateImGuiFont(fontData); } @@ -35,16 +34,14 @@ public: void CreateImGuiFont(const std::vector& fontData); - const std::string fontName_; - const units::font_size::pixels size_; + const std::string fontName_; ImFont* imFont_ {nullptr}; }; -ImGuiFont::ImGuiFont(const std::string& fontName, - const std::vector& fontData, - units::font_size::pixels size) : - p(std::make_unique(fontName, fontData, size)) +ImGuiFont::ImGuiFont(const std::string& fontName, + const std::vector& fontData) : + p(std::make_unique(fontName, fontData)) { } ImGuiFont::~ImGuiFont() = default; @@ -53,11 +50,11 @@ void ImGuiFont::Impl::CreateImGuiFont(const std::vector& fontData) { logger_->debug("Creating Font: {}", fontName_); + static constexpr float kSizePixels_ = 0.0f; + ImFontAtlas* fontAtlas = model::ImGuiContextModel::Instance().font_atlas(); ImFontConfig fontConfig {}; - const float sizePixels = static_cast(size_.value()); - // Do not transfer ownership of font data to ImGui, makes const_cast safe fontConfig.FontDataOwnedByAtlas = false; @@ -69,7 +66,7 @@ void ImGuiFont::Impl::CreateImGuiFont(const std::vector& fontData) const_cast(static_cast(fontData.data())), static_cast(std::clamp( fontData.size(), 0, std::numeric_limits::max())), - sizePixels, + kSizePixels_, &fontConfig); } diff --git a/scwx-qt/source/scwx/qt/types/imgui_font.hpp b/scwx-qt/source/scwx/qt/types/imgui_font.hpp index ace8ba09..d1c28a0c 100644 --- a/scwx-qt/source/scwx/qt/types/imgui_font.hpp +++ b/scwx-qt/source/scwx/qt/types/imgui_font.hpp @@ -4,8 +4,6 @@ #include #include -#include - struct ImFont; namespace scwx @@ -18,9 +16,8 @@ namespace types class ImGuiFont { public: - explicit ImGuiFont(const std::string& fontName, - const std::vector& fontData, - units::font_size::pixels size); + explicit ImGuiFont(const std::string& fontName, + const std::vector& fontData); ~ImGuiFont(); ImGuiFont(const ImGuiFont&) = delete; diff --git a/scwx-qt/source/scwx/qt/util/imgui.cpp b/scwx-qt/source/scwx/qt/util/imgui.cpp index 1fa857c8..1bb8af3c 100644 --- a/scwx-qt/source/scwx/qt/util/imgui.cpp +++ b/scwx-qt/source/scwx/qt/util/imgui.cpp @@ -34,7 +34,7 @@ void ImGui::DrawTooltip(const std::string& hoverText) if (::ImGui::BeginTooltip()) { - ::ImGui::PushFont(tooltipFont->font(), 0.0f); + ::ImGui::PushFont(tooltipFont.first->font(), tooltipFont.second.value()); ::ImGui::TextUnformatted(hoverText.c_str()); ::ImGui::PopFont(); ::ImGui::EndTooltip();