GTK+ 将 gdk_draw_pixbuf 替换为 Cairo

GTK+ Replace gdk_draw_pixbuf with Cairo

我正在研究 GtkDrawingArea,它在 expose 事件中以平铺方式绘制 pixbuf 的一部分。平铺类型因源图像而异;它可以是正交的、等距的或六角形的。用 gdk_draw_pixmap 画这个很简单;这是一个关于它如何寻找等距平铺的示例:

for(y=0,row=0; y+tile_height<height; y+=tile_half_height,++row)
  for(x=((row&1)? tile_half_width : 0); x+tile_width<width; x+=tile_width)
    gdk_draw_pixbuf(widget->window,NULL,
                    pixbuf,src_x,src_y,
                    x,y,tile_width,tile_height,
                    GDK_RGB_DITHER_NONE,0,0);

结果:

不过,和开罗画的一样,结果却大相径庭。这是我目前所拥有的,但没有用:

cairo_t *cr = gdk_cairo_create(widget->window);
gdk_cairo_set_source_pixbuf(cr,pixbuf,src_x,src_y);
for(y=0,row=0; y+tile_height<height; y+=tile_half_height,++row)
  for(x=((row&1)? tile_half_width : 0); x+tile_width<width; x+=tile_width) {
    cairo_rectangle(cr,x,y,tile_width,tile_height);
    cairo_paint(cr);
  }
}
cairo_destroy(cr);

结果:

Cairo 只是拒绝绘制图像,因为会绘制普通的光栅图像。我哪里做错了,怎么解决的?

未经测试,但可能有效。如果没有,请反馈,我会再试一次:

static void _cairo_gdk_draw_pixbuf(cairo_t *cr, cairo_surface_t *source,
        int src_x, int src_y,
        int dest_x, int dest_y,
        int width, int height)
{
    cairo_save(cr);

    /* Move (0, 0) to the destination position */
    cairo_translate(cr, dest_x, dest_y);

    /* Set up the source surface in such a way that (src_x, src_y) maps to
     * (0, 0) in user coordinates. */
    cairo_set_source_surface(cr, source, -src_x, -src_y);

    /* Do the drawing */
    cairo_rectangle(cr, 0, 0, width, height);
    cairo_fill(cr);

    /* Undo all of our modifications to the drawing state */
    cairo_restore(cr);
}

上面的函数应该像gdk_draw_pixbuf一样工作(好吧,类似于它)。