1
0
mirror of synced 2024-11-24 07:40:17 +01:00

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

@ -3,6 +3,7 @@
#include <hex.hpp>
#include <optional>
#include <span>
#include <string>
#include <vector>
#include <map>
@ -601,6 +602,29 @@ namespace hex {
void registerHandler(const std::string &eventName, const impl::MessagingHandler &handler);
}
namespace Fonts {
struct GlyphRange { u16 begin, end; };
struct Offset { float x, y; };
struct Font {
std::string name;
std::vector<u8> fontData;
std::vector<GlyphRange> glyphRanges;
Offset offset;
};
namespace impl {
std::vector<Font>& getFonts();
}
void loadFont(const std::fs::path &path, const std::vector<GlyphRange> &glyphRanges = {}, Offset offset = {});
void loadFont(const std::string &name, const std::span<const u8> &data, const std::vector<GlyphRange> &glyphRanges = {}, Offset offset = {});
}
}
}

View File

@ -751,4 +751,41 @@ namespace hex {
}
namespace ImHexApi::Fonts {
namespace impl {
std::vector<Font>& getFonts() {
static std::vector<Font> fonts;
return fonts;
}
}
void loadFont(const std::fs::path &path, const std::vector<GlyphRange> &glyphRanges, Offset offset) {
wolv::io::File fontFile(path, wolv::io::File::Mode::Read);
if (!fontFile.isValid()) {
log::error("Failed to load font from file '{}'", wolv::util::toUTF8String(path));
return;
}
impl::getFonts().emplace_back(Font {
wolv::util::toUTF8String(path.filename()),
fontFile.readVector(),
glyphRanges,
offset
});
}
void loadFont(const std::string &name, const std::span<const u8> &data, const std::vector<GlyphRange> &glyphRanges, Offset offset) {
impl::getFonts().emplace_back(Font {
name,
{ data.begin(), data.end() },
glyphRanges,
offset
});
}
}
}

View File

@ -6,15 +6,8 @@ project(imgui_fonts)
set(CMAKE_CXX_STANDARD 17)
add_library(imgui_fonts OBJECT
source/unifont_font.c
source/codicons_font.c
source/fontawesome_font.c
)
add_library(imgui_fonts INTERFACE)
target_include_directories(imgui_fonts PUBLIC
target_include_directories(imgui_fonts INTERFACE
include
)
target_link_libraries(imgui_fonts PRIVATE imgui_includes)
set_property(TARGET imgui_fonts PROPERTY POSITION_INDEPENDENT_CODE ON)

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -21,9 +21,6 @@
#include <GLFW/glfw3.h>
#include <opengl_support.h>
#include <fonts/fontawesome_font.h>
#include <wolv/utils/guards.hpp>
#include <unistd.h>
@ -53,7 +50,7 @@ namespace hex::init {
ImHexApi::System::impl::setGPUVendor(reinterpret_cast<const char *>(glGetString(GL_VENDOR)));
EventManager::subscribe<RequestAddInitTask>([this](const std::string& name, bool async, const TaskFunction &function){
this->createTask(Task { name, function, async });
this->m_tasks.push_back(Task{ name, function, async });
});
}
@ -148,10 +145,9 @@ namespace hex::init {
auto startTime = std::chrono::high_resolution_clock::now();
// Loop over all registered init tasks
for (const auto &task : this->m_tasks) {
for (auto it = this->m_tasks.begin(); it != this->m_tasks.end(); ++it) {
// Construct a new task callback
this->createTask(task);
this->createTask(*it);
}
// Check every 100ms if all tasks have run
@ -261,7 +257,7 @@ namespace hex::init {
// Draw version information
// In debug builds, also display the current commit hash and branch
#if defined(DEBUG)
const static auto VersionInfo = hex::format("{0} : {1} {2}@{3}", ImHexApi::System::getImHexVersion(), ICON_FA_CODE_BRANCH, ImHexApi::System::getCommitBranch(), ImHexApi::System::getCommitHash());
const static auto VersionInfo = hex::format("{0} : {1}@{2}", ImHexApi::System::getImHexVersion(), ImHexApi::System::getCommitBranch(), ImHexApi::System::getCommitHash());
#else
const static auto VersionInfo = hex::format("{0}", ImHexApi::System::getImHexVersion());
#endif
@ -425,14 +421,8 @@ namespace hex::init {
cfg.SizePixels = 13.0_scaled;
io.Fonts->AddFontDefault(&cfg);
cfg.MergeMode = true;
ImWchar fontAwesomeRange[] = {
ICON_MIN_FA, ICON_MAX_FA, 0
};
std::uint8_t *px;
int w, h;
io.Fonts->AddFontFromMemoryCompressedTTF(font_awesome_compressed_data, font_awesome_compressed_size, 11.0_scaled, &cfg, fontAwesomeRange);
io.Fonts->GetTexDataAsAlpha8(&px, &w, &h);
// Create new font atlas

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();