#ifndef slic3r_EmbossStyleManager_hpp_ #define slic3r_EmbossStyleManager_hpp_ #include #include #include #include #include #include #include #include #include #include namespace Slic3r::GUI::Emboss { /// /// Manage Emboss text styles /// Cache actual state of style /// + imgui font /// + wx font /// class StyleManager { friend class CreateFontStyleImagesJob; // access to StyleImagesData public: /// Character to load for imgui when initialize imgui font /// Function to create default styles StyleManager(const ImWchar *language_glyph_range, std::function create_default_styles); /// /// Release imgui font and style images from GPU /// ~StyleManager(); /// /// Load font style list from config /// Also select actual activ font /// /// Application configuration loaded from file "PrusaSlicer.ini" /// + cfg is stored to privat variable void init(AppConfig *app_config); /// /// Write font list into AppConfig /// /// Configuration /// When true cache state will be used for store /// When true store activ index into configuration /// True on succes otherwise False. bool store_styles_to_app_config(bool use_modification = true, bool store_active_index = true); /// /// Append actual style to style list /// /// New name for style void add_style(const std::string& name); /// /// Change order of style item in m_style_items. /// Fix selected font index when (i1 || i2) == m_font_selected /// /// First index to m_style_items /// Second index to m_style_items void swap(size_t i1, size_t i2); /// /// Discard changes in activ style /// When no activ style use last used OR first loadable /// void discard_style_changes(); /// /// Remove style from m_style_items. /// Fix selected font index when index is under m_font_selected /// /// Index of style to be removed void erase(size_t index); /// /// Rename actual selected font item /// /// New name void rename(const std::string &name); /// /// load some valid style /// void load_valid_style(); /// /// Change active font /// When font not loaded roll back activ font /// /// New font index(from m_style_items range) /// True on succes. False on fail load font bool load_style(size_t font_index); // load font style not stored in list bool load_style(const EmbossStyle &style); // fastering load font on index by wxFont, ignore type and descriptor bool load_style(const EmbossStyle &style, const wxFont &font); // clear actual selected glyphs cache void clear_glyphs_cache(); // remove cached imgui font for actual selected font void clear_imgui_font(); // getters for private data const EmbossStyle *get_stored_style() const; const EmbossStyle &get_style() const { return m_style_cache.style; } EmbossStyle &get_style() { return m_style_cache.style; } size_t get_style_index() const { return m_style_cache.style_index; } std::string &get_truncated_name() { return m_style_cache.truncated_name; } const ImFontAtlas &get_atlas() const { return m_style_cache.atlas; } const FontProp &get_font_prop() const { return get_style().prop; } FontProp &get_font_prop() { return get_style().prop; } const wxFont &get_wx_font() const { return m_style_cache.wx_font; } const wxFont &get_stored_wx_font() const { return m_style_cache.stored_wx_font; } Slic3r::Emboss::FontFileWithCache &get_font_file_with_cache() { return m_style_cache.font_file; } bool has_collections() const { return m_style_cache.font_file.font_file != nullptr && m_style_cache.font_file.font_file->infos.size() > 1; } // True when activ style has same name as some of stored style bool exist_stored_style() const { return m_style_cache.style_index != std::numeric_limits::max(); } /// /// check whether current style differ to selected /// /// bool is_font_changed() const; /// /// 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. // Extend font atlas when not in glyph range ImFont *get_imgui_font(); // initialize font range by unique symbols in text ImFont *create_imgui_font(const std::string& text, double scale); // init truncated names of styles void init_trunc_names(float max_width); /// /// Initialization texture with rendered font style /// /// 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; // access to all managed font styles const std::vector &get_styles() const; /// /// Describe image in GPU to show settings of style /// struct StyleImage { void* texture_id = 0; // GLuint BoundingBox bounding_box; ImVec2 tex_size, uv0, uv1; Point offset = Point(0, 0); StyleImage() = default; }; /// /// All connected with one style /// keep temporary data and caches for style /// struct Item { // define font, style and other property of text EmbossStyle style; // cache for view font name with maximal width in imgui std::string truncated_name; // visualization of style std::optional image; }; // check if exist selected font style in manager bool is_active_font(); // Limits for imgui loaded font size // Value out of limits is crop static float min_imgui_font_size; static float max_imgui_font_size; static float get_imgui_font_size(const FontProp &prop, const Slic3r::Emboss::FontFile &file, double scale); private: std::function m_create_default_styles; /// /// Cache data from style to reduce amount of: /// 1) loading font from file /// 2) Create atlas of symbols for imgui /// 3) Keep loaded(and modified by style) glyphs from font /// struct StyleCache { // share font file data with emboss job thread Slic3r::Emboss::FontFileWithCache font_file = {}; // must live same as imgui_font inside of atlas ImVector ranges = {}; // Keep only actual style in atlas ImFontAtlas atlas = {}; // wx widget font wxFont wx_font = {}; // cache for view font name with maximal width in imgui std::string truncated_name; // actual used font item EmbossStyle style = {}; // cache for stored wx font to not create every frame wxFont stored_wx_font = {}; // index into m_style_items size_t style_index = std::numeric_limits::max(); } m_style_cache; void make_unique_name(std::string &name); // Privat member std::vector m_style_items; AppConfig *m_app_config; size_t m_last_style_index; /// /// Keep data needed to create Font Style Images in Job /// struct StyleImagesData { struct Item { Slic3r::Emboss::FontFileWithCache font; std::string text; FontProp prop; }; using Items = std::vector; // Keep styles to render Items styles; // Maximal width and height in pixels of image Vec2i max_size; // Text to render std::string text; /// /// 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; // pixel per milimeter (scaled DPI) double ppm; }; std::shared_ptr m_temp_style_images; bool m_exist_style_images; // store all font GLImages //ImFontAtlas m_imgui_font_atlas; const ImWchar *m_imgui_init_glyph_range; }; } // namespace Slic3r #endif // slic3r_EmbossStyleManager_hpp_