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!

What I've been up to

I finally decided to get around to doing something about my site, so I've made a front page for it which is better than a straight redirect onto the blog. Hopefully it's reasonable.

I've been learning a bit more recently about the internals of PHP, and how to write extensions for it. I've been practicing by doing a bit of work on a Cairo wrapper that was started in the Google Summer of Code last year and until now has been mainly looked after by Elizabeth Marie Smith. She's put up with many, many newbie questions from me and so I've learned a lot while doing it. It's now at the stage where it'll run the eZ Components Graph component without too much complaining. It has a dual procedural and object-oriented API, so it should manage to run most things that use the existing cairo_wrapper extension. Thanks to all the regulars on #php.pecl and #php.doc on EFNet, and #php-gtk on Freenode for putting up with me while doing this. I've also written a quick wrapper for Tokyo Cabinet for the DBA extension in PHP which with a bit of luck will get committed to PHP6 some time soon.

In other news, I've been occasionally helping in the maintenance of Jubilee and An Sulaire, the two Sgoth Niseach boats I go sailing on occasionally. Hopefully they'll be going back in the water soon and we can get some sailing done. I'm looking forward to it.

PHP 5.3.0RC2

PHP 5.3.0RC2 has been released, which means that the next version of PHP is just around the corner. It comes right in the middle of the annual PHP Testfest. I rambled on a bit on the Freenode podcast a couple of weeks ago about this - it's an annual project where PHP usergroups and individuals around the world get together to improve the unit testing of PHP. This has benefits for everyone involved - PHP is improved, and more likely to maintain backward compatibility, and the people involved in writing the tests get to contribute in a meaningful way to the project, and hopefully learn something on the way. Several usergroups have events scheduled, and a couple have already taken place to great success. If you're a serious PHP user, and interested in getting involved, it's well worth checking out.

Even just grabbing the latest PHP release, compiling it on your platform, and running "make test" helps the project. It doesn't take much to do, and you can run it in the background while doing something else. Go for it!

 1

About

User