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!

Pango for PHP PECL channel

It occurs to me that though I wrote an introduction post about Pango for PHP, I forgot to mention that it's actually installable using the PECL installer.

 pecl channel-discover pecl.mgdm.net
pecl install channel://pecl.mgdm.net/Pango-0.1.0

Assuming you have the required development packages installed, it should go off and install it. You may still need to add the

extension=pango.so

line to your php.ini. Hope this helps someone give it a go! As ever, any feedback is welcome.

Using Pango for PHP: a taster

The PECL/Cairo library is pretty good at drawing vector graphics (in our opinion, as the developers, at least!), but one thing it's not able to do by itself is draw text with mildly advanced layout. It has the CairoContext::showText() function, but that doesn't really let you do anything GD can't. That's because the developers of the Cairo library decided to let another more specialised library handle the job of text layout. Much of the time, the library that gets used for this is Pango. It has several bindings already for most popular languages on Unix-like platforms. It's quite capable, and able to lay out text while taking care of things such as paragraph alignment, line breaking, bold/italic text, justification, and various other features. In this post, I intend to give a little tour of some of its features.

Quite a while ago now, I wrote an extension to wrap Pango for PHP. It's available on its Github repository. It's not available on PECL right now, but you can check it out from there and install it using the normal method. (Sorry Windows users, I've not had time to test it out on there, and likely won't any time soon. Patches are welcome, though...). It requires that the Pango headers are installed, and that PECL/Cairo is already installed into PHP.

Once installed, you can use it with Cairo. The best way to show it off is probably some example code:

  1.  
  2. <?php
  3. header("Content-Type: image/png");
  4. /* Make a 300x300px image surface */
  5. $s = new CairoImageSurface(CairoFormat::ARGB32, 300, 300);
  6. $c = new CairoContext($s);
  7.  
  8. /* Set the background to white */
  9. $c->setSourceRGB(1, 1, 1);
  10. $c->paint();
  11.  
  12. /* Let's draw using black 'ink' */
  13. $c->setSourceRGB(0, 0, 0);
  14.  
  15. /* Make a Pango layout, set the font, then set the layout size */
  16. $l = new PangoLayout($c);
  17. $desc = new PangoFontDescription("Bitstream Charter 28");
  18. $l->setFontDescription($desc);
  19. $l->setWidth(250 * PANGO_SCALE);
  20.  
  21. /* Here, we use Pango markup to make part of the text bold */
  22. $l->setMarkup("Hello <b>world!</b> Here is a rather long paragraph which should get wrapped");
  23.  
  24. /* Draw the layout on the surface */
  25. $l->showLayout($c);
  26.  
  27. /* Output the PNG to the browser */
  28. $s->writeToPng("php://output");
  29.  

If all goes to plan, you should see a PNG in your browser with the above text.

Going through each step, firstly we set up the Cairo surface to draw on, and the context we use to draw with. Once we have done that, we can create a PangoLayout object which lets us draw the text we require. The PangoLayout is passed a context, so it can invoke the drawing methods itself to draw the text. We pass the PangoLayout a PangoFontDescription object, which lets us choose from the fonts already installed on the machine. This means that we don't need to concern ourselves with the paths to actual TrueType font files, or similar - the fonts are resolved by Fontconfig or whatever system is available on your machine.

Next, we set the width of the layout. We can also set a height, but I haven't bothered on this occasion. This lets Pango know where to wrap the text - if we don't set this, it won't bother, which may result in text falling off the edge of the image. You may note that the width is multiplied by the PANGO_SCALE - this is because Pango deals in units which are a tiny fraction (1/1024, in fact) of a pixel, in order to handle antialiasing properly. Because we're using a CairoImageSurface, this means that they layout will be 250 pixels wide.

Next up, we set the text to be drawn. There are two methods available to do this; PangoLayout::setText() is used when we just want to render text with no formatting instructions. In this case, I've opted for the PangoLayout::setMarkup() method, which lets me use Pango's markup language to make part of the text bold. Many other attributes can be changed using this markup, which has shortcuts that somewhat resemble HTML.

Finally, the call to showLayout() renders the layout onto the surface. You can also render just a path, using layoutPath(), which sets the path on the surface so you can then use more advanced effects with Cairo. The layout is drawn using whatever the current source is on the Cairo context, so you can render using flat colours, gradients, other source images, or whatever takes your fancy. Additionally, this means that any transformations you have set on the Cairo context also take effect, allowing you to rotate, scale, shear and otherwise distort the text.

It probably looks a little complex just to write some text on an image, but it is rather flexible. This flexibility is handy when you consider that Cairo can render more than just PNG images; PDFs, PostScript and SVG are also easy to create. I hope that this post may inspire someone else to give it a try. Feedback and reports of issues are always welcome.

CMSes: an update

My previous post was typed in a bit of a hurry (was it that obvious?) and could probably have done with a few more sarcasm tags, though I think I still have a valid, if small, point. It surprised me that these well-maintained projects seem to be content to run with what I would consider to be errors. Though they do run, otherwise nobody would be using them. For what it's worth, SilverStripe and I have made up and it's all going swimmingly at present. Thanks to everyone who commented with recommendations; I'll try and follow some of them up when I get time.

One comment did ask me to define what I meant when I refer to a "lightweight" CMS -- basically, I was looking for something that will allow me to create a hierarchy of pages that consist roughly of a title and content. Nothing fancy. I don't need multiple content types, complex user management, discussion forums, built-in wikis, etc. It's the sort of thing I could have coded up in a couple of days using any number of frameworks, but I figured there'd be something off the shelf that'd do what I was after.

Another commenter questioned why I cared about coding standards warnings seemingly more than avoiding SQL injection and XSS and CSRF protection -- I don't, those are absolute hard requirements for any application. I'd put avoiding the coding standards warnings somewhere above "nice to have", but those warnings are indications that something is being done in a way which is either less than ideal (such as using non-static methods in a static context, a la Concrete5), or deprecated (such as using the ereg functions).

My next post shall try and be a little more constructive, as I release some code.

 1 2 3 4 Next →

About

User