[cairo] surfaces and backends

Behdad Esfahbod behdad at cs.toronto.edu
Fri Feb 24 11:59:06 PST 2006


On Fri, 24 Feb 2006, Carl Worth wrote:

> On Sat, 21 May 2005 11:01:22 -0400, Owen Taylor wrote:
> > What if we had:
> >
> > enum {
> >    CAIRO_IMAGE_SURFACE_TYPE,
> >    CAIRO_XLIB_SURFACE_TYPE,
> >    [...]
> >    CAIRO_FT_FONT_FACE_TYPE,
> > } CairoTypeID;
> >
> > CairoTypeID cairo_surface_get_type (cairo_surface_t *surface);
>
> Any likes or dislikes about the API should be made soon. I'll add an
> implementation shortly, and if I haven't heard any complaints, will
> push this out to the central tree after that.
>
> -Carl

I don't like the hardcoding of backend-specific stuff in cairo.h.
cairo does not export enough internals to allow implementing a
backend out of tree I guess, but still I find the following
approach more flexible:

typedef const char *CairoTypeID;

CairoTypeID cairo_surface_get_type (cairo_surface_t *surface);

and in cairo-xlib.h have:

extern const CairoTypeID cairo_xlib_surface_type;

and in cairo-xlib.c:

const const CairoTypeID cairo_xlib_surface_type = "xlib surface";

or something like that.  To check whether a surface is an xlib
surface, you would do pretty similar to your design:

  assert (cairo_surface_get_type (surface) == cairo_xlib_surface_type);

The advantage is that if you don't know about a surface type, you
can simply write it out in an error message thing...

You do need to fetch the xlib header in to be able to check the
type, but you are doing that anyway most of the time.
Alternatively, if you don't want to fetch the header in, you can
use strcmp...

Something like this.  Of course, if you don't quite like the
char* thing, you can make any arbitrary struct too...  But don't
reinvent gtype ;)

--behdad
http://behdad.org/

"Commandment Three says Do Not Kill, Amendment Two says Blood Will Spill"
	-- Dan Bern, "New American Language"


More information about the cairo mailing list