Reputation: 906
I am making an application that renders texts according to style mentioned on screen using X Windows System and Xft. My code is working fine as shown below.
#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;
}
But i am wondering that how can i add line breaks in the text entered. I tried using "/n" and also tried to make array and used loops but it didn't work.
Upvotes: 0
Views: 907
Reputation: 1537
I'm currently using libXft 2.3.3 and the new lines (\n
) are printed correctly when I use XftDrawStringUtf8
.
int x,y;
char_t utf8_string[8192];
XftColor font_color
XftFont *font;
XftDraw *xft_drawable;
Display *display;
Drawable drawable;
// Get a display
// display = xxx;
// Get its root window
// root_window = xxx;
drawable = XCreatePixmap(display,root_window,640,480,24);
xft_drawable = XftDrawCreate(display, drawable, DefaultVisual(display, DefaultScreen(display) ), DefaultColormap(display, DefaultScreen(display) ) );
if (xft_drawable == NULL) {
printf("can't created Xft drawable\n");
return;
}
// Fill utf8_string and make sure that the string ends with 0
// Calculate the string length by including the \n character in the count
// Initialize a font_color and a font
// Set x and y values to set the text position
XftDrawStringUtf8(xft_drawable, &font_color, font, x, y, (const FcChar8 *) utf8_string, utf8_string_length);
XftDrawDestroy(xft_drawable);
Upvotes: 0
Reputation: 91
New line "\n" will not be rendered by Xft. You need to render each line separately with proper offset, depending on font size and desired spacing. I have modified the ending block of your code with sample text rendered two times on separate lines.
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));
}
Upvotes: 2