lookup linux font on runtime

This commit is contained in:
Love 2024-01-29 12:36:37 +01:00
parent 2a29f01fc2
commit 5f7b9aa116
3 changed files with 19 additions and 14 deletions

View File

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

View File

@ -3,30 +3,35 @@
//
#pragma once
#include <filesystem>
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
}

View File

@ -41,6 +41,7 @@ public:
*/
TextScreen(const std::string &text, SDL_Point *screenSize, std::optional<SDL_Point> 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);