PECL/Cairo 0.3.0 released!

As I'm sure you've all been longing to hear, we've finally got around to releasing a new version of PECL/Cairo! This version fixes some bugs, and adds support for some new features available since Cairo 1.10 — notably, support for subsurfaces, which are surfaces that draw onto a part of a larger surface, and for recording surfaces, which record all drawing operations and can then be used as the source for other drawing operations. Also, Mark Skilbeck has added support for Win32 fonts. Also, the FreeType font handling has been improved, and should now give more informative messages when errors occur.

Any questions, comments, bug reports, or contributed code are welcome. Drop us a line on the PECL mailing list, or try #php.pecl on EFNet.

FrOSCon 2011

I'm very pleased to be able to announce that I'll be giving two talks at this year's FrOSCon conference in St Augustin near Bonn in Germany, on the 20th and 21st of August. The first is on the PHP track, and is an overview of the PECL/Cairo extension for PHP.

The second talk is on the main track, and is about Making Software See, where I'll give an introduction to the OpenCV image processing library, including some examples in various popular programming languages.

I'm extremely happy to have been selected both by the main FrOSCon organisers and the PHP track, organised by the PHP Usergroup Dortmund. I hope to see some of you there!

Cairo talk feedback from DPC10

I noted a couple of comments after my talk on Cairo at DPC 10 yesterday, so I thought I'd respond to them here to clear up what's going on. It's always good to get some feedback about something I've been working on so I'd like to reply and keep the conversation going if I can.

First off, someone mentioned that the wrapper seems a bit beta and incomplete - and yes, that is true. The 0.2.0 release, which is the latest as I wrote this, is still marked as beta. The aim of me coming and talking about it is to try and get some more people interested in using it, so we can get some more information from real users about what works and what doesn't. I probably didn't help by mentioning that there are some features that we intend to implement that aren't there yet, which I'll list:

  • Support for reading and writing images that aren't PNGs The developers of the core Cairo API are not really interested in handling all the various graphics formats that exist, so they decided to implement one and one only which is useful for getting data in and out. This is the functionality that we use at present. We (the developers of the wrapper) would like to fix that and handle JPEG, GIF, TIFF, and whatever new format is cool this week, but we're not quite sure how to go about that yet. We could recycle the code used by GD or use ImageMagick or GraphicsMagick, but those would introduce dependencies that people might not have available. Once we work out a decent solution here, it will be put in.
  • Support for hyperlinks in PDFs and SVGs Again, this is something we'd quite like but it's not supported in the underlying library yet. There have been rumours about adding an API to do this, but there hasn't been a concensus about what's a good way to go about it. It's something I'd quite like to help fix, so I've been learning how Cairo itself is put together with the intention to add the support.

The other comments I saw suggested that the API is a bit fiddly, which I can't deny. It's not quite as simple to set up and get running as some of the other libraries in PHP already. This is nearly intentional - we're wrapping the underlying Cairo API as closely as we can, because we don't want to diverge too far from how other languages with Cairo wrappers work, so that developers who may be familiar with those already know roughly what is going on. It's for this reason that, for example, the API uses floating-point values from 0 to 1 to define the intensity of colours, rather than an integer value from 0-255 which most people are probably used to. This is because Cairo is designed not to make any assumptions about the surface you're going to be drawing to - some day we might get 16-bits-per-channel surfaces, and using float values means that we don't need to change the API or the values we're passing in to allow us to write to them. It'll just carry on working as it did before, just with better colour resolution.

However, if anyone has any ideas for how we could make things simpler, then we'd love to hear about it &emdash; drop us a line on the PECL developers mailing list (which you can find on the PECL mailing lists page), or find us on IRC on #php.pecl on EFnet.

There are a couple of things that I have worked on which sit on top of the Cairo extension, which can do some interesting things. If you're still at DPC I'll be demoing a couple of them on the uncon track at 2pm. It's only a 15 minute talk, so it shouldn't be too boring...!

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.

 1 2 Next →

About

User