1
0
mirror of synced 2025-01-29 19:17:28 +01:00

feat: Added setting for font bold, italic and antialiasing

This commit is contained in:
WerWolv 2023-11-24 11:29:05 +01:00
parent 8bdb39983e
commit d34ad33c3c
4 changed files with 47 additions and 22 deletions

View File

@ -1,6 +1,8 @@
#include "init/tasks.hpp"
#include <imgui.h>
#include <imgui_freetype.h>
#include <romfs/romfs.hpp>
#include <hex/helpers/http_requests.hpp>
@ -22,8 +24,6 @@
#include <fonts/codicons_font.h>
#include <fonts/unifont_font.h>
#include <filesystem>
#include <nlohmann/json.hpp>
#include <wolv/io/fs.hpp>
@ -239,18 +239,18 @@ namespace hex::init {
ImFontGlyphRangesBuilder glyphRangesBuilder;
{
constexpr static ImWchar controlCodeRange[] = { 0x0001, 0x001F, 0 };
constexpr static ImWchar extendedAsciiRange[] = { 0x007F, 0x00FF, 0 };
constexpr static std::array<ImWchar, 3> controlCodeRange = { 0x0001, 0x001F, 0 };
constexpr static std::array<ImWchar, 3> extendedAsciiRange = { 0x007F, 0x00FF, 0 };
glyphRangesBuilder.AddRanges(controlCodeRange);
glyphRangesBuilder.AddRanges(controlCodeRange.data());
glyphRangesBuilder.AddRanges(fonts->GetGlyphRangesDefault());
glyphRangesBuilder.AddRanges(extendedAsciiRange);
glyphRangesBuilder.AddRanges(extendedAsciiRange.data());
}
if (loadUnicode) {
constexpr static ImWchar fullRange[] = { 0x0100, 0xFFEF, 0 };
constexpr static std::array<ImWchar, 3> fullRange = { 0x0100, 0xFFEF, 0 };
glyphRangesBuilder.AddRanges(fullRange);
glyphRangesBuilder.AddRanges(fullRange.data());
} else {
glyphRangesBuilder.AddRanges(fonts->GetGlyphRangesJapanese());
glyphRangesBuilder.AddRanges(fonts->GetGlyphRangesChineseFull());
@ -264,12 +264,12 @@ namespace hex::init {
}
// Glyph range for font awesome icons
static ImWchar fontAwesomeRange[] = {
constexpr static std::array<ImWchar, 3> fontAwesomeRange = {
ICON_MIN_FA, ICON_MAX_FA, 0
};
// Glyph range for codicons icons
static ImWchar codiconsRange[] = {
constexpr static std::array<ImWchar, 3> codiconsRange = {
ICON_MIN_VS, ICON_MAX_VS, 0
};
@ -279,6 +279,13 @@ namespace hex::init {
fonts->Clear();
fonts->AddFontDefault(&cfg);
} else {
if (ContentRegistry::Settings::read("hex.builtin.setting.font", "hex.builtin.setting.font.bold", false))
cfg.FontBuilderFlags |= ImGuiFreeTypeBuilderFlags_Bold;
if (ContentRegistry::Settings::read("hex.builtin.setting.font", "hex.builtin.setting.font.italic", false))
cfg.FontBuilderFlags |= ImGuiFreeTypeBuilderFlags_Oblique;
if (!ContentRegistry::Settings::read("hex.builtin.setting.font", "hex.builtin.setting.font.antialias", false))
cfg.FontBuilderFlags |= ImGuiFreeTypeBuilderFlags_Monochrome | ImGuiFreeTypeBuilderFlags_MonoHinting;
auto font = fonts->AddFontFromFileTTF(wolv::util::toUTF8String(fontFile).c_str(), 0, &cfg, ranges.Data);
if (font == nullptr) {
log::warn("Failed to load custom font! Falling back to default font.");
@ -295,8 +302,8 @@ namespace hex::init {
// 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);
fonts->AddFontFromMemoryCompressedTTF(codicons_compressed_data, codicons_compressed_size, 0, &cfg, codiconsRange);
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());
cfg.GlyphOffset = ImVec2(0, 0);
// Add unifont if unicode support is enabled

View File

@ -529,6 +529,10 @@
"hex.builtin.setting.font": "Font",
"hex.builtin.setting.font.glyphs": "Glyphs",
"hex.builtin.setting.font.custom_font": "Custom Font",
"hex.builtin.setting.font.custom_font_enable": "Use custom font",
"hex.builtin.setting.font.font_bold": "Bold",
"hex.builtin.setting.font.font_italic": "Italic",
"hex.builtin.setting.font.font_antialias": "Antialiasing",
"hex.builtin.setting.font.font_path": "Custom Font Path",
"hex.builtin.setting.font.font_size": "Font Size",
"hex.builtin.setting.font.font_size.tooltip": "The font size can only be adjusted when a custom font has been selected above.\n\nThis is because ImHex uses a pixel-perfect bitmap font by default. Scaling it by any non-integer factor will only cause it to become blurry.",

View File

@ -389,20 +389,29 @@ namespace hex::plugin::builtin {
auto customFontEnabledSetting = ContentRegistry::Settings::add<Widgets::Checkbox>("hex.builtin.setting.font", "hex.builtin.setting.font.custom_font", "hex.builtin.setting.font.custom_font_enable", false).requiresRestart();
const auto fontSettingsEnabled = [customFontEnabledSetting]{
auto &checkBox = static_cast<Widgets::Checkbox &>(customFontEnabledSetting.getWidget());
return checkBox.isChecked();
};
ContentRegistry::Settings::add<Widgets::FilePicker>("hex.builtin.setting.font", "hex.builtin.setting.font.custom_font", "hex.builtin.setting.font.font_path")
.requiresRestart()
.setEnabledCallback([customFontEnabledSetting]{
auto &checkBox = static_cast<Widgets::Checkbox &>(customFontEnabledSetting.getWidget());
return checkBox.isChecked();
});
.setEnabledCallback(fontSettingsEnabled);
ContentRegistry::Settings::add<Widgets::SliderInteger>("hex.builtin.setting.font", "hex.builtin.setting.font.custom_font", "hex.builtin.setting.font.font_size", 13, 0, 100)
.requiresRestart()
.setEnabledCallback([customFontEnabledSetting]{
auto &checkBox = static_cast<Widgets::Checkbox &>(customFontEnabledSetting.getWidget());
.setEnabledCallback(fontSettingsEnabled);
ContentRegistry::Settings::add<Widgets::Checkbox>("hex.builtin.setting.font", "hex.builtin.setting.font.custom_font", "hex.builtin.setting.font.font_bold", false)
.requiresRestart()
.setEnabledCallback(fontSettingsEnabled);
ContentRegistry::Settings::add<Widgets::Checkbox>("hex.builtin.setting.font", "hex.builtin.setting.font.custom_font", "hex.builtin.setting.font.font_italic", false)
.requiresRestart()
.setEnabledCallback(fontSettingsEnabled);
ContentRegistry::Settings::add<Widgets::Checkbox>("hex.builtin.setting.font", "hex.builtin.setting.font.custom_font", "hex.builtin.setting.font.font_antialias", false)
.requiresRestart()
.setEnabledCallback(fontSettingsEnabled);
return checkBox.isChecked();
});
/* Folders */

View File

@ -113,7 +113,12 @@ namespace hex::plugin::builtin {
// If a restart is required, ask the user if they want to restart
if (!this->getWindowOpenState() && this->m_restartRequested) {
PopupQuestion::open("hex.builtin.view.settings.restart_question"_lang, ImHexApi::System::restartImHex, []{});
PopupQuestion::open("hex.builtin.view.settings.restart_question"_lang,
ImHexApi::System::restartImHex,
[this]{
this->m_restartRequested = false;
}
);
}
}