[cairo] Clipping and stroking

Carl Worth cworth at redhat.com
Fri Apr 29 17:57:08 PDT 2005


On Fri, 29 Apr 2005 19:25:50 -0400, Owen Taylor wrote:
>  cairo_save (cr);
>  cairo_identity_matrix (cr);
>  cairo_stroke_extents (cr, &x1, &x2, &y1, &y2);
>  cairo_restore()

That's a really cute trick with cairo_identity_matrix. Thanks for
suggesting it. Of course, in order to do something useful with the
numbers, (such as cairo_clip), one might also want to have an identity
matrix in place for that operation as well. See below.

>  x1 = floor (x1);
>  x2 = ceiling (x2);
>  y1 = floor (y1);
>  y2 = ceiling (y2);

I hadn't realized that cairo_stroke_extents and cairo_fill_extents
return a pair of coordinates x1,y1,x2,y2 rather than x,y,width,height
like cairo_rectangle, (and almost like cairo_text_extents). I propose
to change these functions to return x,y,width,height for consistency.

With that change, one could have a complete operation for "clip to
stroke extents (and destroy current path)" fairly easily with:

	cairo_save (cr);
	{
	    cairo_identity_matrix (cr);
	    cairo_stroke_extents (cr, &x, &y, &width, &height);
	    x = floor (x);
	    y = floor (y);
	    width = ceil (width);
	    height = ceil (height);
	    cairo_new_path (cr);
	    cairo_rectangle (cr, x, y, width, height);
	}
	cairo_restore (cr);

	cairo_clip (cr);

Notice that the cairo_rectangle call is within the save/restore block
so that it will get the identity matrix it needs, but also that
cairo_clip is outside the block as it must be in order to have the
desired effect. This construct also only works because the path is not
part of the graphics state that is saved/restored, so I think we've
got yet another piece of evidence that that was a good change.

If destroying the current path isn't desired, it could be saved
at the beginning with:

	cairo_path_t *path = cairo_copy_path (cr);

and restored at the end with:

	cairo_append_path (cr, path);
	cairo_path_destroy (path);

-Carl
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : http://lists.freedesktop.org/archives/cairo/attachments/20050429/cc216165/attachment.pgp


More information about the cairo mailing list