From d638a6092bd6291c983490ba3f966162c7ca06c2 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sat, 31 Oct 2020 09:46:32 +0100 Subject: Load fonts on demand --- src/QuickMedia.cpp | 101 ++++++++++++++--------------------------------------- 1 file changed, 26 insertions(+), 75 deletions(-) (limited to 'src/QuickMedia.cpp') diff --git a/src/QuickMedia.cpp b/src/QuickMedia.cpp index e16f8a4..3c76079 100644 --- a/src/QuickMedia.cpp +++ b/src/QuickMedia.cpp @@ -20,6 +20,7 @@ #include "../include/Entry.hpp" #include "../include/NetUtils.hpp" #include "../include/SfmlFixes.hpp" +#include "../include/FontLoader.hpp" #include "../external/hash-library/sha256.h" #include @@ -318,45 +319,6 @@ namespace QuickMedia { resources_root = "../../../"; } - 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(!cjk_font) { - fprintf(stderr, "Failed to find NotoSansCJK-Regular.ttc in /usr/share/fonts/noto and /usr/share/fonts/truetype/noto\n"); - abort(); - } - if(!circle_mask_shader.loadFromFile(resources_root + "shaders/circle_mask.glsl", sf::Shader::Type::Fragment)) { fprintf(stderr, "Failed to load %s/shaders/circle_mask.glsl", resources_root.c_str()); abort(); @@ -627,7 +589,7 @@ namespace QuickMedia { auto window_size_u = window.getSize(); window_size.x = window_size_u.x; window_size.y = window_size_u.y; - sf::Text loading_text("Loading...", *font.get(), 24); + sf::Text loading_text("Loading...", *FontLoader::get_font(FontLoader::FontType::LATIN), 24); loading_text.setPosition(window_size.x * 0.5f - loading_text.getLocalBounds().width * 0.5f, window_size.y * 0.5f - loading_text.getLocalBounds().height * 0.5f); window.clear(back_color); window.draw(loading_text); @@ -874,30 +836,18 @@ namespace QuickMedia { body_size = sf::Vector2f(body_width, window_size.y - body_padding_vertical - related_videos_text_height); } - class LoginTab { - public: - LoginTab(sf::Font &font) : - username(std::make_unique(font, nullptr, "Token...")), - password(std::make_unique(font, nullptr, "PIN...", true)) - { - - } - std::unique_ptr username; - std::unique_ptr password; - }; - bool Program::is_tor_enabled() { return use_tor; } std::unique_ptr Program::create_body() { - auto body = std::make_unique(this, font.get(), bold_font.get(), cjk_font.get(), loading_icon); + auto body = std::make_unique(this, loading_icon); body->thumbnail_mask_shader = &circle_mask_shader; return body; } std::unique_ptr Program::create_search_bar(const std::string &placeholder, int search_delay) { - auto search_bar = std::make_unique(*font, &plugin_logo, placeholder); + auto search_bar = std::make_unique(&plugin_logo, placeholder); search_bar->text_autosearch_delay = search_delay; return search_bar; } @@ -986,7 +936,7 @@ namespace QuickMedia { std::vector tab_associated_data; for(size_t i = 0; i < tabs.size(); ++i) { TabAssociatedData data; - data.search_result_text = sf::Text("", *font, 30); + data.search_result_text = sf::Text("", *FontLoader::get_font(FontLoader::FontType::LATIN), 30); tab_associated_data.push_back(std::move(data)); } @@ -997,7 +947,7 @@ namespace QuickMedia { const float gradient_height = 5.0f; sf::Vertex gradient_points[4]; - sf::Text tab_text("", *font, tab_text_size); + sf::Text tab_text("", *FontLoader::get_font(FontLoader::FontType::LATIN), tab_text_size); int selected_tab = 0; bool loop_running = true; @@ -1669,7 +1619,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", *FontLoader::get_font(FontLoader::FontType::LATIN_BOLD), 20); const float related_videos_text_height = related_videos_text.getCharacterSize(); auto related_media_body = create_body(); @@ -1681,8 +1631,9 @@ namespace QuickMedia { XSync(disp, False); }; - auto load_video_error_check = [this, &related_media_body, &video_url, &video_title, &video_player, previous_page, &time_watched_timer, &added_recommendations, page]() mutable { + auto load_video_error_check = [this, &related_media_body, &video_url, &video_title, &video_player, previous_page, &time_watched_timer, &video_loaded, &added_recommendations, page]() mutable { time_watched_timer.restart(); + video_loaded = false; added_recommendations = false; watched_videos.insert(video_url); VideoPlayer::Error err = video_player->load_video(video_url.c_str(), window.getSystemHandle(), plugin_name); @@ -2178,7 +2129,7 @@ namespace QuickMedia { sf::Texture image_texture; sf::Sprite image; - sf::Text error_message("", *font, 30); + sf::Text error_message("", *FontLoader::get_font(FontLoader::FontType::LATIN), 30); error_message.setFillColor(sf::Color::White); bool download_in_progress = false; @@ -2242,7 +2193,7 @@ namespace QuickMedia { bool error = !error_message.getString().isEmpty(); bool redraw = true; - sf::Text chapter_text(images_page->manga_name + " | " + images_page->get_chapter_name() + " | Page " + std::to_string(image_index + 1) + "/" + std::to_string(num_images), *font, 14); + sf::Text chapter_text(images_page->manga_name + " | " + images_page->get_chapter_name() + " | Page " + std::to_string(image_index + 1) + "/" + std::to_string(num_images), *FontLoader::get_font(FontLoader::FontType::LATIN), 14); if(image_index == num_images) chapter_text.setString(images_page->manga_name + " | " + images_page->get_chapter_name() + " | End"); chapter_text.setFillColor(sf::Color::White); @@ -2423,7 +2374,7 @@ namespace QuickMedia { json_chapter = Json::Value(Json::objectValue); } - ImageViewer image_viewer(images_page, images_page->manga_name, images_page->get_chapter_name(), image_index, content_cache_dir, font.get()); + ImageViewer image_viewer(images_page, images_page->manga_name, images_page->get_chapter_name(), image_index, content_cache_dir); json_chapter["current"] = std::min(latest_read, image_viewer.get_num_pages()); json_chapter["total"] = image_viewer.get_num_pages(); @@ -2501,7 +2452,7 @@ namespace QuickMedia { sf::Sprite attached_image_sprite; GoogleCaptchaChallengeInfo challenge_info; - sf::Text challenge_description_text("", *font, 24); + sf::Text challenge_description_text("", *FontLoader::get_font(FontLoader::FontType::LATIN), 24); challenge_description_text.setFillColor(sf::Color::White); const size_t captcha_num_columns = 3; const size_t captcha_num_rows = 3; @@ -2567,7 +2518,7 @@ namespace QuickMedia { }, is_tor_enabled()); }; - Entry comment_input("Press m to begin writing a comment...", font.get(), cjk_font.get()); + Entry comment_input("Press m to begin writing a comment..."); comment_input.draw_background = false; comment_input.set_editable(false); @@ -2955,11 +2906,11 @@ namespace QuickMedia { void Program::chat_login_page() { assert(strcmp(plugin_name, "matrix") == 0); - SearchBar login_input(*font, nullptr, "Username"); - SearchBar password_input(*font, nullptr, "Password", true); - SearchBar homeserver_input(*font, nullptr, "Homeserver"); + SearchBar login_input(nullptr, "Username"); + SearchBar password_input(nullptr, "Password", true); + SearchBar homeserver_input(nullptr, "Homeserver"); - sf::Text status_text("", *font, 18); + sf::Text status_text("", *FontLoader::get_font(FontLoader::FontType::LATIN), 18); const int num_inputs = 3; SearchBar *inputs[num_inputs] = { &login_input, &password_input, &homeserver_input }; @@ -3118,19 +3069,19 @@ namespace QuickMedia { std::vector tabs; ChatTab pinned_tab; - pinned_tab.body = std::make_unique(this, font.get(), bold_font.get(), cjk_font.get(), loading_icon); + pinned_tab.body = std::make_unique(this, loading_icon); pinned_tab.body->thumbnail_max_size = CHAT_MESSAGE_THUMBNAIL_MAX_SIZE; pinned_tab.body->thumbnail_mask_shader = &circle_mask_shader; //pinned_tab.body->line_separator_color = sf::Color::Transparent; - pinned_tab.text = sf::Text("Pinned messages", *font, tab_text_size); + pinned_tab.text = sf::Text("Pinned messages", *FontLoader::get_font(FontLoader::FontType::LATIN), tab_text_size); tabs.push_back(std::move(pinned_tab)); ChatTab messages_tab; - messages_tab.body = std::make_unique(this, font.get(), bold_font.get(), cjk_font.get(), loading_icon); + messages_tab.body = std::make_unique(this, loading_icon); messages_tab.body->thumbnail_max_size = CHAT_MESSAGE_THUMBNAIL_MAX_SIZE; messages_tab.body->thumbnail_mask_shader = &circle_mask_shader; //messages_tab.body->line_separator_color = sf::Color::Transparent; - messages_tab.text = sf::Text("Messages", *font, tab_text_size); + messages_tab.text = sf::Text("Messages", *FontLoader::get_font(FontLoader::FontType::LATIN), tab_text_size); tabs.push_back(std::move(messages_tab)); const int PINNED_TAB_INDEX = 0; @@ -3151,13 +3102,13 @@ namespace QuickMedia { ChatState chat_state = ChatState::NAVIGATING; std::shared_ptr currently_operating_on_item; - sf::Text replying_to_text("Replying to:", *font, 18); + sf::Text replying_to_text("Replying to:", *FontLoader::get_font(FontLoader::FontType::LATIN), 18); sf::Sprite logo_sprite(plugin_logo); logo_sprite.setScale(0.8f, 0.8f); sf::Vector2f logo_size(plugin_logo.getSize().x * logo_sprite.getScale().x, plugin_logo.getSize().y * logo_sprite.getScale().y); - sf::Text room_name_text("", *bold_font, 18); + sf::Text room_name_text("", *FontLoader::get_font(FontLoader::FontType::LATIN_BOLD), 18); const float room_name_text_height = 20.0f; const float room_name_text_padding_y = 10.0f; const float room_name_total_height = room_name_text_height + room_name_text_padding_y * 2.0f; @@ -3271,7 +3222,7 @@ namespace QuickMedia { tabs[PINNED_TAB_INDEX].body->set_selected_item(selected_before); }; - Body url_selection_body(this, font.get(), bold_font.get(), cjk_font.get(), loading_icon); + Body url_selection_body(this, loading_icon); Messages all_messages; matrix->get_all_synced_room_messages(current_room, all_messages); @@ -3291,7 +3242,7 @@ namespace QuickMedia { read_marker_timeout_ms = 0; redraw = true; - Entry chat_input("Press m to begin writing a message...", font.get(), cjk_font.get()); + Entry chat_input("Press m to begin writing a message..."); chat_input.draw_background = false; chat_input.set_editable(false); -- cgit v1.2.3