diff --git a/src/slic3r/GUI/Jobs/CreateFontStyleImagesJob.cpp b/src/slic3r/GUI/Jobs/CreateFontStyleImagesJob.cpp index 9d95818da9..43f93590b4 100644 --- a/src/slic3r/GUI/Jobs/CreateFontStyleImagesJob.cpp +++ b/src/slic3r/GUI/Jobs/CreateFontStyleImagesJob.cpp @@ -13,9 +13,14 @@ using namespace Slic3r; using namespace Slic3r::GUI; -CreateFontStyleImagesJob::CreateFontStyleImagesJob(StyleImagesData &&input) +CreateFontStyleImagesJob::CreateFontStyleImagesJob( + FontManager::StyleImagesData &&input) : m_input(std::move(input)) -{} +{ + assert(m_input.result != nullptr); + assert(!m_input.styles.empty()); + assert(m_input.max_width > 1); +} void CreateFontStyleImagesJob::process(Ctl &ctl) { @@ -24,7 +29,7 @@ void CreateFontStyleImagesJob::process(Ctl &ctl) std::vector scales(m_input.styles.size()); images = std::vector(m_input.styles.size()); - for (StyleImagesData::Item &item : m_input.styles) { + 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); @@ -124,20 +129,12 @@ void CreateFontStyleImagesJob::finalize(bool canceled, std::exception_ptr &) // set up texture id void *texture_id = (void *) (intptr_t) tex_id; - for (FontManager::StyleImage &image : images) { + for (FontManager::StyleImage &image : images) image.texture_id = texture_id; - size_t index = &image - &images.front(); - StyleImagesData::Item &style = m_input.styles[index]; - // find manager image and copy to it - for (auto& it: m_input.mng->m_font_list) { - if (it.font_item.name != style.text || - !(it.font_item.prop == style.prop)) - continue; - it.image = image; - break; - } - } + // move to result + m_input.result->styles = std::move(m_input.styles); + m_input.result->images = std::move(images); // bind default texture GLuint no_texture_id = 0; diff --git a/src/slic3r/GUI/Jobs/CreateFontStyleImagesJob.hpp b/src/slic3r/GUI/Jobs/CreateFontStyleImagesJob.hpp index ad9dffd73d..e66e66ce2b 100644 --- a/src/slic3r/GUI/Jobs/CreateFontStyleImagesJob.hpp +++ b/src/slic3r/GUI/Jobs/CreateFontStyleImagesJob.hpp @@ -9,29 +9,6 @@ namespace Slic3r::GUI { -/// -/// Data needed to create Font Style Images -/// -struct StyleImagesData -{ - struct Item - { - Emboss::FontFileWithCache font; - std::string text; - FontProp prop; - }; - using Items = std::vector; - - // Keep styles to render - Items styles; - - // maximal width in pixels of image - int max_width; - - // is used in finalize to set result - // and I Can't proof of alive - FontManager *mng; -}; /// /// Create texture with name of styles written by its style @@ -39,7 +16,7 @@ struct StyleImagesData /// class CreateFontStyleImagesJob : public Job { - StyleImagesData m_input; + FontManager::StyleImagesData m_input; // Output data // texture size @@ -50,7 +27,7 @@ class CreateFontStyleImagesJob : public Job std::vector images; public: - CreateFontStyleImagesJob(StyleImagesData &&input); + CreateFontStyleImagesJob(FontManager::StyleImagesData &&input); void process(Ctl &ctl) override; void finalize(bool canceled, std::exception_ptr &) override; }; diff --git a/src/slic3r/Utils/FontManager.cpp b/src/slic3r/Utils/FontManager.cpp index 3814a382c3..0761ae032f 100644 --- a/src/slic3r/Utils/FontManager.cpp +++ b/src/slic3r/Utils/FontManager.cpp @@ -15,6 +15,7 @@ FontManager::FontManager(const ImWchar *language_glyph_range) : m_imgui_init_glyph_range(language_glyph_range) , m_font_selected(std::numeric_limits::max()) , m_exist_style_images(false) + , m_temp_style_images(nullptr) {} FontManager::~FontManager() { @@ -382,6 +383,36 @@ void FontManager::init_style_images(int max_width) { // check already initialized if (m_exist_style_images) return; + // check is initializing + if (m_temp_style_images != nullptr) { + // is initialization finished + if (!m_temp_style_images->styles.empty()) { + assert(m_temp_style_images->images.size() == + m_temp_style_images->styles.size()); + // copy images into styles + for (FontManager::StyleImage &image : m_temp_style_images->images){ + size_t index = &image - &m_temp_style_images->images.front(); + StyleImagesData::Item &style = m_temp_style_images->styles[index]; + + // find style in font list and copy to it + for (auto &it : m_font_list) { + if (it.font_item.name != style.text || + !(it.font_item.prop == style.prop)) + continue; + it.image = image; + break; + } + } + m_temp_style_images = nullptr; + m_exist_style_images = true; + return; + } + // in process of initialization inside of job + return; + } + + // create job for init images + m_temp_style_images = std::make_shared(); StyleImagesData::Items styles; styles.reserve(m_font_list.size()); for (Item &item : m_font_list) { @@ -396,10 +427,8 @@ void FontManager::init_style_images(int max_width) { } auto &worker = wxGetApp().plater()->get_ui_job_worker(); - StyleImagesData data{styles, max_width, this}; - queue_job(worker, std::make_unique(std::move(data))); - - m_exist_style_images = true; + StyleImagesData data{std::move(styles), max_width, m_temp_style_images}; + queue_job(worker, std::make_unique(std::move(data))); } void FontManager::free_style_images() { diff --git a/src/slic3r/Utils/FontManager.hpp b/src/slic3r/Utils/FontManager.hpp index ce66864853..4fda626f53 100644 --- a/src/slic3r/Utils/FontManager.hpp +++ b/src/slic3r/Utils/FontManager.hpp @@ -17,7 +17,7 @@ namespace Slic3r::GUI { /// class FontManager { - friend class CreateFontStyleImagesJob; + friend class CreateFontStyleImagesJob; // access to StyleImagesData public: FontManager(const ImWchar *language_glyph_range); @@ -196,6 +196,40 @@ private: std::vector m_font_list; size_t m_font_selected; // index to m_font_list + /// + /// Keep data needed to create Font Style Images in Job + /// + struct StyleImagesData + { + struct Item + { + Emboss::FontFileWithCache font; + std::string text; + FontProp prop; + }; + using Items = std::vector; + + // Keep styles to render + Items styles; + + // Maximal width in pixels of image + int max_width; + + /// + /// Result of job + /// + struct StyleImages + { + // vector of inputs + StyleImagesData::Items styles; + // job output + std::vector images; + }; + + // place to store result in main thread in Finalize + std::shared_ptr result; + }; + std::shared_ptr m_temp_style_images; bool m_exist_style_images; // store all font GLImages