1
0
mirror of synced 2024-11-12 02:00:52 +01:00

impr: Improve font loading speed

This commit is contained in:
WerWolv 2023-11-29 09:50:57 +01:00
parent 80984f28ec
commit 540e8458a5
2 changed files with 26 additions and 10 deletions

View File

@ -25,15 +25,13 @@ namespace hex::plugin::builtin {
{
{ glyph(ICON_FA_BACKSPACE), glyph(ICON_FA_INFINITY), glyph(ICON_FA_TACHOMETER_ALT), glyph(ICON_FA_MICROCHIP), glyph(ICON_FA_CODE_BRANCH) }
},
{ 0, 0 },
ImGuiFreeTypeBuilderFlags_Monochrome | ImGuiFreeTypeBuilderFlags_MonoHinting);
{ 0, 0 });
ImHexApi::Fonts::loadFont("VS Codicons", romfs::get("fonts/codicons.ttf").span<u8>(),
{
{ ICON_MIN_VS, ICON_MAX_VS }
},
{ 0, 0 },
ImGuiFreeTypeBuilderFlags_Monochrome | ImGuiFreeTypeBuilderFlags_MonoHinting);
{ 0, 0 });
ImHexApi::Fonts::loadFont("Unifont", romfs::get("fonts/unifont.otf").span<u8>());
}

View File

@ -257,15 +257,33 @@ namespace hex::plugin::builtin {
cfg.FontBuilderFlags = startFlags | font.flags;
cfg.MergeMode = false;
ImFontAtlas atlas;
auto queryFont = atlas.AddFontFromMemoryTTF(font.fontData.data(), int(font.fontData.size()), 0, &cfg, ranges.back().Data);
atlas.Build();
float descent = [&] {
ImFontAtlas atlas;
// Disable merge mode for this font but retain the rest of the configuration
cfg.MergeMode = false;
ON_SCOPE_EXIT { cfg.MergeMode = true; };
// Construct a range that only contains the first glyph of the font
ImVector<ImWchar> queryRange;
{
auto firstGlyph = font.glyphRanges.empty() ? defaultGlyphRanges.front() : font.glyphRanges.front().begin;
queryRange.push_back(firstGlyph);
queryRange.push_back(firstGlyph);
}
queryRange.push_back(0x00);
// Build the font atlas with the query range
auto newFont = atlas.AddFontFromMemoryTTF(font.fontData.data(), int(font.fontData.size()), 0, &cfg, queryRange.Data);
atlas.Build();
return newFont->Descent;
}();
cfg.MergeMode = true;
std::memset(cfg.Name, 0x00, sizeof(cfg.Name));
std::strncpy(cfg.Name, font.name.c_str(), sizeof(cfg.Name) - 1);
cfg.GlyphOffset = { font.offset.x, font.offset.y - defaultFont->Descent + queryFont->Descent };
cfg.GlyphOffset = { font.offset.x, font.offset.y - defaultFont->Descent + descent };
fonts->AddFontFromMemoryTTF(font.fontData.data(), int(font.fontData.size()), 0, &cfg, ranges.back().Data);
}