From ce328ac76191886e279aa6a2e68d1cd37978e78d Mon Sep 17 00:00:00 2001 From: Patrick Wuttke Date: Sun, 2 Mar 2025 21:22:38 +0100 Subject: [PATCH] Added Noto Sans as default font. --- .gitattributes | 1 + data/data/fonts/NotoSans-Regular.ttf | 3 +++ private/raid/application.cpp | 39 +++++++++++++++++++++++++++- public/raid/raid.hpp | 4 ++- 4 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 .gitattributes create mode 100644 data/data/fonts/NotoSans-Regular.ttf diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..e4c28ea --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +*.ttf filter=lfs diff=lfs merge=lfs -text diff --git a/data/data/fonts/NotoSans-Regular.ttf b/data/data/fonts/NotoSans-Regular.ttf new file mode 100644 index 0000000..defecf9 --- /dev/null +++ b/data/data/fonts/NotoSans-Regular.ttf @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:3be6b371cef19ed6add589bd106444ab74c9793bc812d3159298b73d00ee011c +size 582748 diff --git a/private/raid/application.cpp b/private/raid/application.cpp index 964c13b..53069fb 100644 --- a/private/raid/application.cpp +++ b/private/raid/application.cpp @@ -161,6 +161,35 @@ int Application::run(int argc, char** argv) return 0; } +bool Application::loadFont(const fs::path& path) +{ + ImGuiIO& imguiIO = ImGui::GetIO(); + + std::unique_ptr fontFile; + if (const mijin::StreamError error = mFS.open(path, mijin::FileOpenMode::READ, fontFile); + error != mijin::StreamError::SUCCESS) + { + msgError("Error opening font file {}: {}.", path.generic_string(), mijin::errorName(error)); + return false; + } + + mijin::TypelessBuffer data; + if (const mijin::StreamError readError = fontFile->readRest(data); readError != mijin::StreamError::SUCCESS) + { + msgError("Error reading font data from {}: {}.", path.generic_string(), mijin::errorName(readError)); + return false; + } + + // by default ImGui takes ownership of the data, that's now what we want + ImFontConfig config; + config.FontDataOwnedByAtlas = false; + imguiIO.Fonts->AddFontFromMemoryTTF(data.data(), static_cast(data.byteSize()), 20, &config); + + // but in that case Build() has to be run before the data gets deleted + imguiIO.Fonts->Build(); + return true; +} + void Application::handleMessage(const Message& message) { switch (message.severity) @@ -271,7 +300,15 @@ bool Application::initImGui() } // init font - imguiIO.Fonts->AddFontDefault(); + if (imguiIO.Fonts->Fonts.empty()) + { + static const char* const DEFAULT_FONT = "/data/fonts/NotoSans-Regular.ttf"; + if (!loadFont(DEFAULT_FONT)) + { + imguiIO.Fonts->AddFontDefault(); + } + } + return true; } diff --git a/public/raid/raid.hpp b/public/raid/raid.hpp index bb53905..5920bf7 100644 --- a/public/raid/raid.hpp +++ b/public/raid/raid.hpp @@ -56,12 +56,14 @@ public: ImGuiWindowFlags getMainWindowFlags() const { return mMainWindowFlags; } void setMainWindowFlags(ImGuiWindowFlags flags) { mMainWindowFlags = flags; } - void requestQuit() { mRunning = false; } [[nodiscard]] int run(int argc, char* argv[]); + [[nodiscard]] + bool loadFont(const fs::path& path); + protected: virtual void render() = 0; virtual std::string getFolderName() = 0;