[cairo] Bump: ImageSurface from a Pattern

Donn donn.ingle at gmail.com
Thu Dec 11 23:45:10 PST 2014

Hello list. It's heading for the weekend, so I'm gonna risk a bump
before the quiet..

In the C below, my intent is:
1. Draw a 100px rectangle.
2. Set it as a clip - so that the size will be 100px.
3. pop_group into 'pat' var.
4. Get the surface of that pattern.
5. Get the width of that surface (i.e. 100px)

The code is giving me 400px - the size of the initial cr surface.

I am making many patterns and keeping them in my objects. I am drawing 
from the pattern rather than re-drawing by cairo draw commands - for speed.

I'd like to know if those patterns are the size I want them to be, not 
to mention keeping track of how much memory they are taking.

If I draw a 10px by 10px circle, then the pattern *should* be 10px by 
10px - not the size of the main window, for example.

I hope someone can assist.

C code pasted:
//gcc thiscode.c -o test -lm `pkg-config --cflags --libs cairo`

#include <cairo.h>
#include <stdio.h>

static cairo_t *get_surface(int width, int height)
	cairo_surface_t *surface = cairo_image_surface_create 
(CAIRO_FORMAT_ARGB32, width, height);
	cairo_t *cr = cairo_create (surface);
	cairo_surface_destroy (surface);
	return cr;

int main(void) {

   cairo_t *cr = get_surface (400, 400);
	cairo_set_source_rgb (cr, 0, 0, 0);
	cairo_paint (cr);

   cairo_pattern_t *pat;


     cairo_rectangle (cr, 0,0,100,100);

     cairo_clip_preserve (cr);//try to limit the size to 100 px

     cairo_set_source_rgb (cr,1,1,1);
     cairo_stroke (cr); //Draw it.

   pat = cairo_pop_group(cr); //record it.

   cairo_set_source(cr,pat); //Just..
   cairo_paint(cr); //show it anyway.

   cairo_surface_t *surface;

   cairo_pattern_get_surface (pat, &surface); //Get the surface of the 

   //..and fail to get 100px as the width.
   printf ("width: %d\n", cairo_image_surface_get_width (surface));

   //Make a png for kicks.
   cairo_surface_write_to_png (cairo_get_target (cr), "pat.png");


   return 0;



More information about the cairo mailing list