aboutsummaryrefslogtreecommitdiff
path: root/src/ResourceLoader.cpp
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2021-09-03 01:48:00 +0200
committerdec05eba <dec05eba@protonmail.com>2021-09-03 01:48:00 +0200
commitf22a3b83c053167511b576884355d667cf39d467 (patch)
tree580aaa7e399c281f49d06c5510116dcef04bbfab /src/ResourceLoader.cpp
parentdc5813f7ce0d5a36dbd4470f213637bfebcdd68f (diff)
Add QM_USE_SYSTEM_FONTS environment variable to use system fonts instead of noto fonts
Diffstat (limited to 'src/ResourceLoader.cpp')
-rw-r--r--src/ResourceLoader.cpp59
1 files changed, 50 insertions, 9 deletions
diff --git a/src/ResourceLoader.cpp b/src/ResourceLoader.cpp
index fbcbe39..2725898 100644
--- a/src/ResourceLoader.cpp
+++ b/src/ResourceLoader.cpp
@@ -1,4 +1,7 @@
#include "../include/ResourceLoader.hpp"
+#include "../include/Program.hpp"
+#include "../include/Path.hpp"
+#include "../include/StringUtils.hpp"
#include <SFML/Graphics/Font.hpp>
#include <SFML/Graphics/Texture.hpp>
#include <array>
@@ -9,6 +12,7 @@
static std::string resource_root;
static std::array<std::unique_ptr<sf::Font>, 4> font_cache;
static std::unordered_map<std::string, std::unique_ptr<sf::Texture>> texture_cache;
+static bool use_system_fonts = false;
namespace QuickMedia {
void set_resource_loader_root_path(const char *new_resource_root) {
@@ -18,9 +22,21 @@ namespace QuickMedia {
const char* get_resource_loader_root_path() {
return resource_root.c_str();
}
+
+ void set_use_system_fonts(bool use) {
+ use_system_fonts = use;
+ }
}
namespace QuickMedia::FontLoader {
+ static int accumulate_string(char *data, int size, void *userdata) {
+ std::string *str = (std::string*)userdata;
+ if(str->size() + size > 1024 * 1024 * 100) // 100mb sane limit, TODO: make configurable
+ return 1;
+ str->append(data, size);
+ return 0;
+ }
+
sf::Font* get_font(FontType font_type) {
sf::Font *font = font_cache[(size_t)font_type].get();
if(!font) {
@@ -29,24 +45,49 @@ namespace QuickMedia::FontLoader {
std::string font_file_name;
switch(font_type) {
case FontType::LATIN: {
- noto_directories.push_back("/usr/share/fonts/noto");
- noto_directories.push_back("/usr/share/fonts/truetype/noto");
- font_file_name = "NotoSans-Regular.ttf";
+ std::string output;
+ const char *args[] = { "fc-match", "sans:lang=en", "file", nullptr };
+ if(use_system_fonts && exec_program(args, accumulate_string, &output) == 0 && output.size() > 6) {
+ Path path = strip(output.substr(6));
+ noto_directories.push_back(path.parent().data);
+ font_file_name = path.filename();
+ } else {
+ noto_directories.push_back("/usr/share/fonts/noto");
+ noto_directories.push_back("/usr/share/fonts/truetype/noto");
+ font_file_name = "NotoSans-Regular.ttf";
+ }
break;
}
case FontType::LATIN_BOLD: {
- noto_directories.push_back("/usr/share/fonts/noto");
- noto_directories.push_back("/usr/share/fonts/truetype/noto");
- font_file_name = "NotoSans-Bold.ttf";
+ std::string output;
+ const char *args[] = { "fc-match", "sans:bold:lang=en", "file", nullptr };
+ if(use_system_fonts && exec_program(args, accumulate_string, &output) == 0 && output.size() > 6) {
+ Path path = strip(output.substr(6));
+ noto_directories.push_back(path.parent().data);
+ font_file_name = path.filename();
+ } else {
+ noto_directories.push_back("/usr/share/fonts/noto");
+ noto_directories.push_back("/usr/share/fonts/truetype/noto");
+ font_file_name = "NotoSans-Bold.ttf";
+ }
break;
}
case FontType::CJK: {
- noto_directories.push_back("/usr/share/fonts/noto-cjk");
- noto_directories.push_back("/usr/share/fonts/opentype/noto");
- font_file_name = "NotoSansCJK-Regular.ttc";
+ std::string output;
+ const char *args[] = { "fc-match", "sans:lang=ja", "file", nullptr };
+ if(use_system_fonts && exec_program(args, accumulate_string, &output) == 0 && output.size() > 6) {
+ Path path = strip(output.substr(6));
+ noto_directories.push_back(path.parent().data);
+ font_file_name = path.filename();
+ } else {
+ noto_directories.push_back("/usr/share/fonts/noto-cjk");
+ noto_directories.push_back("/usr/share/fonts/truetype/noto-cjk");
+ font_file_name = "NotoSansCJK-Regular.ttc";
+ }
break;
}
case FontType::SYMBOLS: {
+ // TODO: Allow changing with system font setting
noto_directories.push_back("/usr/share/fonts/noto");
noto_directories.push_back("/usr/share/fonts/truetype/noto");
font_file_name = "NotoSansSymbols2-Regular.ttf";