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 --- include/AsyncTask.hpp | 9 ----- include/Body.hpp | 5 +-- include/Entry.hpp | 2 +- include/FontLoader.hpp | 19 +++++++++ include/ImageViewer.hpp | 2 +- include/QuickMedia.hpp | 3 -- include/SearchBar.hpp | 2 +- include/Text.hpp | 20 ++++++---- src/Body.cpp | 20 +++++----- src/Entry.cpp | 7 ++-- src/FontLoader.cpp | 52 +++++++++++++++++++++++++ src/ImageViewer.cpp | 7 ++-- src/QuickMedia.cpp | 101 +++++++++++++----------------------------------- src/SearchBar.cpp | 7 ++-- src/Text.cpp | 54 ++++++++++++++++---------- 15 files changed, 168 insertions(+), 142 deletions(-) delete mode 100644 include/AsyncTask.hpp create mode 100644 include/FontLoader.hpp create mode 100644 src/FontLoader.cpp diff --git a/include/AsyncTask.hpp b/include/AsyncTask.hpp deleted file mode 100644 index 81be1ee..0000000 --- a/include/AsyncTask.hpp +++ /dev/null @@ -1,9 +0,0 @@ -#pragma once - -#include -#include -#include - -namespace QuickMedia { - -} \ No newline at end of file diff --git a/include/Body.hpp b/include/Body.hpp index 1dc1fe8..1a82443 100644 --- a/include/Body.hpp +++ b/include/Body.hpp @@ -135,7 +135,7 @@ namespace QuickMedia { class Body { public: - Body(Program *program, sf::Font *font, sf::Font *bold_font, sf::Font *cjk_font, sf::Texture &loading_icon_texture); + Body(Program *program, sf::Texture &loading_icon_texture); // Select previous page, ignoring invisible items. Returns true if the item was changed. This can be used to check if the top was hit when wrap_around is set to false bool select_previous_page(); @@ -199,9 +199,6 @@ namespace QuickMedia { float get_page_scroll() const { return page_scroll; } bool is_last_item_fully_visible() const { return last_item_fully_visible; } - sf::Font *font; - sf::Font *bold_font; - sf::Font *cjk_font; sf::Text progress_text; sf::Text replies_text; sf::Text embedded_item_load_text; diff --git a/include/Entry.hpp b/include/Entry.hpp index 27c3517..32dcda2 100644 --- a/include/Entry.hpp +++ b/include/Entry.hpp @@ -17,7 +17,7 @@ namespace QuickMedia { class Entry { public: - Entry(const std::string &placeholder_text, sf::Font *font, sf::Font *cjk_font); + Entry(const std::string &placeholder_text); void process_event(sf::Event &event); void draw(sf::RenderWindow &window); diff --git a/include/FontLoader.hpp b/include/FontLoader.hpp new file mode 100644 index 0000000..bff0f18 --- /dev/null +++ b/include/FontLoader.hpp @@ -0,0 +1,19 @@ +#pragma once + +#include + +namespace sf { + class Font; +} + +namespace QuickMedia::FontLoader { + enum class FontType { + LATIN, + LATIN_BOLD, + CJK, + EMOJI + }; + + // Note: not thread-safe + sf::Font* get_font(FontType font_type); +} \ No newline at end of file diff --git a/include/ImageViewer.hpp b/include/ImageViewer.hpp index ea53558..f886709 100644 --- a/include/ImageViewer.hpp +++ b/include/ImageViewer.hpp @@ -46,7 +46,7 @@ namespace QuickMedia { class ImageViewer { public: - ImageViewer(MangaImagesPage *manga_images_page, const std::string &content_title, const std::string &chapter_title, int current_page, const Path &chapter_cache_dir, sf::Font *font); + ImageViewer(MangaImagesPage *manga_images_page, const std::string &content_title, const std::string &chapter_title, int current_page, const Path &chapter_cache_dir); ~ImageViewer(); ImageViewerAction draw(sf::RenderWindow &window); // Returns page as 1 indexed diff --git a/include/QuickMedia.hpp b/include/QuickMedia.hpp index b9ec2d9..821a427 100644 --- a/include/QuickMedia.hpp +++ b/include/QuickMedia.hpp @@ -104,9 +104,6 @@ namespace QuickMedia { Matrix *matrix = nullptr; int monitor_hz; sf::Vector2f window_size; - std::unique_ptr font; - std::unique_ptr bold_font; - std::unique_ptr cjk_font; const char *plugin_name = nullptr; sf::Texture plugin_logo; sf::Texture loading_icon; diff --git a/include/SearchBar.hpp b/include/SearchBar.hpp index eb7a9f2..de3b686 100644 --- a/include/SearchBar.hpp +++ b/include/SearchBar.hpp @@ -21,7 +21,7 @@ namespace QuickMedia { class SearchBar { public: - SearchBar(sf::Font &font, sf::Texture *plugin_logo, const std::string &placeholder, bool input_masked = false); + SearchBar(sf::Texture *plugin_logo, const std::string &placeholder, bool input_masked = false); void draw(sf::RenderWindow &window, bool draw_shadow = true); void on_event(sf::Event &event); void update(); diff --git a/include/Text.hpp b/include/Text.hpp index 7ce7fa3..e471441 100644 --- a/include/Text.hpp +++ b/include/Text.hpp @@ -36,15 +36,21 @@ namespace QuickMedia { TEXT }; + + enum class TextType { + LATIN, + CJK, + EMOJI + }; TextElement() {} - TextElement(const StringViewUtf32 &_text, Type _type) : text(_text), type(_type), is_cjk(false) {} + TextElement(const StringViewUtf32 &_text, Type _type) : text(_text), type(_type), text_type(TextType::LATIN) {} StringViewUtf32 text; sf::Vector2f position; Type type; //bool ownLine; // Currently only used for emoji, to make emoji bigger when it's the only thing on a line - bool is_cjk; + TextType text_type; }; struct VertexRef { @@ -57,8 +63,8 @@ namespace QuickMedia class Text { public: - Text(const sf::Font *font, const sf::Font *cjk_font); - Text(sf::String str, const sf::Font *font, const sf::Font *cjk_font, unsigned int characterSize, float maxWidth); + Text(bool bold_font); + Text(sf::String str, bool bold_font, unsigned int characterSize, float maxWidth); void setString(sf::String str); const sf::String& getString() const; @@ -73,8 +79,7 @@ namespace QuickMedia void setCharacterSize(unsigned int characterSize); unsigned int getCharacterSize() const; - - const sf::Font* getFont() const; + void setFillColor(sf::Color color); void setLineSpacing(float lineSpacing); @@ -127,8 +132,7 @@ namespace QuickMedia int get_vertex_line(int index) const; private: sf::String str; // TODO: Remove this for non-editable text??? also replace with std::string? then we get more efficient editing of text - const sf::Font *font; - const sf::Font *cjk_font; + const bool bold_font; unsigned int characterSize; sf::VertexArray vertices[2]; float maxWidth; diff --git a/src/Body.cpp b/src/Body.cpp index 3f5c755..06907c8 100644 --- a/src/Body.cpp +++ b/src/Body.cpp @@ -1,6 +1,7 @@ #include "../include/Body.hpp" #include "../include/QuickMedia.hpp" #include "../include/Scale.hpp" +#include "../include/FontLoader.hpp" #include "../plugins/Plugin.hpp" #include #include @@ -79,13 +80,10 @@ namespace QuickMedia { return *this; } - Body::Body(Program *program, sf::Font *font, sf::Font *bold_font, sf::Font *cjk_font, sf::Texture &loading_icon_texture) : - font(font), - bold_font(bold_font), - cjk_font(cjk_font), - progress_text("", *font, 14), - replies_text("", *font, 14), - embedded_item_load_text("", *font, 14), + Body::Body(Program *program, sf::Texture &loading_icon_texture) : + progress_text("", *FontLoader::get_font(FontLoader::FontType::LATIN), 14), + replies_text("", *FontLoader::get_font(FontLoader::FontType::LATIN), 14), + embedded_item_load_text("", *FontLoader::get_font(FontLoader::FontType::LATIN), 14), draw_thumbnails(true), wrap_around(false), line_separator_color(sf::Color(32, 37, 43, 255)), @@ -475,7 +473,7 @@ namespace QuickMedia { if(body_item->title_text) body_item->title_text->setString(std::move(str)); else - body_item->title_text = std::make_unique(std::move(str), font, cjk_font, 16, width - 50 - image_padding_x * 2.0f); + body_item->title_text = std::make_unique(std::move(str), false, 16, width - 50 - image_padding_x * 2.0f); body_item->title_text->setFillColor(body_item->get_title_color()); body_item->title_text->updateGeometry(); } @@ -486,7 +484,7 @@ namespace QuickMedia { if(body_item->description_text) body_item->description_text->setString(std::move(str)); else - body_item->description_text = std::make_unique(std::move(str), font, cjk_font, 14, width - 50 - image_padding_x * 2.0f); + body_item->description_text = std::make_unique(std::move(str), false, 14, width - 50 - image_padding_x * 2.0f); body_item->description_text->setFillColor(body_item->get_description_color()); body_item->description_text->updateGeometry(); } @@ -497,7 +495,7 @@ namespace QuickMedia { if(body_item->author_text) body_item->author_text->setString(std::move(str)); else - body_item->author_text = std::make_unique(std::move(str), bold_font, cjk_font, 14, width - 50 - image_padding_x * 2.0f); + body_item->author_text = std::make_unique(std::move(str), true, 14, width - 50 - image_padding_x * 2.0f); body_item->author_text->setFillColor(body_item->get_author_color()); body_item->author_text->updateGeometry(); } @@ -525,7 +523,7 @@ namespace QuickMedia { if(body_item->timestamp_text) body_item->timestamp_text->setString(time_str); else - body_item->timestamp_text = std::make_unique(time_str, *font, 10); + body_item->timestamp_text = std::make_unique(time_str, *FontLoader::get_font(FontLoader::FontType::LATIN), 10); body_item->timestamp_text->setFillColor(sf::Color(185, 190, 198, 100)); } diff --git a/src/Entry.cpp b/src/Entry.cpp index 2b9e573..ae370c5 100644 --- a/src/Entry.cpp +++ b/src/Entry.cpp @@ -1,4 +1,5 @@ #include "../include/Entry.hpp" +#include "../include/FontLoader.hpp" #include #include #include @@ -9,13 +10,13 @@ const float padding_vertical = 3.0f; const float background_margin_vertical = 0.0f; namespace QuickMedia { - Entry::Entry(const std::string &placeholder_text, sf::Font *font, sf::Font *cjk_font) : + Entry::Entry(const std::string &placeholder_text) : on_submit_callback(nullptr), draw_background(true), - text("", font, cjk_font, 16, 0.0f), + text("", false, 16, 0.0f), width(0.0f), background(sf::Vector2f(1.0f, 1.0f), 7.0f, 10), - placeholder(placeholder_text, *font, 16) + placeholder(placeholder_text, *FontLoader::get_font(FontLoader::FontType::LATIN), 16) { text.setEditable(true); background.setFillColor(sf::Color(55, 60, 68)); diff --git a/src/FontLoader.cpp b/src/FontLoader.cpp new file mode 100644 index 0000000..ca33377 --- /dev/null +++ b/src/FontLoader.cpp @@ -0,0 +1,52 @@ +#include "../include/FontLoader.hpp" +#include +#include +#include + +static std::array, 3> font_cache; + +namespace QuickMedia::FontLoader { + sf::Font* get_font(FontType font_type) { + sf::Font *font = font_cache[(size_t)font_type].get(); + if(!font) { + auto new_font = std::make_unique(); + std::vector noto_directories; + std::string font_file_name; + switch(font_type) { + case FontType::LATIN: { + noto_directories.push_back("/usr/share/fonts/noto"); + noto_directories.push_back("/usr/share/fonts/truetype/noto"); + font_file_name = "NotoSans-Regular.ttf"; + break; + } + case FontType::LATIN_BOLD: { + noto_directories.push_back("/usr/share/fonts/noto"); + noto_directories.push_back("/usr/share/fonts/truetype/noto"); + font_file_name = "NotoSans-Bold.ttf"; + break; + } + case FontType::CJK: { + 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"; + break; + } + case FontType::EMOJI: { + noto_directories.push_back("/usr/share/fonts/noto"); + noto_directories.push_back("/usr/share/fonts/truetype/noto"); + font_file_name = "NotoColorEmoji.ttf"; + break; + } + } + + for(const std::string ¬o_dir : noto_directories) { + if(new_font->loadFromFile(noto_dir + "/" + font_file_name)) + break; + } + + font_cache[(size_t)font_type] = std::move(new_font); + font = font_cache[(size_t)font_type].get(); + } + return font; + } +} \ No newline at end of file diff --git a/src/ImageViewer.cpp b/src/ImageViewer.cpp index c704485..be03781 100644 --- a/src/ImageViewer.cpp +++ b/src/ImageViewer.cpp @@ -2,6 +2,7 @@ #include "../include/Notification.hpp" #include "../include/Storage.hpp" #include "../include/SfmlFixes.hpp" +#include "../include/FontLoader.hpp" #include "../plugins/Manga.hpp" #include #include @@ -9,15 +10,15 @@ #include namespace QuickMedia { - ImageViewer::ImageViewer(MangaImagesPage *manga_images_page, const std::string &content_title, const std::string &chapter_title, int current_page, const Path &chapter_cache_dir, sf::Font *font) : + ImageViewer::ImageViewer(MangaImagesPage *manga_images_page, const std::string &content_title, const std::string &chapter_title, int current_page, const Path &chapter_cache_dir) : current_page(current_page), num_pages(0), content_title(content_title), chapter_title(chapter_title), chapter_cache_dir(chapter_cache_dir), focused_page(current_page), - font(font), - page_text("", *font, 14) + font(FontLoader::get_font(FontLoader::FontType::LATIN)), + page_text("", *FontLoader::get_font(FontLoader::FontType::LATIN), 14) { if(manga_images_page->get_number_of_images(num_pages) != ImageResult::OK) { show_notification("QuickMedia", "Failed to get number of images", Urgency::CRITICAL); 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); diff --git a/src/SearchBar.cpp b/src/SearchBar.cpp index 6895dcb..aafe987 100644 --- a/src/SearchBar.cpp +++ b/src/SearchBar.cpp @@ -1,5 +1,6 @@ #include "../include/SearchBar.hpp" #include "../include/Scale.hpp" +#include "../include/FontLoader.hpp" #include #include #include @@ -17,7 +18,7 @@ static const float padding_bottom = 15.0f; static const float background_margin_vertical = 4.0f; namespace QuickMedia { - SearchBar::SearchBar(sf::Font &font, sf::Texture *plugin_logo, const std::string &placeholder, bool input_masked) : + SearchBar::SearchBar(sf::Texture *plugin_logo, const std::string &placeholder, bool input_masked) : onTextUpdateCallback(nullptr), onTextSubmitCallback(nullptr), onTextBeginTypingCallback(nullptr), @@ -25,8 +26,8 @@ namespace QuickMedia { text_autosearch_delay(0), autocomplete_search_delay(0), caret_visible(true), - text(placeholder, font, 16), - autocomplete_text("", font, 16), + text(placeholder, *FontLoader::get_font(FontLoader::FontType::LATIN), 16), + autocomplete_text("", *FontLoader::get_font(FontLoader::FontType::LATIN), 16), background(sf::Vector2f(1.0f, 1.0f), 10.0f, 10), placeholder_str(placeholder), show_placeholder(true), diff --git a/src/Text.cpp b/src/Text.cpp index 360f290..9650964 100644 --- a/src/Text.cpp +++ b/src/Text.cpp @@ -1,4 +1,5 @@ #include "../include/Text.hpp" +#include "../include/FontLoader.hpp" #include #include #include @@ -24,11 +25,10 @@ namespace QuickMedia return -1; } - Text::Text(const sf::Font *_font, const sf::Font *_cjk_font) : Text("", _font, _cjk_font, 0, 0.0f) {} + Text::Text(bool bold_font) : Text("", bold_font, 0, 0.0f) {} - Text::Text(sf::String _str, const sf::Font *_font, const sf::Font *_cjk_font, unsigned int _characterSize, float _maxWidth) : - font(_font), - cjk_font(_cjk_font), + Text::Text(sf::String _str, bool bold_font, unsigned int _characterSize, float _maxWidth) : + bold_font(bold_font), characterSize(_characterSize), maxWidth(_maxWidth), color(sf::Color::White), @@ -123,11 +123,6 @@ namespace QuickMedia return characterSize; } - const sf::Font* Text::getFont() const - { - return font; - } - void Text::setFillColor(sf::Color color) { if(color != this->color) @@ -251,7 +246,7 @@ namespace QuickMedia else offset = find_end_of_non_cjk(str.getData() + index + 1, size - index - 1); textElements.push_back({ StringViewUtf32(str.getData() + index, offset + 1), TextElement::Type::TEXT }); - textElements.back().is_cjk = is_cjk; + textElements.back().text_type = is_cjk ? TextElement::TextType::CJK : TextElement::TextType::LATIN; index += 1 + offset; } } @@ -313,18 +308,24 @@ namespace QuickMedia vertices[1].clear(); boundingBox = sf::FloatRect(); - float hspace = font->getGlyph(' ', characterSize, false).advance + characterSpacing; - float vspace = font->getLineSpacing(characterSize); // TODO: What about japanese font??? + sf::Font *latin_font; + if(bold_font) + latin_font = FontLoader::get_font(FontLoader::FontType::LATIN_BOLD); + else + latin_font = FontLoader::get_font(FontLoader::FontType::LATIN); + + float hspace = latin_font->getGlyph(' ', characterSize, false).advance + characterSpacing; + float vspace = latin_font->getLineSpacing(characterSize); // TODO: What about japanese font??? sf::Vector2f glyphPos; sf::Uint32 prevCodePoint = 0; for(usize textElementIndex = 0; textElementIndex < textElements.size(); ++textElementIndex) { TextElement &textElement = textElements[textElementIndex]; - const sf::Font *ff = font; + const sf::Font *ff = latin_font; int vertices_index = 0; - if(textElement.is_cjk) { - ff = cjk_font; + if(textElement.text_type == TextElement::TextType::CJK) { + ff = FontLoader::get_font(FontLoader::FontType::CJK); vertices_index = 1; } @@ -498,9 +499,16 @@ namespace QuickMedia void Text::updateCaret() { assert(!dirty && !dirtyText); + + sf::Font *latin_font; + if(bold_font) + latin_font = FontLoader::get_font(FontLoader::FontType::LATIN_BOLD); + else + latin_font = FontLoader::get_font(FontLoader::FontType::LATIN); + if(vertices_linear.empty()) { caretIndex = 0; - caretPosition = sf::Vector2f(0.0f, floor(font->getLineSpacing(characterSize))); + caretPosition = sf::Vector2f(0.0f, floor(latin_font->getLineSpacing(characterSize))); caret_offset_x = 0.0f; return; } @@ -546,10 +554,10 @@ namespace QuickMedia caretPosition.x = 0.0f; else caretPosition.x = get_text_quad_right_side(last_vertex); - caretPosition.y = (1 + get_vertex_line(caretIndex)) * floor(font->getLineSpacing(characterSize) + lineSpacing); + caretPosition.y = (1 + get_vertex_line(caretIndex)) * floor(latin_font->getLineSpacing(characterSize) + lineSpacing); } else { caretPosition.x = get_caret_offset_by_caret_index(caretIndex); - caretPosition.y = (1 + get_vertex_line(caretIndex)) * floor(font->getLineSpacing(characterSize) + lineSpacing); + caretPosition.y = (1 + get_vertex_line(caretIndex)) * floor(latin_font->getLineSpacing(characterSize) + lineSpacing); } } @@ -735,10 +743,16 @@ namespace QuickMedia sf::Vector2f pos = position; - const float vspace = font->getLineSpacing(characterSize); + sf::Font *latin_font; + if(bold_font) + latin_font = FontLoader::get_font(FontLoader::FontType::LATIN_BOLD); + else + latin_font = FontLoader::get_font(FontLoader::FontType::LATIN); + + const float vspace = latin_font->getLineSpacing(characterSize); pos.y += floor(vspace); // Origin is at bottom left, we want it to be at top left - const sf::Font *fonts[] = { font, cjk_font }; + const sf::Font *fonts[] = { latin_font, FontLoader::get_font(FontLoader::FontType::CJK) }; for(size_t i = 0; i < 2; ++i) { sf::RenderStates states; states.transform.translate(pos); -- cgit v1.2.3