使用 X11 渲染文本时如何添加换行符,

How can i add line breaks when rendering Text using X11,

我正在制作一个应用程序,它使用 X Windows 系统和 Xft 根据屏幕上提到的样式呈现文本。我的代码运行良好,如下所示。

#include <X11/Xlib.h>
#include <X11/Xft/Xft.h>
char * nowShowing() 
{
 return strdup("This is a sample text This is rendered with new Driver installed This is a sample text his is rendered with new Driver installed"); 
}
int main()
{
XftFont *font;
XftDraw *draw;
XRenderColor render_color;
XftColor xft_color;
char *str;
str = nowShowing();
int x = 70;
int y = 150;
Display *dis = XOpenDisplay (0);
int screen = DefaultScreen (dis);
Window  w = XCreateSimpleWindow (dis, RootWindow (dis, screen),
                                 0, 0, 1200, 300, 1,
                                 BlackPixel (dis, screen),
                                 WhitePixel (dis, screen));
XEvent ev;

render_color.red = 0;
render_color.green =0;
render_color.blue = 0;
render_color.alpha = 0xffff;

XftColorAllocValue (dis,
                    DefaultVisual(dis, screen),
                    DefaultColormap(dis, screen),
                    &render_color,
                    &xft_color);

//font = XftFontOpen(dis, screen,
  //                 XFT_FAMILY, XftTypeString, "charter",
  //                 XFT_SIZE, XftTypeDouble, 20.0,
   //                 NULL);
font = XftFontOpenName(dis,screen,"URW Palladio L:style=Bold Italic"); //it takes a Fontconfig pattern string

draw = XftDrawCreate(dis, w,
                     DefaultVisual(dis, screen),
                     DefaultColormap(dis, screen));

XSelectInput (dis, w, ExposureMask);
XMapWindow (dis, w);
for (;;)
{
    XNextEvent (dis, &ev);
    if (ev.type == Expose)
        XftDrawString8(draw, &xft_color, font, x, y, (XftChar8 *) str,
                       strlen(str));
}
return 0;
}

但我想知道如何在输入的文本中添加换行符。我试过使用“/n”,也试过制作数组和使用循环,但没有用。

Xft 不会呈现新行“\n”。您需要根据字体大小和所需间距分别渲染具有适当偏移量的每一行。 我已使用在不同行上呈现两次的示例文本修改了代码的结尾块。

if (ev.type == Expose)
    {
     int fonth = font->ascent + font->descent;

     XftDrawString8(draw, &xft_color, font, x, y, (XftChar8 *) str,
                   strlen(str));
     XftDrawString8(draw, &xft_color, font, x, y+fonth, (XftChar8 *) str,
                   strlen(str));
    }