简体   繁体   English

在xlib和cairo中绘制带有透明度的图标

[英]draw icon with transparency in xlib and cairo

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. 我有一个掩码,图标为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. 我找不到用互联网上的cairo来解决这个问题的例子。 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. 先感谢您。

Here is an example from awesome where it "turns" the icon into a cairo surface: https://github.com/awesomeWM/awesome/blob/430f4fab15bb101b4af9fadbebb9a9bfa47ba9de/objects/client.c#L1501 这是一个很棒的例子,它将图标“转变”为一个开放表面: https//github.com/awesomeWM/awesome/blob/430f4fab15bb101b4af9fadbebb9a9bfa47ba9de/objects/client.c#L1501

This uses xcb instead of Xlib, but you should manage to still understand this. 这使用xcb而不是Xlib,但你应该设法仍然理解这一点。 The part that handles a mask begins in line 1538. Basically, a new cairo surface is created and a cairo context is set up for it. 处理掩码的部分从第1538行开始。基本上,创建一个新的cairo表面并为其设置一个cairo上下文。 The source surface is the icon and the mask is applied via cairo_mask_surface ("use the alpha channel of some cairo surface as the mask of a drawing operation"). 源表面是图标,掩码通过cairo_mask_surface应用(“使用某些cairo表面的alpha通道作为绘图操作的掩码”)。 You can just copy this part to your code so that you don't have to draw the icons to a temporary surface. 您只需将此部件复制到代码中,这样就不必将图标绘制到临时表面。

TL;DR: The answer to your //How do I apply the mask? TL; DR:你的答案//How do I apply the mask? is: Use cairo_mask_surface() instead of cairo_paint() . 是:使用cairo_mask_surface()而不是cairo_paint()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM