refactor: Moved over to more flexible font loader

This commit is contained in:
WerWolv
2023-11-28 01:55:41 +01:00
parent f6d4d5ab22
commit c02c27b63d
15 changed files with 111 additions and 120334 deletions

View File

@@ -44,6 +44,7 @@ add_imhex_plugin(
source/content/file_extraction.cpp
source/content/report_generators.cpp
source/content/init_tasks.cpp
source/content/fonts.cpp
source/content/dpn/basic_nodes.cpp
source/content/dpn/control_nodes.cpp

View File

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -0,0 +1,18 @@
#include <hex/api/imhex_api.hpp>
#include <romfs/romfs.hpp>
#include <hex/helpers/utils.hpp>
#include <fonts/fontawesome_font.h>
#include <fonts/codicons_font.h>
namespace hex::plugin::builtin {
void loadFonts() {
ImHexApi::Fonts::loadFont("Unifont", romfs::get("fonts/unifont.otf").span<u8>());
ImHexApi::Fonts::loadFont("VS Codicons", romfs::get("fonts/codicons.ttf").span<u8>(), { { ICON_MIN_VS, ICON_MAX_VS } }, { 0, 3_scaled });
ImHexApi::Fonts::loadFont("Font Awesome 5", romfs::get("fonts/fontawesome.otf").span<u8>(), { { ICON_MIN_FA, ICON_MAX_FA } }, { 0, 3_scaled });
}
}

View File

@@ -173,7 +173,7 @@ namespace hex::plugin::builtin {
fonts->TexDesiredWidth = 4096;
// Configure font glyph ranges that should be loaded from the default font and unifont
static ImVector<ImWchar> ranges;
static ImVector<ImWchar> defaultGlyphRanges;
{
ImFontGlyphRangesBuilder glyphRangesBuilder;
@@ -199,19 +199,9 @@ namespace hex::plugin::builtin {
glyphRangesBuilder.AddRanges(fonts->GetGlyphRangesVietnamese());
}
glyphRangesBuilder.BuildRanges(&ranges);
glyphRangesBuilder.BuildRanges(&defaultGlyphRanges);
}
// Glyph range for font awesome icons
constexpr static std::array<ImWchar, 3> fontAwesomeRange = {
ICON_MIN_FA, ICON_MAX_FA, 0
};
// Glyph range for codicons icons
constexpr static std::array<ImWchar, 3> codiconsRange = {
ICON_MIN_VS, ICON_MAX_VS, 0
};
// Load main font
// If a custom font has been specified, load it, otherwise load the default ImGui font
if (fontFile.empty()) {
@@ -225,7 +215,7 @@ 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, ranges.Data);
auto font = fonts->AddFontFromFileTTF(wolv::util::toUTF8String(fontFile).c_str(), 0, &cfg, defaultGlyphRanges.Data);
cfg.FontBuilderFlags = 0;
@@ -241,15 +231,27 @@ namespace hex::plugin::builtin {
// Merge all fonts into one big font atlas
cfg.MergeMode = true;
cfg.FontDataOwnedByAtlas = false;
// Add font awesome and codicons icons to font atlas
cfg.GlyphOffset = ImVec2(0, 3_scaled);
fonts->AddFontFromMemoryCompressedTTF(font_awesome_compressed_data, font_awesome_compressed_size, 0, &cfg, fontAwesomeRange.data());
fonts->AddFontFromMemoryCompressedTTF(codicons_compressed_data, codicons_compressed_size, 0, &cfg, codiconsRange.data());
std::list<ImVector<ImWchar>> ranges;
for (auto &font : ImHexApi::Fonts::impl::getFonts()) {
ImVector<ImWchar> fontRange;
if (font.glyphRanges.empty()) {
fontRange = defaultGlyphRanges;
} else {
for (const auto &range : font.glyphRanges) {
fontRange.push_back(range.begin);
fontRange.push_back(range.end);
}
fontRange.push_back(0x00);
}
cfg.GlyphOffset = ImVec2(0, 0);
// Add unifont if unicode support is enabled
fonts->AddFontFromMemoryCompressedTTF(unifont_compressed_data, unifont_compressed_size, 0, &cfg, ranges.Data);
ranges.push_back(fontRange);
std::strncpy(cfg.Name, font.name.c_str(), sizeof(cfg.Name));
cfg.GlyphOffset = { font.offset.x, font.offset.y };
fonts->AddFontFromMemoryTTF(font.fontData.data(), font.fontData.size(), 0, &cfg, ranges.back().Data);
}
// Try to build the font atlas
if (!fonts->Build()) {

View File

@@ -45,6 +45,7 @@ namespace hex::plugin::builtin {
void addToolbarItems();
void addGlobalUIItems();
void addInitTasks();
void loadFonts();
void handleBorderlessWindowMode();
@@ -76,6 +77,7 @@ IMHEX_PLUGIN_SETUP("Built-in", "WerWolv", "Default ImHex functionality") {
hex::ContentRegistry::Language::addLocalization(nlohmann::json::parse(romfs::get(path).string()));
addInitTasks();
loadFonts();
registerEventHandlers();
registerDataVisualizers();