mirror of
https://github.com/WerWolv/ImHex.git
synced 2026-04-02 13:37:42 -05:00
impr: Try to align additional fonts automatically
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
#include <imgui_internal.h>
|
||||
#include <hex/api/imhex_api.hpp>
|
||||
|
||||
#include <romfs/romfs.hpp>
|
||||
@@ -7,18 +8,34 @@
|
||||
#include <fonts/fontawesome_font.h>
|
||||
#include <fonts/codicons_font.h>
|
||||
|
||||
#include <imgui_freetype.h>
|
||||
|
||||
namespace hex::plugin::builtin {
|
||||
|
||||
void loadFonts() {
|
||||
using namespace ImHexApi::Fonts;
|
||||
|
||||
/**
|
||||
* !!IMPORTANT!!
|
||||
* Always load the font files in decreasing size of their glyphs. This will make the rasterize be more
|
||||
* efficient when packing the glyphs into the font atlas and therefor make the atlas much smaller.
|
||||
*/
|
||||
|
||||
ImHexApi::Fonts::loadFont("Font Awesome 5", romfs::get("fonts/fontawesome.otf").span<u8>(), { { ICON_MIN_FA, ICON_MAX_FA } }, { 0, 3_scaled });
|
||||
ImHexApi::Fonts::loadFont("VS Codicons", romfs::get("fonts/codicons.ttf").span<u8>(), { { ICON_MIN_VS, ICON_MAX_VS } }, { 0, 3_scaled });
|
||||
ImHexApi::Fonts::loadFont("Unifont", romfs::get("fonts/unifont.otf").span<u8>());
|
||||
ImHexApi::Fonts::loadFont("Font Awesome 5", romfs::get("fonts/fontawesome.otf").span<u8>(),
|
||||
{
|
||||
{ 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);
|
||||
|
||||
ImHexApi::Fonts::loadFont("VS Codicons", romfs::get("fonts/codicons.ttf").span<u8>(),
|
||||
{
|
||||
{ ICON_MIN_VS, ICON_MAX_VS }
|
||||
},
|
||||
{ 0, 0 },
|
||||
ImGuiFreeTypeBuilderFlags_Monochrome | ImGuiFreeTypeBuilderFlags_MonoHinting);
|
||||
|
||||
ImHexApi::Fonts::loadFont("Unifont", romfs::get("fonts/unifont.otf").span<u8>());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -208,9 +208,10 @@ namespace hex::plugin::builtin {
|
||||
|
||||
// Load main font
|
||||
// If a custom font has been specified, load it, otherwise load the default ImGui font
|
||||
ImFont *defaultFont;
|
||||
if (fontFile.empty()) {
|
||||
fonts->Clear();
|
||||
fonts->AddFontDefault(&cfg);
|
||||
defaultFont = fonts->AddFontDefault(&cfg);
|
||||
} else {
|
||||
if (ContentRegistry::Settings::read("hex.builtin.setting.font", "hex.builtin.setting.font.font_bold", false))
|
||||
cfg.FontBuilderFlags |= ImGuiFreeTypeBuilderFlags_Bold;
|
||||
@@ -219,24 +220,29 @@ namespace hex::plugin::builtin {
|
||||
if (!ContentRegistry::Settings::read("hex.builtin.setting.font", "hex.builtin.setting.font.font_antialias", false))
|
||||
cfg.FontBuilderFlags |= ImGuiFreeTypeBuilderFlags_Monochrome | ImGuiFreeTypeBuilderFlags_MonoHinting;
|
||||
|
||||
auto font = fonts->AddFontFromFileTTF(wolv::util::toUTF8String(fontFile).c_str(), 0, &cfg, defaultGlyphRanges.Data);
|
||||
defaultFont = fonts->AddFontFromFileTTF(wolv::util::toUTF8String(fontFile).c_str(), 0, &cfg, defaultGlyphRanges.Data);
|
||||
|
||||
cfg.FontBuilderFlags = 0;
|
||||
|
||||
if (font == nullptr) {
|
||||
if (defaultFont == nullptr) {
|
||||
log::warn("Failed to load custom font! Falling back to default font.");
|
||||
|
||||
ImHexApi::System::impl::setFontSize(defaultFontSize);
|
||||
cfg.SizePixels = defaultFontSize;
|
||||
fonts->Clear();
|
||||
fonts->AddFontDefault(&cfg);
|
||||
defaultFont = fonts->AddFontDefault(&cfg);
|
||||
}
|
||||
}
|
||||
|
||||
// Build default font to get its metrics
|
||||
fonts->Build();
|
||||
|
||||
// Merge all fonts into one big font atlas
|
||||
cfg.MergeMode = true;
|
||||
cfg.FontDataOwnedByAtlas = false;
|
||||
|
||||
// Add all other fonts to the atlas
|
||||
auto startFlags = cfg.FontBuilderFlags;
|
||||
std::list<ImVector<ImWchar>> ranges;
|
||||
for (auto &font : ImHexApi::Fonts::impl::getFonts()) {
|
||||
ImVector<ImWchar> fontRange;
|
||||
@@ -252,9 +258,11 @@ namespace hex::plugin::builtin {
|
||||
|
||||
ranges.push_back(fontRange);
|
||||
|
||||
cfg.FontBuilderFlags = startFlags | font.flags;
|
||||
|
||||
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 };
|
||||
cfg.GlyphOffset = { font.offset.x, font.offset.y - defaultFont->Descent };
|
||||
fonts->AddFontFromMemoryTTF(font.fontData.data(), int(font.fontData.size()), 0, &cfg, ranges.back().Data);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user