From 1301234d33310fa41b55e668b922f37f7811c43b Mon Sep 17 00:00:00 2001 From: Filip Sykala Date: Mon, 13 Sep 2021 11:19:52 +0200 Subject: [PATCH] separate wxFontUtils --- src/libslic3r/Emboss.cpp | 19 ----- src/libslic3r/Emboss.hpp | 4 +- src/slic3r/GUI/Gizmos/GLGizmoEmboss.cpp | 105 ++++++++++++++++-------- src/slic3r/GUI/Gizmos/GLGizmoEmboss.hpp | 7 -- 4 files changed, 74 insertions(+), 61 deletions(-) diff --git a/src/libslic3r/Emboss.cpp b/src/libslic3r/Emboss.cpp index 11d1075106..0717d94d65 100644 --- a/src/libslic3r/Emboss.cpp +++ b/src/libslic3r/Emboss.cpp @@ -412,25 +412,6 @@ std::optional Emboss::load_font(std::vector data) // load information about line gap stbtt_GetFontVMetrics(info, &res.ascent, &res.descent, &res.linegap); - // TrueType Reference Manual - The 'name' table https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6name.html - // OpenType™ Specification - The Naming Table http://www.microsoft.com/typography/otspec/name.htm - int length = 0; - //PLATFORM_ID_UNICODE PLATFORM_ID_MAC PLATFORM_ID_ISO PLATFORM_ID_MICROSOFT - int platformId = STBTT_PLATFORM_ID_MICROSOFT; - // UNICODE_EID_UNICODE_1_0 UNICODE_EID_UNICODE_1_1 UNICODE_EID_ISO_10646 UNICODE_EID_UNICODE_2_0_BMP - // UNICODE_EID_UNICODE_2_0_FULL MS_EID_SYMBOL MS_EID_UNICODE_BMP MS_EID_SHIFTJIS MS_EID_UNICODE_FULL - // MAC_EID_ROMAN MAC_EID_JAPANESE MAC_EID_CHINESE_TRAD MAC_EID_KOREAN MAC_EID_ARABIC MAC_EID_HEBREW MAC_EID_GREEK - // MAC_EID_RUSSIAN - int encodingID = STBTT_MS_EID_SYMBOL; - // MS_LANG_ENGLISH MS_LANG_CHINESE MS_LANG_DUTCH MS_LANG_FRENCH MS_LANG_GERMAN MS_LANG_HEBREW MS_LANG_ITALIAN - // MS_LANG_JAPANESE MS_LANG_KOREAN MS_LANG_RUSSIAN MS_LANG_SPANISH MS_LANG_SWEDISH MAC_LANG_ENGLISH - // MAC_LANG_ARABIC MAC_LANG_DUTCH MAC_LANG_FRENCH MAC_LANG_GERMAN MAC_LANG_HEBREW MAC_LANG_ITALIAN - // MAC_LANG_JAPANESE MAC_LANG_KOREAN MAC_LANG_RUSSIAN MAC_LANG_SPANISH MAC_LANG_SWEDISH - // MAC_LANG_CHINESE_SIMPLIFIED MAC_LANG_CHINESE_TRAD - int languageID = STBTT_MS_LANG_ENGLISH; - int nameID = 4; // human readable - http://www.microsoft.com/typography/otspec/name.htm - const char *name_char = stbtt_GetFontNameString(info, &length, platformId, encodingID, languageID, nameID); - res.name = std::string(name_char, length); return res; } diff --git a/src/libslic3r/Emboss.hpp b/src/libslic3r/Emboss.hpp index 9162cd182e..ac83dfda97 100644 --- a/src/libslic3r/Emboss.hpp +++ b/src/libslic3r/Emboss.hpp @@ -92,9 +92,7 @@ public: // vertical position is "scale*(ascent - descent + lineGap)" int ascent = 0, descent = 0, linegap = 0; - std::string name; - - Emboss::Glyphs cache; + Emboss::Glyphs cache; // cache of glyphs }; /// diff --git a/src/slic3r/GUI/Gizmos/GLGizmoEmboss.cpp b/src/slic3r/GUI/Gizmos/GLGizmoEmboss.cpp index d316ee97bd..2d19c8fcb4 100644 --- a/src/slic3r/GUI/Gizmos/GLGizmoEmboss.cpp +++ b/src/slic3r/GUI/Gizmos/GLGizmoEmboss.cpp @@ -11,6 +11,27 @@ #include #include + +namespace Slic3r { +class WxFontUtils +{ +public: + WxFontUtils() = delete; + + // os specific load of wxFont + static std::optional load_font(const wxFont &font); + + // load font used by Operating system as default GUI + static Slic3r::Emboss::FontItem get_os_font(); + static std::string get_human_readable_name(const wxFont &font); + + // serialize / deserialize font + static std::string store_wxFont(const wxFont &font); + static wxFont load_wxFont(const std::string &font_descriptor); +}; +} // namespace Slic3r + +using namespace Slic3r; using namespace Slic3r::GUI; GLGizmoEmboss::GLGizmoEmboss(GLCanvas3D & parent, @@ -31,7 +52,8 @@ GLGizmoEmboss::GLGizmoEmboss(GLCanvas3D & parent, // (copy & paste) unicode symbols from web bool is_font_loaded = load_font(); - add_fonts(Emboss::get_font_list()); + //add_fonts(Emboss::get_font_list()); + //add_fonts({WxFontUtils::get_os_font()}); if (!is_font_loaded) { // can't load so erase it from list @@ -72,24 +94,7 @@ void GLGizmoEmboss::on_render_input_window(float x, float y, float bottom_limit) int flag = ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoCollapse; m_imgui->begin(on_get_name(), flag); - if (m_font.has_value()) { - ImGui::Text("Selected font is %s END.", m_font->name.c_str()); - } else { - ImGui::Text("No selected font yet."); - } draw_font_list(); - - - - // TODO: fix load string each render - wxSystemSettings ss; - wxFont ssFont = ss.GetFont(wxSYS_ANSI_VAR_FONT); - std::string fontDesc = std::string(ssFont.GetNativeFontInfoDesc().c_str()); - ImGui::Text("%s", fontDesc.c_str()); - if (ImGui::Button(_L("Load font").c_str())) { - wxFont font = load_wxFont(fontDesc); - set_font(font); - } static std::string fontName; if (ImGui::Button(_L("choose font").c_str())) { @@ -365,7 +370,7 @@ void GLGizmoEmboss::draw_font_list() ImGui::EndCombo(); } ImGui::SameLine(); - if (m_font.has_value()) { + if (m_font.has_value() && m_font->count > 1) { if (ImGui::BeginCombo("##font_collection_selector", std::to_string(m_font->index).c_str())) { for (size_t i = 0; i < m_font->count; ++i) { @@ -383,39 +388,75 @@ void GLGizmoEmboss::draw_font_list() bool GLGizmoEmboss::load_font() { - auto font_path = m_font_list[m_font_selected].path.c_str(); - m_font = Emboss::load_font(font_path); - return m_font.has_value(); + const Emboss::FontItem &fi = m_font_list[m_font_selected]; + std::optional loaded_font; + switch (fi.type) { + case Emboss::FontItem::Type::file_path: + loaded_font = Emboss::load_font(fi.path.c_str()); + break; + case Emboss::FontItem::Type::wx_font_descr: + loaded_font = WxFontUtils::load_font(WxFontUtils::load_wxFont(fi.path)); + break; + } + + + bool is_loaded = loaded_font.has_value(); + if (is_loaded) { + m_font = std::move(loaded_font); + } + return is_loaded; } -void GLGizmoEmboss::set_font(const wxFont &font) { - //std::string m_font_name = std::string(( - // font.GetFamilyString() + " " + - // font.GetStyleString() + " " + - // font.GetWeightString() - // ).c_str()); +std::optional WxFontUtils::load_font(const wxFont &font) +{ + if (!font.IsOk()) return {}; #ifdef _WIN32 - m_font = Emboss::load_font(font.GetHFONT()); + return Slic3r::Emboss::load_font(font.GetHFONT()); #elif __linux__ // use file path + return {}; #elif __APPLE__ const wxNativeFontInfo *info = font.GetNativeFontInfo(); CTFontDescriptorRef descriptor = info3->GetCTFontDescriptor(); CFDictionaryRef attribs = CTFontDescriptorCopyAttributes(descriptor); CFStringRef url = (CFStringRef)CTFontDescriptorCopyAttribute(descriptor, kCTFontURLAttribute); std::string str(CFStringGetCStringPtr(CFURLGetString(anUrl),kCFStringEncodingUTF8)); - m_font = Emboss::load_font(str); + return Emboss::load_font(str); #endif } -std::string GLGizmoEmboss::store_wxFont(const wxFont &font) +Slic3r::Emboss::FontItem WxFontUtils::get_os_font() +{ + wxSystemSettings ss; + wxFont ss_font = ss.GetFont(wxSYS_ANSI_VAR_FONT); + std::string name = get_human_readable_name(ss_font) + " (" + _u8L("OS default") + ")"; + std::string fontDesc = std::string(ss_font.GetNativeFontInfoDesc().c_str()); + return Emboss::FontItem(name, fontDesc, Emboss::FontItem::Type::wx_font_descr); +} + +std::string WxFontUtils::get_human_readable_name(const wxFont &font) +{ + if (!font.IsOk()) return "Font is NOT ok."; + // Face name is optional in wxFont + if (!font.GetFaceName().empty()) { + return std::string(font.GetFaceName().c_str()); + } else { + return std::string(( + font.GetFamilyString() + " " + + font.GetStyleString() + " " + + font.GetWeightString() + ).c_str()); + } +} + +std::string WxFontUtils::store_wxFont(const wxFont &font) { //wxString os = wxPlatformInfo::Get().GetOperatingSystemIdName(); wxString font_descriptor = font.GetNativeFontInfoDesc(); return std::string(font_descriptor.c_str()); } -wxFont GLGizmoEmboss::load_wxFont(const std::string &font_descriptor) +wxFont WxFontUtils::load_wxFont(const std::string &font_descriptor) { wxString font_descriptor_wx(font_descriptor); return wxFont(font_descriptor_wx); diff --git a/src/slic3r/GUI/Gizmos/GLGizmoEmboss.hpp b/src/slic3r/GUI/Gizmos/GLGizmoEmboss.hpp index 7ad608d209..823c3bfb4e 100644 --- a/src/slic3r/GUI/Gizmos/GLGizmoEmboss.hpp +++ b/src/slic3r/GUI/Gizmos/GLGizmoEmboss.hpp @@ -41,13 +41,6 @@ private: void draw_add_button(); bool load_font(); - // os specific set of wxFont - void set_font(const wxFont &font); - - // serialize / deserialize font - static std::string store_wxFont(const wxFont& font); - static wxFont load_wxFont(const std::string &font_descriptor); - void sort_fonts(); void add_fonts(const Emboss::FontList &font_list);