[cairo] Filling everything but the shape

Travis Griggs tgriggs at cincom.com
Mon May 26 11:31:43 PDT 2008

On May 26, 2008, at 3:36 AM, Ricky Wood wrote:

> I'm new to cairo and probably missing some obvious concept, please  
> can someone enlighten me with this problem:
> Basically, I have a surface with some shapes already drawn onto it.   
> I now want to paint every part of the surface a single colour except  
> a circle, i.e. leave the contents of the surface, where the circle  
> lies, undisturbed.
> The approach I took was:
> - create a mask
> - paint the mask totally opaque
> - fill a transparent arc on the mask
> - set the colour on the source
> - cairo_mask
> I thought the source colour would be transferred to the destination,  
> leaving the circle masked.
> Where have I gone wrong, or is there a better approach?

There is an (imo) easier approach.

- get the extents of the current clip
- use rectangle() to add these extents as a path
- use the *negative* arc API to add your circle to the path
- fill

The important part is that your arc goes _backwards_ from the winding  
direction of the first rectangle. This causes a "negative clip". This  
trick of paying attention to which direction your paths wind, can do  
all kinds of intersting things. I've used it to fill in one op, a  
"cheese grater" effect, where the rectangle (with rounded corners) had  
a variety of "holes" cut into it, in the shapes of hearts, clovers,  
diamonds, horseshoes, moons, and rainbows... :) It made a good demo.

Travis Griggs
tgriggs at cincom.com
"The dissenter is every human being at those moments of his life when  
he resigns momentarily from the herd and thinks for himself." - 
Archibald MacLeish, poet and librarian

More information about the cairo mailing list