Add UiFont class

This commit is contained in:
Georg Hagen
2025-11-23 21:59:43 +01:00
parent b6f2e2e16e
commit dbb46f70d5
2 changed files with 86 additions and 0 deletions

View File

@@ -0,0 +1,47 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#include "UiFont.hpp"
#include <Base/Logger.hpp>
#include "Host/FontResolver.hpp"
#include <map>
#include <imgui.h>
namespace OpenVulkano::Scene::UI
{
void UiFont::SetActive()
{
if (!m_font) MakeFont();
if (m_font) ImGui::PushFont(m_font);
}
void UiFont::MakeFont()
{
if (m_pixelSize <= 0 || m_font) return;
auto fontData = FontResolver::GetFontData(m_name);
if (!fontData)
{
Logger::APP->warn("Failed to find font {}", m_name);
m_pixelSize = 0;
return;
}
ImFontConfig conf;
strlcpy(conf.Name, m_name.data(), std::min(m_name.size(), sizeof(conf.Name)));
conf.FontDataOwnedByAtlas = false;
conf.RasterizerDensity = 3; // TODO check window scale factor
ImGuiIO& io = ImGui::GetIO();
m_font = io.Fonts->AddFontFromMemoryTTF(fontData.Data(), static_cast<int>(fontData.Size()), m_pixelSize, &conf);
}
UiFont* UiFont::GetFont(const std::string& fontName, int pixelSize)
{
static std::map<std::pair<std::string, int>, UiFont> FONTS;
UiFont& font = FONTS[{fontName, pixelSize}];
if (font.m_pixelSize != pixelSize) font = UiFont(fontName, (float)pixelSize);
font.MakeFont();
return &font;
}
}