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

@ -1,4 +1,6 @@
#include <scwx/qt/gl/draw/placefile_text.hpp> #include <scwx/qt/gl/draw/placefile_text.hpp>
#include <scwx/qt/manager/font_manager.hpp>
#include <scwx/qt/manager/placefile_manager.hpp>
#include <scwx/qt/util/maplibre.hpp> #include <scwx/qt/util/maplibre.hpp>
#include <scwx/qt/util/tooltip.hpp> #include <scwx/qt/util/tooltip.hpp>
#include <scwx/util/logger.hpp> #include <scwx/util/logger.hpp>
@ -37,6 +39,7 @@ public:
const std::string& text, const std::string& text,
const std::string& hoverText, const std::string& hoverText,
boost::gil::rgba8_pixel_t color, boost::gil::rgba8_pixel_t color,
std::size_t fontNumber,
float x, float x,
float y); float y);
@ -62,6 +65,9 @@ public:
std::mutex listMutex_ {}; std::mutex listMutex_ {};
std::vector<std::shared_ptr<const gr::Placefile::TextDrawItem>> textList_ {}; std::vector<std::shared_ptr<const gr::Placefile::TextDrawItem>> textList_ {};
std::vector<std::shared_ptr<const gr::Placefile::TextDrawItem>> newList_ {}; std::vector<std::shared_ptr<const gr::Placefile::TextDrawItem>> newList_ {};
std::vector<std::shared_ptr<types::ImGuiFont>> fonts_ {};
std::vector<std::shared_ptr<types::ImGuiFont>> newFonts_ {};
}; };
PlacefileText::PlacefileText(const std::shared_ptr<GlContext>& context, PlacefileText::PlacefileText(const std::shared_ptr<GlContext>& context,
@ -155,6 +161,7 @@ void PlacefileText::Impl::RenderTextDrawItem(
di->text_, di->text_,
di->hoverText_, di->hoverText_,
di->color_, di->color_,
std::clamp<std::size_t>(di->fontNumber_, 1, 8),
rotatedX + di->x_ + halfWidth_, rotatedX + di->x_ + halfWidth_,
rotatedY + di->y_ + halfHeight_); rotatedY + di->y_ + halfHeight_);
} }
@ -165,6 +172,7 @@ void PlacefileText::Impl::RenderText(
const std::string& text, const std::string& text,
const std::string& hoverText, const std::string& hoverText,
boost::gil::rgba8_pixel_t color, boost::gil::rgba8_pixel_t color,
std::size_t fontNumber,
float x, float x,
float y) float y)
{ {
@ -184,10 +192,12 @@ void PlacefileText::Impl::RenderText(
ImGuiWindowFlags_NoBackground); ImGuiWindowFlags_NoBackground);
// Render text // Render text
ImGui::PushFont(fonts_[fontNumber - 1]->font());
ImGui::PushStyleColor(ImGuiCol_Text, ImGui::PushStyleColor(ImGuiCol_Text,
IM_COL32(color[0], color[1], color[2], color[3])); IM_COL32(color[0], color[1], color[2], color[3]));
ImGui::TextUnformatted(text.c_str()); ImGui::TextUnformatted(text.c_str());
ImGui::PopStyleColor(); ImGui::PopStyleColor();
ImGui::PopFont();
// Store hover text for mouse picking pass // Store hover text for mouse picking pass
if (!hoverText.empty() && ImGui::IsItemHovered()) if (!hoverText.empty() && ImGui::IsItemHovered())
@ -231,6 +241,28 @@ void PlacefileText::StartText()
p->newList_.clear(); p->newList_.clear();
} }
void PlacefileText::SetFonts(
const boost::unordered_flat_map<std::size_t,
std::shared_ptr<types::ImGuiFont>>& fonts)
{
auto defaultFont = manager::FontManager::Instance().GetImGuiFont(
types::FontCategory::Default);
// Valid font numbers are from 1 to 8, place in 0-based font vector
for (std::size_t i = 1; i <= 8; ++i)
{
auto it = fonts.find(i);
if (it != fonts.cend())
{
p->newFonts_.push_back(it->second);
}
else
{
p->newFonts_.push_back(defaultFont);
}
}
}
void PlacefileText::AddText( void PlacefileText::AddText(
const std::shared_ptr<gr::Placefile::TextDrawItem>& di) const std::shared_ptr<gr::Placefile::TextDrawItem>& di)
{ {
@ -246,9 +278,11 @@ void PlacefileText::FinishText()
// Swap text lists // Swap text lists
p->textList_.swap(p->newList_); p->textList_.swap(p->newList_);
p->fonts_.swap(p->newFonts_);
// Clear the new list // Clear the new list
p->newList_.clear(); p->newList_.clear();
p->newFonts_.clear();
} }
} // namespace draw } // namespace draw

View file

@ -2,8 +2,11 @@
#include <scwx/qt/gl/gl_context.hpp> #include <scwx/qt/gl/gl_context.hpp>
#include <scwx/qt/gl/draw/draw_item.hpp> #include <scwx/qt/gl/draw/draw_item.hpp>
#include <scwx/qt/types/imgui_font.hpp>
#include <scwx/gr/placefile.hpp> #include <scwx/gr/placefile.hpp>
#include <boost/unordered/unordered_flat_map.hpp>
namespace scwx namespace scwx
{ {
namespace qt namespace qt
@ -44,6 +47,16 @@ public:
*/ */
void StartText(); void StartText();
/**
* Configures the fonts for drawing the placefile text.
*
* @param [in] fonts A map of ImGui fonts
*/
void
SetFonts(const boost::unordered_flat_map<std::size_t,
std::shared_ptr<types::ImGuiFont>>&
fonts);
/** /**
* Adds placefile text to the internal draw list. * Adds placefile text to the internal draw list.
* *

View file

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

View file

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

View file

@ -197,6 +197,8 @@ void PlacefileLayer::ReloadData()
p->placefileIcons_->SetIconFiles(placefile->icon_files(), p->placefileIcons_->SetIconFiles(placefile->icon_files(),
placefile->name()); placefile->name());
p->placefileText_->SetFonts(
placefileManager->placefile_fonts(p->placefileName_));
for (auto& drawItem : placefile->GetDrawItems()) for (auto& drawItem : placefile->GetDrawItems())
{ {