Freetype2多个面孔

FreeType2 multiple faces

本文关键字:面孔 Freetype2      更新时间:2023-10-16

我正在使用freetype2库在OpenGL应用中实现字体。我的问题是,初始化一个FT_Face对象会崩溃。我尝试使用FT_Library的单个实例,并为每个新字体调用FT_Init_FreeType()。我还尝试了每种字体的Havind单独的FT_Library实例。这两个初始化都可以正常工作,但是随后我从malloc.c获得了new操作员的主张,无论下次,无论何处,都在下一行代码上。

这是字体对象创建方法,其中freetype2 lib初始化:

Font::Font* create(const char* path, unsigned int size) {
  Font* font = new Font();
  FT_Init_FreeType(&(font->_ft));
  if(FT_New_Face(font->_ft, path, 0, font->_face)) {
    std::cerr << "Font: Could not load font from file " << path << "." << std::endl;
    return NULL;
  }
  FT_Set_Pixel_Sizes(*(font->_face), 0, size);
}

,如果我一次称呼它,那么这种代码的平静就可以了。如果我第二次打电话给它,然后在程序的另一部分中稍后拨打,我通过new创建对象,应用程序崩溃。

这里有什么问题?应该有一些好方法来加载几个字体...

断言消息:malloc.c:2365: sysmalloc: Assertion (old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 * (sizeof(size_t))) - 1)) & ~((2 * (sizeof(size_t))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long)old_end & pagemask) == 0)' failed.

更新:

字体类声明,ft库实例每个类别的版本:

class Font
{
  public:
    static Font* create(const char* font, unsigned int size);
    void renderText(const wchar_t* text, float x, float y, float sx, float sy);
  private:
    Font();
    ~Font();
    FT_Library _ft;
    FT_Face* _face;
};

renderText()方法基本使用_face窥视需要炭并渲染它们。

在呼叫FT_New_Face之前,您确定font->_face是新的吗?因此,如果font->_face为null,则需要新的FT_Face

NOTE :对于每个字体实例,不需要启动FT_Library。您可以使Font::_ft成为静态。

最后,代码应为:

class Font
{
public:
    static FT_Library      _ft;
    static Font* create(const char* path, unsigned int size)
    {
        Font* font = new Font();
        if (Font::_ft == NULL)
            if (FT_Init_FreeType(&Font::_ft))
                return NULL;
        if (font->_face == NULL)
            font->_face = new FT_Face;
        if (FT_New_Face(font->_ft, path, 0, font->_face)) {
            std::cerr << "Font: Could not load font from file " << path << "." << std::endl;
            return NULL;
        }
        FT_Set_Pixel_Sizes(*(font->_face), 0, size);
        return font;
    }
private:
    // Set the member - _face to NULL when call constructor
    Font() : _face(NULL) {}
    ~Font() { /* release the memory */ }
    FT_Face*        _face;
};
// Set the static member - _ft to NULL
FT_Library Font::_ft = NULL;

终于,您需要在呼叫驱动器时非专业化/发布有关freetype2的所有内存。

注意FT_New_Face的第四变量(FT_Face)必须是实例。FT_New_Face不为FT_Face分配内存。