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

时间:2017-03-15 02:33:51

标签: c x11 freetype2

我正在制作一个应用程序,根据屏幕上提到的样式使用X Windows System和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”并尝试制作数组并使用循环,但它不起作用。

1 个答案:

答案 0 :(得分:0)

新线&#34; \ n&#34;将不会由Xft呈现。您需要使用适当的偏移量分别渲染每条线,具体取决于字体大小和所需间距。 我修改了代码的结束块,样本文本在不同的行上呈现两次。

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));
    }