Integration with FreeType library


I have a problem during the integration with the FreeType library.
With the standard C, the code should be similar to this snippet:

FT_Library value;
FT_Error status;
FT_Face face;

status = FT_Init_FreeType (& value);
status = FT_New_Face (value, filename, 0, & face);

NOTE: FT_Face and FT_Library are typedef to struct pointers.

Using swift, i have tried several solutions with Pointers but every tentative generates a segmentation fault.
My last not working solution was:

self.lib = FT_Library(UnsafeRawPointer(UnsafeMutableRawPointer.allocate(byteCount: 1, alignment: 1)))
let libPt = UnsafeMutablePointer<FT_Library?>(self.lib)
let status = FT_Init_FreeType(libPt)
var face: UnsafeMutablePointer<FT_Face?>!
let status = FT_New_Face(lib, fileName, 0, face)

Any help?


  • Linux Debian
  • Swift version - swift-4.1.2-RELEASE -

I do not have concrete experience with FreeType, but here are my thoughts: The library allocates the structures, not the caller. You have to pass the address of pointer variables to the library functions, and the variables are set on return.

The corresponding Swift code should be something like:

var status: FT_Error
var lib: FT_Library?
var face: FT_Face?

status = FT_Init_FreeType(&lib)
status = FT_New_Face(lib, "filename", 0, &face)

lib and face have to be declared as optionals because that's how nullable pointers are represented in Swift.

Later you have to release the resources with the FT_Done_XXX functions.

1 Like

Thank you very much, it works.
Honestly, i have a little problems understanding the swift pointer management very well....but is my fault.

Too work and limited time to study swift :smile: