I want to display cyrillic symbols with XDrawString()
, but on screen they are shown in wrong encoding.
I have installed xfonts-cyrillic
in my system (ubuntu 14.04), but still any font from xlsfonts
list shows wrong result.
My code (main.cpp):
#include <X11/Xlib.h>
#include <string.h>
int main(int, char**)
{
Display *d = XOpenDisplay(0);
Window r = DefaultRootWindow(d);
Window w = XCreateSimpleWindow(d, r, 0, 0, 256, 256, 0, 0, 0xffffff);
GC gc = DefaultGC(d, 0);
XMapRaised(d, w);
XSelectInput(d, w, ExposureMask);
Font font = XLoadFont(d, "9x15-cyrillic");
XSetFont(d, gc, font);
const char *msg = "тут текст"; // cyrillic symbols
while (1)
{
XEvent e;
XNextEvent(d, &e);
XDrawString(d, w, gc, 16, 16, msg, (int) strlen(msg));
}
}
Compile:
g++ -Wall -g -std=c++11 main.cpp -L/usr/lib/X11 -lX11 -o output
My result is:
Why in result window text is in wrong encoding? What I'm missing?
X11 predates Unicode by several millennia (in Internet years). Your program probably uses UTF-8, and X11 by default does not. Try Xutf8DrawString.
Alternatively, figure out which encodings your fonts use (xlsfonts will tell you, because the encoding is a part of XLFD, but it looks like KOI8-R to me) and use that encoding for your string.
It should be noted that real programs rarely use server-side fonts these days. See this for more info.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With