feat: Added more granular font settings

Fixes #1260
This commit is contained in:
WerWolv
2025-01-18 23:34:43 +01:00
parent 3129d6e8fd
commit 117eb1e2a7
37 changed files with 878 additions and 590 deletions

View File

@@ -1,326 +1,66 @@
#include <imgui.h>
#include <imgui_internal.h>
#include <imgui_freetype.h>
#include <imgui_impl_opengl3.h>
#include <memory>
#include <list>
#include <hex/api/imhex_api.hpp>
#include <hex/api/content_registry.hpp>
#include <hex/api/event_manager.hpp>
#include <hex/api/task_manager.hpp>
#include <hex/helpers/auto_reset.hpp>
#include <hex/helpers/logger.hpp>
#include <hex/helpers/fs.hpp>
#include <hex/helpers/utils.hpp>
#include <hex/helpers/default_paths.hpp>
#include <romfs/romfs.hpp>
#include <wolv/io/file.hpp>
#include <wolv/utils/string.hpp>
#include <font_atlas.hpp>
namespace hex::fonts {
namespace {
class Font {
public:
Font() = default;
float getDescent() const {
return m_font->Descent;
}
private:
explicit Font(ImFont *font) : m_font(font) { }
private:
friend class FontAtlas;
ImFont *m_font;
};
class FontAtlas {
public:
FontAtlas() : m_fontAtlas(IM_NEW(ImFontAtlas)) {
enableUnicodeCharacters(false);
// Set the default configuration for the font atlas
m_config.OversampleH = m_config.OversampleV = 1;
m_config.PixelSnapH = true;
m_config.MergeMode = false;
// Make sure the font atlas doesn't get too large, otherwise weaker GPUs might reject it
m_fontAtlas->Flags |= ImFontAtlasFlags_NoPowerOfTwoHeight;
m_fontAtlas->TexDesiredWidth = 4096;
}
~FontAtlas() {
IM_DELETE(m_fontAtlas);
}
Font addDefaultFont() {
ImFontConfig config = m_config;
config.FontBuilderFlags |= ImGuiFreeTypeBuilderFlags_Monochrome | ImGuiFreeTypeBuilderFlags_MonoHinting;
config.SizePixels = std::floor(getAdjustedFontSize(ImHexApi::System::getGlobalScale() * 13.0F));
auto font = m_fontAtlas->AddFontDefault(&config);
m_fontSizes.emplace_back(false, config.SizePixels);
m_config.MergeMode = true;
return Font(font);
}
Font addFontFromMemory(const std::vector<u8> &fontData, float fontSize, bool scalable, ImVec2 offset, const ImVector<ImWchar> &glyphRange = {}) {
auto &storedFontData = m_fontData.emplace_back(fontData);
ImFontConfig config = m_config;
config.FontDataOwnedByAtlas = false;
config.GlyphOffset = { offset.x, offset.y };
auto font = m_fontAtlas->AddFontFromMemoryTTF(storedFontData.data(), int(storedFontData.size()), getAdjustedFontSize(fontSize), &config, !glyphRange.empty() ? glyphRange.Data : m_glyphRange.Data);
m_fontSizes.emplace_back(scalable, fontSize);
m_config.MergeMode = true;
return Font(font);
}
Font addFontFromRomFs(const std::fs::path &path, float fontSize, bool scalable, ImVec2 offset, const ImVector<ImWchar> &glyphRange = {}) {
auto data = romfs::get(path).span<u8>();
return addFontFromMemory({ data.begin(), data.end() }, fontSize, scalable, offset, glyphRange);
}
Font addFontFromFile(const std::fs::path &path, float fontSize, bool scalable, ImVec2 offset, const ImVector<ImWchar> &glyphRange = {}) {
wolv::io::File file(path, wolv::io::File::Mode::Read);
auto data = file.readVector();
return addFontFromMemory(data, fontSize, scalable, offset, glyphRange);
}
void setBold(bool enabled) {
if (enabled)
m_config.FontBuilderFlags |= ImGuiFreeTypeBuilderFlags_Bold;
else
m_config.FontBuilderFlags &= ~ImGuiFreeTypeBuilderFlags_Bold;
}
void setItalic(bool enabled) {
if (enabled)
m_config.FontBuilderFlags |= ImGuiFreeTypeBuilderFlags_Oblique;
else
m_config.FontBuilderFlags &= ~ImGuiFreeTypeBuilderFlags_Oblique;
}
void setAntiAliasing(bool enabled) {
if (enabled)
m_config.FontBuilderFlags &= ~ImGuiFreeTypeBuilderFlags_Monochrome | ImGuiFreeTypeBuilderFlags_MonoHinting;
else
m_config.FontBuilderFlags |= ImGuiFreeTypeBuilderFlags_Monochrome | ImGuiFreeTypeBuilderFlags_MonoHinting;
}
void enableUnicodeCharacters(bool enabled) {
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(m_fontAtlas->GetGlyphRangesDefault());
glyphRangesBuilder.AddRanges(extendedAsciiRange.data());
glyphRangesBuilder.AddRanges(latinExtendedARange.data());
}
if (enabled) {
constexpr static std::array<ImWchar, 3> fullRange = { 0x0180, 0xFFEF, 0 };
glyphRangesBuilder.AddRanges(fullRange.data());
} else {
glyphRangesBuilder.AddRanges(m_fontAtlas->GetGlyphRangesJapanese());
glyphRangesBuilder.AddRanges(m_fontAtlas->GetGlyphRangesChineseFull());
glyphRangesBuilder.AddRanges(m_fontAtlas->GetGlyphRangesCyrillic());
glyphRangesBuilder.AddRanges(m_fontAtlas->GetGlyphRangesKorean());
glyphRangesBuilder.AddRanges(m_fontAtlas->GetGlyphRangesThai());
glyphRangesBuilder.AddRanges(m_fontAtlas->GetGlyphRangesVietnamese());
glyphRangesBuilder.AddText("⌘⌥⌃⇧⏎⇥⌫⇪");
}
m_glyphRange.clear();
glyphRangesBuilder.BuildRanges(&m_glyphRange);
}
bool build() const {
return m_fontAtlas->Build();
}
[[nodiscard]] ImFontAtlas* getAtlas() {
auto result = m_fontAtlas;
return result;
}
float calculateFontDescend(const ImHexApi::Fonts::Font &font, float fontSize) const {
auto atlas = std::make_unique<ImFontAtlas>();
auto cfg = m_config;
// Calculate the expected font size
auto size = fontSize;
if (font.defaultSize.has_value())
size = font.defaultSize.value() * std::max(1.0F, std::floor(ImHexApi::Fonts::getFontSize() / ImHexApi::Fonts::DefaultFontSize));
else
size = std::max(1.0F, std::floor(size / ImHexApi::Fonts::DefaultFontSize)) * ImHexApi::Fonts::DefaultFontSize;
cfg.MergeMode = false;
cfg.SizePixels = size;
cfg.FontDataOwnedByAtlas = false;
// Construct a range that only contains the first glyph of the font
ImVector<ImWchar> queryRange;
{
auto firstGlyph = font.glyphRanges.empty() ? m_glyphRange.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;
}
void reset() {
IM_DELETE(m_fontAtlas);
m_fontAtlas = IM_NEW(ImFontAtlas);
m_fontData.clear();
m_config.MergeMode = false;
}
void updateFontScaling(float newScaling) {
for (int i = 0; i < m_fontAtlas->ConfigData.size(); i += 1) {
const auto &[scalable, fontSize] = m_fontSizes[i];
auto &configData = m_fontAtlas->ConfigData[i];
if (!scalable) {
configData.SizePixels = fontSize * std::floor(newScaling);
} else {
configData.SizePixels = fontSize * newScaling;
}
}
}
private:
float getAdjustedFontSize(float fontSize) const {
// Since macOS reports half the framebuffer size that's actually available,
// we'll multiply all font sizes by that and then divide the global font scale
// by the same amount to get super crisp font rendering.
return fontSize * hex::ImHexApi::System::getBackingScaleFactor();
}
private:
ImFontAtlas* m_fontAtlas;
std::vector<std::pair<bool, float>> m_fontSizes;
ImFontConfig m_config;
ImVector<ImWchar> m_glyphRange;
std::list<std::vector<u8>> m_fontData;
};
std::fs::path findCustomFontPath() {
// Find the custom font file specified in the settings
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 : paths::Resources.read()) {
auto path = dir / "font.ttf";
if (wolv::io::fs::exists(path)) {
log::info("Loading custom font from {}", wolv::util::toUTF8String(path));
fontFile = path;
break;
}
}
}
return fontFile;
bool buildFontAtlas(FontAtlas *fontAtlas, std::fs::path fontPath, bool pixelPerfectFont, float fontSize, bool loadUnicodeCharacters, bool bold, bool italic, bool antialias) {
if (fontAtlas == nullptr) {
return false;
}
float getFontSize() {
const auto pixelPerfectFont = ContentRegistry::Settings::read<bool>("hex.builtin.setting.font", "hex.builtin.setting.font.pixel_perfect_default_font", true);
if (pixelPerfectFont)
return 13.0F * ImHexApi::System::getGlobalScale();
else
return float(ContentRegistry::Settings::read<int>("hex.builtin.setting.font", "hex.builtin.setting.font.font_size", 13)) * ImHexApi::System::getGlobalScale();
}
}
bool buildFontAtlasImpl(bool loadUnicodeCharacters) {
static FontAtlas fontAtlas;
fontAtlas.reset();
fontAtlas->reset();
// 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) &&
ContentRegistry::Settings::read<bool>("hex.fonts.setting.font", "hex.builtin.fonts.font.load_all_unicode_chars", false) &&
ImHexApi::System::getGPUVendor() != "VMware, Inc.";
if (!loadUnicodeCharacters)
shouldLoadUnicode = false;
fontAtlas.enableUnicodeCharacters(shouldLoadUnicode);
fontAtlas->enableUnicodeCharacters(shouldLoadUnicode);
auto pixelPerfectFont = ContentRegistry::Settings::read<bool>("hex.builtin.setting.font", "hex.builtin.setting.font.pixel_perfect_default_font", true);
std::fs::path customFontPath;
// If a custom font is set in the settings, load the rest of the settings as well
if (!pixelPerfectFont) {
fontAtlas.setBold(ContentRegistry::Settings::read<bool>("hex.builtin.setting.font", "hex.builtin.setting.font.font_bold", false));
fontAtlas.setItalic(ContentRegistry::Settings::read<bool>("hex.builtin.setting.font", "hex.builtin.setting.font.font_italic", false));
fontAtlas.setAntiAliasing(ContentRegistry::Settings::read<bool>("hex.builtin.setting.font", "hex.builtin.setting.font.font_antialias", true));
customFontPath = findCustomFontPath();
fontAtlas->setBold(bold);
fontAtlas->setItalic(italic);
fontAtlas->setAntiAliasing(antialias);
} else {
fontPath.clear();
}
ImHexApi::Fonts::impl::setFontSize(getFontSize());
const auto fontSize = ImHexApi::Fonts::getFontSize();
// Try to load the custom font if one was set
std::optional<Font> defaultFont;
if (!customFontPath.empty()) {
defaultFont = fontAtlas.addFontFromFile(customFontPath, fontSize, true, ImVec2());
if (!fontAtlas.build()) {
log::error("Failed to load custom font '{}'! Falling back to default font", wolv::util::toUTF8String(customFontPath));
if (!fontPath.empty()) {
defaultFont = fontAtlas->addFontFromFile(fontPath, fontSize, true, ImVec2());
if (!fontAtlas->build()) {
log::error("Failed to load custom font '{}'! Falling back to default font", wolv::util::toUTF8String(fontPath));
defaultFont.reset();
}
}
// If there's no custom font set, or it failed to load, fall back to the default font
if (!defaultFont.has_value()) {
if (pixelPerfectFont)
defaultFont = fontAtlas.addDefaultFont();
else
defaultFont = fontAtlas.addFontFromRomFs("fonts/JetBrainsMono.ttf", fontSize, true, ImVec2());
if (pixelPerfectFont) {
defaultFont = fontAtlas->addDefaultFont();
fontSize = std::floor(fontAtlas->getAdjustedFontSize(ImHexApi::System::getGlobalScale() * 13.0F));
} else
defaultFont = fontAtlas->addFontFromRomFs("fonts/JetBrainsMono.ttf", fontSize, true, ImVec2());
if (!fontAtlas.build()) {
if (!fontAtlas->build()) {
log::fatal("Failed to load default font!");
return false;
}
@@ -345,48 +85,25 @@ namespace hex::fonts {
glyphRanges.push_back(glyphRange);
// Calculate the glyph offset for the font
ImVec2 offset = { font.offset.x, font.offset.y - (defaultFont->getDescent() - fontAtlas.calculateFontDescend(font, fontSize)) };
const ImVec2 offset = { font.offset.x, font.offset.y - (defaultFont->getDescent() - fontAtlas->calculateFontDescend(font, fontSize)) };
// Load the font
fontAtlas.addFontFromMemory(font.fontData, font.defaultSize.value_or(fontSize), !font.defaultSize.has_value(), offset, glyphRanges.back());
fontAtlas->addFontFromMemory(font.fontData, font.defaultSize.value_or(fontSize), !font.defaultSize.has_value(), offset, glyphRanges.back());
}
}
EventDPIChanged::subscribe([](float, float newScaling) {
fontAtlas.updateFontScaling(newScaling);
if (fontAtlas.build()) {
ImGui_ImplOpenGL3_DestroyFontsTexture();
ImGui_ImplOpenGL3_CreateFontsTexture();
ImHexApi::Fonts::impl::setFontAtlas(fontAtlas.getAtlas());
}
});
// Build the font atlas
const bool result = fontAtlas.build();
if (result) {
// Set the font atlas if the build was successful
ImHexApi::Fonts::impl::setFontAtlas(fontAtlas.getAtlas());
if (fontAtlas->build()) {
return true;
}
// If the build wasn't successful and Unicode characters are enabled, try again without them
// If they were disabled already, something went wrong, and we can't recover from it
if (!shouldLoadUnicode) {
// Reset Unicode loading and scaling factor settings back to default to make sure the user can still use the application
ContentRegistry::Settings::write<bool>("hex.builtin.setting.font", "hex.builtin.setting.font.load_all_unicode_chars", false);
ContentRegistry::Settings::write<float>("hex.builtin.setting.interface", "hex.builtin.setting.interface.scaling_factor", 1.0F);
ImHexApi::System::impl::setGlobalScale(1.0F);
return false;
} else {
return buildFontAtlasImpl(false);
return buildFontAtlas(fontAtlas, fontPath, pixelPerfectFont, fontSize, false, bold, italic, antialias);
}
}
bool buildFontAtlas() {
return buildFontAtlasImpl(true);
}
}

View File

@@ -0,0 +1,206 @@
#include <font_settings.hpp>
#include <hex/api/content_registry.hpp>
#include <wolv/utils/string.hpp>
#include <hex/helpers/utils.hpp>
#include <imgui.h>
namespace hex::fonts {
constexpr static auto PixelPerfectName = "Pixel-Perfect Default Font (Proggy Clean)";
constexpr static auto SmoothName = "Smooth Default Font (JetbrainsMono)";
constexpr static auto CustomName = "Custom Font";
bool FontFilePicker::draw(const std::string &name) {
bool changed = false;
const bool pixelPerfectFont = isPixelPerfectFontSelected();
bool customFont = updateSelectedFontName();
if (ImGui::BeginCombo(name.c_str(), m_selectedFontName.c_str())) {
if (ImGui::Selectable(PixelPerfectName, m_path.empty() && pixelPerfectFont)) {
m_path.clear();
m_pixelPerfectFont = true;
changed = true;
}
if (ImGui::Selectable(SmoothName, m_path.empty() && !pixelPerfectFont)) {
m_path.clear();
m_pixelPerfectFont = false;
changed = true;
}
if (ImGui::Selectable(CustomName, customFont)) {
changed = fs::openFileBrowser(fs::DialogMode::Open, { { "TTF Font", "ttf" }, { "OTF Font", "otf" } }, [this](const std::fs::path &path) {
m_path = path;
m_pixelPerfectFont = false;
});
}
for (const auto &[path, fontName] : hex::getFonts()) {
if (ImGui::Selectable(limitStringLength(fontName, 50).c_str(), m_path == path)) {
m_path = path;
m_pixelPerfectFont = false;
changed = true;
}
ImGui::SetItemTooltip("%s", fontName.c_str());
}
ImGui::EndCombo();
}
return changed;
}
bool FontFilePicker::isPixelPerfectFontSelected() const {
return m_pixelPerfectFont;
}
const std::string& FontFilePicker::getSelectedFontName() const {
return m_selectedFontName;
}
void FontFilePicker::load(const nlohmann::json& data) {
FilePicker::load(data["path"]);
m_pixelPerfectFont = data["pixel_perfect_font"];
updateSelectedFontName();
}
nlohmann::json FontFilePicker::store() {
nlohmann::json data = nlohmann::json::object();
data["path"] = FilePicker::store();
data["pixel_perfect_font"] = m_pixelPerfectFont;
return data;
}
bool FontFilePicker::updateSelectedFontName() {
const auto &fonts = hex::getFonts();
bool customFont = false;
const bool pixelPerfectFont = isPixelPerfectFontSelected();
if (m_path.empty() && pixelPerfectFont) {
m_selectedFontName = PixelPerfectName;
} else if (m_path.empty() && !pixelPerfectFont) {
m_selectedFontName = SmoothName;
} else if (fonts.contains(m_path)) {
m_selectedFontName = fonts.at(m_path);
} else {
m_selectedFontName = wolv::util::toUTF8String(m_path.filename());
customFont = true;
}
return customFont;
}
static float pixelsToPoints(float pixels) {
return pixels * (72_scaled / 96.0F);
}
static float pointsToPixels(float points) {
return points / (72_scaled / 96.0F);
}
bool SliderPoints::draw(const std::string &name) {
float value = pixelsToPoints(m_value);
float min = pixelsToPoints(m_min);
float max = pixelsToPoints(m_max);
auto changed = ImGui::SliderFloat(name.c_str(), &value, min, max, "%.0f pt");
m_value = pointsToPixels(value);
return changed;
}
bool FontSelector::draw(const std::string &name) {
ImGui::PushID(name.c_str());
ON_SCOPE_EXIT { ImGui::PopID(); };
if (ImGui::Button(m_fontFilePicker.getSelectedFontName().c_str(), ImVec2(300_scaled, 0))) {
ImGui::OpenPopup("Fonts");
}
ImGui::SameLine();
ImGui::TextUnformatted(name.c_str());
ImGui::SetNextWindowPos(ImGui::GetCursorScreenPos());
return drawPopup();
}
nlohmann::json FontSelector::store() {
nlohmann::json json = nlohmann::json::object();
json["font_file"] = m_fontFilePicker.store();
json["font_size"] = m_fontSize.store();
json["bold"] = m_bold.store();
json["italic"] = m_italic.store();
json["antialiased"] = m_antiAliased.store();
return json;
}
void FontSelector::load(const nlohmann::json& data) {
m_fontFilePicker.load(data["font_file"]);
m_fontSize.load(data["font_size"]);
m_bold.load(data["bold"]);
m_italic.load(data["italic"]);
m_antiAliased.load(data["antialiased"]);
}
bool FontSelector::drawPopup() {
bool changed = false;
if (ImGui::BeginPopup("Fonts")) {
m_fontFilePicker.draw("hex.fonts.setting.font.custom_font"_lang);
ImGui::BeginDisabled(m_fontFilePicker.isPixelPerfectFontSelected());
{
m_fontSize.draw("hex.fonts.setting.font.font_size"_lang);
m_bold.draw("hex.fonts.setting.font.font_bold"_lang);
m_italic.draw("hex.fonts.setting.font.font_italic"_lang);
m_antiAliased.draw("hex.fonts.setting.font.font_antialias"_lang);
}
ImGui::EndDisabled();
ImGui::NewLine();
if (ImGui::Button("hex.ui.common.apply"_lang))
changed = true;
ImGui::EndPopup();
}
return changed;
}
[[nodiscard]] const std::fs::path& FontSelector::getFontPath() const {
return m_fontFilePicker.getPath();
}
[[nodiscard]] bool FontSelector::isPixelPerfectFont() const {
return m_fontFilePicker.isPixelPerfectFontSelected();
}
[[nodiscard]] float FontSelector::getFontSize() const {
return m_fontSize.getValue();
}
[[nodiscard]] bool FontSelector::isBold() const {
return m_bold.isChecked();
}
[[nodiscard]] bool FontSelector::isItalic() const {
return m_italic.isChecked();
}
[[nodiscard]] bool FontSelector::isAntiAliased() const {
return m_antiAliased.isChecked();
}
ContentRegistry::Settings::Widgets::Widget::Interface& addFontSettingsWidget(UnlocalizedString name) {
return ContentRegistry::Settings::add<FontSelector>("hex.fonts.setting.font", "hex.fonts.setting.font.custom_font", std::move(name));
}
}

View File

@@ -4,16 +4,109 @@
#include <hex/helpers/logger.hpp>
#include <romfs/romfs.hpp>
#include <font_atlas.hpp>
#include <font_settings.hpp>
#include <imgui_impl_opengl3.h>
#include <fonts/fonts.hpp>
namespace hex::fonts {
void registerFonts();
bool buildFontAtlas();
bool buildFontAtlas(FontAtlas *fontAtlas, std::fs::path fontPath, bool pixelPerfectFont, float fontSize, bool loadUnicodeCharacters, bool bold, bool italic, bool antialias);
static AutoReset<std::map<ImFont*, FontAtlas>> s_fontAtlases;
void loadFont(const ContentRegistry::Settings::Widgets::Widget &widget, const UnlocalizedString &name, ImFont **font) {
const auto &settings = static_cast<const FontSelector&>(widget);
FontAtlas atlas;
const bool atlasBuilt = buildFontAtlas(
&atlas,
settings.getFontPath(),
settings.isPixelPerfectFont(),
settings.getFontSize(),
true,
settings.isBold(),
settings.isItalic(),
settings.isAntiAliased()
);
if (!atlasBuilt) {
buildFontAtlas(
&atlas,
"",
false,
settings.getFontSize(),
false,
settings.isBold(),
settings.isItalic(),
settings.isAntiAliased()
);
log::error("Failed to load font {}! Reverting back to default font!", name.get());
}
if (*font != nullptr) {
EventDPIChanged::unsubscribe(*font);
}
*font = atlas.getAtlas()->Fonts[0];
s_fontAtlases->insert_or_assign(*font, std::move(atlas));
(*s_fontAtlases)[*font] = std::move(atlas);
EventDPIChanged::subscribe(*font, [&atlas, font](float, float newScaling) {
atlas.updateFontScaling(newScaling);
if (atlas.build()) {
auto &io = ImGui::GetIO();
auto prevFont = io.Fonts;
{
io.Fonts = atlas.getAtlas();
ImGui_ImplOpenGL3_DestroyFontsTexture();
ImGui_ImplOpenGL3_CreateFontsTexture();
io.Fonts = prevFont;
}
*font = atlas.getAtlas()->Fonts[0];
}
});
}
bool setupFonts() {
ContentRegistry::Settings::add<ContentRegistry::Settings::Widgets::Checkbox>("hex.fonts.setting.font", "hex.fonts.setting.font.glyphs", "hex.fonts.setting.font.load_all_unicode_chars", false).requiresRestart();
for (auto &[name, font] : ImHexApi::Fonts::impl::getFontDefinitions()) {
auto &widget = addFontSettingsWidget(name)
.setChangedCallback([name, &font, firstLoad = true](auto &widget) mutable {
if (firstLoad) {
firstLoad = false;
return;
}
loadFont(widget, name, &font);
});
loadFont(widget.getWidget(), name, &font);
}
return true;
}
}
IMHEX_LIBRARY_SETUP("Fonts") {
hex::log::debug("Using romfs: '{}'", romfs::name());
for (auto &path : romfs::list("lang"))
hex::ContentRegistry::Language::addLocalization(nlohmann::json::parse(romfs::get(path).string()));
hex::ImHexApi::System::addStartupTask("Loading fonts", true, hex::fonts::buildFontAtlas);
hex::ImHexApi::Fonts::registerFont("hex.fonts.font.default");
hex::ImHexApi::Fonts::registerFont("hex.fonts.font.hex_editor");
hex::ImHexApi::Fonts::registerFont("hex.fonts.font.code_editor");
hex::ImHexApi::System::addStartupTask("Loading fonts", true, hex::fonts::setupFonts);
hex::fonts::registerFonts();
}