[cairo] Proposal: cairo_save_state/cairo_restore_state

Behdad Esfahbod behdad at cs.toronto.edu
Mon Jan 23 20:49:23 PST 2006

On Mon, 23 Jan 2006, Carl Worth wrote:

> On Tue, 18 Jan 2005 10:16:01 -0500, Carl Worth wrote:
> >
> > Yes. That's a bug in naming. I think replacing cairo_copy with
> > cairo_current_gstate and cairo_set_gstate should clear this up.
> It's interesting to go back sometimes and see how old some ideas are.
> Apparently, very close to a year ago I had an idea to eliminate
> cairo_copy and replace it with (what we would call today)
> cairo_get_gstate and cairo_set_gstate.
> What happened back then was that we did eliminate cairo_copy. And the
> primary reason for doing so is still valid:
>     Copying a cairo_t is not a well-defined or generally useful
>     concept. A cairo_t contains several things that don't make sense
>     to copy generally:
> 	the target surface
> 	a stack of graphics "states"
> 	a path[1]

Ah, that's what I was missing when commenting in the bug

> Here's the API that I think is needed:
> 	cairo_state_t *
> 	cairo_save_state (cairo_t *cr);
> 	void
> 	cairo_restore_state (cairo_t *cr, cairo_state_t *state);
> along with the necessary:
> 	void
> 	cairo_state_reference (cairo_state_t *state);
> 	void
> 	cairo_state_destroy (cairo_state_t *state);
> These functions should be _really_ easy to implement. I'll come up
> with a patch soon if nobody beats me to it.

So cairo_state_t is going to be an alias for cairo_gstate_t if I
understand it correctly, right?

Before you get too much into implementation, can I request a
feature?  Make it copy-on-write?  All you need to do is to add a
flag to cairo_state_t stating if it's a saved state.  Then, any
function modifying a cairo_state_t will first check if it's a
saved state, if it is, it will duplicate it first and update the
cario_t.  It probably needs a bit of refactoring since you may
have not the cairo_t around when modifying cairo_state_t, but
this feature makes creating a cairo_t, configuring it (using
saved states) and using it extremely fast.  That was the
intention of the bug reporter at least...



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

More information about the cairo mailing list