Added Noto Sans as default font.

This commit is contained in:
Patrick 2025-03-02 21:22:38 +01:00
parent cd40148961
commit ce328ac761
4 changed files with 45 additions and 2 deletions

1
.gitattributes vendored Normal file
View File

@ -0,0 +1 @@
*.ttf filter=lfs diff=lfs merge=lfs -text

BIN
data/data/fonts/NotoSans-Regular.ttf (Stored with Git LFS) Normal file

Binary file not shown.

View File

@ -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<mijin::Stream> 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<int>(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;
}

View File

@ -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;