From 5f7b9aa1166f91030d02b6bc076cdc28f069c66d Mon Sep 17 00:00:00 2001 From: Love Billenius Date: Mon, 29 Jan 2024 12:36:37 +0100 Subject: [PATCH] lookup linux font on runtime --- README.md | 5 ++--- src/defaultfont.h | 27 ++++++++++++++++----------- src/text/TextScreen.h | 1 + 3 files changed, 19 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 89d28d0..bb1f1a0 100644 --- a/README.md +++ b/README.md @@ -11,9 +11,8 @@ the following command. apt install cmake build-essential libsdl2-dev libsdl2-gfx-dev libsdl2-ttf-dev ``` -DejaVuSans is also expected to be in the same place as it is under compile time. You will need to make sure to compile -for the same distro that you're using, and also have the font installed. The package under debian is -called `fonts-dejavu-core` +DejaVuSans is also expected to be present at the default location under linux whilst Arial is under Windows and macOS. +The package under debian is called `fonts-dejavu-core` ## Compiling diff --git a/src/defaultfont.h b/src/defaultfont.h index 886d1b8..3180970 100644 --- a/src/defaultfont.h +++ b/src/defaultfont.h @@ -3,30 +3,35 @@ // #pragma once + #include -const char* getLinuxFilePath() { - const char *fonts[] = { - "/usr/share/fonts/truetype/DejaVuSans-Bold.ttf", // openSUSE - "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", // Debian - "/usr/share/fonts/TTF/DejaVuSans-Bold.ttf", // Arch - "/usr/share/fonts/dejavu-sans-fonts/DejaVuSans-Bold.ttf", // Fedora +const char *getLinuxFilePath() { + const char *fonts[] = {"/usr/share/fonts/truetype/DejaVuSans-Bold.ttf", // openSUSE + "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", // Debian + "/usr/share/fonts/TTF/DejaVuSans-Bold.ttf", // Arch + "/usr/share/fonts/dejavu-sans-fonts/DejaVuSans-Bold.ttf", // Fedora }; - for (const char *font : fonts) + for (const char *font: fonts) if (std::filesystem::exists(font)) return font; return nullptr; } +const char *getDefaultFontPath() { #if defined(_WIN32) || defined(_WIN64) -const char* defaultFontPath = "C:\\Windows\\Fonts\\Arial.ttf"; + return "C:\\Windows\\Fonts\\Arial.ttf"; #elif defined(__linux__) -const char *defaultFontPath = getLinuxFilePath(); + return getLinuxFilePath(); #elif defined(__APPLE__) || defined(__MACH__) -const char *defaultFontPath = "/System/Library/Fonts/Supplemental/Arial.ttf"; + return "/System/Library/Fonts/Supplemental/Arial.ttf"; #else -const char* defaultFontPath = nullptr; + return nullptr; #endif +} + + + diff --git a/src/text/TextScreen.h b/src/text/TextScreen.h index e0a84ad..289ec39 100644 --- a/src/text/TextScreen.h +++ b/src/text/TextScreen.h @@ -41,6 +41,7 @@ public: */ TextScreen(const std::string &text, SDL_Point *screenSize, std::optional basePosition) : hasUpdated( false), screenSize(screenSize), basePosition(basePosition) { + const char *defaultFontPath = getDefaultFontPath(); if (defaultFontPath == nullptr) { std::cerr << "Font path is not set for this platform (null)" << std::endl; exit(-1);