1
0
mirror of synced 2025-01-18 00:56:49 +01:00

impr: Try to align additional fonts automatically

This commit is contained in:
WerWolv 2023-11-28 13:52:26 +01:00
parent 37ce37862a
commit 23fc232c47
4 changed files with 81 additions and 14 deletions

View File

@ -612,6 +612,7 @@ namespace hex {
std::vector<u8> fontData;
std::vector<GlyphRange> glyphRanges;
Offset offset;
u32 flags;
};
namespace impl {
@ -620,8 +621,13 @@ namespace hex {
}
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 = {});
GlyphRange glyph(const char *glyph);
GlyphRange glyph(u32 codepoint);
GlyphRange range(const char *glyphBegin, const char *glyphEnd);
GlyphRange range(u32 codepointBegin, u32 codepointEnd);
void loadFont(const std::fs::path &path, const std::vector<GlyphRange> &glyphRanges = {}, Offset offset = {}, u32 flags = 0);
void loadFont(const std::string &name, const std::span<const u8> &data, const std::vector<GlyphRange> &glyphRanges = {}, Offset offset = {}, u32 flags = 0);
}

View File

@ -13,6 +13,7 @@
#include <unistd.h>
#include <imgui.h>
#include <imgui_internal.h>
#include <nlohmann/json.hpp>
@ -763,7 +764,40 @@ namespace hex {
}
void loadFont(const std::fs::path &path, const std::vector<GlyphRange> &glyphRanges, Offset offset) {
GlyphRange glyph(const char *glyph) {
u32 codepoint;
ImTextCharFromUtf8(&codepoint, glyph, nullptr);
return {
.begin = u16(codepoint),
.end = u16(codepoint)
};
}
GlyphRange glyph(u32 codepoint) {
return {
.begin = u16(codepoint),
.end = u16(codepoint)
};
}
GlyphRange range(const char *glyphBegin, const char *glyphEnd) {
u32 codepointBegin, codepointEnd;
ImTextCharFromUtf8(&codepointBegin, glyphBegin, nullptr);
ImTextCharFromUtf8(&codepointEnd, glyphEnd, nullptr);
return {
.begin = u16(codepointBegin),
.end = u16(codepointEnd)
};
}
GlyphRange range(u32 codepointBegin, u32 codepointEnd) {
return {
.begin = u16(codepointBegin),
.end = u16(codepointEnd)
};
}
void loadFont(const std::fs::path &path, const std::vector<GlyphRange> &glyphRanges, Offset offset, u32 flags) {
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));
@ -774,16 +808,18 @@ namespace hex {
wolv::util::toUTF8String(path.filename()),
fontFile.readVector(),
glyphRanges,
offset
offset,
flags
});
}
void loadFont(const std::string &name, const std::span<const u8> &data, 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, u32 flags) {
impl::getFonts().emplace_back(Font {
name,
{ data.begin(), data.end() },
glyphRanges,
offset
offset,
flags
});
}
}

View File

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

View File

@ -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);
}