Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XDrawString incorrect encoding

Tags:

c++

gcc

x11

xlib

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:

enter image description here

Why in result window text is in wrong encoding? What I'm missing?

like image 350
dima.rus Avatar asked Jan 06 '23 08:01

dima.rus


1 Answers

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.

like image 84
n. 1.8e9-where's-my-share m. Avatar answered Jan 14 '23 15:01

n. 1.8e9-where's-my-share m.