FIX: init_font crash

Change-Id: I2d2fd9f297e2980e789b124d3e026d0db1fa1602
This commit is contained in:
liz.li 2022-11-29 22:41:33 +08:00 committed by Lane.Wei
parent dfde6e518f
commit 1f0ce4ac47
5 changed files with 36 additions and 10 deletions

View File

@ -14,3 +14,7 @@ add_library(imgui STATIC
imgui_draw.cpp
imgui_widgets.cpp
)
if(Boost_FOUND)
include_directories(${Boost_INCLUDE_DIRS})
endif()

View File

@ -53,6 +53,7 @@ Index of this file:
#include <stdlib.h> // alloca
#endif
#endif
#include <boost/log/trivial.hpp>
// 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];

View File

@ -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);

View File

@ -65,6 +65,8 @@ std::vector<std::string> 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<std::string> 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";

View File

@ -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) {