Handling fonts in PECL/Cairo

(This is just a quick note to get some information out there for reference, I am adding it to the PHP manual as well!)

Currently, in PECL/Cairo the only way to draw text is the referred to as the "toy" text API, which is a very basic way of handling text compared to the facilities available in the Cairo library itself. However, it's sufficient for most purposes that I've come across so far. In version 0.1.0 of PECL/Cairo, there was only one way to choose what font you wished to use, which was the CairoContext::selectFontFace() method. You pass a string to this method with the name of the font you want, along with the optional slant and weight parameters. This then invokes your system's font handling to find the font you're after, or an alternative if it's not available, so you need to have the font you want installed into your system's font library.This is occasionally not handy.

In version 0.2.0 FreeType support was added. It allows you to choose any font file you'd like, as long as PHP's streams API can find it. Yes, this means 'http://' streams, but I wouldn't recommend it.

  1.  
  2. <?php
  3. /* Set up the surface, and make the background white */
  4. $s = new CairoImageSurface(CairoFormat::ARGB32, 300, 100);
  5. $c = new CairoContext($s);
  6. $c->setSourceRgb(1, 1, 1);
  7. $c->paint();
  8.  
  9. /* Draw the text using the Vollkorn font, from
  10.   http://friedrichalthausen.de/2006/01/01/vollkorn/ */
  11. $c->setSourceRGB(0, 0, 0);
  12. $c->moveTo(10, 60);
  13. $f = new CairoFtFontFace(dirname(__FILE__) . "/vollkorn.otf");
  14. $c->setFontFace($f);
  15. $c->setFontSize(50);
  16. $c->showText("Hello world");
  17.  
  18. /* Send the image to the browser */
  19. header("Content-type: image/png");
  20. $s->writeToPng("php://output");
  21. ?>
  22.  

The output image should hopefully look like this:

Script output

(For those who are familiar with the Cairo library, this function maps to the cairo_ft_font_face_create_for_ft_face function in its API.)

There is still some work to be done in this area, notably to support the Windows and Mac OS X font systems, but they'll be coming in a future release, we hope. If anyone would like to help us with that, or any other aspect of it (including documentation!), you can get in touch on the PECL dev mailing list, or if you're on IRC, drop in to #php.pecl on EFnet. All contributions are welcome!

Speaking at DPC10

DPC10 speaker badge

I'm happy to say that I'll be speaking at the Dutch PHP Conference in June in Amsterdam, on the subject of the PECL/Cairo extension I've been helping out by working on for the past few months. This will be my first appearance as a speaker at a technical conference so I'm a little nervous, but I've no doubt I'll be practicing a bit before it happens. Apologies in advance to anyone I inflict the talk on before the event.

PECL/Cairo 0.2.0 released

The first beta version of the PECL/Cairo extension has just been released. This version includes support for loading arbitrary fonts via Freetype, cloning matrices, and has a rather large set of bug fixes. If you've been using 0.1.0 for anything at all I'd really recommend an upgrade. Windows builds will appear soon over at perisama.net for all the major PHP variants courtesy of Elizabeth M. Smith. Many thanks to Mark Skilbeck for helping get this release working on Windows!

Cairo article published

I've got 3 posts or so queued up in my mind to write out, but some of them rather depend on doing other things first, so this will just be a quick note to say I have an article in the most recent php|architect magazine on the subject of "Vector Graphics with Cairo". This is a basic introduction to the PECL/Cairo extension which I've been helping out on for a while now. Thanks to Keith Casey for his input, to the folks at php|architect for letting me do it, and the long-suffering Elizabeth Marie Smith for putting up with all my stupid questions while working on this and other extensions!

Now, back to my regularly scheduled coding...

A brief update

Last weekend I spent a couple of days on the yacht Hebridean as part of an RYA sail cruising day skipper course. This was the second half, as we were aboard before last month but had to cancel due to the weather. This time, we combined the course with delivering the yacht to Oban Marina on Kerrera where it will spend the winter. I'm happy to say I passed, so theoretically at least I could be able to charter a yacht for a trip at some stage. I might investigate that for one summer, but I'll want a bit more mileage and experience first. I also noticed that the local college were running a Coastal Skipper theory course, so I'm on that now too.

I'm considering going to FOSDEM in Belgium in February. I missed LugRadio Live this year so I'm thinking of it as a sort of substitute. Also considering PHP London, but we'll see.

 1 2 3 … 7 Next →

About

User