Skip to content

Commit

Permalink
impr: Better font loading logic
Browse files Browse the repository at this point in the history
  • Loading branch information
WerWolv committed Jun 16, 2024
1 parent deee76e commit f49715c
Show file tree
Hide file tree
Showing 8 changed files with 351 additions and 264 deletions.
2 changes: 1 addition & 1 deletion lib/libimhex/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ target_link_directories(libimhex ${LIBIMHEX_LIBRARY_TYPE} ${MBEDTLS_LIBRARY_DIR}

if (NOT EMSCRIPTEN)
# curl is only used in non-emscripten builds
target_link_libraries(libimhex ${LIBIMHEX_LIBRARY_TYPE} ${CURL_LIBRARIES})
target_link_libraries(libimhex ${LIBIMHEX_LIBRARY_TYPE} CURL::libcurl)
endif()


Expand Down
6 changes: 3 additions & 3 deletions lib/libimhex/source/api/imhex_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -932,9 +932,9 @@ namespace hex {
s_fontSize = size;
}

static AutoReset<std::shared_ptr<ImFontAtlas>> s_fontAtlas;
static AutoReset<ImFontAtlas*> s_fontAtlas;
void setFontAtlas(ImFontAtlas* fontAtlas) {
s_fontAtlas = std::unique_ptr<ImFontAtlas, void(*)(ImFontAtlas*)>(fontAtlas, IM_DELETE);
s_fontAtlas = fontAtlas;
}

static ImFont *s_boldFont = nullptr;
Expand Down Expand Up @@ -1017,7 +1017,7 @@ namespace hex {
}

ImFontAtlas* getFontAtlas() {
return impl::s_fontAtlas->get();
return impl::s_fontAtlas;
}

ImFont* Bold() {
Expand Down
1 change: 0 additions & 1 deletion main/gui/source/window/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,6 @@ namespace hex {
glfwWaitEventsTimeout(targetFrameTime - frameTime);

// glfwWaitEventsTimeout might return early if there's an event
const auto frameTime = glfwGetTime() - m_lastStartFrameTime;
if (frameTime < targetFrameTime) {
const auto timeToSleepMs = (int)((targetFrameTime - frameTime) * 1000);
std::this_thread::sleep_for(std::chrono::milliseconds(timeToSleepMs));
Expand Down
256 changes: 0 additions & 256 deletions plugins/builtin/source/content/init_tasks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,10 @@

#include <hex/api/task_manager.hpp>
#include <hex/helpers/http_requests.hpp>
#include <hex/helpers/fs.hpp>
#include <hex/helpers/utils.hpp>
#include <hex/helpers/logger.hpp>

#include <wolv/utils/string.hpp>
#include <wolv/hash/uuid.hpp>

#include <imgui.h>
#include <imgui_freetype.h>

#include <cstring>

namespace hex::plugin::builtin {

namespace {
Expand Down Expand Up @@ -119,253 +111,6 @@ namespace hex::plugin::builtin {
return true;
}

bool loadFontsImpl(bool loadUnicode) {
float defaultFontSize = ImHexApi::Fonts::DefaultFontSize * ImHexApi::System::getGlobalScale();

if (defaultFontSize == 0.0F)
defaultFontSize = ImHexApi::Fonts::DefaultFontSize;

// Reset used font size back to the default size
ImHexApi::Fonts::impl::setFontSize(defaultFontSize);

// Load custom font related settings
if (ContentRegistry::Settings::read<bool>("hex.builtin.setting.font", "hex.builtin.setting.font.custom_font_enable", false)) {
auto fontFile = ContentRegistry::Settings::read<std::fs::path>("hex.builtin.setting.font", "hex.builtin.setting.font.font_path", "");
if (!fontFile.empty()) {
if (!wolv::io::fs::exists(fontFile) || !wolv::io::fs::isRegularFile(fontFile)) {
log::warn("Custom font file {} not found! Falling back to default font.", wolv::util::toUTF8String(fontFile));
fontFile.clear();
}

log::info("Loading custom font from {}", wolv::util::toUTF8String(fontFile));
}

// If no custom font has been specified, search for a file called "font.ttf" in one of the resource folders
if (fontFile.empty()) {
for (const auto &dir : fs::getDefaultPaths(fs::ImHexPath::Resources)) {
auto path = dir / "font.ttf";
if (wolv::io::fs::exists(path)) {
log::info("Loading custom font from {}", wolv::util::toUTF8String(path));

fontFile = path;
break;
}
}
}

ImHexApi::Fonts::impl::setCustomFontPath(fontFile);

// If a custom font has been loaded now, also load the font size
float fontSize = defaultFontSize;
if (!fontFile.empty()) {
fontSize = float(ContentRegistry::Settings::read<int>("hex.builtin.setting.font", "hex.builtin.setting.font.font_size", 13)) * ImHexApi::System::getGlobalScale();
}

ImHexApi::Fonts::impl::setFontSize(fontSize);
}

float fontSize = ImHexApi::Fonts::getFontSize();

const auto &fontFile = ImHexApi::Fonts::getCustomFontPath();

// Setup basic font configuration
auto fonts = IM_NEW(ImFontAtlas)();
ImFontConfig cfg = {};
cfg.OversampleH = cfg.OversampleV = 1, cfg.PixelSnapH = true;
cfg.SizePixels = fontSize;

fonts->Flags |= ImFontAtlasFlags_NoPowerOfTwoHeight;
fonts->TexDesiredWidth = 4096;

// Configure font glyph ranges that should be loaded from the default font and unifont
static ImVector<ImWchar> defaultGlyphRanges;
defaultGlyphRanges = { };
{
ImFontGlyphRangesBuilder glyphRangesBuilder;

{
constexpr static std::array<ImWchar, 3> controlCodeRange = { 0x0001, 0x001F, 0 };
constexpr static std::array<ImWchar, 3> extendedAsciiRange = { 0x007F, 0x00FF, 0 };
constexpr static std::array<ImWchar, 3> latinExtendedARange = { 0x0100, 0x017F, 0 };

glyphRangesBuilder.AddRanges(controlCodeRange.data());
glyphRangesBuilder.AddRanges(fonts->GetGlyphRangesDefault());
glyphRangesBuilder.AddRanges(extendedAsciiRange.data());
glyphRangesBuilder.AddRanges(latinExtendedARange.data());
}

if (loadUnicode) {
constexpr static std::array<ImWchar, 3> fullRange = { 0x0180, 0xFFEF, 0 };

glyphRangesBuilder.AddRanges(fullRange.data());
} else {
glyphRangesBuilder.AddRanges(fonts->GetGlyphRangesJapanese());
glyphRangesBuilder.AddRanges(fonts->GetGlyphRangesChineseFull());
glyphRangesBuilder.AddRanges(fonts->GetGlyphRangesCyrillic());
glyphRangesBuilder.AddRanges(fonts->GetGlyphRangesKorean());
glyphRangesBuilder.AddRanges(fonts->GetGlyphRangesThai());
glyphRangesBuilder.AddRanges(fonts->GetGlyphRangesVietnamese());
}

glyphRangesBuilder.BuildRanges(&defaultGlyphRanges);
}

std::vector<u8> customFontData;

if (fontFile.empty()) {
fonts->Clear();
} else {
wolv::io::File file(fontFile, wolv::io::File::Mode::Read);
customFontData = file.readVector();
}

if (ContentRegistry::Settings::read<bool>("hex.builtin.setting.font", "hex.builtin.setting.font.custom_font_enable", false)) {
if (ContentRegistry::Settings::read<bool>("hex.builtin.setting.font", "hex.builtin.setting.font.font_bold", false))
cfg.FontBuilderFlags |= ImGuiFreeTypeBuilderFlags_Bold;
if (ContentRegistry::Settings::read<bool>("hex.builtin.setting.font", "hex.builtin.setting.font.font_italic", false))
cfg.FontBuilderFlags |= ImGuiFreeTypeBuilderFlags_Oblique;
if (!ContentRegistry::Settings::read<bool>("hex.builtin.setting.font", "hex.builtin.setting.font.font_antialias", true))
cfg.FontBuilderFlags |= ImGuiFreeTypeBuilderFlags_Monochrome | ImGuiFreeTypeBuilderFlags_MonoHinting;
}

auto loadDefaultFont = [&](const char *fontName, u32 flags = 0) {
ImFontConfig defaultConfig = cfg;

defaultConfig.FontBuilderFlags |= flags;

std::strncpy(defaultConfig.Name, fontName, sizeof(defaultConfig.Name) - 1);

if (!fontFile.empty()) {
if (!customFontData.empty()) {
defaultConfig.FontDataOwnedByAtlas = false;
return fonts->AddFontFromMemoryTTF(customFontData.data(), customFontData.size(), 0, &defaultConfig, defaultGlyphRanges.Data);
}
}

defaultConfig.FontBuilderFlags |= ImGuiFreeTypeBuilderFlags_Monochrome | ImGuiFreeTypeBuilderFlags_MonoHinting;
defaultConfig.SizePixels = std::floor(ImHexApi::Fonts::getFontSize() / ImHexApi::Fonts::DefaultFontSize) * ImHexApi::Fonts::DefaultFontSize;

return fonts->AddFontDefault(&defaultConfig);
};

// Load main font
// If a custom font has been specified, load it, otherwise load the default ImGui font
ImFont *defaultFont = loadDefaultFont("Default Font");
if (defaultFont == nullptr) {
log::warn("Failed to load custom font! Falling back to default font.");

ImHexApi::Fonts::impl::setFontSize(defaultFontSize);
cfg.SizePixels = defaultFontSize;
defaultFont = fonts->AddFontDefault(&cfg);
}

fonts->Build();

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

ranges.push_back(fontRange);

cfg.FontBuilderFlags = font.flags;

float descent = [&] {
ImFontAtlas atlas;

// Disable merge mode for this font but retain the rest of the configuration
cfg.MergeMode = false;

auto size = fontSize;
if (font.defaultSize.has_value())
size = font.defaultSize.value() * std::floor(ImHexApi::Fonts::getFontSize() / ImHexApi::Fonts::DefaultFontSize);
else
size = std::max(1.0F, std::floor(size / ImHexApi::Fonts::DefaultFontSize)) * ImHexApi::Fonts::DefaultFontSize;

cfg.SizePixels = size;

ON_SCOPE_EXIT { cfg.MergeMode = true; };

// Construct a range that only contains the first glyph of the font
ImVector<ImWchar> queryRange;
{
auto firstGlyph = font.glyphRanges.empty() ? defaultGlyphRanges.front() : font.glyphRanges.front().begin;
queryRange.push_back(firstGlyph);
queryRange.push_back(firstGlyph);
}
queryRange.push_back(0x00);

// Build the font atlas with the query range
auto newFont = atlas.AddFontFromMemoryTTF(const_cast<u8 *>(font.fontData.data()), int(font.fontData.size()), 0, &cfg, queryRange.Data);
atlas.Build();

return newFont->Descent;
}();


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 - defaultFont->Descent + descent };
fonts->AddFontFromMemoryTTF(const_cast<u8 *>(font.fontData.data()), int(font.fontData.size()), 0, &cfg, ranges.back().Data);
}
cfg.FontBuilderFlags = startFlags;

// Create bold and italic font
cfg.MergeMode = false;
ImFont *boldFont = loadDefaultFont("Bold Font", ImGuiFreeTypeBuilderFlags_Bold);
ImFont *italicFont = loadDefaultFont("Italic Font", ImGuiFreeTypeBuilderFlags_Oblique);
ImHexApi::Fonts::impl::setFonts(boldFont, italicFont);

// Try to build the font atlas
if (!fonts->Build()) {
// The main reason the font atlas failed to build is that the font is too big for the GPU to handle
// If unicode support is enabled, therefor try to load the font atlas without unicode support
// If that still didn't work, there's probably something else going on with the graphics drivers
// Especially Intel GPU drivers are known to have various bugs

if (loadUnicode) {
log::error("Failed to build font atlas! Disabling Unicode support.");
IM_DELETE(fonts);

// Disable unicode support in settings
ContentRegistry::Settings::write<bool>("hex.builtin.setting.font", "hex.builtin.setting.font.load_all_unicode_chars", false);

// Try to load the font atlas again
return loadFontsImpl(false);
} else {
log::error("Failed to build font atlas! Check your Graphics driver!");
return false;
}
}

// Configure ImGui to use the font atlas
ImHexApi::Fonts::impl::setFontAtlas(fonts);

return true;
}

bool loadFonts() {
// Check if unicode support is enabled in the settings and that the user doesn't use the No GPU version on Windows
// The Mesa3D software renderer on Windows identifies itself as "VMware, Inc."
bool shouldLoadUnicode =
ContentRegistry::Settings::read<bool>("hex.builtin.setting.font", "hex.builtin.setting.font.load_all_unicode_chars", false) &&
ImHexApi::System::getGPUVendor() != "VMware, Inc.";

return loadFontsImpl(shouldLoadUnicode);
}

bool loadWindowSettings() {
bool multiWindowEnabled = ContentRegistry::Settings::read<bool>("hex.builtin.setting.interface", "hex.builtin.setting.interface.multi_windows", false);
ImHexApi::System::impl::setMultiWindowMode(multiWindowEnabled);
Expand All @@ -392,7 +137,6 @@ namespace hex::plugin::builtin {
void addInitTasks() {
ImHexApi::System::addStartupTask("Load Window Settings", false, loadWindowSettings);
ImHexApi::System::addStartupTask("Configuring UI scale", true, configureUIScale);
ImHexApi::System::addStartupTask("Loading fonts", true, loadFonts);
ImHexApi::System::addStartupTask("Checking for updates", true, checkForUpdates);
}
}
1 change: 1 addition & 0 deletions plugins/fonts/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ add_imhex_plugin(
fonts
SOURCES
source/library_fonts.cpp
source/font_loader.cpp
source/fonts.cpp
INCLUDES
include
Expand Down
Loading

0 comments on commit f49715c

Please sign in to comment.