Use fonts defined in placefiles

This commit is contained in:
Dan Paulat 2023-10-08 22:05:17 -05:00
parent 11ea4676cf
commit 810b61f8f9
5 changed files with 92 additions and 5 deletions

View file

@ -52,7 +52,8 @@ public:
void ReadPlacefileSettings();
void WritePlacefileSettings();
static void
static boost::unordered_flat_map<std::size_t,
std::shared_ptr<types::ImGuiFont>>
LoadFontResources(const std::shared_ptr<gr::Placefile>& placefile);
static std::vector<std::shared_ptr<boost::gil::rgba8_image_t>>
LoadImageResources(const std::shared_ptr<gr::Placefile>& placefile);
@ -66,7 +67,7 @@ public:
std::shared_ptr<config::RadarSite> radarSite_ {};
std::vector<std::shared_ptr<PlacefileRecord>> placefileRecords_ {};
std::unordered_map<std::string, std::shared_ptr<PlacefileRecord>>
boost::unordered_flat_map<std::string, std::shared_ptr<PlacefileRecord>>
placefileRecordMap_ {};
std::shared_mutex placefileRecordLock_ {};
};
@ -137,6 +138,10 @@ public:
std::mutex refreshMutex_ {};
std::mutex timerMutex_ {};
boost::unordered_flat_map<std::size_t, std::shared_ptr<types::ImGuiFont>>
fonts_ {};
std::mutex fontsMutex_ {};
std::vector<std::shared_ptr<boost::gil::rgba8_image_t>> images_ {};
std::string lastRadarSite_ {};
@ -211,6 +216,20 @@ PlacefileManager::placefile(const std::string& name)
return nullptr;
}
boost::unordered_flat_map<std::size_t, std::shared_ptr<types::ImGuiFont>>
PlacefileManager::placefile_fonts(const std::string& name)
{
std::shared_lock lock(p->placefileRecordLock_);
auto it = p->placefileRecordMap_.find(name);
if (it != p->placefileRecordMap_.cend())
{
std::unique_lock fontsLock {it->second->fontsMutex_};
return it->second->fonts_;
}
return {};
}
void PlacefileManager::set_placefile_enabled(const std::string& name,
bool enabled)
{
@ -281,6 +300,7 @@ void PlacefileManager::set_placefile_url(const std::string& name,
auto placefileRecord = it->second;
placefileRecord->name_ = normalizedUrl;
placefileRecord->placefile_ = nullptr;
placefileRecord->fonts_.clear();
placefileRecord->images_.clear();
p->placefileRecordMap_.erase(it);
p->placefileRecordMap_.insert_or_assign(normalizedUrl, placefileRecord);
@ -590,7 +610,7 @@ void PlacefileManager::Impl::PlacefileRecord::Update()
if (updatedPlacefile != nullptr)
{
// Load placefile resources
Impl::LoadFontResources(updatedPlacefile);
auto newFonts = Impl::LoadFontResources(updatedPlacefile);
auto newImages = Impl::LoadImageResources(updatedPlacefile);
// Check the name matches, in case the name updated
@ -601,6 +621,13 @@ void PlacefileManager::Impl::PlacefileRecord::Update()
title_ = placefile_->title();
lastUpdateTime_ = std::chrono::system_clock::now();
// Update font resources
{
std::unique_lock fontsLock {fontsMutex_};
fonts_.swap(newFonts);
newFonts.clear();
}
// Update image resources
images_.swap(newImages);
newImages.clear();
@ -688,9 +715,12 @@ std::shared_ptr<PlacefileManager> PlacefileManager::Instance()
return placefileManager;
}
void PlacefileManager::Impl::LoadFontResources(
boost::unordered_flat_map<std::size_t, std::shared_ptr<types::ImGuiFont>>
PlacefileManager::Impl::LoadFontResources(
const std::shared_ptr<gr::Placefile>& placefile)
{
boost::unordered_flat_map<std::size_t, std::shared_ptr<types::ImGuiFont>>
imGuiFonts {};
auto fonts = placefile->fonts();
for (auto& font : fonts)
@ -707,8 +737,12 @@ void PlacefileManager::Impl::LoadFontResources(
styles.push_back("italic");
}
FontManager::Instance().LoadImGuiFont(font.second->face_, styles, size);
auto imGuiFont = FontManager::Instance().LoadImGuiFont(
font.second->face_, styles, size);
imGuiFonts.emplace(font.first, std::move(imGuiFont));
}
return imGuiFonts;
}
std::vector<std::shared_ptr<boost::gil::rgba8_image_t>>

View file

@ -2,8 +2,10 @@
#include <scwx/gr/placefile.hpp>
#include <scwx/qt/config/radar_site.hpp>
#include <scwx/qt/types/imgui_font.hpp>
#include <QObject>
#include <boost/unordered/unordered_flat_map.hpp>
namespace scwx
{
@ -24,6 +26,8 @@ public:
bool placefile_thresholded(const std::string& name);
std::string placefile_title(const std::string& name);
std::shared_ptr<gr::Placefile> placefile(const std::string& name);
boost::unordered_flat_map<std::size_t, std::shared_ptr<types::ImGuiFont>>
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);