mirror of
https://github.com/WerWolv/ImHex.git
synced 2026-04-02 05:27:41 -05:00
refactor: Moved over to more flexible font loader
This commit is contained in:
@@ -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
|
||||
|
||||
0
plugins/builtin/romfs/fonts/asdasd
Normal file
0
plugins/builtin/romfs/fonts/asdasd
Normal file
BIN
plugins/builtin/romfs/fonts/codicons.ttf
Normal file
BIN
plugins/builtin/romfs/fonts/codicons.ttf
Normal file
Binary file not shown.
BIN
plugins/builtin/romfs/fonts/fontawesome.otf
Normal file
BIN
plugins/builtin/romfs/fonts/fontawesome.otf
Normal file
Binary file not shown.
BIN
plugins/builtin/romfs/fonts/unifont.otf
Normal file
BIN
plugins/builtin/romfs/fonts/unifont.otf
Normal file
Binary file not shown.
18
plugins/builtin/source/content/fonts.cpp
Normal file
18
plugins/builtin/source/content/fonts.cpp
Normal 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 });
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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()) {
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user