1.使用xlsfonts -display :0.0,查看display支持fonts格式。
2.使用xlsfonts -ll -fn “fonts格式”,查看本机是否支持该文件格式。
例子仅供参考:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <X11/Xlib.h>
#include <X11/extensions/Xv.h>
#include <X11/extensions/Xvlib.h>
#include <X11/Xlocale.h>
int main()
{
Display* disp;
Screen* screen;
Window root;
Window win;
GC gc;
XGCValues values;
int depth, black, white;
int screen_num = 0;
int width = 1920;
int height = 1080;
XFontStruct *fs16;
XFontSet fontset;
char** missing_charsets;
int num_missing_charsets;
char* default_string;
disp = XOpenDisplay(":0.0");
screen = DefaultScreenOfDisplay(disp);
screen_num = DefaultScreen(disp);
root = DefaultRootWindow(disp);
white = XWhitePixel(disp, screen_num);
black = XBlackPixel(disp, screen_num);
depth = DefaultDepth(disp, screen_num);
#if 0
if(setlocale(LC_ALL, "") == NULL)
{
printf("cannot set locale\n");
return 0;
}
if(!XSupportsLocale())
{
printf("X dose not support locale\n");
return 0;
}
if(XSetLocaleModifiers("") == NULL)
{
printf("Warning: cannot set local");
}
#endif
fs16 = XLoadQueryFont(disp, "fixed");
if(fs16 == NULL)
{
printf("connot load font\n");
return 0;
}
win = XCreateSimpleWindow(disp, root, 0, 0, width, height, 0, black, white);
gc = XCreateGC(disp, win, 0, &values);
#if 0
fontset = XCreateFontSet(disp, "-*-*-*-*-*-*-16-*-*-*-*-*-*-*",
&missing_charsets, &num_missing_charsets, &default_string);
if(num_missing_charsets > 0)
{
for(int i = 0; i < num_missing_charsets; i++)
{}
XFreeStringList(missing_charsets);
}
#endif
XSetFont(disp, gc, fs16->fid);
//show window
XMapWindow(disp, win);
XFlush(disp);
sleep(1);
#if 0
int x_width = (width >> 1) - 12;
int x_height = (height >> 1) - 1;
// XDrawLine(disp, win, gc, 0, 0, width, height);
XmbDrawString(disp, win, fontset, gc, x_width, x_height, "我是中国人", strlen("我是中国人"));
#endif
XDrawString(disp, win, gc, 10, 10, "ensure", strlen("ensure"));
XFlush(disp);
XSync(disp, False);
// XFreeFontSet(disp, fontset);
for(int i = 0; i < 10; i++){
sleep(1);
}
XFreeGC(disp, gc);
XCloseDisplay(disp);
return 0;
}