我正在尝试使用 FreeType 库编写一个简单的脚本。段错误是在执行 FT_Set_Pixel_Sizes 方法期间发生的,尽管我正确使用了它。任何帮助都会很棒。这是完整的代码:
#include <ft2build.h>
#include FT_FREETYPE_H
main() {
FT_Library library;
FT_Face face;
FT_GlyphSlot slot;
FT_UInt glyph_index = 30;
char* font_file = "/usr/share/fonts/truetype/freefont/FreeMono.ttf";
// Render font
FT_New_Face(library, font_file, 0, &face);
FT_Set_Pixel_Sizes(face, 0, 16); /* THIS LINE IS CAUSING THE SEGFAULT */
slot = face->glyph;
FT_Load_Glyph(face, glyph_index, FT_LOAD_DEFAULT);
FT_Render_Glyph(face->glyph, FT_RENDER_MODE_NORMAL);
}
最佳答案
您没有初始化您的 Library
变量:参见 FT_LIBRARY
documentation .你应该使用 FT_Init_FreeType
:
FT_Init_FreeType
Defined in FT_FREETYPE_H (freetype/freetype.h).
FT_EXPORT( FT_Error ) FT_Init_FreeType( FT_Library *alibrary );
Initialize a new FreeType library object. The set of modules that are registered by this function is determined at build time.
output alibrary A handle to a new library object.
return FreeType error code. 0 means success.
您可以先习惯这个库,然后再使用 tutorial .也要注意检查返回值...
关于c - 非常简单的源上的段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12334326/