From f3724c8dac8e0d2b0431cf53c423ba8431d352f6 Mon Sep 17 00:00:00 2001 From: Filip Sykala - NTB T15p Date: Wed, 23 Nov 2022 13:01:11 +0100 Subject: [PATCH] Truncate font name on demand --- src/slic3r/GUI/Gizmos/GLGizmoEmboss.cpp | 27 +++++++++++-------------- src/slic3r/GUI/Gizmos/GLGizmoEmboss.hpp | 2 +- src/slic3r/Utils/WxFontUtils.hpp | 1 - 3 files changed, 13 insertions(+), 17 deletions(-) diff --git a/src/slic3r/GUI/Gizmos/GLGizmoEmboss.cpp b/src/slic3r/GUI/Gizmos/GLGizmoEmboss.cpp index 4ecaac1d9..46d4be0b1 100644 --- a/src/slic3r/GUI/Gizmos/GLGizmoEmboss.cpp +++ b/src/slic3r/GUI/Gizmos/GLGizmoEmboss.cpp @@ -1406,18 +1406,10 @@ void GLGizmoEmboss::init_face_names() { // to reload fonts from system, when install new one wxFontEnumerator::InvalidateCache(); - auto create_truncated_names = [&facenames = m_face_names, &width = m_gui_cfg->face_name_max_width]() { - for (FaceName &face : facenames.faces) { - std::string name_str(face.wx_name.ToUTF8().data()); - face.name_truncated = ImGuiWrapper::trunc(name_str, width); - } - }; - // try load cache // Only not OS enumerated face has hash value 0 if (m_face_names.hash == 0) { load(m_face_names); - create_truncated_names(); } using namespace std::chrono; @@ -1459,11 +1451,11 @@ void GLGizmoEmboss::init_face_names() { auto it = std::lower_bound(bad.begin(), bad.end(), name); if (it != bad.end() && *it == name) return false; - //wxFont wx_font(wxFontInfo().FaceName(name).Encoding(encoding)); + wxFont wx_font(wxFontInfo().FaceName(name).Encoding(encoding)); //* // Faster chech if wx_font is loadable but not 100% // names could contain not loadable font - //if (!WxFontUtils::can_load(wx_font)) return false; + if (!WxFontUtils::can_load(wx_font)) return false; /*/ // Slow copy of font files to try load font @@ -1487,7 +1479,6 @@ void GLGizmoEmboss::init_face_names() { } } assert(std::is_sorted(m_face_names.bad.begin(), m_face_names.bad.end())); - create_truncated_names(); store(m_face_names); } @@ -1521,7 +1512,7 @@ void GLGizmoEmboss::init_font_name_texture() { } } -void GLGizmoEmboss::draw_font_preview(FaceName& face) +void GLGizmoEmboss::draw_font_preview(FaceName& face, bool is_visible) { unsigned int &count_opened_fonts = m_face_names.count_opened_font_files; ImVec2 size(m_gui_cfg->face_name_size.x(), m_gui_cfg->face_name_size.y()); @@ -1533,11 +1524,11 @@ void GLGizmoEmboss::draw_font_preview(FaceName& face) size_t texture_index = face.texture_index; uv0 = ImVec2(0.f, texture_index / (float) m_face_names.count_cached_textures), uv1 = ImVec2(1.f, (texture_index + 1) / (float) m_face_names.count_cached_textures); - } else if (!ImGui::IsItemVisible()) { + } else if (!is_visible) { face.is_created = nullptr; face.cancel->store(true); } - } else if (ImGui::IsItemVisible() && count_opened_fonts < m_gui_cfg->max_count_opened_font_files) { + } else if (is_visible && count_opened_fonts < m_gui_cfg->max_count_opened_font_files) { ++count_opened_fonts; face.cancel = std::make_shared(false); face.is_created = std::make_shared(false); @@ -1647,7 +1638,13 @@ void GLGizmoEmboss::draw_font_list() if (ImGui::IsItemHovered()) ImGui::SetTooltip("%s", wx_face_name.ToUTF8().data()); if (is_selected) ImGui::SetItemDefaultFocus(); - draw_font_preview(face); + bool is_visible = ImGui::IsItemVisible(); + if (is_visible && face.name_truncated.empty()) { + float width = m_gui_cfg->face_name_max_width; + std::string name_str(face.wx_name.ToUTF8().data()); + face.name_truncated = ImGuiWrapper::trunc(name_str, width); + } + draw_font_preview(face, is_visible); } #ifdef SHOW_FONT_COUNT ImGui::TextColored(ImGuiWrapper::COL_GREY_LIGHT, "Count %d", diff --git a/src/slic3r/GUI/Gizmos/GLGizmoEmboss.hpp b/src/slic3r/GUI/Gizmos/GLGizmoEmboss.hpp index fecbeadaa..f8a6a4d1f 100644 --- a/src/slic3r/GUI/Gizmos/GLGizmoEmboss.hpp +++ b/src/slic3r/GUI/Gizmos/GLGizmoEmboss.hpp @@ -112,7 +112,7 @@ private: void draw_style_add_button(); void init_font_name_texture(); struct FaceName; - void draw_font_preview(FaceName &face); + void draw_font_preview(FaceName &face, bool is_visible); void draw_font_list(); void draw_style_edit(); bool draw_italic_button(); diff --git a/src/slic3r/Utils/WxFontUtils.hpp b/src/slic3r/Utils/WxFontUtils.hpp index 3115a5bd7..c470b2741 100644 --- a/src/slic3r/Utils/WxFontUtils.hpp +++ b/src/slic3r/Utils/WxFontUtils.hpp @@ -27,7 +27,6 @@ public: static EmbossStyle::Type get_actual_type(); static EmbossStyle create_emboss_style(const wxFont &font, const std::string& name = ""); - // load font used by Operating system as default GUI static std::string get_human_readable_name(const wxFont &font); // serialize / deserialize font