[cairo] save/restore guidelines?

Ian Britten britten at caris.com
Thu Aug 14 12:40:32 PDT 2008

Hi all,
I'm sortof familiar with the save/restore idiom when drawing,
from some previous work with PostScript.  I see that Cairo
provides this mechanism, and was wondering if anyone had any
guidelines about when (or when not) to use it?

For example, I have a 'cairo canvas' object that derives from
our own 'canvas' object, and it has an interface to "draw a
line", given a list of points and one of our Pens.
Would I do a save/restore at the start/end of this operation?

Given that the Pen I'm passed contains most/all draw parameters
(colour, cap, thickness, etc), and given that I'll have to
propagate all of those from our class to Cairo, it seems to
me that I'd be explicitly qualifying every aspect of the draw,
each time I draw something.  As such, that makes me wonder if
I'd need to bother doing the save/restore?  Wouldn't
continually modifying the context be suitable?

I could see using save/restore if I had somehow set a default
presentation for my lines, and only wanted to update the
context for an occasional line that was non-default.  However,
that doesn't match our use-case, where every line is
potentially different, and thus each is "fully qualified".

I'd also be wondering about any performance consideration,
especially if using Cairo for on-screen graphics, etc.

Is it maybe just clipping operations that would benefit
from save/restore calls?  Or have I overlooked/misunderstood

Many thanks for any input!  I'm just trying to get my
groundwork/techniques right :)

More information about the cairo mailing list