impr: Nicer font registering API

This commit is contained in:
WerWolv
2025-07-28 17:50:04 +02:00
parent a40c420b70
commit 46b13b6606
4 changed files with 35 additions and 25 deletions

View File

@@ -772,6 +772,8 @@ EXPORT_MODULE namespace hex {
void pop() const;
[[nodiscard]] operator ImFont*() const;
[[nodiscard]] UnlocalizedString getUnlocalizedName() const { return m_fontName; }
private:
void push(float size, ImFont *font) const;
@@ -792,10 +794,10 @@ EXPORT_MODULE namespace hex {
}
void loadFont(const std::fs::path &path, Offset offset = {}, std::optional<u32> defaultSize = std::nullopt);
void loadFont(const std::string &name, const std::span<const u8> &data, Offset offset = {}, std::optional<u32> defaultSize = std::nullopt);
void registerMergeFont(const std::fs::path &path, Offset offset = {}, std::optional<u32> defaultSize = std::nullopt);
void registerMergeFont(const std::string &name, const std::span<const u8> &data, Offset offset = {}, std::optional<u32> defaultSize = std::nullopt);
void registerFont(const UnlocalizedString &fontName);
void registerFont(const Font& font);
FontDefinition getFont(const UnlocalizedString &fontName);
void setDefaultFont(const Font& font);

View File

@@ -1032,8 +1032,6 @@ namespace hex {
}
Font::Font(UnlocalizedString fontName) : m_fontName(std::move(fontName)) {
Fonts::registerFont(m_fontName);
if (impl::s_defaultFont == nullptr)
impl::s_defaultFont = this;
}
@@ -1081,7 +1079,7 @@ namespace hex {
return getFont(m_fontName).regular;
}
void loadFont(const std::fs::path &path, Offset offset, std::optional<u32> defaultSize) {
void registerMergeFont(const std::fs::path &path, Offset offset, std::optional<u32> defaultSize) {
wolv::io::File fontFile(path, wolv::io::File::Mode::Read);
if (!fontFile.isValid()) {
log::error("Failed to load font from file '{}'", wolv::util::toUTF8String(path));
@@ -1096,7 +1094,7 @@ namespace hex {
});
}
void loadFont(const std::string &name, const std::span<const u8> &data, Offset offset, std::optional<u32> defaultSize) {
void registerMergeFont(const std::string &name, const std::span<const u8> &data, Offset offset, std::optional<u32> defaultSize) {
impl::s_fonts->emplace_back(MergeFont {
name,
{ data.begin(), data.end() },
@@ -1105,8 +1103,8 @@ namespace hex {
});
}
void registerFont(const UnlocalizedString &fontName) {
(*impl::s_fontDefinitions)[fontName] = {};
void registerFont(const Font& font) {
(*impl::s_fontDefinitions)[font.getUnlocalizedName()] = {};
}
FontDefinition getFont(const UnlocalizedString &fontName) {

View File

@@ -5,17 +5,29 @@
namespace hex::fonts {
static auto s_defaultFont = ImHexApi::Fonts::Font("hex.fonts.font.default");
const ImHexApi::Fonts::Font& Default() { return s_defaultFont; }
static auto s_hexEditorFont = ImHexApi::Fonts::Font("hex.fonts.font.hex_editor");
const ImHexApi::Fonts::Font& HexEditor() { return s_hexEditorFont; }
static auto s_codeEditorFont = ImHexApi::Fonts::Font("hex.fonts.font.code_editor");
const ImHexApi::Fonts::Font& CodeEditor() { return s_codeEditorFont; }
const ImHexApi::Fonts::Font& Default() {
static auto font = ImHexApi::Fonts::Font("hex.fonts.font.default");
return font;
}
const ImHexApi::Fonts::Font& HexEditor() {
static auto font = ImHexApi::Fonts::Font("hex.fonts.font.hex_editor");
return font;
}
const ImHexApi::Fonts::Font& CodeEditor() {
static auto font = ImHexApi::Fonts::Font("hex.fonts.font.code_editor");
return font;
}
void registerFonts() {
ImHexApi::Fonts::loadFont("Blender Icons", romfs::get("fonts/blendericons.ttf").span<u8>(), { -1, -1 });
ImHexApi::Fonts::loadFont("VS Codicons", romfs::get("fonts/codicons.ttf").span<u8>(), { 0, -2 });
ImHexApi::Fonts::loadFont("Unifont", romfs::get("fonts/unifont.otf").span<u8>(), { 0, 0 }, 10);
void registerUIFonts() {
ImHexApi::Fonts::registerFont(Default());
ImHexApi::Fonts::registerFont(HexEditor());
ImHexApi::Fonts::registerFont(CodeEditor());
}
void registerMergeFonts() {
ImHexApi::Fonts::registerMergeFont("Blender Icons", romfs::get("fonts/blendericons.ttf").span<u8>(), { -1, -1 });
ImHexApi::Fonts::registerMergeFont("VS Codicons", romfs::get("fonts/codicons.ttf").span<u8>(), { 0, -2 });
ImHexApi::Fonts::registerMergeFont("Unifont", romfs::get("fonts/unifont.otf").span<u8>(), { 0, 0 }, 10);
}
}

View File

@@ -12,7 +12,8 @@
namespace hex::fonts {
void registerFonts();
void registerUIFonts();
void registerMergeFonts();
namespace loader {
bool loadFonts();
@@ -26,11 +27,8 @@ IMHEX_LIBRARY_SETUP("Fonts") {
for (auto &path : romfs::list("lang"))
hex::ContentRegistry::Language::addLocalization(nlohmann::json::parse(romfs::get(path).string()));
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::fonts::registerFonts();
hex::fonts::registerUIFonts();
hex::fonts::registerMergeFonts();
hex::EventImHexStartupFinished::subscribe([] {
hex::fonts::loader::loadFonts();