From 4690ba0cc66338b1f00e08fb6054ee95c1c0dcc6 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Wed, 23 Sep 2020 23:45:21 +0200 Subject: Fallback to cjk font, change font to system noto sans --- src/QuickMedia.cpp | 95 ++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 63 insertions(+), 32 deletions(-) (limited to 'src/QuickMedia.cpp') diff --git a/src/QuickMedia.cpp b/src/QuickMedia.cpp index f6b9d61..295749f 100644 --- a/src/QuickMedia.cpp +++ b/src/QuickMedia.cpp @@ -168,16 +168,47 @@ namespace QuickMedia { resources_root = "../../../"; } - if(!font.loadFromFile(resources_root + "fonts/NotoSans-Regular.ttf")) { - fprintf(stderr, "Failed to load font: NotoSans-Regular.ttf\n"); + const std::string noto_sans_directories[] = { + "/usr/share/fonts/noto", "/usr/share/fonts/truetype/noto", + "/usr/share/fonts/noto-cjk", "/usr/share/fonts/truetype/noto-cjk" + }; + for(const std::string ¬o_sans_dir : noto_sans_directories) { + if(!font) { + auto new_font = std::make_unique(); + if(new_font->loadFromFile(noto_sans_dir + "/NotoSans-Regular.ttf")) + font = std::move(new_font); + } + + if(!bold_font) { + auto new_font = std::make_unique(); + if(new_font->loadFromFile(noto_sans_dir + "/NotoSans-Bold.ttf")) + bold_font = std::move(new_font); + } + + if(!cjk_font) { + auto new_font = std::make_unique(); + if(new_font->loadFromFile(noto_sans_dir + "/NotoSansCJK-Regular.ttc")) + cjk_font = std::move(new_font); + } + } + + if(!font) { + fprintf(stderr, "Failed to find NotoSans-Regular.ttf in /usr/share/fonts/noto and /usr/share/fonts/truetype/noto\n"); + abort(); + } + + if(!bold_font) { + fprintf(stderr, "Failed to find NotoSans-Bold.ttf in /usr/share/fonts/noto and /usr/share/fonts/truetype/noto\n"); abort(); } - if(!bold_font.loadFromFile(resources_root + "fonts/NotoSans-Bold.ttf")) { - fprintf(stderr, "Failed to load font: NotoSans-Bold.ttf\n"); + + if(!cjk_font) { + fprintf(stderr, "Failed to find NotoSansCJK-Regular.ttc in /usr/share/fonts/noto and /usr/share/fonts/truetype/noto\n"); abort(); } - body = new Body(this, &font, &bold_font); - related_media_body = new Body(this, &font, &bold_font); + + body = new Body(this, font.get(), bold_font.get(), cjk_font.get()); + related_media_body = new Body(this, font.get(), bold_font.get(), cjk_font.get()); related_media_body->draw_thumbnails = true; struct sigaction action; @@ -454,7 +485,7 @@ namespace QuickMedia { if(search_placeholder.empty()) search_placeholder = "Search..."; - search_bar = std::make_unique(font, &plugin_logo, search_placeholder); + search_bar = std::make_unique(*font, &plugin_logo, search_placeholder); search_bar->text_autosearch_delay = current_plugin->get_search_delay(); while(window.isOpen()) { @@ -926,16 +957,16 @@ namespace QuickMedia { std::string autocomplete_text; bool autocomplete_running = false; - Body history_body(this, &font, &bold_font); + Body history_body(this, font.get(), bold_font.get(), cjk_font.get()); std::unique_ptr recommended_body; - sf::Text all_tab_text("All", font, tab_text_size); - sf::Text history_tab_text("History", font, tab_text_size); - sf::Text recommended_tab_text("Recommended", font, tab_text_size); - sf::Text login_tab_text("Login", font, tab_text_size); + sf::Text all_tab_text("All", *font, tab_text_size); + sf::Text history_tab_text("History", *font, tab_text_size); + sf::Text recommended_tab_text("Recommended", *font, tab_text_size); + sf::Text login_tab_text("Login", *font, tab_text_size); SearchBar *focused_login_input = nullptr; if(current_plugin->name == "youtube") { - recommended_body = std::make_unique(this, &font, &bold_font); + recommended_body = std::make_unique(this, font.get(), bold_font.get(), cjk_font.get()); recommended_body->draw_thumbnails = true; fill_recommended_items_from_json(load_recommended_json(current_plugin), recommended_body->items); } @@ -973,7 +1004,7 @@ namespace QuickMedia { if(recommended_body) tabs.push_back(Tab{recommended_body.get(), nullptr, SearchSuggestionTab::RECOMMENDED, &recommended_tab_text}); if(is_fourchan) { - tabs.push_back(Tab{nullptr, std::make_unique(font), SearchSuggestionTab::LOGIN, &login_tab_text}); + tabs.push_back(Tab{nullptr, std::make_unique(*font), SearchSuggestionTab::LOGIN, &login_tab_text}); focused_login_input = tabs.back().login_tab->username.get(); tabs.back().login_tab->username->caret_visible = true; @@ -1403,7 +1434,7 @@ namespace QuickMedia { sf::Vector2f related_media_window_size; bool related_media_window_visible = false; - sf::Text related_videos_text("Related videos", bold_font, 20); + sf::Text related_videos_text("Related videos", *bold_font, 20); const float related_videos_text_height = related_videos_text.getCharacterSize(); sf::WindowHandle video_player_window = None; @@ -1794,18 +1825,18 @@ namespace QuickMedia { chapters_tab.type = EpisodeListTabType::CHAPTERS; chapters_tab.body = body; chapters_tab.creator = nullptr; - chapters_tab.text = sf::Text("Chapters", font, tab_text_size); + chapters_tab.text = sf::Text("Chapters", *font, tab_text_size); tabs.push_back(std::move(chapters_tab)); const std::vector& creators = manga->get_creators(); for(const Creator &creator : creators) { EpisodeListTab tab; tab.type = EpisodeListTabType::CREATOR; - tab.body = new Body(this, &font, &bold_font); + tab.body = new Body(this, font.get(), bold_font.get(), cjk_font.get()); tab.body->draw_thumbnails = true; tab.creator = &creator; tab.creator_page_download_future = std::async(std::launch::async, download_creator_page, creator.url); - tab.text = sf::Text(creator.name, font, tab_text_size); + tab.text = sf::Text(creator.name, *font, tab_text_size); tabs.push_back(std::move(tab)); } @@ -2069,7 +2100,7 @@ namespace QuickMedia { sf::Texture image_texture; sf::Sprite image; - sf::Text error_message("", font, 30); + sf::Text error_message("", *font, 30); error_message.setFillColor(sf::Color::White); assert(current_plugin->is_manga()); @@ -2132,7 +2163,7 @@ namespace QuickMedia { bool redraw = true; sf::Event event; - sf::Text chapter_text(content_title + " | " + chapter_title + " | Page " + std::to_string(image_index + 1) + "/" + std::to_string(num_images), font, 14); + sf::Text chapter_text(content_title + " | " + chapter_title + " | Page " + std::to_string(image_index + 1) + "/" + std::to_string(num_images), *font, 14); if(image_index == num_images) chapter_text.setString(content_title + " | " + chapter_title + " | End"); chapter_text.setFillColor(sf::Color::White); @@ -2296,7 +2327,7 @@ namespace QuickMedia { json_chapter = Json::Value(Json::objectValue); } - ImageViewer image_viewer(image_plugin, images_url, content_title, chapter_title, image_index, content_cache_dir, &font); + ImageViewer image_viewer(image_plugin, images_url, content_title, chapter_title, image_index, content_cache_dir, font.get()); json_chapter["current"] = std::min(latest_read, image_viewer.get_num_pages()); json_chapter["total"] = image_viewer.get_num_pages(); @@ -2490,7 +2521,7 @@ namespace QuickMedia { search_bar->text_autosearch_delay = file_manager->get_search_delay(); Page previous_page = pop_page_stack(); - sf::Text current_dir_text(file_manager->get_current_dir().string(), bold_font, 18); + sf::Text current_dir_text(file_manager->get_current_dir().string(), *bold_font, 18); // TODO: Make asynchronous. // TODO: Automatically go to the parent if this fails (recursively). @@ -2664,7 +2695,7 @@ namespace QuickMedia { std::mutex attachment_load_mutex; GoogleCaptchaChallengeInfo challenge_info; - sf::Text challenge_description_text("", font, 24); + sf::Text challenge_description_text("", *font, 24); challenge_description_text.setFillColor(sf::Color::White); const size_t captcha_num_columns = 3; const size_t captcha_num_rows = 3; @@ -3050,11 +3081,11 @@ namespace QuickMedia { void Program::chat_login_page() { assert(current_plugin->name == "matrix"); - SearchBar login_input(font, nullptr, "Username"); - SearchBar password_input(font, nullptr, "Password", true); - SearchBar homeserver_input(font, nullptr, "Homeserver"); + SearchBar login_input(*font, nullptr, "Username"); + SearchBar password_input(*font, nullptr, "Password", true); + SearchBar homeserver_input(*font, nullptr, "Homeserver"); - sf::Text status_text("", font, 18); + sf::Text status_text("", *font, 18); const int num_inputs = 3; SearchBar *inputs[num_inputs] = { &login_input, &password_input, &homeserver_input }; @@ -3150,18 +3181,18 @@ namespace QuickMedia { ChatTab messages_tab; messages_tab.type = ChatTabType::MESSAGES; - messages_tab.body = std::make_unique(this, &font, &bold_font); + messages_tab.body = std::make_unique(this, font.get(), bold_font.get(), cjk_font.get()); messages_tab.body->draw_thumbnails = true; //messages_tab.body->line_seperator_color = sf::Color::Transparent; - messages_tab.text = sf::Text("Messages", font, tab_text_size); + messages_tab.text = sf::Text("Messages", *font, tab_text_size); tabs.push_back(std::move(messages_tab)); ChatTab rooms_tab; rooms_tab.type = ChatTabType::ROOMS; - rooms_tab.body = std::make_unique(this, &font, &bold_font); + rooms_tab.body = std::make_unique(this, font.get(), bold_font.get(), cjk_font.get()); rooms_tab.body->draw_thumbnails = true; //rooms_tab.body->line_seperator_color = sf::Color::Transparent; - rooms_tab.text = sf::Text("Rooms", font, tab_text_size); + rooms_tab.text = sf::Text("Rooms", *font, tab_text_size); tabs.push_back(std::move(rooms_tab)); const int MESSAGES_TAB_INDEX = 0; @@ -3206,7 +3237,7 @@ namespace QuickMedia { plugin_logo.setSmooth(true); } - SearchBar chat_input(font, &plugin_logo, "Send a message..."); + SearchBar chat_input(*font, &plugin_logo, "Send a message..."); chat_input.set_background_color(sf::Color::Transparent); // TODO: Filer for rooms and settings -- cgit v1.2.3