[cairo] cairo with gtk problem

Kristian Høgsberg krh at bitplanet.net
Sun May 28 11:12:47 PDT 2006


On 5/25/06, Yogesh Arora <yogesh.ar at gmail.com> wrote:
> hi
>
> i tried using cairo with gtk using this code

Hi,

It looks like you've gradient example from the cairo snippets as a
starting point.  The cairo snippets code is special, though, since it
sets a transformation that maps the output coordiate space into the
unit square.  This means that all coordinates in the snippets code are
between 0 and 1, and the transformation scales them up to match the
size of the output surface.  If you want to use the snippets code
as-is you could set a similar transformation, but it's easier and more
intuitive to just change it to use the default user space.

>                 cairo_t* cr = gdk_cairo_create (window->gobj());
>
>         cairo_pattern_t *pat = cairo_pattern_create_linear (0.0, 0.0,  0.0, 1.0);

You're specifying the gradient endpoints as (0, 0) and (0, 1).  The
default coordinate system for the cairo_t created from the gdk window
has 1 cairo unit = 1 pixel, so this will give you a tiny gradient in
the upper row of pixels.  You probably want to specify (0, height) as
the end point of the gradient, where 'height' is the height of your
window.

>         cairo_pattern_add_color_stop_rgba (pat, 1, 0, 0, 0, 1);
>         cairo_pattern_add_color_stop_rgba (pat, 0, 1, 1, 1, 1);
>         cairo_rectangle (cr, 0, 0, 1, 1);

Likewise, this will give you a small 1x1 pixel rectangle in the upper
left corner of your window.  It looks like there's a brighter pixel in
the upper left corner of your screenshot, but it's hard to tell
because of the jpeg-artifacts.  Try using cairo_rectangle (cr, 0, 0,
width, height);

In general, when sending screenshots of cairo output, use the png file
format. The png file format is loss-less, which means that it will not
change the pixel contents in order to get better compression.

>         cairo_set_source (cr, pat);
>         cairo_fill (cr);
>         cairo_pattern_destroy (pat);
>
>         pat = cairo_pattern_create_radial (0.45, 0.4, 0.1,
>                                    0.4,  0.4, 0.5);

Similarly, you need to scale the coordinates for the midpoints with
the width and height of your window.

cheers,
Kristian


More information about the cairo mailing list