From 23edd92d4a4ba8abe700a0b0edd6cfea19fbef14 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Thu, 3 Nov 2022 22:49:56 +0100 Subject: Config: add option to set custom fonts without using system config (fontconfig) --- src/Config.cpp | 8 ++++++ src/QuickMedia.cpp | 1 - src/ResourceLoader.cpp | 73 +++++++++++++++++++++++++++++++++++--------------- 3 files changed, 60 insertions(+), 22 deletions(-) (limited to 'src') diff --git a/src/Config.cpp b/src/Config.cpp index 3f2d68c..c303448 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -295,6 +295,14 @@ namespace QuickMedia { } } + const Json::Value &font_json = json_root["font"]; + if(font_json.isObject()) { + get_json_value(font_json, "latin", config->font.latin); + get_json_value(font_json, "latin_bold", config->font.latin_bold); + get_json_value(font_json, "cjk", config->font.cjk); + get_json_value(font_json, "symbols", config->font.symbols); + } + get_json_value(json_root, "use_system_fonts", config->use_system_fonts); get_json_value(json_root, "use_system_mpv_config", config->use_system_mpv_config); get_json_value(json_root, "enable_shaders", config->enable_shaders); diff --git a/src/QuickMedia.cpp b/src/QuickMedia.cpp index 432dccb..856ff82 100644 --- a/src/QuickMedia.cpp +++ b/src/QuickMedia.cpp @@ -692,7 +692,6 @@ namespace QuickMedia { get_theme(); set_resource_loader_root_path(resources_root.c_str()); - set_use_system_fonts(get_config().use_system_fonts); init_body_themes(); set_window_icon(disp, window.get_system_handle(), resources_root + "icons/qm_logo.png"); diff --git a/src/ResourceLoader.cpp b/src/ResourceLoader.cpp index 9e8a884..50bb7b5 100644 --- a/src/ResourceLoader.cpp +++ b/src/ResourceLoader.cpp @@ -2,6 +2,7 @@ #include "../include/Program.hpp" #include "../include/Path.hpp" #include "../include/StringUtils.hpp" +#include "../include/Config.hpp" #include #include #include @@ -15,7 +16,6 @@ static std::array, 4> font_file_cache; // font_cache[(unsigned int)font_type][character_size] static std::array>, 4> font_cache; static std::unordered_map> texture_cache; -static bool use_system_fonts = false; namespace QuickMedia { void set_resource_loader_root_path(const char *new_resource_root) { @@ -25,10 +25,6 @@ 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 { @@ -46,56 +42,91 @@ namespace QuickMedia::FontLoader { mgl::MemoryMappedFile *mapped_file = font_file_cache[(unsigned int)font_type].get(); if(!mapped_file) { - std::vector noto_directories; + std::vector noto_directories = { + "/usr/share/fonts/noto", + "/usr/share/fonts/truetype/noto", + "/usr/share/fonts/noto-cjk", + "/usr/share/fonts/truetype/noto-cjk", + "/usr/share/fonts/TTF", + }; + std::string font_file_name; switch(font_type) { case FontType::LATIN: { 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) { + if(get_config().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"; + Path font_path = get_config().font.latin; + if(font_path.data.empty()) + font_path = "NotoSans-Regular.ttf"; + + if(font_path.data.find('/') == std::string::npos) { + font_file_name = std::move(font_path.data); + } else { + noto_directories.insert(noto_directories.begin(), font_path.parent().data); + font_file_name = font_path.filename(); + } } break; } case FontType::LATIN_BOLD: { 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) { + if(get_config().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"; + Path font_path = get_config().font.latin_bold; + if(font_path.data.empty()) + font_path = "NotoSans-Bold.ttf"; + + if(font_path.data.find('/') == std::string::npos) { + font_file_name = std::move(font_path.data); + } else { + noto_directories.insert(noto_directories.begin(), font_path.parent().data); + font_file_name = font_path.filename(); + } } break; } case FontType::CJK: { 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) { + if(get_config().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"; + Path font_path = get_config().font.cjk; + if(font_path.data.empty()) + font_path = "NotoSansCJK-Regular.ttc"; + + if(font_path.data.find('/') == std::string::npos) { + font_file_name = std::move(font_path.data); + } else { + noto_directories.insert(noto_directories.begin(), font_path.parent().data); + font_file_name = font_path.filename(); + } } 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"; + Path font_path = get_config().font.symbols; + if(font_path.data.empty()) + font_path = "NotoSansSymbols2-Regular.ttf"; + + if(font_path.data.find('/') == std::string::npos) { + font_file_name = std::move(font_path.data); + } else { + noto_directories.insert(noto_directories.begin(), font_path.parent().data); + font_file_name = font_path.filename(); + } break; } } -- cgit v1.2.3