Xft can draw rectangles on Pixmaps, but not text

吃可爱长大的小学妹 提交于 2019-12-25 07:37:26

问题


I am trying to render some text with Xft and set the resulting Pixmap as X root background.

Despite having some diplomacy with X, the code I have is simple:

#include <stdio.h>
#include <unistd.h>

#include <X11/Xlib.h>
#include <X11/Xft/Xft.h>

int main()
{
    char font[] = "helvetica:size=11";
    char buf[] = "Lorem Ipsum";
    int s, x = 12;
    XRenderColor color = {0xFFFF, 0, 0, 0xFFFF};
    Display * d;
    Window r;
    Pixmap p;
    XftFont * f;
    XftDraw * drw;
    XftColor xftc;

    d = XOpenDisplay(NULL);
    s = DefaultScreen(d);
    r = RootWindow(d, s);
    if (!(f = XftFontOpenName(d, s, font))) printf("could not open font\n");
    if(!(p = XCreatePixmap(d, r, 1600, 900, DefaultDepth(d, s)))) printf("could not create pixmap\n");

    drw = XftDrawCreateAlpha(d, p, DefaultDepth(d, s));
    XftColorAllocValue(d, DefaultVisual(d, s), DefaultColormap(d, s), &color, &xftc);
    XftDrawStringUtf8(drw, &xftc, f, 100, 100, (XftChar8 *)buf, x);
    XftDrawRect(drw, &xftc, 10, 10, 50, 50);

    XSetWindowBackgroundPixmap(d, r, p);
    XFlush(d);

    XCloseDisplay(d);

    return 0;
}

This shows only a red rectangle at (10, 10) at root background and not the expected red "Lorem Ipsum" in Helvetica at (100, 100):

If I remove the Pixmap, the call to XftDrawCreateAlpha and XSetWindowBackgroundPixmap and draw directly onto the window, as in

drw = XftDrawCreate(d, r, DefaultVisual(d, s), DefaultColormap(d, s));
XftColorAllocValue(d, DefaultVisual(d, s), DefaultColormap(d, s), &color, &xftc);

while(1) {
    XftDrawStringUtf8(drw, &xftc, f, 100, 100, (XftChar8 *)buf, x);
    XftDrawRect(drw, &xftc, 10, 10, 50, 50);

    XFlush(d);
    sleep(1);
}

the result is as expected:

I cannot see why does text rendering not work when I use a Pixmap and XftDrawCreateAlpha. What can be the reason?

来源:https://stackoverflow.com/questions/40719258/xft-can-draw-rectangles-on-pixmaps-but-not-text

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!