1

Now that I've got the icons I want to draw them correctly. At the moment I'm using Cairo to draw these images on a window. I have a mask and the icon as pixmap.

cairo_surface_t *image;
cairo_surface_t *imask;
cairo_surface_t *surface;
cairo_t *csurface;

surface = cairo_xlib_surface_create(display, d, DefaultVisual(display, screen), 400, 400);
csurface = cairo_create(surface);

Pixmap icon;
Pixmap mask;

//XWM Stuff ...
if(icon != 0)
{
   get_pixmap_geometry(display, icon, &width, &height, &depth);
   image = cairo_xlib_surface_create(display, icon, DefaultVisual(display, screen), width, height);
   cairo_set_source_surface(csurface, image, 0, 0);
   //How do I apply the mask?
   //I tried cairo_set_operator(csurface, CAIRO_OPERATOR_SOURCE);
   cairo_paint(csurface);
}

But the icons do not have any transparencies. I've found no example to solve this with cairo on the internet. There is only a complicated way but it's so badly documented that it doesn't help me at all. Does someone have a link or an example for how to restore the original icon with its transparencies? Thank you in advance.

4

1 回答 1

3

这是一个很棒的例子,它将图标“变成”开罗表面:https ://github.com/awesomeWM/awesome/blob/430f4fab15bb101b4af9fadbebb9a9bfa47ba9de/objects/client.c#L1501

这使用 xcb 而不是 Xlib,但您应该设法仍然理解这一点。处理蒙版的部分从第 1538 行开始。基本上,创建了一个新的 cairo 表面并为其设置了 cairo 上下文。源表面是图标,并且通过cairo_mask_surface(“使用某些 cairo 表面的 alpha 通道作为绘图操作的蒙版”)应用蒙版。您可以将此部分复制到您的代码中,这样您就不必将图标绘制到临时表面。

TL; DR:您的答案//How do I apply the mask?是:使用cairo_mask_surface()而不是cairo_paint().

于 2016-01-17T10:52:00.380 回答