diff options
author | dec05eba <dec05eba@protonmail.com> | 2022-11-03 22:49:56 +0100 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2022-11-03 22:49:56 +0100 |
commit | 23edd92d4a4ba8abe700a0b0edd6cfea19fbef14 (patch) | |
tree | b296ea4e93895f4c6ff43c33d526bd8d7e781a60 | |
parent | 089818f9078c53de7ff9e6596eb7eb82cc8d6727 (diff) |
Config: add option to set custom fonts without using system config (fontconfig)
m--------- | depends/mglpp | 0 | ||||
-rw-r--r-- | example-config.json | 6 | ||||
-rw-r--r-- | include/Config.hpp | 16 | ||||
-rw-r--r-- | include/ResourceLoader.hpp | 1 | ||||
-rw-r--r-- | src/Config.cpp | 8 | ||||
-rw-r--r-- | src/QuickMedia.cpp | 1 | ||||
-rw-r--r-- | src/ResourceLoader.cpp | 73 |
7 files changed, 78 insertions, 27 deletions
diff --git a/depends/mglpp b/depends/mglpp -Subproject 792dfdf5d1c7bc5a96e6d9ac540a503cb9f51ff +Subproject a93ab69dc85a1e4898ccedcad1e59da0986acaa 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<std::string> known_homeservers; }; - struct Peertube { + struct PeertubeConfig { std::vector<std::string> 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 <mglpp/graphics/Font.hpp> #include <mglpp/graphics/Texture.hpp> #include <mglpp/system/MemoryMappedFile.hpp> @@ -15,7 +16,6 @@ static std::array<std::unique_ptr<mgl::MemoryMappedFile>, 4> font_file_cache; // font_cache[(unsigned int)font_type][character_size] static std::array<std::unordered_map<unsigned int, std::unique_ptr<mgl::Font>>, 4> font_cache; static std::unordered_map<std::string, std::unique_ptr<mgl::Texture>> 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<std::string> noto_directories; + std::vector<std::string> 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; } } |