Create PDFs with CoreGraphics and Python

I've just stumbled onto one of the neatest features of for developers. I haven't seen any mention of this before, but then I don't think many folk spent an idle Saturday morning catching up on the news and idly clicking away inside the Developer folder.

It turns out I have a couple of disk space issues (my policy of doing integral laptop backups leads to a lot of redundant files), so I was looking for a way to visualize disk usage in . One of the most intuitive applications I have for that (under Windows) is SpaceMonger, a very neat utility that gives me a graphical overview of disk usage using a treemap-like approach. TkDu provides mostly the same experience under X11, but I wanted something that didn't require X11 to run (or Tk, for that matter).

Despite not going in for Objective-C and having studiously avoided coding in Cocoa so far (it's not a portable enough skill to invest my time in), I started looking around for OpenGL samples. Nevertheless, I had a stroke of luck: while hunting through the Developer/Examples folder, I came upon Quartz/Python and the mega-cool CoreGraphics bindings for , which enable you to create PDFs with nothing more than the Panther-bundled and a little ingenuity:

#  Simple python example to draw a circle

from CoreGraphics import *
import math   # for pi

pageRect = CGRectMake (0, 0, 612, 792)   #  landscape
c = CGPDFContextCreateWithFilename ("circle.pdf", pageRect)
c.beginPage (pageRect)

Being an old Postscript buff, the Quartz primitives are very familiar to me, and after reading through the great sample (which renders a contact sheet of an image folder), I felt confident enough to start hacking my own version of TkDu with PDF output.

After all, I don't really need a full-blown GUI app - running a simple script and getting Preview to pop up with the file map is enough, and I get Preview's built in zooming and panning functionality for free. Sure, it's not interactive, but I'd rather wait until Mono becomes easier to use in and I can hack together a more portable tool.

Code will be up in the CVS section once I can get it to work properly (the coordinate system is bit screwed up right now, and there are a few issues with fonts). In the meantime, I suggest you spread the word amongst the buffs - there are a lot more bindings in there.

Mono on X

I've taken a bit more interest in Mono and DotGNU lately. There's been a lot of both dumb and insightful press coverage on it, but MonoDevelop and the push for getting it to play well with Gnome are turning Mono into a more palpable (and relevant) technology.

Of course getting it to run "properly" (i.e., easily) in will entail integration, but that seems to be in the works. I can't find any follow-up on this, though - and I guess it will be a while until we can get a nice installer (or a set of Fink packages) to make it easier to get Mono running on .

Odds and Ends

I've picked up my Bayesian RSS classifier project again, and at this point I've put portions of it up on CVS. I'm testing the classifier with a custom harness and trying to figure out a decent UI for it at the same time, and found quite a few usability issues (for instance, making it easy to train the classifier on specific RSS entries with a single click, or delivering an acceptable experience on a PDA). It's going to take a while...

This page is referenced in: