From a1914f5f94b44a3835be18af870a3c9b64b0594e Mon Sep 17 00:00:00 2001 From: Filip Sykala - NTB T15p Date: Wed, 3 Aug 2022 19:28:02 +0200 Subject: [PATCH] add getter on font atlas into style manager - get imgui font could return nonValid imgui font to recognize font without loaded symbol --- src/slic3r/GUI/Gizmos/GLGizmoEmboss.cpp | 174 +++++++++++------------- src/slic3r/GUI/Gizmos/GLGizmoEmboss.hpp | 2 - src/slic3r/Utils/EmbossStyleManager.cpp | 18 ++- src/slic3r/Utils/EmbossStyleManager.hpp | 4 + 4 files changed, 93 insertions(+), 105 deletions(-) diff --git a/src/slic3r/GUI/Gizmos/GLGizmoEmboss.cpp b/src/slic3r/GUI/Gizmos/GLGizmoEmboss.cpp index b84a9b1dad..d847bfacf2 100644 --- a/src/slic3r/GUI/Gizmos/GLGizmoEmboss.cpp +++ b/src/slic3r/GUI/Gizmos/GLGizmoEmboss.cpp @@ -53,13 +53,7 @@ #define ALLOW_REVERT_ALL_STYLES #endif // ALLOW_DEBUG_MODE -//#define SHOW_WX_FONT_DESCRIPTOR -//#define SHOW_FONT_FILE_PROPERTY -//#define SHOW_FONT_COUNT #define SHOW_CONTAIN_3MF_FIX -//#define ALLOW_ADD_FONT_BY_FILE -//#define ALLOW_ADD_FONT_BY_OS_SELECTOR -//#define ALLOW_REVERT_ALL_STYLES using namespace Slic3r; using namespace Slic3r::GUI; @@ -129,6 +123,7 @@ GLGizmoEmboss::GLGizmoEmboss(GLCanvas3D &parent) m_rotate_gizmo.set_group_id(0); // TODO: add suggestion to use https://fontawesome.com/ // (copy & paste) unicode symbols from web + // paste HEX unicode into notepad move cursor after unicode press [alt] + [x] } void GLGizmoEmboss::set_fine_position() @@ -901,7 +896,7 @@ bool GLGizmoEmboss::process() if (m_text.empty()) return false; // exist loaded font file? - if (!m_style_manager.get_font_file_with_cache().has_value()) return false; + if (!m_style_manager.is_activ_font()) return false; // Cancel previous Job, when it is in process // Can't use cancel, because I want cancel only previous EmbossUpdateJob no other jobs @@ -984,18 +979,14 @@ void GLGizmoEmboss::draw_window() } #endif // ALLOW_DEBUG_MODE - bool is_selected_style = m_style_manager.is_activ_font(); - bool exist_font_file = is_selected_style && - m_style_manager.get_font_file_with_cache().has_value(); - if (!is_selected_style) { - m_imgui->text_colored(ImGuiWrapper::COL_ORANGE_LIGHT, _L("Warning: No text style is selected.")); - }else if (!exist_font_file) { + bool is_activ_font = m_style_manager.is_activ_font(); + if (!is_activ_font) m_imgui->text_colored(ImGuiWrapper::COL_ORANGE_LIGHT, _L("Warning: No font is selected. Select correct one.")); - } + draw_text_input(); draw_model_type(); draw_style_list(); - m_imgui->disabled_begin(!is_selected_style); + m_imgui->disabled_begin(!is_activ_font); ImGui::TreePush(); draw_style_edit(); ImGui::TreePop(); @@ -1008,7 +999,7 @@ void GLGizmoEmboss::draw_window() ImGui::TreePop(); } else if (m_is_advanced_edit_style) set_minimal_window_size(false); - m_imgui->disabled_end(); // !is_selected_style + m_imgui->disabled_end(); // !is_activ_font #ifdef SHOW_WX_FONT_DESCRIPTOR if (is_selected_style) @@ -1018,13 +1009,13 @@ void GLGizmoEmboss::draw_window() if (ImGui::Button(_u8L("Close").c_str())) close(); // Option to create text volume when reselecting volumes - m_imgui->disabled_begin(!exist_font_file); + m_imgui->disabled_begin(!is_activ_font); if (m_volume == nullptr) { ImGui::SameLine(); if (ImGui::Button(_u8L("Generate object").c_str())) create_volume(ModelVolumeType::MODEL_PART); } - m_imgui->disabled_end(); + m_imgui->disabled_end(); // !is_activ_font #ifdef SHOW_CONTAIN_3MF_FIX if (m_volume!=nullptr && @@ -1052,7 +1043,7 @@ void GLGizmoEmboss::draw_window() ImGui::Image((void *) t.get_id(), ImVec2(t.get_width(), t.get_height())); #endif //SHOW_ICONS_TEXTURE #ifdef SHOW_IMGUI_ATLAS - auto &atlas = m_style_manager.m_imgui_font_atlas; + const auto &atlas = m_style_manager.get_atlas(); ImGui::Image(atlas.TexID, ImVec2(atlas.TexWidth, atlas.TexHeight)); #endif // SHOW_IMGUI_ATLAS } @@ -1070,14 +1061,20 @@ void GLGizmoEmboss::draw_text_input() static const ImGuiInputTextFlags flags = ImGuiInputTextFlags_AllowTabInput | ImGuiInputTextFlags_AutoSelectAll; + ImFont *imgui_font = m_style_manager.get_imgui_font(); if (imgui_font == nullptr) { // try create new imgui font m_style_manager.create_imgui_font(create_range_text()); imgui_font = m_style_manager.get_imgui_font(); } - bool exist_font = imgui_font != nullptr; - assert(!exist_font || imgui_font->IsLoaded()); + bool exist_font = + imgui_font != nullptr && + imgui_font->IsLoaded() && + imgui_font->Scale > 0.f && + imgui_font->ContainerAtlas != nullptr; + // NOTE: Symbol fonts doesn't have atlas + // when their glyph range is out of language character range if (exist_font) ImGui::PushFont(imgui_font); // flag for extend font ranges if neccessary @@ -1155,9 +1152,10 @@ void GLGizmoEmboss::draw_text_input() ImGui::SetCursorPos(cursor); } + // NOTE: must be after ImGui::font_pop() + // -> imgui_font has to be unused // IMPROVE: only extend not clear // Extend font ranges - // imgui_font has to be unused if (!range_text.empty() && !m_imgui->contain_all_glyphs(imgui_font, range_text) ) { m_style_manager.clear_imgui_font(); @@ -1649,27 +1647,6 @@ void GLGizmoEmboss::draw_style_add_button() } } -void GLGizmoEmboss::draw_style_undo_button() { - const EmbossStyle *stored_style = nullptr; - bool is_stored = m_style_manager.exist_stored_style(); - if (is_stored) - stored_style = m_style_manager.get_stored_style(); - const EmbossStyle &style = m_style_manager.get_style(); - bool is_changed = (stored_style)? !(*stored_style == style) : true; - bool can_undo = is_stored && is_changed; - if (draw_button(IconType::undo, !can_undo)) { - discard_changes_in_style(); - process(); - } else if (ImGui::IsItemHovered()) { - if (can_undo) - ImGui::SetTooltip("%s", _u8L("Discard style changes.").c_str()); - else if (!is_stored) - ImGui::SetTooltip("%s", _u8L("Not stored yet.").c_str()); - else if (!is_changed) - ImGui::SetTooltip("%s", _u8L("No change to discard.").c_str()); - } -} - void GLGizmoEmboss::draw_delete_style_button() { bool is_stored = m_style_manager.exist_stored_style(); bool is_last = m_style_manager.get_styles().size() == 1; @@ -1731,51 +1708,51 @@ void GLGizmoEmboss::draw_delete_style_button() { } } -void GLGizmoEmboss::discard_changes_in_style() -{ - if (!m_style_manager.exist_stored_style()) return; - - EmbossStyle &emboss_style = m_style_manager.get_style(); - const EmbossStyle* stored_style = m_style_manager.get_stored_style(); - assert(stored_style != nullptr); - - // is rotation changed - auto &angle = emboss_style.prop.angle; - const auto &angle_ = stored_style->prop.angle; - // TODO: compare with approx - if (angle.has_value() != angle_.has_value() || - (angle.has_value() && !is_approx(*angle, *angle_))) { - auto &tc = m_volume->text_configuration; - if (m_volume != nullptr && tc.has_value()) { - // change actual text configuration - tc->style.prop.angle = angle_; - float act_angle = angle_.has_value() ? *angle_ : .0f; - float prev_angle = angle.has_value() ? *angle : .0f; - do_rotate(act_angle - prev_angle); - } - } - - // is distance changed - auto &distance = emboss_style.prop.distance; - const auto &distance_ = stored_style->prop.distance; - if (distance.has_value() != distance_.has_value() || - (distance.has_value() && !is_approx(*distance, *distance_))) { - auto &tc = m_volume->text_configuration; - if (m_volume != nullptr && tc.has_value()) { - tc->style.prop.distance = distance_; - float act_distance = distance_.has_value() ? *distance_ : .0f; - float prev_distance = distance.has_value() ? *distance : .0f; - do_translate(Vec3d::UnitZ() * (act_distance - prev_distance)); - } - } - - if (emboss_style.path != stored_style->path) { - // NOTE: load font file again - m_style_manager.load_style(m_style_manager.get_style_index()); - //m_style_manager.get_wx_font() = WxFontUtils::load_wxFont(emboss_style.path); - //m_style_manager.wx_font_changed(); - } -} +//void GLGizmoEmboss::discard_changes_in_style() +//{ +// if (!m_style_manager.exist_stored_style()) return; +// +// EmbossStyle &emboss_style = m_style_manager.get_style(); +// const EmbossStyle* stored_style = m_style_manager.get_stored_style(); +// assert(stored_style != nullptr); +// +// // is rotation changed +// auto &angle = emboss_style.prop.angle; +// const auto &angle_ = stored_style->prop.angle; +// // TODO: compare with approx +// if (angle.has_value() != angle_.has_value() || +// (angle.has_value() && !is_approx(*angle, *angle_))) { +// auto &tc = m_volume->text_configuration; +// if (m_volume != nullptr && tc.has_value()) { +// // change actual text configuration +// tc->style.prop.angle = angle_; +// float act_angle = angle_.has_value() ? *angle_ : .0f; +// float prev_angle = angle.has_value() ? *angle : .0f; +// do_rotate(act_angle - prev_angle); +// } +// } +// +// // is distance changed +// auto &distance = emboss_style.prop.distance; +// const auto &distance_ = stored_style->prop.distance; +// if (distance.has_value() != distance_.has_value() || +// (distance.has_value() && !is_approx(*distance, *distance_))) { +// auto &tc = m_volume->text_configuration; +// if (m_volume != nullptr && tc.has_value()) { +// tc->style.prop.distance = distance_; +// float act_distance = distance_.has_value() ? *distance_ : .0f; +// float prev_distance = distance.has_value() ? *distance : .0f; +// do_translate(Vec3d::UnitZ() * (act_distance - prev_distance)); +// } +// } +// +// if (emboss_style.path != stored_style->path) { +// // NOTE: load font file again +// m_style_manager.load_style(m_style_manager.get_style_index()); +// //m_style_manager.get_wx_font() = WxFontUtils::load_wxFont(emboss_style.path); +// //m_style_manager.wx_font_changed(); +// } +//} void GLGizmoEmboss::draw_revert_all_styles_button() { if (draw_button(IconType::revert_all)) { @@ -1821,8 +1798,16 @@ void GLGizmoEmboss::draw_style_list() { // allow click delete button ImGuiSelectableFlags_ flags = ImGuiSelectableFlags_AllowItemOverlap; if (ImGui::Selectable(item.truncated_name.c_str(), is_selected, flags, select_size)) { - if (m_style_manager.load_style(index)) + if (m_style_manager.load_style(index)) { + // TODO: + // fix volume transformation after change style --> + // rotation or z-move + // HELP: discard_changes_in_style() + // void style_changed(const FontProp &prev, const FontProp &act); process(); + } else { + // TODO: inform user that can't load and erase style + } } else if (ImGui::IsItemHovered()) ImGui::SetTooltip("%s", actual_style_name.c_str()); @@ -1864,11 +1849,7 @@ void GLGizmoEmboss::draw_style_list() { ImGui::SameLine(); draw_style_add_button(); - - // undo button - ImGui::SameLine(); - draw_style_undo_button(); - + #ifdef ALLOW_REVERT_ALL_STYLES ImGui::SameLine(); draw_revert_all_styles_button(); @@ -2644,7 +2625,7 @@ EmbossDataBase GLGizmoEmboss::create_emboss_data_base() { std::string default_text_for_emboss = _u8L("Embossed text"); EmbossStyle es = m_style_manager.get_style(); TextConfiguration tc{es, default_text_for_emboss}; - // TODO: investigace how to initialize + // TODO: investigate how to initialize return tc; } @@ -2652,7 +2633,8 @@ EmbossDataBase GLGizmoEmboss::create_emboss_data_base() { // actualize font path - during changes in gui it could be corrupted // volume must store valid path assert(m_style_manager.get_wx_font().has_value()); - es.path = WxFontUtils::store_wxFont(*m_style_manager.get_wx_font()); + assert(es.path.compare(WxFontUtils::store_wxFont(*m_style_manager.get_wx_font())) == 0); + //es.path = WxFontUtils::store_wxFont(*m_style_manager.get_wx_font()); return TextConfiguration{es, m_text}; }; diff --git a/src/slic3r/GUI/Gizmos/GLGizmoEmboss.hpp b/src/slic3r/GUI/Gizmos/GLGizmoEmboss.hpp index a9d063df84..3889064c3c 100644 --- a/src/slic3r/GUI/Gizmos/GLGizmoEmboss.hpp +++ b/src/slic3r/GUI/Gizmos/GLGizmoEmboss.hpp @@ -101,14 +101,12 @@ private: void draw_model_type(); void draw_style_list(); void draw_delete_style_button(); - void discard_changes_in_style(); void draw_revert_all_styles_button(); void draw_style_rename_popup(); void draw_style_rename_button(); void draw_style_save_button(); void draw_style_save_as_popup(); void draw_style_add_button(); - void draw_style_undo_button(); void draw_font_list(); void draw_style_edit(); bool draw_italic_button(); diff --git a/src/slic3r/Utils/EmbossStyleManager.cpp b/src/slic3r/Utils/EmbossStyleManager.cpp index 0047655649..02967504ce 100644 --- a/src/slic3r/Utils/EmbossStyleManager.cpp +++ b/src/slic3r/Utils/EmbossStyleManager.cpp @@ -124,8 +124,14 @@ void EmbossStyleManager::discard_style_changes() { if (m_style_cache.style == m_style_items[m_stored_activ_index].style && m_style_cache.wx_font == m_style_cache.stored_wx_font) return; // already loaded - if (load_style(m_style_cache.style_index)) return; - } + if (load_style(m_style_cache.style_index)) + return; // correct reload style + } + + if (load_style(m_stored_activ_index)) + return; // correct reload last activ font + + // try to save situation by load some font load_first_valid_font(); } @@ -226,6 +232,7 @@ bool EmbossStyleManager::exist_stored_style() const { return m_style_cache.style size_t EmbossStyleManager::get_style_index() const { return m_style_cache.style_index; } Emboss::FontFileWithCache &EmbossStyleManager::get_font_file_with_cache() { return m_style_cache.font_file; } std::string &EmbossStyleManager::get_truncated_name() { return m_style_cache.truncated_name; } +const ImFontAtlas &EmbossStyleManager::get_atlas() const { return m_style_cache.atlas; } void EmbossStyleManager::clear_glyphs_cache() { @@ -249,10 +256,6 @@ ImFont *EmbossStyleManager::get_imgui_font() if (f_size != 1) return nullptr; ImFont *font = fonts.front(); if (font == nullptr) return nullptr; - if (!font->IsLoaded()) return nullptr; - if (font->Scale <= 0.f) return nullptr; - // Symbol fonts doesn't have atlas because their glyph range is out of language range - if (font->ContainerAtlas == nullptr) return nullptr; return font; } @@ -377,7 +380,7 @@ void EmbossStyleManager::free_style_images() { float EmbossStyleManager::min_imgui_font_size = 18.f; float EmbossStyleManager::max_imgui_font_size = 60.f; float EmbossStyleManager::get_imgui_font_size(const FontProp &prop, - const Emboss::FontFile &file) + const Emboss::FontFile &file) { const auto &cn = prop.collection_number; unsigned int font_index = (cn.has_value()) ? *cn : 0; @@ -464,6 +467,7 @@ ImFont *EmbossStyleManager::create_imgui_font(const std::string &text) assert(!m_style_cache.atlas.Fonts.empty()); if (m_style_cache.atlas.Fonts.empty()) return nullptr; assert(font == m_style_cache.atlas.Fonts.back()); + if (!font->IsLoaded()) return nullptr; assert(font->IsLoaded()); return font; } diff --git a/src/slic3r/Utils/EmbossStyleManager.hpp b/src/slic3r/Utils/EmbossStyleManager.hpp index 4fb1770180..950657b795 100644 --- a/src/slic3r/Utils/EmbossStyleManager.hpp +++ b/src/slic3r/Utils/EmbossStyleManager.hpp @@ -118,10 +118,14 @@ public: const FontProp &get_font_prop() const; FontProp &get_font_prop(); + const ImFontAtlas& get_atlas() const; + // getter on activ wx font const std::optional &get_wx_font() const; + // True when activ style has same name as some of stored style bool exist_stored_style() const; + size_t get_style_index() const; // getter on font file with cache