[cairo] ImageSurface from a Pattern

Uli Schlachter psychon at znc.in
Wed Dec 10 10:15:18 PST 2014


Hi Donn,

Am 10.12.2014 um 15:28 schrieb Donn:
> I'm doing some push_group, draw, pop_group() stuff. Once I have a ref to 
> the pattern, I'd like to know its width and height.
> 
> (I'm really hoping to find the size in bytes - just in case I'm creating 
> a cache-beast gone mad! *)
> 
> The docs are leading me in dizzy circles. I seem to be able to get a 
> generic Surface from the pattern, but not an ImageSurface on which I can 
> use cairo_image_surface_get_width()  etc.
> 
> Can you help? Python, Vala or C code is good.

C: If it really is an image surface: cairo_image_surface_get_width().

Generic (well, "the best thing possible", unbounded recording surfaces are
quite, well, unbounded):

static void
get_surface_size(cairo_surface_t *surface, int *width, int *height)
{
    double x1, y1, x2, y2;
    cairo_t *cr = cairo_create(surface);

    cairo_clip_extents(cr, &x1, &y1, &x2, &y2);
    cairo_destroy(cr);
    *width = x2 - x1;
    *height = y2 - y1;
}


> * On this subject: If I create a clip and draw within that, followed by 
> a pop_group, will the pattern's image surface be size (area) of the clip?
>   Is a clip even needed, i.e. is cairo clever enough to give me a 
> pattern of the appropriate size for the drawing just drawn?

Cairo does immediate drawing. This also includes the creation of groups. The
size of the surface that is used for the group is chosen during
cairo_push_group(). So no, cairo is not clever enough.

And to answer your first question: It uses the extents of the clip intersected
with the extents of the surface (if it is bounded). If the top-left position of
the clip is not at (0, 0), it uses cairo_surface_set_device_offset() to hide this.
So yeah, it will be the size of the clip.

Cheers,
Uli
-- 
"For saving the Earth.. and eating cheesecake!"


More information about the cairo mailing list