Continuing my experimentations with the FreeType library, I've been looking at how I can use it from Perl. There's a Perl module called FONT::FT2, but it doesn't do any of the outline extraction stuff I want, and it only seems to segfault when I do anything with it.
I've never tried using the Inline modules before, but I thought Inline::C might be a good way of getting access to FreeType relatively easy. I read an article about it on perl.com and had a go. It turns out to work pretty well, although I did find one minor bug.
FreeType is all based around objects, representing the ‘instance’
of the library (an FT_Library object), font faces, glyphs, etc.
It makes sense to me to have simple Perl modules wrapping each of those.
To try out the idea I've just written a module for the library objects,
and all it does so far is create and destroy them. It seems to work
fine. The objects themselves are references to scalars which hold the
address of the FreeType library object. That makes things easy to deal
with in C.
All the examples I've seen of Inline code actually have the code inline
(in Perl strings, often using here docs, or after the __END__ thing).
I want to have the C in a separate file so that it gets syntax coloured
right. I've come up with a little formula that allows me to install the
C source in with the module and have it loaded from there before giving
it to Inline:
use File::Slurp; use Path::Class qw( file ); my $c_code; BEGIN { our $PATH = file($INC{file('Font', 'FreeType.pm')}) ->dir->subdir('FreeType')->stringify; $c_code = read_file(file($PATH, 'library.c')->stringify); } use Inline ( C => $c_code, LIBS => '-lfreetype', INC => '-I/usr/include/freetype2', );
Well, that seems to work OK. This stuff is quite fun so far, so I'll play with it some more over the weekend. Here's my code so far: Font-FreeType-0.00.tar.gz.
(Update, Jun 9th: I've now released Font::FreeType 0.01, which has a proper XS binding.)