/* * 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 #include "Host/FontResolver.hpp" #include #include 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(fontData.Size()), m_pixelSize, &conf); } UiFont* UiFont::GetFont(const std::string& fontName, int pixelSize) { static std::map, UiFont> FONTS; UiFont& font = FONTS[{fontName, pixelSize}]; if (font.m_pixelSize != pixelSize) font = UiFont(fontName, (float)pixelSize); font.MakeFont(); return &font; } }