Using Perl Inline C to access FreeType library

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.)

< Random thoughts about code quality, timesheeting and CVS | Procmail archiving and mail loops >

Miniblog

(nuggets of inanity)

Tuesday Apr 24th 2007, 16:54 »
Just took the annual web design survey that AListApart do. I don't realy consider myself to be a web designer, but I have been doing a lot of HTML and CSS lately.
Monday Apr 23rd 2007, 18:23 »
Strange, there appears to be a bare-knuckle boxing match going on in the field outside my flat. Wish they wouldn't make so much noise about it.
Thursday Mar 1st 2007, 18:47 »
“In its written form, Hebrew has no vowels, making it the ideal language for texting.”
—Said in jest on some Radio 4 programme just now.

Archive: 2007 · 2006 · 2005 · 2004
Feed