[cairo] drawing into GDK bitmap (pixmap of depth 1)?

Eric Smith eric at brouhaha.com
Sun Feb 19 16:11:55 PST 2006


I'm trying to convert a simple GTK application from using GDK drawing
primitives to Cairo (1.0.2 on Fedora Core 5 test 2 x86_64).  Most of it
works fine.  But the application wants a round window, so it creates a
GDK bitmap, fills it with black, draws a filled white circle in it, and
passes that to gtk_widget_shape_combine_mask() to get a round window.
That worked fine drawing into the bitmap with GDK, but I can't seem to
make it work with Cairo.  My attempt is:

    bitmap = (GdkDrawable *) gdk_pixmap_new (NULL, width, height, 1);
    cr = gdk_cairo_create (bitmap);

    // fill bitmap with black
    cairo_rectangle (cr, 0, 0, width, height);
    cairo_set_source_rgb (cr, 0, 0, 0);
    cairo_fill (cr);

    // draw white filled circle
    cairo_arc (cr, width / 2, height / 2, width / 2, 0, 2 * M_PI);
    cairo_set_source_rgb (cr, 1, 1, 1);
    cairo_fill (cr);

    cairo_destroy (cr);

What happens is the entire bitmap gets filled with white, so the window
ends up being rectangular.  If I comment out the section that attempts
to fill the rectangle with black, as expected I get random (leftover)
black and white pixels outside the circle, and white pixels inside the
circle.

I'm new to Cairo; am I doing something wrong?

The old code that works is:

    bitmap = (GdkDrawable *) gdk_pixmap_new (NULL, width, height, 1);
    gc = gdk_gc_new (bitmap);

    // fill bitmap with black
    gdk_gc_set_foreground (gc, & black);
    gdk_gc_set_background (gc, & white);
    gdk_draw_rectangle (bitmap, gc, TRUE, 0, 0, width, height);

    // draw white filled circle
    gdk_gc_set_foreground (gc, & white);
    gdk_gc_set_background (gc, & black);
    gdk_draw_arc (bitmap, gc, TRUE, 0, 0, width, height, 0, 360*64);

Thanks!
Eric



More information about the cairo mailing list