1
0
mirror of synced 2024-11-28 01:20:51 +01:00

feat: Add support for bold and italic fonts

This commit is contained in:
WerWolv 2023-11-29 23:47:37 +01:00
parent 5e32b693f3
commit 09904b77eb
6 changed files with 124 additions and 90 deletions

View File

@ -13,6 +13,7 @@
using ImGuiID = unsigned int; using ImGuiID = unsigned int;
struct ImVec2; struct ImVec2;
struct ImFontAtlas; struct ImFontAtlas;
struct ImFont;
namespace hex { namespace hex {
@ -358,10 +359,6 @@ namespace hex {
void setBorderlessWindowMode(bool enabled); void setBorderlessWindowMode(bool enabled);
void setCustomFontPath(const std::fs::path &path);
void setFontSize(float size);
void setFontAtlas(ImFontAtlas *fontAtlas);
void setGPUVendor(const std::string &vendor); void setGPUVendor(const std::string &vendor);
void setPortableVersion(bool enabled); void setPortableVersion(bool enabled);
@ -465,27 +462,6 @@ namespace hex {
*/ */
std::map<std::string, std::string> &getInitArguments(); std::map<std::string, std::string> &getInitArguments();
constexpr static float DefaultFontSize = 13.0;
/**
* @brief Gets the current custom font path
* @return The current custom font path
*/
std::filesystem::path &getCustomFontPath();
/**
* @brief Gets the current font size
* @return The current font size
*/
float getFontSize();
/**
* @brief Gets the current font atlas
* @return Current font atlas
*/
ImFontAtlas* getFontAtlas();
/** /**
* @brief Sets if ImHex should follow the system theme * @brief Sets if ImHex should follow the system theme
* @param enabled Whether to follow the system theme * @param enabled Whether to follow the system theme
@ -619,6 +595,11 @@ namespace hex {
std::vector<Font>& getFonts(); std::vector<Font>& getFonts();
void setCustomFontPath(const std::fs::path &path);
void setFontSize(float size);
void setFontAtlas(ImFontAtlas *fontAtlas);
void setFonts(ImFont *bold, ImFont *italic);
} }
GlyphRange glyph(const char *glyph); GlyphRange glyph(const char *glyph);
@ -629,6 +610,29 @@ namespace hex {
void loadFont(const std::fs::path &path, const std::vector<GlyphRange> &glyphRanges = {}, Offset offset = {}, u32 flags = 0); 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); void loadFont(const std::string &name, const std::span<const u8> &data, const std::vector<GlyphRange> &glyphRanges = {}, Offset offset = {}, u32 flags = 0);
constexpr static float DefaultFontSize = 13.0;
ImFont* Bold();
ImFont* Italic();
/**
* @brief Gets the current custom font path
* @return The current custom font path
*/
std::filesystem::path &getCustomFontPath();
/**
* @brief Gets the current font size
* @return The current font size
*/
float getFontSize();
/**
* @brief Gets the current font atlas
* @return Current font atlas
*/
ImFontAtlas* getFontAtlas();
} }
} }

View File

@ -419,21 +419,6 @@ namespace hex {
s_borderlessWindowMode = enabled; s_borderlessWindowMode = enabled;
} }
static std::fs::path s_customFontPath;
void setCustomFontPath(const std::fs::path &path) {
s_customFontPath = path;
}
static float s_fontSize = DefaultFontSize;
void setFontSize(float size) {
s_fontSize = size;
}
static ImFontAtlas *s_fontAtlas;
void setFontAtlas(ImFontAtlas* fontAtlas) {
s_fontAtlas = fontAtlas;
}
static std::string s_gpuVendor; static std::string s_gpuVendor;
void setGPUVendor(const std::string &vendor) { void setGPUVendor(const std::string &vendor) {
@ -517,23 +502,8 @@ namespace hex {
return initArgs; return initArgs;
} }
std::fs::path &getCustomFontPath() {
return impl::s_customFontPath;
}
float getFontSize() {
return impl::s_fontSize;
}
ImFontAtlas* getFontAtlas() {
return impl::s_fontAtlas;
}
static bool s_systemThemeDetection; static bool s_systemThemeDetection;
void enableSystemThemeDetection(bool enabled) { void enableSystemThemeDetection(bool enabled) {
s_systemThemeDetection = enabled; s_systemThemeDetection = enabled;
@ -762,6 +732,29 @@ namespace hex {
return fonts; return fonts;
} }
static std::fs::path s_customFontPath;
void setCustomFontPath(const std::fs::path &path) {
s_customFontPath = path;
}
static float s_fontSize = DefaultFontSize;
void setFontSize(float size) {
s_fontSize = size;
}
static std::unique_ptr<ImFontAtlas> s_fontAtlas;
void setFontAtlas(ImFontAtlas* fontAtlas) {
s_fontAtlas = std::unique_ptr<ImFontAtlas>(fontAtlas);
}
static ImFont *s_boldFont = nullptr;
static ImFont *s_italicFont = nullptr;
void setFonts(ImFont *bold, ImFont *italic) {
s_boldFont = bold;
s_italicFont = italic;
}
} }
GlyphRange glyph(const char *glyph) { GlyphRange glyph(const char *glyph) {
@ -822,6 +815,28 @@ namespace hex {
flags flags
}); });
} }
std::fs::path &getCustomFontPath() {
return impl::s_customFontPath;
}
float getFontSize() {
return impl::s_fontSize;
}
ImFontAtlas* getFontAtlas() {
return impl::s_fontAtlas.get();
}
ImFont* Bold() {
return impl::s_boldFont;
}
ImFont* Italic() {
return impl::s_italicFont;
}
} }
} }

View File

@ -84,8 +84,8 @@ namespace hex::init {
ImHexApi::HexEditor::impl::getTooltips().clear(); ImHexApi::HexEditor::impl::getTooltips().clear();
ImHexApi::HexEditor::impl::getTooltipFunctions().clear(); ImHexApi::HexEditor::impl::getTooltipFunctions().clear();
ImHexApi::System::getAdditionalFolderPaths().clear(); ImHexApi::System::getAdditionalFolderPaths().clear();
ImHexApi::System::getCustomFontPath().clear();
ImHexApi::Messaging::impl::getHandlers().clear(); ImHexApi::Messaging::impl::getHandlers().clear();
ImHexApi::Fonts::getCustomFontPath().clear();
ImHexApi::Fonts::impl::getFonts().clear(); ImHexApi::Fonts::impl::getFonts().clear();
ContentRegistry::Settings::impl::getSettings().clear(); ContentRegistry::Settings::impl::getSettings().clear();
@ -149,8 +149,6 @@ namespace hex::init {
fs::setFileBrowserErrorCallback(nullptr); fs::setFileBrowserErrorCallback(nullptr);
IM_DELETE(ImHexApi::System::getFontAtlas());
return true; return true;
} }

View File

@ -1139,7 +1139,7 @@ namespace hex {
void Window::initImGui() { void Window::initImGui() {
IMGUI_CHECKVERSION(); IMGUI_CHECKVERSION();
auto fonts = ImHexApi::System::getFontAtlas(); auto fonts = ImHexApi::Fonts::getFontAtlas();
// Initialize ImGui and all other ImGui extensions // Initialize ImGui and all other ImGui extensions
GImGui = ImGui::CreateContext(fonts); GImGui = ImGui::CreateContext(fonts);

View File

@ -121,10 +121,10 @@ namespace hex::plugin::builtin {
} }
bool loadFontsImpl(bool loadUnicode) { bool loadFontsImpl(bool loadUnicode) {
const float defaultFontSize = ImHexApi::System::DefaultFontSize * std::round(ImHexApi::System::getGlobalScale()); const float defaultFontSize = ImHexApi::Fonts::DefaultFontSize * std::round(ImHexApi::System::getGlobalScale());
// Reset used font size back to the default size // Reset used font size back to the default size
ImHexApi::System::impl::setFontSize(defaultFontSize); ImHexApi::Fonts::impl::setFontSize(defaultFontSize);
// Load custom font related settings // Load custom font related settings
if (ContentRegistry::Settings::read("hex.builtin.setting.font", "hex.builtin.setting.font.custom_font_enable", false).get<bool>()) { if (ContentRegistry::Settings::read("hex.builtin.setting.font", "hex.builtin.setting.font.custom_font_enable", false).get<bool>()) {
@ -151,7 +151,7 @@ namespace hex::plugin::builtin {
} }
} }
ImHexApi::System::impl::setCustomFontPath(fontFile); ImHexApi::Fonts::impl::setCustomFontPath(fontFile);
// If a custom font has been loaded now, also load the font size // If a custom font has been loaded now, also load the font size
float fontSize = defaultFontSize; float fontSize = defaultFontSize;
@ -159,12 +159,12 @@ namespace hex::plugin::builtin {
fontSize = float(ContentRegistry::Settings::read("hex.builtin.setting.font", "hex.builtin.setting.font.font_size", 13).get<int>()) * ImHexApi::System::getGlobalScale(); fontSize = float(ContentRegistry::Settings::read("hex.builtin.setting.font", "hex.builtin.setting.font.font_size", 13).get<int>()) * ImHexApi::System::getGlobalScale();
} }
ImHexApi::System::impl::setFontSize(fontSize); ImHexApi::Fonts::impl::setFontSize(fontSize);
} }
float fontSize = ImHexApi::System::getFontSize(); float fontSize = ImHexApi::Fonts::getFontSize();
const auto &fontFile = ImHexApi::System::getCustomFontPath(); const auto &fontFile = ImHexApi::Fonts::getCustomFontPath();
// Setup basic font configuration // Setup basic font configuration
auto fonts = IM_NEW(ImFontAtlas)(); auto fonts = IM_NEW(ImFontAtlas)();
@ -206,32 +206,43 @@ namespace hex::plugin::builtin {
glyphRangesBuilder.BuildRanges(&defaultGlyphRanges); glyphRangesBuilder.BuildRanges(&defaultGlyphRanges);
} }
if (fontFile.empty())
fonts->Clear();
if (ContentRegistry::Settings::read("hex.builtin.setting.font", "hex.builtin.setting.font.font_bold", false))
cfg.FontBuilderFlags |= ImGuiFreeTypeBuilderFlags_Bold;
if (ContentRegistry::Settings::read("hex.builtin.setting.font", "hex.builtin.setting.font.font_italic", false))
cfg.FontBuilderFlags |= ImGuiFreeTypeBuilderFlags_Oblique;
if (!ContentRegistry::Settings::read("hex.builtin.setting.font", "hex.builtin.setting.font.font_antialias", false))
cfg.FontBuilderFlags |= ImGuiFreeTypeBuilderFlags_Monochrome | ImGuiFreeTypeBuilderFlags_MonoHinting;
auto loadDefaultFont = [&](const char *fontName, u32 flags = 0) {
ImFontConfig defaultConfig = cfg;
ImFont *defaultFont;
defaultConfig.FontBuilderFlags |= flags;
std::strncpy(defaultConfig.Name, fontName, sizeof(defaultConfig.Name) - 1);
if (fontFile.empty()) {
defaultConfig.FontBuilderFlags |= ImGuiFreeTypeBuilderFlags_Monochrome | ImGuiFreeTypeBuilderFlags_MonoHinting;
defaultFont = fonts->AddFontDefault(&defaultConfig);
} else {
defaultFont = fonts->AddFontFromFileTTF(wolv::util::toUTF8String(fontFile).c_str(), 0, &defaultConfig, defaultGlyphRanges.Data);
}
return defaultFont;
};
// Load main font // Load main font
// If a custom font has been specified, load it, otherwise load the default ImGui font // If a custom font has been specified, load it, otherwise load the default ImGui font
ImFont *defaultFont; ImFont *defaultFont = loadDefaultFont("Default Font");
if (fontFile.empty()) { if (defaultFont == nullptr) {
fonts->Clear(); log::warn("Failed to load custom font! Falling back to default font.");
ImHexApi::Fonts::impl::setFontSize(defaultFontSize);
cfg.SizePixels = defaultFontSize;
defaultFont = 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;
if (ContentRegistry::Settings::read("hex.builtin.setting.font", "hex.builtin.setting.font.font_italic", false))
cfg.FontBuilderFlags |= ImGuiFreeTypeBuilderFlags_Oblique;
if (!ContentRegistry::Settings::read("hex.builtin.setting.font", "hex.builtin.setting.font.font_antialias", false))
cfg.FontBuilderFlags |= ImGuiFreeTypeBuilderFlags_Monochrome | ImGuiFreeTypeBuilderFlags_MonoHinting;
defaultFont = fonts->AddFontFromFileTTF(wolv::util::toUTF8String(fontFile).c_str(), 0, &cfg, defaultGlyphRanges.Data);
cfg.FontBuilderFlags = 0;
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();
defaultFont = fonts->AddFontDefault(&cfg);
}
} }
fonts->Build(); fonts->Build();
@ -287,6 +298,12 @@ namespace hex::plugin::builtin {
fonts->AddFontFromMemoryTTF(font.fontData.data(), int(font.fontData.size()), 0, &cfg, ranges.back().Data); fonts->AddFontFromMemoryTTF(font.fontData.data(), int(font.fontData.size()), 0, &cfg, ranges.back().Data);
} }
// 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 // Try to build the font atlas
if (!fonts->Build()) { if (!fonts->Build()) {
// The main reason the font atlas failed to build is that the font is too big for the GPU to handle // The main reason the font atlas failed to build is that the font is too big for the GPU to handle
@ -310,7 +327,7 @@ namespace hex::plugin::builtin {
} }
// Configure ImGui to use the font atlas // Configure ImGui to use the font atlas
ImHexApi::System::impl::setFontAtlas(fonts); ImHexApi::Fonts::impl::setFontAtlas(fonts);
return true; return true;
} }

View File

@ -626,7 +626,7 @@ namespace hex::plugin::builtin {
auto lastMonthDay = std::chrono::year_month_day_last(date.year(), date.month() / std::chrono::last); auto lastMonthDay = std::chrono::year_month_day_last(date.year(), date.month() / std::chrono::last);
auto firstWeekDay = std::chrono::weekday(std::chrono::year_month_day(date.year(), date.month(), std::chrono::day(1))); auto firstWeekDay = std::chrono::weekday(std::chrono::year_month_day(date.year(), date.month(), std::chrono::day(1)));
const auto scale = 1_scaled * (ImHexApi::System::getFontSize() / ImHexApi::System::DefaultFontSize); const auto scale = 1_scaled * (ImHexApi::Fonts::getFontSize() / ImHexApi::Fonts::DefaultFontSize);
// Draw calendar // Draw calendar
if (ImGui::BeginTable("##month_table", 2)) { if (ImGui::BeginTable("##month_table", 2)) {