From 664fb8120c28fb3ba1fbdb4946938e2da0f873ee Mon Sep 17 00:00:00 2001 From: Filip Sykala Date: Fri, 13 May 2022 15:06:31 +0200 Subject: [PATCH] font style combo contain grayed style text --- src/slic3r/GUI/Gizmos/GLGizmoEmboss.cpp | 58 ++++++++----------- src/slic3r/GUI/Gizmos/GLGizmoEmboss.hpp | 6 +- .../GUI/Jobs/CreateFontStyleImagesJob.cpp | 17 ++++-- src/slic3r/Utils/FontManager.cpp | 16 ++++- src/slic3r/Utils/FontManager.hpp | 16 +++-- 5 files changed, 64 insertions(+), 49 deletions(-) diff --git a/src/slic3r/GUI/Gizmos/GLGizmoEmboss.cpp b/src/slic3r/GUI/Gizmos/GLGizmoEmboss.cpp index c9655b2ec8..027794eab0 100644 --- a/src/slic3r/GUI/Gizmos/GLGizmoEmboss.cpp +++ b/src/slic3r/GUI/Gizmos/GLGizmoEmboss.cpp @@ -654,7 +654,6 @@ void GLGizmoEmboss::initialize() if (cfg.icon_width % 2 != 0) ++cfg.icon_width; float icon_width_with_spacing = cfg.icon_width + space; - float scroll_width = icon_width_with_spacing; // TODO: fix it cfg.delete_pos_x = cfg.max_font_name_width + space; int count_line_of_text = 3; cfg.text_size = ImVec2(-FLT_MIN, line_height_with_spacing * count_line_of_text); @@ -720,9 +719,10 @@ void GLGizmoEmboss::initialize() ImVec2(cfg.minimal_window_size.x, cfg.minimal_window_size.y + advance_height); - cfg.min_style_image_height = line_height_with_spacing; - cfg.max_style_image_width = cfg.max_font_name_width - + int max_style_image_width = cfg.max_font_name_width - 2 * style.FramePadding.x; + int max_style_image_height = 2 * input_height; + cfg.max_style_image_size = Vec2i(max_style_image_width, max_style_image_height); m_gui_cfg.emplace(std::move(cfg)); init_icons(); @@ -1359,7 +1359,6 @@ void GLGizmoEmboss::draw_font_list() // select font file by file browser if (draw_button(IconType::open_file)) { if (choose_true_type_file()) { - m_font_manager.free_style_images(); process(); } } else if (ImGui::IsItemHovered()) @@ -1369,8 +1368,7 @@ void GLGizmoEmboss::draw_font_list() #ifdef ALLOW_ADD_FONT_BY_OS_SELECTOR ImGui::SameLine(); if (draw_button(IconType::system_selector)) { - if (choose_font_by_wxdialog()) { - m_font_manager.free_style_images(); + if (choose_font_by_wxdialog()) { process(); } } else if (ImGui::IsItemHovered()) @@ -1453,8 +1451,14 @@ void GLGizmoEmboss::draw_model_type() } } -void GLGizmoEmboss::draw_rename_style(bool start_rename) +void GLGizmoEmboss::draw_rename_style_botton() { + bool start_rename = false; + if (draw_button(IconType::rename)) + start_rename = true; + else if (ImGui::IsItemHovered()) + ImGui::SetTooltip("%s", _u8L("Rename actual style.").c_str()); + std::string title = _u8L("Rename style"); const char * popup_id = title.c_str(); static FontItem * rename_item; @@ -1493,7 +1497,6 @@ void GLGizmoEmboss::draw_rename_style(bool start_rename) m_imgui->button(_L("ok"), ImVec2(0.f, 0.f), allow_change)) { rename_item->name = new_name; m_font_manager.get_truncated_name().clear(); - m_font_manager.free_style_images(); ImGui::CloseCurrentPopup(); select_stored_font_item(); } @@ -1600,7 +1603,6 @@ void GLGizmoEmboss::draw_undo_style_button(bool is_stored, bool is_changed) m_font_manager.get_wx_font() = WxFontUtils::load_wxFont(font_item.path); m_font_manager.wx_font_changed(); } - m_font_manager.free_style_images(); process(); } else if (ImGui::IsItemHovered()) { if (can_undo) @@ -1640,7 +1642,8 @@ void GLGizmoEmboss::draw_style_list() { ImGui::SameLine(m_gui_cfg->style_offset); ImGui::SetNextItemWidth(m_gui_cfg->input_width); if (ImGui::BeginCombo("##style_selector", trunc_name.c_str())) { - m_font_manager.init_style_images(m_gui_cfg->max_style_image_width); + m_font_manager.init_style_images(m_gui_cfg->max_style_image_size, m_text); + m_font_manager.init_trunc_names(max_width); std::optional> swap_indexes; const std::vector &styles = m_font_manager.get_styles(); for (const auto &item : styles) { @@ -1650,18 +1653,11 @@ void GLGizmoEmboss::draw_style_list() { ImGui::PushID(actual_style_name.c_str()); bool is_selected = (&fi == &actual_font_item); - ImVec2 select_size(0,0); // 0,0 --> calculate in draw - std::string selectable_name = "##style_select"; // ## --> no visible - if (item.image.has_value()) { - const FontManager::StyleImage &img = *item.image; - select_size = ImVec2(0.f, std::max(img.tex_size.y, m_gui_cfg->min_style_image_height)); - } else { - selectable_name = ImGuiWrapper::trunc(actual_style_name, max_width); - } - + ImVec2 select_size(0,m_gui_cfg->max_style_image_size.y()); // 0,0 --> calculate in draw + const std::optional img = item.image; // allow click delete button ImGuiSelectableFlags_ flags = ImGuiSelectableFlags_AllowItemOverlap; - if (ImGui::Selectable(selectable_name.c_str(), is_selected, flags, select_size)) { + if (ImGui::Selectable(item.truncated_name.c_str(), is_selected, flags, select_size)) { if (m_font_manager.load_font(index)) { process(); select_stored_font_item(); @@ -1681,10 +1677,9 @@ void GLGizmoEmboss::draw_style_list() { } // draw style name - if (item.image.has_value()) { - const FontManager::StyleImage &img = *item.image; + if (img.has_value()) { ImGui::SameLine(); - ImGui::Image(img.texture_id, img.tex_size, img.uv0, img.uv1); + ImGui::Image(img->texture_id, img->tex_size, img->uv0, img->uv1); } ImGui::PopID(); @@ -1693,15 +1688,15 @@ void GLGizmoEmboss::draw_style_list() { m_font_manager.swap(swap_indexes->first, swap_indexes->second); ImGui::EndCombo(); - }else if (ImGui::IsItemHovered()) - ImGui::SetTooltip("%s", _u8L("Style selector").c_str()); + } else { + // do not keep in memory style images when no combo box open + m_font_manager.free_style_images(); + if (ImGui::IsItemHovered()) + ImGui::SetTooltip("%s", _u8L("Style selector").c_str()); + } ImGui::SameLine(); - bool start_rename = false; - if (draw_button(IconType::rename)) start_rename = true; - else if (ImGui::IsItemHovered()) - ImGui::SetTooltip("%s", _u8L("Rename actual style.").c_str()); - draw_rename_style(start_rename); + draw_rename_style_botton(); ImGui::SameLine(); if (draw_button(IconType::duplicate)) m_font_manager.duplicate(); @@ -1815,7 +1810,6 @@ bool GLGizmoEmboss::bold_button() { // bold font can't be loaded // set up boldness boldness = 20.f; - m_font_manager.free_style_images(); //font_file->cache.empty(); } return true; @@ -1952,7 +1946,6 @@ void GLGizmoEmboss::draw_style_edit() { } if (exist_change) { - m_font_manager.free_style_images(); m_font_manager.clear_glyphs_cache(); process(); } @@ -2293,7 +2286,6 @@ void GLGizmoEmboss::draw_advanced() } if (exist_change) { - m_font_manager.free_style_images(); m_font_manager.clear_glyphs_cache(); process(); } diff --git a/src/slic3r/GUI/Gizmos/GLGizmoEmboss.hpp b/src/slic3r/GUI/Gizmos/GLGizmoEmboss.hpp index 0be3a16336..5a48d8ada4 100644 --- a/src/slic3r/GUI/Gizmos/GLGizmoEmboss.hpp +++ b/src/slic3r/GUI/Gizmos/GLGizmoEmboss.hpp @@ -102,7 +102,7 @@ private: void draw_delete_style_button(); void draw_undo_style_button(bool is_stored, bool is_changed); void draw_revert_all_styles_button(); - void draw_rename_style(bool start_rename); + void draw_rename_style_botton(); void draw_font_list(); void draw_style_edit(); bool italic_button(); @@ -175,8 +175,8 @@ private: float max_font_name_width = 0.f; unsigned int icon_width = 0; - float min_style_image_height = 0.f; - int max_style_image_width = 0.f; + // maximal width and height of style image + Vec2i max_style_image_size = Vec2i(0, 0); float style_offset = 0.f; float input_offset = 0.f; diff --git a/src/slic3r/GUI/Jobs/CreateFontStyleImagesJob.cpp b/src/slic3r/GUI/Jobs/CreateFontStyleImagesJob.cpp index 4e9ad2492f..5ae257f979 100644 --- a/src/slic3r/GUI/Jobs/CreateFontStyleImagesJob.cpp +++ b/src/slic3r/GUI/Jobs/CreateFontStyleImagesJob.cpp @@ -19,7 +19,9 @@ CreateFontStyleImagesJob::CreateFontStyleImagesJob( { assert(m_input.result != nullptr); assert(!m_input.styles.empty()); - assert(m_input.max_width > 1); + assert(!m_input.text.empty()); + assert(m_input.max_size.x() > 1); + assert(m_input.max_size.y() > 1); } void CreateFontStyleImagesJob::process(Ctl &ctl) @@ -32,7 +34,7 @@ void CreateFontStyleImagesJob::process(Ctl &ctl) for (auto &item : m_input.styles) { size_t index = &item - &m_input.styles.front(); ExPolygons &shapes = name_shapes[index]; - shapes = Emboss::text2shapes(item.font, item.text.c_str(), item.prop); + shapes = Emboss::text2shapes(item.font, m_input.text.c_str(), item.prop); // create image description FontManager::StyleImage &image = images[index]; @@ -60,8 +62,11 @@ void CreateFontStyleImagesJob::process(Ctl &ctl) image.tex_size.y = std::ceil(bb2.max.y() - bb2.min.y()); // crop image width - if (image.tex_size.x > m_input.max_width) - image.tex_size.x = m_input.max_width; + if (image.tex_size.x > m_input.max_size.x()) + image.tex_size.x = m_input.max_size.x(); + // crop image height + if (image.tex_size.y > m_input.max_size.y()) + image.tex_size.y = m_input.max_size.y(); } // arrange bounding boxes @@ -100,6 +105,8 @@ void CreateFontStyleImagesJob::process(Ctl &ctl) // copy rastered data to pixels sla::RasterEncoder encoder = [&offset = image.offset, &pix = pixels, w=width,h=height] (const void *ptr, size_t width, size_t height, size_t num_components) { + // bigger value create darker image + unsigned char gray_level = 5; size_t size {static_cast(w*h)}; assert((offset.x() + width) <= w); assert((offset.y() + height) <= h); @@ -109,7 +116,7 @@ void CreateFontStyleImagesJob::process(Ctl &ctl) size_t index = (offset.y() + y)*w + offset.x() + x; assert(index < size); if (index >= size) continue; - pix[index] = ptr2[y * width + x]; + pix[index] = ptr2[y * width + x] / gray_level; } return sla::EncodedRaster(); }; diff --git a/src/slic3r/Utils/FontManager.cpp b/src/slic3r/Utils/FontManager.cpp index 45d8bc2d04..a3a3ddcde2 100644 --- a/src/slic3r/Utils/FontManager.cpp +++ b/src/slic3r/Utils/FontManager.cpp @@ -344,13 +344,23 @@ ImFont* FontManager::extend_imgui_font_range(size_t index, const std::string& te return create_imgui_font(index, text); } + +void FontManager::init_trunc_names(float max_width) { + for (auto &s : m_font_list) { + if (s.truncated_name.empty()) + s.truncated_name = ImGuiWrapper::trunc(s.font_item.name, max_width); + } +} + #include "slic3r/GUI/Jobs/CreateFontStyleImagesJob.hpp" // for access to worker #include "slic3r/GUI/GUI_App.hpp" #include "slic3r/GUI/Plater.hpp" -void FontManager::init_style_images(int max_width) { +void FontManager::init_style_images(const Vec2i &max_size, + const std::string &text) +{ // check already initialized if (m_exist_style_images) return; @@ -398,13 +408,13 @@ void FontManager::init_style_images(int max_width) { } auto &worker = wxGetApp().plater()->get_ui_job_worker(); - StyleImagesData data{std::move(styles), max_width, m_temp_style_images}; + StyleImagesData data{std::move(styles), max_size, text, m_temp_style_images}; queue_job(worker, std::make_unique(std::move(data))); } void FontManager::free_style_images() { - if (!is_activ_font()) return; if (!m_exist_style_images) return; + if (!is_activ_font()) return; GLuint tex_id = 0; diff --git a/src/slic3r/Utils/FontManager.hpp b/src/slic3r/Utils/FontManager.hpp index dc9a081e33..7cb356779d 100644 --- a/src/slic3r/Utils/FontManager.hpp +++ b/src/slic3r/Utils/FontManager.hpp @@ -105,11 +105,16 @@ public: // free used memory and font file data void free_except_active_font(); + + // init truncated names of styles + void init_trunc_names(float max_width); /// - /// initialization texture with rendered font style + /// Initialization texture with rendered font style /// - void init_style_images(int max_width); + /// Maximal width and height of one style texture + /// Text to render by style + void init_style_images(const Vec2i& max_size, const std::string &text); void free_style_images(); struct Item; @@ -208,9 +213,10 @@ private: // Keep styles to render Items styles; - - // Maximal width in pixels of image - int max_width; + // Maximal width and height in pixels of image + Vec2i max_size; + // Text to render + std::string text; /// /// Result of job