diff --git a/src/slic3r/GUI/Gizmos/GLGizmoEmboss.cpp b/src/slic3r/GUI/Gizmos/GLGizmoEmboss.cpp index 0c64a8627f..794e28941a 100644 --- a/src/slic3r/GUI/Gizmos/GLGizmoEmboss.cpp +++ b/src/slic3r/GUI/Gizmos/GLGizmoEmboss.cpp @@ -1281,7 +1281,7 @@ void GLGizmoEmboss::draw_font_list() // Set partial wxString actual_face_name; if (m_style_manager.is_activ_font()) { - std::optional &wx_font_opt = m_style_manager.get_wx_font(); + const std::optional &wx_font_opt = m_style_manager.get_wx_font(); if (wx_font_opt.has_value()) actual_face_name = wx_font_opt->GetFaceName(); } @@ -1882,23 +1882,26 @@ void GLGizmoEmboss::draw_style_list() { bool GLGizmoEmboss::draw_italic_button() { - std::optional &wx_font = m_style_manager.get_wx_font(); + const std::optional &wx_font_opt = m_style_manager.get_wx_font(); const auto& ff = m_style_manager.get_font_file_with_cache(); - if (!wx_font.has_value() || !ff.has_value()) { + if (!wx_font_opt.has_value() || !ff.has_value()) { draw_icon(IconType::italic, IconState::disabled); return false; } + const wxFont& wx_font = *wx_font_opt; std::optional &skew = m_style_manager.get_font_prop().skew; - bool is_font_italic = skew.has_value() || WxFontUtils::is_italic(*wx_font); + bool is_font_italic = skew.has_value() || WxFontUtils::is_italic(wx_font); if (is_font_italic) { // unset italic if (draw_clickable(IconType::italic, IconState::hovered, IconType::unitalic, IconState::hovered)) { skew.reset(); - if (wx_font->GetStyle() != wxFontStyle::wxFONTSTYLE_NORMAL) { - wx_font->SetStyle(wxFontStyle::wxFONTSTYLE_NORMAL); - m_style_manager.wx_font_changed(); + if (wx_font.GetStyle() != wxFontStyle::wxFONTSTYLE_NORMAL) { + wxFont new_wx_font = wx_font; // copy + new_wx_font.SetStyle(wxFontStyle::wxFONTSTYLE_NORMAL); + if(!m_style_manager.set_wx_font(new_wx_font)) + return false; } return true; } @@ -1907,9 +1910,11 @@ bool GLGizmoEmboss::draw_italic_button() } else { // set italic if (draw_button(IconType::italic)) { - auto new_ff = WxFontUtils::set_italic(*wx_font, *ff.font_file); + wxFont new_wx_font = wx_font; // copy + auto new_ff = WxFontUtils::set_italic(new_wx_font, *ff.font_file); if (new_ff != nullptr) { - m_style_manager.wx_font_changed(std::move(new_ff)); + if(!m_style_manager.set_wx_font(new_wx_font, std::move(new_ff))) + return false; } else { // italic font doesn't exist // add skew when wxFont can't set it @@ -1924,23 +1929,26 @@ bool GLGizmoEmboss::draw_italic_button() } bool GLGizmoEmboss::draw_bold_button() { - std::optional &wx_font = m_style_manager.get_wx_font(); + const std::optional &wx_font_opt = m_style_manager.get_wx_font(); const auto& ff = m_style_manager.get_font_file_with_cache(); - if (!wx_font.has_value() || !ff.has_value()) { + if (!wx_font_opt.has_value() || !ff.has_value()) { draw_icon(IconType::bold, IconState::disabled); return false; } + const wxFont &wx_font = *wx_font_opt; std::optional &boldness = m_style_manager.get_font_prop().boldness; - bool is_font_bold = boldness.has_value() || WxFontUtils::is_bold(*wx_font); + bool is_font_bold = boldness.has_value() || WxFontUtils::is_bold(wx_font); if (is_font_bold) { // unset bold if (draw_clickable(IconType::bold, IconState::hovered, IconType::unbold, IconState::hovered)) { boldness.reset(); - if (wx_font->GetWeight() != wxFontWeight::wxFONTWEIGHT_NORMAL) { - wx_font->SetWeight(wxFontWeight::wxFONTWEIGHT_NORMAL); - m_style_manager.wx_font_changed(); + if (wx_font.GetWeight() != wxFontWeight::wxFONTWEIGHT_NORMAL) { + wxFont new_wx_font = wx_font; // copy + new_wx_font.SetWeight(wxFontWeight::wxFONTWEIGHT_NORMAL); + if(!m_style_manager.set_wx_font(new_wx_font)) + return false; } return true; } @@ -1949,9 +1957,11 @@ bool GLGizmoEmboss::draw_bold_button() { } else { // set bold if (draw_button(IconType::bold)) { - auto new_ff = WxFontUtils::set_bold(*wx_font, *ff.font_file); + wxFont new_wx_font = wx_font; // copy + auto new_ff = WxFontUtils::set_bold(new_wx_font, *ff.font_file); if (new_ff != nullptr) { - m_style_manager.wx_font_changed(std::move(new_ff)); + if(!m_style_manager.set_wx_font(new_wx_font, std::move(new_ff))) + return false; } else { // bold font can't be loaded // set up boldness @@ -2049,33 +2059,34 @@ bool GLGizmoEmboss::rev_checkbox(const std::string &name, void GLGizmoEmboss::draw_style_edit() { const GuiCfg::Translations &tr = m_gui_cfg->translations; - std::optional &wx_font = m_style_manager.get_wx_font(); + const std::optional &wx_font_opt = m_style_manager.get_wx_font(); EmbossStyle &style = m_style_manager.get_font_item(); - assert(wx_font.has_value()); - // TODO: should not be there - // when actual font not loaded try to load - if (!wx_font.has_value() && style.type == WxFontUtils::get_actual_type()) - wx_font = WxFontUtils::load_wxFont(style.path); + assert(wx_font_opt.has_value()); + if (!wx_font_opt.has_value()) { + ImGui::TextColored(ImGuiWrapper::COL_ORANGE_DARK, "%s", _u8L("Font is not loaded properly. ")); + return; + } + bool exist_stored_style = m_style_manager.exist_stored_style(); bool is_font_changed = false; - if (exist_stored_style && - wx_font.has_value()) { + if (exist_stored_style && wx_font_opt.has_value()) { + const wxFont &wx_font = *wx_font_opt; const EmbossStyle *stored_style = m_style_manager.get_stored_font_item(); assert(stored_style != nullptr); const std::optional &stored_wx = m_style_manager.get_stored_wx_font(); assert(stored_wx.has_value()); - bool is_font_face_changed = stored_wx->GetFaceName() != wx_font->GetFaceName(); + bool is_font_face_changed = stored_wx->GetFaceName() != wx_font.GetFaceName(); const std::optional &skew = m_style_manager.get_font_prop().skew; - bool is_italic = skew.has_value() || WxFontUtils::is_italic(*wx_font); + bool is_italic = skew.has_value() || WxFontUtils::is_italic(wx_font); const std::optional &skew_stored = stored_style->prop.skew; bool is_stored_italic = skew_stored.has_value() || WxFontUtils::is_italic(*stored_wx); bool is_italic_changed = is_italic != is_stored_italic; const std::optional &boldness = m_style_manager.get_font_prop().boldness; - bool is_bold = boldness.has_value() || WxFontUtils::is_bold(*wx_font); + bool is_bold = boldness.has_value() || WxFontUtils::is_bold(wx_font); const std::optional &boldness_stored = stored_style->prop.boldness; bool is_stored_bold = boldness_stored.has_value() || WxFontUtils::is_bold(*stored_wx); bool is_bold_changed = is_bold != is_stored_bold; @@ -2107,9 +2118,10 @@ void GLGizmoEmboss::draw_style_edit() { style.prop.boldness = stored_style->prop.boldness; style.prop.skew = stored_style->prop.skew; - wx_font = WxFontUtils::load_wxFont(style.path); - m_style_manager.wx_font_changed(); - exist_change = true; + std::optional new_wx_font = WxFontUtils::load_wxFont(style.path); + if (new_wx_font.has_value() && + m_style_manager.set_wx_font(*new_wx_font)) + exist_change = true; } else if (ImGui::IsItemHovered()) ImGui::SetTooltip("%s", _u8L("Revert font changes.").c_str()); } @@ -2134,10 +2146,10 @@ void GLGizmoEmboss::draw_style_edit() { // store font size into path if (style.type == WxFontUtils::get_actual_type()) { - if (wx_font.has_value()) { - wx_font->SetPointSize( - static_cast(font_prop.size_in_mm)); - m_style_manager.wx_font_changed(); + if (wx_font_opt.has_value()) { + wxFont wx_font = *wx_font_opt; + wx_font.SetPointSize(static_cast(font_prop.size_in_mm)); + m_style_manager.set_wx_font(wx_font); } } process(); diff --git a/src/slic3r/Utils/EmbossStyleManager.cpp b/src/slic3r/Utils/EmbossStyleManager.cpp index 3438d4c5be..a7bb1b2667 100644 --- a/src/slic3r/Utils/EmbossStyleManager.cpp +++ b/src/slic3r/Utils/EmbossStyleManager.cpp @@ -148,25 +148,6 @@ void EmbossStyleManager::rename(const std::string& name) { } } -bool EmbossStyleManager::wx_font_changed(std::unique_ptr font_file) -{ - if (!is_activ_font()) return false; - auto &wx_font = get_wx_font(); - if (!wx_font.has_value()) return false; - - if (font_file == nullptr) { - font_file = WxFontUtils::create_font_file(*wx_font); - if (font_file == nullptr) return false; - } - m_style_cache.font_file = Emboss::FontFileWithCache(std::move(font_file)); - EmbossStyle &style = get_font_item(); - style.type = WxFontUtils::get_actual_type(); - style.path = WxFontUtils::store_wxFont(*wx_font); - clear_imgui_font(); - free_style_images(); - return true; -} - bool EmbossStyleManager::load_font(size_t font_index) { if (font_index >= m_style_items.size()) return false; @@ -229,7 +210,6 @@ const EmbossStyle &EmbossStyleManager::get_font_item() const { return m_style_ca const FontProp &EmbossStyleManager::get_font_prop() const { return get_font_item().prop; } FontProp &EmbossStyleManager::get_font_prop() { return get_font_item().prop; } const std::optional &EmbossStyleManager::get_wx_font() const { return m_style_cache.wx_font; } - std::optional &EmbossStyleManager::get_wx_font() { return m_style_cache.wx_font; } bool EmbossStyleManager::exist_stored_style() const { return m_style_cache.font_index != std::numeric_limits::max(); } size_t EmbossStyleManager::get_style_index() const { return m_style_cache.font_index; } @@ -480,12 +460,20 @@ ImFont *EmbossStyleManager::create_imgui_font(const std::string &text) bool EmbossStyleManager::set_wx_font(const wxFont &wx_font) { std::unique_ptr font_file = WxFontUtils::create_font_file(wx_font); + return set_wx_font(wx_font, std::move(font_file)); +} + +bool EmbossStyleManager::set_wx_font(const wxFont &wx_font, std::unique_ptr font_file) +{ if (font_file == nullptr) return false; m_style_cache.font_file = Emboss::FontFileWithCache(std::move(font_file)); - m_style_cache.wx_font = wx_font; // copy + EmbossStyle &style = m_style_cache.font_item; style.type = WxFontUtils::get_actual_type(); + // update string path + style.path = WxFontUtils::store_wxFont(wx_font); clear_imgui_font(); + free_style_images(); return true; -} +} \ No newline at end of file diff --git a/src/slic3r/Utils/EmbossStyleManager.hpp b/src/slic3r/Utils/EmbossStyleManager.hpp index 5d3a70155c..da727b902c 100644 --- a/src/slic3r/Utils/EmbossStyleManager.hpp +++ b/src/slic3r/Utils/EmbossStyleManager.hpp @@ -79,14 +79,7 @@ public: /// /// New name void rename(const std::string &name); - - /// - /// Actual wx font was changed - /// Clear caches - /// - /// font file created by WxFontUtils::create_font_file(wx_font) - bool wx_font_changed(std::unique_ptr font_file = nullptr); - + /// /// Change active font /// When font not loaded roll back activ font @@ -125,7 +118,6 @@ public: // getter on activ wx font const std::optional &get_wx_font() const; - std::optional &get_wx_font(); bool exist_stored_style() const; size_t get_style_index() const; @@ -136,9 +128,23 @@ public: // Getter for cached trucated name for style list selector std::string &get_truncated_name(); - // setter of font for actual selection + // setter of wx font for actual selection + + /// + /// Setter on wx_font when changed + /// + /// new wx font + /// True on success set otherwise FALSE bool set_wx_font(const wxFont &wx_font); + /// + /// Faster way of set wx_font when font file is known(do not load font file twice) + /// When you not sure that wx_font is made by font_file use only set_wx_font(wx_font) + /// + /// Must be source of font file + /// font file created by WxFontUtils::create_font_file(wx_font) + /// True on success otherwise false + bool set_wx_font(const wxFont &wx_font, std::unique_ptr font_file ); // Getter on acitve font pointer for imgui // Initialize imgui font(generate texture) when doesn't exist yet.