【问题标题】:freetype 2 library crashes when trying to access characters map尝试访问字符映射时,freetype 2 库崩溃
【发布时间】:2012-09-10 00:51:29
【问题描述】:

我正在尝试在我的多平台库中使用 this 很棒的 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 );
    [...]

因为 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;
}

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

有什么想法吗?

【问题讨论】:

    标签: c++ freetype2


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2020-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-09
      • 1970-01-01
      • 1970-01-01
      • 2018-10-21
      相关资源
      最近更新 更多