[Cairo] Ruby/Cairo 0.1

Evan Martin martine at danga.com
Mon Oct 27 07:53:28 PST 2003


I've been working on Ruby bindings for Cairo.  I think I have most of
the hard part done, but I have a few functions/enums left.  (Most of it
is autogenerated from cairo.h-- thanks for such a consistent header!)

I'm mostly posting this so there's less possibility of duplicating
effort, because it's not complete.  However, I have written a bunch of
test programs with it.

I'm especially unsure of how to handle the API that deals with X,
because I'm not sure what the popular way to wrap X in Ruby is.  If
anyone could point me at a definitive package (Google reveals two) I'll
work with that.  For now, I have a super-simple X wrapper that's just
enough to put up a window.

In the future, I also hope to add a module to Ruby/GNOME2 (the GTK
wrappers) for a "cairo" widget (isn't there something like that in one
of the sample programs?).

Anyway, here's a snapshot of what I have:

  http://neugierig.org/software/ruby/cairo/ruby-cairo-0.1.tar.bz2


And here's the obligatory small sample program (this one randomly draws
splines over the window whenever it refreshes).  It demonstrates some of
the Rubyisms I've tried to fit the API to (see the lines with
"line_width" and "stroke"):


require 'cairo'

class SplineWindow < Cairo::Xlib::Window
  def initialize(dpy)
    @width = @height = 400
    super(dpy, @width, @height, false)
  end

  def randx
    10 + rand(@width-20)
  end
  def randy
    10 + rand(@height-20)
  end

  def draw
    @cairo.set_rgb_color(1, 1, 1)
    @cairo.line_width = 2 + ((rand**2.0) * 50)
    @cairo.stroke {
      @cairo.move_to(randx, randy)
      @cairo.curve_to(randx, randy, randx, randy, randx, randy)
    }
  end

  def keypress(key)
    case key.chr
    when 'q'
      @dpy.quit
    end
  end
end

dpy = Cairo::Xlib::Display.new
sw = SplineWindow.new(dpy)
dpy.mainloop

-- 
Evan Martin
martine at danga.com
http://neugierig.org




More information about the cairo mailing list