尝试访问字符映射时,freetype 2库崩溃

Tut*_*men 2 c++ freetype2

我正试图在我的多平台库中使用这个非常棒的freetype 2版本.我还没有在Android上测试它,但我尝试在win32下编译并测试它.它编译得很好没有错误,但是当库试图读取TT_CMap的"数据"(总是坏指针)成员时它总是崩溃.它发生的功能仅取决于字体文件,即一种字体 - 一个地方.我尝试过来自Windows,MacOS X和Android的不同ttd字体,但它只会影响位置,会发生分段错误.例如,如果我尝试检索标准窗口arial.ttf字体的任何字符的索引,它将始终崩溃在这里:

static FT_UInt tt_cmap4_char_map_binary( TT_CMap     cmap, FT_UInt32*  pcharcode, FT_Bool     next )
{
    [...]
    p = cmap->data + 6;
    num_segs2 = FT_PAD_FLOOR( TT_PEEK_USHORT( p ), 2 );
    [...]
Run Code Online (Sandbox Code Playgroud)

因为cmap-> data是无效的指针.

我的测试代码如下所示:

int error = 0;

// Initialize freetype library first
error = FT_Init_FreeType( &ft_lib );
if ( error ) {
    ODF(("Unable to init freetype library! Error code is %d.\n", error));
    return false;
}

// Load font file into the memory buffer
iMemBuff faceFileBuff;
if (igelLoadResource(iString("Data/arial.ttf"), faceFileBuff) != ROR_Success) {
    ODF(("ERROR: Unable to open font file!\n"));
    return false;
}

// Init font
error = FT_New_Memory_Face( ft_lib, (FT_Byte*)faceFileBuff.GetPtr(), faceFileBuff.GetSize(), 0, &ft_face );
if ( error == FT_Err_Unknown_File_Format ) {
    ODF(("ERROR: the font file could be opened and read, but it appears that its font format is unsupported.\n"));
    return false;
} else if ( error ) {
    ODF(("ERROR: %d error code means that the font file could not be opened or read, or simply that it is broken...\n", error));
    return false;
}

// Setup font metrics
error = FT_Set_Pixel_Sizes( ft_face, 0, 16 );
// error = FT_Set_Char_Size(ft_face, 0, 16*64, 300, 300 );
if ( error  ) {
    ODF(("ERROR: Unable to set char/pixel size! Error code %d.\n", error));
    return false;
}



sint32 ox = 0, oy = 0;
int error = 0;
FT_GlyphSlot  slot = ft_face->glyph;  // a small shortcut
for (uint32 cc=0; cc<text.StringSize(); ++cc) {

    // Find the character 'a' index
    int glyph_index = FT_Get_Char_Index( ft_face, text.CStr()[cc] );

    // Load glyph
    error = FT_Load_Glyph( ft_face, glyph_index, FT_LOAD_DEFAULT );
    if ( error  ) {
        ODF(("ERROR: Unable to load face from the font! Error code %d.\n", error));
        continue;
    }

    // Render the glyph
    error = FT_Render_Glyph( ft_face->glyph, FT_RENDER_MODE_NORMAL );
    if ( error  ) {
        ODF(("ERROR: Unable to render glyph! Error code %d.\n", error));
        continue;
    }

    // Draw the glyph to our target surface
    ComposeGlyph(image, &slot->bitmap, ox + slot->bitmap_left, oy + slot->bitmap_top);
    ox += slot->advance.x >> 6;
}
Run Code Online (Sandbox Code Playgroud)

当我用任何字符调用FT_Get_Char_Index时它崩溃了......

任何的想法?

小智 6

在我的情况下,当我释放传递给FT_New_Memory_Face()的字体内存块时,问题是cmap-> data变为坏指针.只要我保留内存块,cmap-> data就会保持有效.

虽然这似乎不是你的情况,但只想提供一些意见.