[cairo] How is cairo_show_surface supposed to work?

Per Bjornsson perbj at stanford.edu
Wed Feb 9 20:05:35 PST 2005


Hi,

I'm having some real trouble understanding what the cairo_show_surface
function is supposed to do. Given the declaration

void cairo_show_surface (cairo_t *cr, cairo_surface_t *surface,
                         int width,int height);

I would have guessed that it composites the surface pointed to by
"surface" onto the surface associated with "cr", with the upper right
corner (modulo rotations) at the current point and the size (in user-
space units) of the composited surface given by "width" and "height" (I
guess that the fact that "width" and "height" are integers are a hint
that I might be wrong though). Given this, I hacked up a little snippet
(can be used with cairo_snippets, just put it in the snippet directory
and make) which I thought should draw an X translucently on top of an
outlined square. Instead, given the current parameters, it paints a
uniform (as far as I can tell) shade of blue across the whole output
image. (Mucking around with linewidth and alpha parameters I can get it
to produce a gradient or nothing as well.) The snippet is attached.

Is my understanding of what cairo_show_surface is supposed to do
completely wrong (I tried to dig through the code but I still can't seem
to wrap my brain around all the different spaces used in
_cairo_gstate_show_surface)? Should I be doing something entirely
different?

Best regards,
Per Bjornsson

-------------- next part --------------
cairo_surface_t *overlay;

snippet_normalize (cr, width, height);

overlay = cairo_surface_create_similar(cairo_current_target_surface(cr),
					CAIRO_FORMAT_ARGB32,width,height);

cairo_save(cr);
cairo_set_target_surface(cr,overlay);

/* Draw a blue cross on the overlay surface */
cairo_set_rgb_color(cr,0.0,0.0,1.0);

/* If this line width is set to a value >0.55 something is drawn 
 * (a gradient or a solid color depending on the value)
 * For 0.55 or lower it looks completely transparent to me. */
cairo_set_line_width(cr,0.56);
cairo_set_line_cap(cr,CAIRO_LINE_CAP_ROUND);
cairo_move_to(cr,0.2,0.2);
cairo_line_to(cr,0.8,0.8);
cairo_stroke(cr);
cairo_move_to(cr,0.2,0.8);
cairo_line_to(cr,0.8,0.2);
cairo_stroke(cr);

/* Now draw a square on the main surface and composite the overlay 
   surface on top of it */
cairo_restore(cr);
cairo_rectangle(cr,0.25,0.25,0.5,0.5);
cairo_set_rgb_color(cr,0.0,0.0,0.0);
cairo_stroke(cr);

cairo_set_alpha(cr,0.8);
cairo_show_surface(cr,overlay,1,1);
cairo_surface_destroy(overlay);


More information about the cairo mailing list