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