From 1f0ce4ac47a45e191f85d555f01f62da15726825 Mon Sep 17 00:00:00 2001 From: "liz.li" Date: Tue, 29 Nov 2022 22:41:33 +0800 Subject: [PATCH] FIX: init_font crash Change-Id: I2d2fd9f297e2980e789b124d3e026d0db1fa1602 --- src/imgui/CMakeLists.txt | 4 ++++ src/imgui/imgui_draw.cpp | 10 ++++++++-- src/imgui/imstb_truetype.h | 19 ++++++++++++++----- src/libslic3r/Shape/TextShape.cpp | 12 +++++++++--- src/slic3r/GUI/ImGuiWrapper.cpp | 1 + 5 files changed, 36 insertions(+), 10 deletions(-) diff --git a/src/imgui/CMakeLists.txt b/src/imgui/CMakeLists.txt index 235afe110..213b3b830 100644 --- a/src/imgui/CMakeLists.txt +++ b/src/imgui/CMakeLists.txt @@ -14,3 +14,7 @@ add_library(imgui STATIC imgui_draw.cpp imgui_widgets.cpp ) + +if(Boost_FOUND) + include_directories(${Boost_INCLUDE_DIRS}) +endif() \ No newline at end of file diff --git a/src/imgui/imgui_draw.cpp b/src/imgui/imgui_draw.cpp index 0f54c7e17..8dac5adbc 100644 --- a/src/imgui/imgui_draw.cpp +++ b/src/imgui/imgui_draw.cpp @@ -53,6 +53,7 @@ Index of this file: #include // alloca #endif #endif +#include // Visual Studio warnings #ifdef _MSC_VER @@ -2345,7 +2346,6 @@ static bool ImFontAtlasBuildWithStbTruetype(ImFontAtlas* atlas) dst_tmp_array.resize(atlas->Fonts.Size); memset(src_tmp_array.Data, 0, (size_t)src_tmp_array.size_in_bytes()); memset(dst_tmp_array.Data, 0, (size_t)dst_tmp_array.size_in_bytes()); - // 1. Initialize font loading structure, check font data validity for (int src_i = 0; src_i < atlas->ConfigData.Size; src_i++) { @@ -2365,9 +2365,15 @@ static bool ImFontAtlasBuildWithStbTruetype(ImFontAtlas* atlas) } // Initialize helper structure for font loading and verify that the TTF/OTF data is correct const int font_offset = stbtt_GetFontOffsetForIndex((unsigned char*)cfg.FontData, cfg.FontNo); + if (font_offset < 0) + BOOST_LOG_TRIVIAL(info) << "font_name: " << cfg.Name << ", font_offset: " << font_offset << ", font_no: " << cfg.FontNo << ", font_data_tag:" + << ((unsigned char*)cfg.FontData)[0] << ((unsigned char*)cfg.FontData)[1] << ((unsigned char*)cfg.FontData)[2] << ((unsigned char*)cfg.FontData)[3]; IM_ASSERT(font_offset >= 0 && "FontData is incorrect, or FontNo cannot be found."); - if (!stbtt_InitFont(&src_tmp.FontInfo, (unsigned char*)cfg.FontData, font_offset)) + if (!stbtt_InitFont(&src_tmp.FontInfo, (unsigned char*)cfg.FontData, font_offset)) { + BOOST_LOG_TRIVIAL(info) << "stbtt_InitFont failed, font_name: " << cfg.Name << ", font_data_tag:" + << ((unsigned char*)cfg.FontData)[0] << ((unsigned char*)cfg.FontData)[1] << ((unsigned char*)cfg.FontData)[2] << ((unsigned char*)cfg.FontData)[3];; return false; + } // Measure highest codepoints ImFontBuildDstData& dst_tmp = dst_tmp_array[src_tmp.DstIndex]; diff --git a/src/imgui/imstb_truetype.h b/src/imgui/imstb_truetype.h index fc815d745..17cbcbcfd 100644 --- a/src/imgui/imstb_truetype.h +++ b/src/imgui/imstb_truetype.h @@ -1311,11 +1311,11 @@ static int stbtt_GetFontOffsetForIndex_internal(unsigned char *font_collection, if (ttULONG(font_collection+4) == 0x00010000 || ttULONG(font_collection+4) == 0x00020000) { stbtt_int32 n = ttLONG(font_collection+8); if (index >= n) - return -1; + return -2; return ttULONG(font_collection+12+index*4); } } - return -1; + return -3; } static int stbtt_GetNumberOfFonts_internal(unsigned char *font_collection) @@ -1365,19 +1365,28 @@ static int stbtt_InitFont_internal(stbtt_fontinfo *info, unsigned char *data, in info->kern = stbtt__find_table(data, fontstart, "kern"); // not required info->gpos = stbtt__find_table(data, fontstart, "GPOS"); // not required - if (!cmap || !info->head || !info->hhea || !info->hmtx) + if (!cmap || !info->head || !info->hhea || !info->hmtx) { + BOOST_LOG_TRIVIAL(info) << "Cannot find cmap/head/hhea/hmtx table"; return 0; + } if (info->glyf) { // required for truetype - if (!info->loca) return 0; + if (!info->loca) { + BOOST_LOG_TRIVIAL(info) << "Cannot find loca table"; + return 0; + } } else { // initialization for CFF / Type2 fonts (OTF) + BOOST_LOG_TRIVIAL(info) << "initialization for CFF / Type2 fonts (OTF)"; stbtt__buf b, topdict, topdictidx; stbtt_uint32 cstype = 2, charstrings = 0, fdarrayoff = 0, fdselectoff = 0; stbtt_uint32 cff; cff = stbtt__find_table(data, fontstart, "CFF "); - if (!cff) return 0; + if (!cff) { + BOOST_LOG_TRIVIAL(info) << "Cannot find cff table"; + return 0; + } info->fontdicts = stbtt__new_buf(NULL, 0); info->fdselect = stbtt__new_buf(NULL, 0); diff --git a/src/libslic3r/Shape/TextShape.cpp b/src/libslic3r/Shape/TextShape.cpp index 7d37053ef..7d326ecb4 100644 --- a/src/libslic3r/Shape/TextShape.cpp +++ b/src/libslic3r/Shape/TextShape.cpp @@ -65,6 +65,8 @@ std::vector init_occt_fonts() if(afn->String().StartsWith(".")) continue; #endif + if(afn->Search("Emoji") != -1 || afn->Search("emoji") != -1) + continue; bool repeat = false; for (size_t i = 0; i < fonts_suffix.size(); i++) { if (afn->SearchFromEnd(fonts_suffix[i]) != -1) { @@ -77,9 +79,13 @@ std::vector init_occt_fonts() Handle(Font_SystemFont) sys_font = aFontMgr->GetFont(afn->ToCString()); TCollection_AsciiString font_path = sys_font->FontPath(Font_FontAspect::Font_FontAspect_Regular); - if (!font_path.IsEmpty() && !font_path.EndsWith(".dfont")) { - g_occt_fonts_maps.insert(std::make_pair(afn->ToCString(), decode_path(font_path.ToCString()))); - stdFontNames.push_back(afn->ToCString()); + if (!font_path.IsEmpty() && font_path.SearchFromEnd(".") != -1) { + auto file_type = font_path.SubString(font_path.SearchFromEnd(".") + 1, font_path.Length()); + file_type.LowerCase(); + if (file_type == "ttf" || file_type == "otf" || file_type == "ttc") { + g_occt_fonts_maps.insert(std::make_pair(afn->ToCString(), decode_path(font_path.ToCString()))); + stdFontNames.push_back(afn->ToCString()); + } } } BOOST_LOG_TRIVIAL(info) << "init_occt_fonts end"; diff --git a/src/slic3r/GUI/ImGuiWrapper.cpp b/src/slic3r/GUI/ImGuiWrapper.cpp index 8093210e4..b55aad2f5 100644 --- a/src/slic3r/GUI/ImGuiWrapper.cpp +++ b/src/slic3r/GUI/ImGuiWrapper.cpp @@ -1979,6 +1979,7 @@ void ImGuiWrapper::init_font(bool compress) unsigned char* pixels; int width, height; io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height); // Load as RGBA 32-bits (75% of the memory is wasted, but default font is so small) because it is more likely to be compatible with user's existing shaders. If your ImTextureId represent a higher-level concept than just a GL texture id, consider calling GetTexDataAsAlpha8() instead to save on GPU memory. + BOOST_LOG_TRIVIAL(trace) << "Build texture done. width: " << width << ", height: " << height; // Fill rectangles from the SVG-icons for (auto icon : font_icons) {