Archive for the ‘cairo’ Category

SVG Cairo goodness

Monday, January 9th, 2006

I’ve been hacking on binding the libsvg-cairo library as an add-on to the existing cairo bindings. It’s actually quite a nice small API.


Gtk2Hs can already load SVG files, but only as fixed size bitmaps. With this API we get a proper vector method. Since the drawing is done via cairo in the Render monad we can apply any 2D transformation. These screenshots are of a simple svg viewer. It’s only 30 lines of code overall. The following snippet is what does the actual drawing.

win <- drawingAreaGetDrawWindow canvas
let (width, height) = sizeSVG svg
(width', height') <- drawingAreaGetSize canvas
renderWithDrawable win $ do
  scale (realToFrac width' / realToFrac width)
        (realToFrac height' / realToFrac height)
  renderSVG svg

You can see that we setup the scaling transformation before rendering the svg picture. We scale the image so that it fits the size of the canvas. So as we resize the window the image scales nicely.


Getting started with Cairo

Thursday, November 10th, 2005

Using ghci is a great way of experimenting with Cairo. You might experience problems due to the way ghci and Gtk2Hs both require to be run concurrently. I’ve created a little code snippet that opens a dialog box, displays the given Cairo action and waits for you to click the Close button.

The run function will execute the given Cairo statements and will then continue to run the Gtk main loop until the dialog is actually removed from screen. This function can conveniently be used to test various Cairo commands. For example, try:

run (moveTo 200 200 >> lineTo 0 0 >> stroke)

If straight lines are too boring, you can do this:

run (moveTo 200 200 >> setLineWidth 20 >> curveTo 200 0 100 200 0 0 >> stroke)

To render text, you need to create a layout first:

run (moveTo 20 20 >> rotate (pi/4) >> createLayout "Haskell and Cairo is cool." >>= showLayout)

Note that the return value of createLayout is passed to showLayout.

Koch rules

Friday, September 2nd, 2005

Some people complained about the images on the previous post being a little similar, or mirrored to each other. So I picked up the book boegel suggested (a wonderful reading so far, in some sense it reminds me of the GEB), and I tried writing a more complex L-System - these are the nice results using the exactly same grammar every time (yes I know, it still grows downwards…):


I also managed to draw Koch’s LSystems, (thanks basti!) here’s the Koch’s Curve:


…and the Koch’s Snowflake:


Next on the todo list: add weights to rules to “manipulate” randomness! :-D

Random is sexy

Thursday, September 1st, 2005

Hiya, this was a fun night, I can’t stop playing with cairo code, yay! I hacked non-determinism inside the LSystem module - now the same grammar, if contains multiple rules for the same variable, will produce different images in every render!

Another step towards making a cool, and complete, Haskell version of Chris Coyne’s Context-Free Now, some new porn (notice that I’m using the same grammar every time)


I’m dedicating this images to Sebastien Bacher, who’s packaging GNOME for Ubuntu… alone! And “due to peer pressure” *cough* he packaged cairo 1.0.0 like 10 mins after its release, thank you, seb! :-)