mirror of
https://github.com/WerWolv/ImHex.git
synced 2026-03-28 07:47:03 -05:00
feat: Added ability to draw Bold and Italic text
This commit is contained in:
@@ -759,17 +759,29 @@ EXPORT_MODULE namespace hex {
|
||||
explicit Font(UnlocalizedString fontName);
|
||||
|
||||
void push(float size = 0.0F) const;
|
||||
void pushBold(float size = 0.0F) const;
|
||||
void pushItalic(float size = 0.0F) const;
|
||||
|
||||
void pop() const;
|
||||
|
||||
[[nodiscard]] operator ImFont*() const;
|
||||
private:
|
||||
void push(float size, ImFont *font) const;
|
||||
|
||||
private:
|
||||
UnlocalizedString m_fontName;
|
||||
};
|
||||
|
||||
struct FontDefinition {
|
||||
ImFont* regular;
|
||||
ImFont* bold;
|
||||
ImFont* italic;
|
||||
};
|
||||
|
||||
namespace impl {
|
||||
|
||||
const std::vector<MergeFont>& getMergeFonts();
|
||||
std::map<UnlocalizedString, ImFont*>& getFontDefinitions();
|
||||
std::map<UnlocalizedString, FontDefinition>& getFontDefinitions();
|
||||
|
||||
}
|
||||
|
||||
@@ -777,7 +789,7 @@ EXPORT_MODULE namespace hex {
|
||||
void loadFont(const std::string &name, const std::span<const u8> &data, Offset offset = {}, std::optional<u32> defaultSize = std::nullopt);
|
||||
|
||||
void registerFont(const UnlocalizedString &fontName);
|
||||
ImFont* getFont(const UnlocalizedString &fontName);
|
||||
FontDefinition getFont(const UnlocalizedString &fontName);
|
||||
|
||||
void setDefaultFont(const Font& font);
|
||||
const Font& getDefaultFont();
|
||||
|
||||
@@ -1011,8 +1011,8 @@ namespace hex {
|
||||
return *s_fonts;
|
||||
}
|
||||
|
||||
static AutoReset<std::map<UnlocalizedString, ImFont*>> s_fontDefinitions;
|
||||
std::map<UnlocalizedString, ImFont*>& getFontDefinitions() {
|
||||
static AutoReset<std::map<UnlocalizedString, FontDefinition>> s_fontDefinitions;
|
||||
std::map<UnlocalizedString, FontDefinition>& getFontDefinitions() {
|
||||
return *s_fontDefinitions;
|
||||
}
|
||||
|
||||
@@ -1028,8 +1028,18 @@ namespace hex {
|
||||
}
|
||||
|
||||
void Font::push(float size) const {
|
||||
auto font = getFont(m_fontName);
|
||||
push(size, getFont(m_fontName).regular);
|
||||
}
|
||||
|
||||
void Font::pushBold(float size) const {
|
||||
push(size, getFont(m_fontName).bold);
|
||||
}
|
||||
|
||||
void Font::pushItalic(float size) const {
|
||||
push(size, getFont(m_fontName).italic);
|
||||
}
|
||||
|
||||
void Font::push(float size, ImFont* font) const {
|
||||
if (font != nullptr) {
|
||||
if (size <= 0.0F) {
|
||||
size = font->LegacySize;
|
||||
@@ -1044,12 +1054,13 @@ namespace hex {
|
||||
ImGui::PushFont(font, size);
|
||||
}
|
||||
|
||||
|
||||
void Font::pop() const {
|
||||
ImGui::PopFont();
|
||||
}
|
||||
|
||||
Font::operator ImFont*() const {
|
||||
return getFont(m_fontName);
|
||||
return getFont(m_fontName).regular;
|
||||
}
|
||||
|
||||
void loadFont(const std::fs::path &path, Offset offset, std::optional<u32> defaultSize) {
|
||||
@@ -1077,15 +1088,16 @@ namespace hex {
|
||||
}
|
||||
|
||||
void registerFont(const UnlocalizedString &fontName) {
|
||||
(*impl::s_fontDefinitions)[fontName] = nullptr;
|
||||
(*impl::s_fontDefinitions)[fontName] = {};
|
||||
}
|
||||
|
||||
ImFont* getFont(const UnlocalizedString &fontName) {
|
||||
FontDefinition getFont(const UnlocalizedString &fontName) {
|
||||
auto it = impl::s_fontDefinitions->find(fontName);
|
||||
|
||||
if (it == impl::s_fontDefinitions->end())
|
||||
return ImGui::GetDefaultFont();
|
||||
else
|
||||
if (it == impl::s_fontDefinitions->end()) {
|
||||
const auto defaultFont = ImGui::GetDefaultFont();
|
||||
return { defaultFont, defaultFont, defaultFont };
|
||||
} else
|
||||
return it->second;
|
||||
}
|
||||
|
||||
|
||||
@@ -10,19 +10,15 @@
|
||||
|
||||
namespace hex::fonts::loader {
|
||||
|
||||
void loadFont(const ContentRegistry::Settings::Widgets::Widget &widget, const UnlocalizedString &name, ImFont **imguiFont) {
|
||||
void loadFont(const ContentRegistry::Settings::Widgets::Widget &widget, const UnlocalizedString &name, ImGuiFreeTypeLoaderFlags extraFlags, ImFont **imguiFont) {
|
||||
const auto &settings = static_cast<const FontSelector&>(widget);
|
||||
|
||||
auto atlas = ImGui::GetIO().Fonts;
|
||||
const auto atlas = ImGui::GetIO().Fonts;
|
||||
|
||||
{
|
||||
auto &font = *imguiFont;
|
||||
if (auto &font = *imguiFont; font != nullptr) {
|
||||
atlas->RemoveFont(font);
|
||||
|
||||
if (font != nullptr) {
|
||||
atlas->RemoveFont(font);
|
||||
|
||||
font = nullptr;
|
||||
}
|
||||
font = nullptr;
|
||||
}
|
||||
|
||||
ImFontConfig config;
|
||||
@@ -47,13 +43,14 @@ namespace hex::fonts::loader {
|
||||
config.FontLoaderFlags |= ImGuiFreeTypeLoaderFlags_SubPixel;
|
||||
break;
|
||||
}
|
||||
|
||||
config.FontLoaderFlags |= extraFlags;
|
||||
} else {
|
||||
config.FontLoaderFlags |= ImGuiFreeTypeLoaderFlags_NoHinting;
|
||||
}
|
||||
|
||||
{
|
||||
const auto fontPath = settings.getFontPath();
|
||||
if (!fontPath.empty())
|
||||
if (const auto fontPath = settings.getFontPath(); !fontPath.empty())
|
||||
*imguiFont = atlas->AddFontFromFileTTF(fontPath.string().c_str(), 0.0F, &config);
|
||||
|
||||
if (*imguiFont == nullptr) {
|
||||
@@ -81,21 +78,27 @@ namespace hex::fonts::loader {
|
||||
}
|
||||
}
|
||||
|
||||
void loadFontVariations(const ContentRegistry::Settings::Widgets::Widget &widget, const UnlocalizedString &name, ImHexApi::Fonts::FontDefinition &fontDefinition) {
|
||||
loadFont(widget, name, 0, &fontDefinition.regular);
|
||||
loadFont(widget, name, ImGuiFreeTypeLoaderFlags_Bold, &fontDefinition.bold);
|
||||
loadFont(widget, name, ImGuiFreeTypeLoaderFlags_Oblique, &fontDefinition.italic);
|
||||
}
|
||||
|
||||
bool loadFonts() {
|
||||
for (auto &[name, font] : ImHexApi::Fonts::impl::getFontDefinitions()) {
|
||||
for (auto &[name, fontDefinition] : ImHexApi::Fonts::impl::getFontDefinitions()) {
|
||||
auto &widget = addFontSettingsWidget(name)
|
||||
.setChangedCallback([name, &font, firstLoad = true](auto &widget) mutable {
|
||||
.setChangedCallback([name, &fontDefinition, firstLoad = true](auto &widget) mutable {
|
||||
if (firstLoad) {
|
||||
firstLoad = false;
|
||||
return;
|
||||
}
|
||||
|
||||
TaskManager::doLater([name, &font, &widget] {
|
||||
loadFont(widget, name, &font);
|
||||
TaskManager::doLater([name, &fontDefinition, &widget] {
|
||||
loadFontVariations(widget, name, fontDefinition);
|
||||
});
|
||||
});
|
||||
|
||||
loadFont(widget.getWidget(), name, &font);
|
||||
loadFontVariations(widget.getWidget(), name, fontDefinition);
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
Reference in New Issue
Block a user