Result
Code
#include #include #include #include #include void
DrawCenteredMbString(Display* dpy, Window w, XFontSet fontset, GC gc, char* str, int num_bytes, int x, int y, int width,
int height) {
XRectangle boundingbox;
XRectangle dummy;
int originx, originy;
(void)XmbTextExtents(fontset, str, num_bytes, &dummy, &boundingbox);
originx = x + (width - boundingbox.width) / 2 - boundingbox.x;
originy = y + (height - boundingbox.height) / 2 - boundingbox.y;
XmbDrawString(dpy, w, fontset, gc, originx, originy, str, num_bytes);
}
int main(void) {
Display* d;
Window w;
GC gc;
XGCValues values;
XEvent e;
XFontStruct* fs, * fs16;
char** fonts;
int font_count;
XFontSet fontset;
char** missing_charsets;
int num_missing_charsets;
char* default_string;
char* msg = “Hello, World!”;
char* msg2 = “啊,世界,啊啊啊”;
int s;
if (setlocale(LC_ALL, “”) == NULL) {
printf(“cannot set locale \n”);
exit(1);
}
if (!XSupportsLocale()) {
printf(“X does not support locale %s\n”, setlocale(LC_ALL, NULL));
exit(1);
}
if (XSetLocaleModifiers(“”) == NULL) {
printf(“Warning: cannot set locale modifiers\n”);
}
d = XOpenDisplay(NULL);
if (d == NULL) {
fprintf(stderr, “Cannot open display\n”);
exit(1);
}
fonts = XListFonts(d, “*hanzi*“, 10000, &font_count);
for (int i = 0; i < font_count; i++)
printf(“%s\n”, fonts[i]);
XFreeFontNames(fonts);
if ((fs16 = XLoadQueryFont(d, “hanzigb16st”)) == NULL) {
printf(“Cannot load font\n”);
exit(1);
}
s = DefaultScreen(d);
w = XCreateSimpleWindow(d, RootWindow(d, s), 100, 100, 500, 500, 1,
777215, 111111);
printf(“BlackPixel(d, s) is %d\n”, (int)BlackPixel(d, s));
printf(“WhitePixel(d, s) is %d\n”, (int)WhitePixel(d, s));
fontset = XCreateFontSet(
d, “-*-*-*-*-*-*-16-*-*-*-*-*-*-*“,
&missing_charsets, &num_missing_charsets,
&default_string);
if (num_missing_charsets > 0) {
printf(“The following charsets are missing\n”);
for (int i = 0; i < num_missing_charsets; i++) {
printf(“%s \n”, missing_charsets);
printf(“The string is %s \n”, default_string);
printf(“of any characters from those sets\n”);
}
XFreeStringList(missing_charsets);
}
XSetLineAttributes(d, gc, 5, LineSolid, CapRound, JoinRound);
XSelectInput(d, w, ExposureMask KeyPressMask);
gc = XCreateGC(d, w, 0, &values);
XMapWindow(d, w);
int y = 50;
for (int i = 0; i < 10; i++) {
//XFillRectangle(d, w, DefaultGC(d, s), 20, 20, 10, 10);
XDrawString(d, w, DefaultGC(d, s), 10, y + i * 20, msg, strlen(msg));
//XSetFont(d, gc, fs16->fid);
//XDrawString16(d, w, gc, 100, y + i * 20, (XChar2b *) msg2, strlen(msg2) / 2);
int msg_len = strlen(msg2);
DrawCenteredMbString(
d, w, fontset, gc,
msg2,
msg_len, 50, y + i * 20, 100, 50);
XFlush(d);
sleep(1);
}
/*while(1) {
XNextEvent(d, &e);
if (e.type == KeyPress)
break;
}*/
XCloseDisplay(d);
return 0;
}