Just for fun tonight I wrote a Ruby binding for Cairo, so I can learn my way around it more. If anyone's morbidly curious, it's in CVS under mental/cairo-testbed in the experimental module.
More details:
The Ruby extension is in the cairo/ subdirectory; the cairo-testbed directory proper will contain some scripts and things that use it (right now it just contains a simple demo script, demo-0.rb, that generates a PNG).
To build the extension, just run:
ruby extconf.rb make
The binding is moderately complete at this point; the only two areas I've not really covered yet are fonts and patterns, and I need to expose more surface types (currently only in-memory and PNG output surfaces are exposed).
In terms of the major classes at this point:
* Cairo - a cairo graphics context * Cairo::Surface - a cairo drawing surface * Cairo::Transform - AKA Cairo::Matrix, a matrix representing an affine transformation
In general the way things work are that you create a cairo context, set a target surface for it to draw on, and draw away, calling show_page when you're done.
require 'cairo'
File.open("foo.png", "wb") {|port| ctx = Cairo.new ctx.target_surface = \ Cairo::Surface.new_png(port, Cairo::FORMAT_ARGB32, 200, 200)
# fill in the background ctx.new_path ctx.rectangle(0, 0, 200, 200)
ctx.set_rgb_color(1.0, 1.0, 1.0) # white ctx.fill
# draw a big X ctx.new_path ctx.move_to(20, 20) ctx.line_to(180, 180) ctx.move_to(20, 180) ctx.line_to(180, 20)
ctx.set_rgb_color(0.0, 0.0, 0.0) # black ctx.line_width = 5 ctx.line_cap = Cairo::LINE_CAP_ROUND ctx.stroke
ctx.show_page # write the PNG }
-mental
participants (1)
-
MenTaLguY