Fixed loading of multiple fonts by having one array per glyph range to keep it alive until the fonts are built.

This commit is contained in:
Patrick 2025-03-07 10:41:45 +01:00
parent 9ed87aa692
commit 63f6db2a0c

View File

@ -155,21 +155,23 @@ bool Application::loadFonts(std::span<const FontConfig> fonts)
// by default ImGui takes ownership of the data, that's now what we want // by default ImGui takes ownership of the data, that's now what we want
ImFontConfig config; ImFontConfig config;
config.FontDataOwnedByAtlas = false; config.FontDataOwnedByAtlas = false;
std::vector<ImWchar> glyphRangesConverted; std::vector<std::vector<ImWchar>> glyphRangesConverted;
glyphRangesConverted.reserve(fonts.size());
for (const auto& [font, data] : mijin::zip(fonts, buffers)) for (const auto& [font, data] : mijin::zip(fonts, buffers))
{ {
ImWchar* glyphRanges = nullptr; ImWchar* glyphRanges = nullptr;
if (!font.glyphRanges.empty()) if (!font.glyphRanges.empty())
{ {
glyphRangesConverted.reserve(2 * font.glyphRanges.size() + 1); std::vector<ImWchar>& glyphData = glyphRangesConverted.emplace_back();
glyphRangesConverted.clear(); glyphData.reserve(2 * font.glyphRanges.size() + 1);
glyphData.clear();
for (const std::pair<ImWchar, ImWchar>& range : font.glyphRanges) for (const std::pair<ImWchar, ImWchar>& range : font.glyphRanges)
{ {
glyphRangesConverted.push_back(range.first); glyphData.push_back(range.first);
glyphRangesConverted.push_back(range.second); glyphData.push_back(range.second);
} }
glyphRangesConverted.push_back(0); glyphData.push_back(0);
glyphRanges = glyphRangesConverted.data(); glyphRanges = glyphData.data();
} }
config.PixelSnapH = font.flags.pixelSnapH; config.PixelSnapH = font.flags.pixelSnapH;
imguiIO.Fonts->AddFontFromMemoryTTF(data.data(), static_cast<int>(data.byteSize()), font.size, &config, glyphRanges); imguiIO.Fonts->AddFontFromMemoryTTF(data.data(), static_cast<int>(data.byteSize()), font.size, &config, glyphRanges);