diff options
author | dec05eba <dec05eba@protonmail.com> | 2021-09-03 01:48:00 +0200 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2021-09-03 01:48:00 +0200 |
commit | f22a3b83c053167511b576884355d667cf39d467 (patch) | |
tree | 580aaa7e399c281f49d06c5510116dcef04bbfab /src | |
parent | dc5813f7ce0d5a36dbd4470f213637bfebcdd68f (diff) |
Add QM_USE_SYSTEM_FONTS environment variable to use system fonts instead of noto fonts
Diffstat (limited to 'src')
-rw-r--r-- | src/QuickMedia.cpp | 6 | ||||
-rw-r--r-- | src/ResourceLoader.cpp | 59 |
2 files changed, 56 insertions, 9 deletions
diff --git a/src/QuickMedia.cpp b/src/QuickMedia.cpp index 8ef16c4..fe3e53c 100644 --- a/src/QuickMedia.cpp +++ b/src/QuickMedia.cpp @@ -603,6 +603,11 @@ namespace QuickMedia { return focused_monitor_center; } + static bool config_use_system_fonts() { + char *qm_use_system_fonts = getenv("QM_USE_SYSTEM_FONTS"); + return qm_use_system_fonts && qm_use_system_fonts[0] == '1'; + } + void Program::init(Window parent_window, std::string &program_path) { disp = XOpenDisplay(NULL); if (!disp) { @@ -668,6 +673,7 @@ namespace QuickMedia { } set_resource_loader_root_path(resources_root.c_str()); + set_use_system_fonts(config_use_system_fonts()); init_themes(); if(!is_touch_enabled()) { 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"; |