[cairo] Hit detect surface?

Bill Spitzak spitzak at d2.com
Tue Feb 28 14:07:12 PST 2006


This talk about GnomeCanvas needing complicated hit detection made me 
wonder if Cairo could support hit detection. This could be done with an 
entire new surface type and thus not affect any existing cairo backends. 
As I see it it would work something like this:

1. Program creates or reuses the hit-detect surface. This acts like an 
image surface of exactly one pixel. Program uses a surface-specific 
function to set a callback pointer.

2. Program adds a transformation so that the region of interest is 
transformed to the square 0,0..1,1.

3. Program uses any Cairo calls it wants to draw into this surface.

4. All attempts to modify the one pixel do the callback function, giving 
it the source color, the mask, and the op used, and possibly other 
information.

This can be used to see if some arbitrary Cairo code draws anything (or 
anything non-transparent) in a parallelogram-shaped region. To do 
accurate hit detection the caller should loop, using smaller areas each 
time, until only one object is hit or a minimum size is reached.

I'm not sure when exactly it should do the callback function. I think it 
should not call it if the pixel is clipped, or is outside the path, or 
the mask is zero. It should call it if the source color is zero, so that 
clicking on transparent parts of images works. It should also call it 
(with zero alpha) for any clear pixels inside the bounding boxes of 
glyphs, allowing the program to click on lines of text or letters 
themselves. However it could do the callback on *every* call, provided 
enough information is provided so the callback can ignore things that 
are outside the region of interest.


More information about the cairo mailing list