From 46da98c432e729951f825e9c5c484b459c10cbbf Mon Sep 17 00:00:00 2001 From: "zhimin.zeng" Date: Fri, 17 May 2024 16:39:09 +0800 Subject: [PATCH] FIX: add thread for load font jira: none Change-Id: Iba53e1cf55beb77acbdbd731f052940b55349b8f --- src/slic3r/GUI/GLTexture.cpp | 3 --- src/slic3r/GUI/Gizmos/GLGizmoText.cpp | 29 +++++++++++++++++++++++++-- src/slic3r/GUI/Gizmos/GLGizmoText.hpp | 6 ++++++ 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/src/slic3r/GUI/GLTexture.cpp b/src/slic3r/GUI/GLTexture.cpp index 92d66929c..5e9a6e894 100644 --- a/src/slic3r/GUI/GLTexture.cpp +++ b/src/slic3r/GUI/GLTexture.cpp @@ -552,9 +552,6 @@ bool GLTexture::generate_from_text(const std::string &text_str, wxFont &font, wx bool GLTexture::generate_texture_from_text(const std::string& text_str, wxFont& font, int& ww, int& hh, int& hl, wxColor background, wxColor foreground) { - if(!can_generate_text_shape(text_str)) - return false; - if (text_str.empty()) { BOOST_LOG_TRIVIAL(error) << __FUNCTION__ << ":no text string, should not happen\n"; diff --git a/src/slic3r/GUI/Gizmos/GLGizmoText.cpp b/src/slic3r/GUI/Gizmos/GLGizmoText.cpp index aee2d1432..744c740f0 100644 --- a/src/slic3r/GUI/Gizmos/GLGizmoText.cpp +++ b/src/slic3r/GUI/Gizmos/GLGizmoText.cpp @@ -190,6 +190,9 @@ GLGizmoText::GLGizmoText(GLCanvas3D& parent, const std::string& icon_filename, u GLGizmoText::~GLGizmoText() { + if (m_thread.joinable()) + m_thread.join(); + for (int i = 0; i < m_textures.size(); i++) { if (m_textures[i].texture != nullptr) delete m_textures[i].texture; @@ -198,10 +201,13 @@ GLGizmoText::~GLGizmoText() bool GLGizmoText::on_init() { + m_init_texture = false; m_avail_font_names = init_face_names(); + m_thread = std::thread(&GLGizmoText::update_font_status, this); + //m_avail_font_names = init_occt_fonts(); - update_font_texture(); + //update_font_texture(); m_scale = m_imgui->get_font_size(); m_shortcut_key = WXK_CONTROL_T; @@ -232,7 +238,8 @@ void GLGizmoText::update_font_texture() auto retina_scale = m_parent.get_scale(); wxFont font { (int)round(retina_scale * FONT_SIZE), wxFONTFAMILY_SWISS, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL, false, face }; int w, h, hl; - if (texture->generate_texture_from_text(m_avail_font_names[i], font, w, h, hl, FONT_TEXTURE_BG, FONT_TEXTURE_FG)) { + std::unique_lock lock(m_mutex); + if (m_font_status[i] && texture->generate_texture_from_text(m_avail_font_names[i], font, w, h, hl, FONT_TEXTURE_BG, FONT_TEXTURE_FG)) { //if (h < m_imgui->scaled(2.f)) { TextureInfo info; info.texture = texture; @@ -689,6 +696,11 @@ void GLGizmoText::pop_combo_style() // BBS void GLGizmoText::on_render_input_window(float x, float y, float bottom_limit) { + if (!m_init_texture) { + update_font_texture(); + m_init_texture = true; + } + if (m_imgui->get_font_size() != m_scale) { m_scale = m_imgui->get_font_size(); update_font_texture(); @@ -992,6 +1004,19 @@ void GLGizmoText::reset_text_info() m_is_modify = false; } +void GLGizmoText::update_font_status() { + std::unique_lock lock(m_mutex); + m_font_status.reserve(m_avail_font_names.size()); + for (std::string font_name : m_avail_font_names) { + if (!can_generate_text_shape(font_name)) { + m_font_status.emplace_back(false); + } + else { + m_font_status.emplace_back(true); + } + } +} + bool GLGizmoText::update_text_positions(const std::vector& texts) { std::vector text_lengths; diff --git a/src/slic3r/GUI/Gizmos/GLGizmoText.hpp b/src/slic3r/GUI/Gizmos/GLGizmoText.hpp index 58d9e8447..2e5a0470b 100644 --- a/src/slic3r/GUI/Gizmos/GLGizmoText.hpp +++ b/src/slic3r/GUI/Gizmos/GLGizmoText.hpp @@ -57,6 +57,11 @@ private: std::vector m_font_names; + bool m_init_texture = false; + std::vector m_font_status; + std::mutex m_mutex; + std::thread m_thread; + bool m_is_modify = false; bool m_need_update_text = false; @@ -108,6 +113,7 @@ protected: private: ModelVolume *get_selected_single_volume(int& out_object_idx, int& out_volume_idx) const; + void update_font_status(); void reset_text_info(); bool update_text_positions(const std::vector& texts); TriangleMesh get_text_mesh(const char* text_str, const Vec3d &position, const Vec3d &normal, const Vec3d &text_up_dir);