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) --- depends/mglpp | 2 +- example-config.json | 6 ++++ include/Config.hpp | 16 +++++++--- include/ResourceLoader.hpp | 1 - src/Config.cpp | 8 +++++ src/QuickMedia.cpp | 1 - src/ResourceLoader.cpp | 73 +++++++++++++++++++++++++++++++++------------- 7 files changed, 79 insertions(+), 28 deletions(-) diff --git a/depends/mglpp b/depends/mglpp index 792dfdf..a93ab69 160000 --- a/depends/mglpp +++ b/depends/mglpp @@ -1 +1 @@ -Subproject commit 792dfdf5d1c7bc5a96e6d9ac540a503cb9f51ff6 +Subproject commit a93ab69dc85a1e4898ccedcad1e59da0986acaa5 diff --git a/example-config.json b/example-config.json index f217793..e6c2738 100644 --- a/example-config.json +++ b/example-config.json @@ -79,6 +79,12 @@ "music_directory": "~/Music", "file_directory": "~/Downloads" }, + "font": { + "latin": "", + "latin_bold": "", + "cjk": "", + "symbols": "" + }, "use_system_fonts": false, "use_system_mpv_config": false, "enable_shaders": true, diff --git a/include/Config.hpp b/include/Config.hpp index d7bfddf..bd80021 100644 --- a/include/Config.hpp +++ b/include/Config.hpp @@ -51,17 +51,24 @@ namespace QuickMedia { std::vector known_homeservers; }; - struct Peertube { + struct PeertubeConfig { std::vector known_instances; }; - struct Download { + struct DownloadConfig { std::string video_directory; std::string image_directory; std::string music_directory; std::string file_directory; }; + struct FontConfig { + std::string latin; + std::string latin_bold; + std::string cjk; + std::string symbols; + }; + struct Config { Config() = default; Config(const Config&) = delete; @@ -76,8 +83,9 @@ namespace QuickMedia { LocalAnimeConfig local_anime; YoutubeConfig youtube; MatrixConfig matrix; - Peertube peertube; - Download download; + PeertubeConfig peertube; + DownloadConfig download; + FontConfig font; bool use_system_fonts = false; bool use_system_mpv_config = false; bool enable_shaders = true; diff --git a/include/ResourceLoader.hpp b/include/ResourceLoader.hpp index f2b2888..c60f1f3 100644 --- a/include/ResourceLoader.hpp +++ b/include/ResourceLoader.hpp @@ -8,7 +8,6 @@ namespace mgl { namespace QuickMedia { void set_resource_loader_root_path(const char *resource_root); const char* get_resource_loader_root_path(); - void set_use_system_fonts(bool use); } namespace QuickMedia::FontLoader { 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