|
楼主 |
发表于 2009-9-24 13:44:12
|
显示全部楼层
知道了:
X系统对字库文件, 采用了搜索和注册机制.
X启动时对知道目录进行搜索, 根据fonts.dir, fonts.scale 将字库注册到系统中, 包括字库支持的编码体系, 大小, 名称等等, 并分配一个长长的字体名. 长长的字体名可以通过fonts.alias变成短短的字体名...
示例1: 用X绘制GB2312编码的混合文字. (系统中必须有zh_CN.GB2312的locale, 参见LFS中GLIBC的编译安装)
- // Written by Ch. Tronche ([url]http://tronche.lri.fr:8000/[/url])
- // Copyright by the author. This is unmaintained, no-warranty free software.
- // Please use freely. It is appreciated (but by no means mandatory) to
- // acknowledge the author's contribution. Thank you.
- // Started on Thu Jun 26 23:29:03 1997
- //
- // Xlib tutorial: 2nd program
- // Make a window appear on the screen and draw a line inside.
- // If you don't understand this program, go to
- // [url]http://tronche.lri.fr:8000/gui/x/xlib-tutorial/2nd-program-anatomy.html[/url]
- //
- // Modified by Zhang Lihui <swordhui@263.net>, for GB2312 text drawing.
- #include <X11/Xlib.h> // Every Xlib program must include this
- #include <assert.h> // I include this to test return values the lazy way
- #include <unistd.h> // So we got the profile for 10 seconds
- #include <string.h>
- #include <stdio.h>
- #include <locale.h>
- #define NIL (0) // A name for the void pointer
- const char* g_csMsg="abc测试1245";
- //font set, cover ASCII and GB2312.
- const char* g_csFontName="-misc-fixed-*-*-*-*-15-*-*-*-*-*-*-*,\
- -*-fangsong ti-*-*-*-*-16-*-*-*-*-*-gb2312.1980-*";
- main(int argc, char**argv)
- {
- XFontStruct *pFontStruct;
- XFontSet fontSet;
- char **plistMissingChars=NULL;
- int n_listMissingChars=0;
- char *defreturn=NULL;
- if ( setlocale (LC_ALL,"zh_CN.GB2312") == NULL ) {
- (void) fprintf (stderr, "%s: cannot set locale.n",
- argv[0] );
- }
- // Open the display
- Display *dpy = XOpenDisplay(NIL);
- assert(dpy);
- // Get some colors
- int blackColor = BlackPixel(dpy, DefaultScreen(dpy));
- int whiteColor = WhitePixel(dpy, DefaultScreen(dpy));
- // Create the window
- Window w = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 0, 0,
- 200, 100, 0, blackColor, whiteColor);
- // We want to get MapNotify events
- XSelectInput(dpy, w, StructureNotifyMask);
- // "Map" the window (that is, make it appear on the screen)
- XMapWindow(dpy, w);
- //Create Font Set.
- fontSet=XCreateFontSet(dpy, g_csFontName, &plistMissingChars,
- &n_listMissingChars,
- &defreturn);
- if(fontSet==NULL)
- {
- printf("[Error] Create font set failed.\n");
- }
- else
- {
- if(n_listMissingChars!=0)
- printf("[Error] missing chars=%d\n", n_listMissingChars);
- else
- printf("Font set create OK\n");
- }
- // Create a "Graphics Context"
- GC gc = XCreateGC(dpy, w, 0, NIL);
- // Tell the GC we draw using the white color
- XSetForeground(dpy, gc, blackColor);
- // Wait for the MapNotify event
- for(;;) {
- XEvent e;
- XNextEvent(dpy, &e);
- if (e.type == MapNotify)
- break;
- }
- // Draw the line
- XDrawLine(dpy, w, gc, 10, 60, 180, 20);
- // Draw GB2312 message.
- XmbDrawString(dpy, w, fontSet, gc, 10, 20, g_csMsg,
- strlen(g_csMsg));
- // Send the "DrawLine" request to the server
- XFlush(dpy);
- // Wait for 10 seconds
- sleep(10);
- }
复制代码
Makefile如下:
- all: prog2
- @echo "Done."
- prog2: prog-2.cc
- g++ -pipe -o $@ $^ -lX11
- clean:
- rm prog2
复制代码
测试对Unicode的支持.
先定义一个Unicode串.
- //Unicode, "一丁12fgh"
- const wchar_t g_csUniMsg[]={0x4e00, 0x4e01, 0x0031, 0x0032, 0x0066, 0x0067, 0x0068};
复制代码
将上面画串的函数后加入:
- // Draw Unicode message.
- XwcDrawString(dpy, w, fontSet, gc, 10, 40, g_csUniMsg,
- sizeof(g_csUniMsg)/sizeof(wchar_t));
复制代码
屏幕上会出现"一丁12fgh".
使用GB18030更好些, 因为GB18030包含所有的Unicode汉字.
我们再测试一下utf8字串的绘制.
首先定义一个utf8字串.
- //utf-8 message. "01一丁fg"
- const char g_csUtf8Msg[]={0x30, 0x31, 0xe4, 0xb8, 0x80, 0xe4, 0xb8, 0x81, 0x66, 0x67};
复制代码
然后改变程序的Locale, 为"zh_CN.utf8", 这个名字很奇怪.. 直接叫utf8好了, 干嘛还加zh_CN? 和中文完全无关嘛
- if ( setlocale (LC_ALL,"zh_CN.utf8") == NULL ) {
- (void) fprintf (stderr, "%s: cannot set locale.n",
- argv[0] );
- }
复制代码
然后绘制字符串
- // Draw utf8 message.
- XmbDrawString(dpy, w, fontSet, gc, 10, 20, g_csUtf8Msg,
- sizeof(g_csUtf8Msg));
复制代码
屏幕上出现"12一丁fg".
如果想创建一个包含所有编码区间的超级字符集, 可以考虑安装unifont, 然后用下面语句:
- fontset = XCreateFontSet (dpy, "-*-*-*-*-*-*-16-*-*-*-*-*-*-*",
- &missing_charsets, &num_missing_charsets,
- &default_string);
复制代码
大多数程序使用GTK+Pango, 或QT4, 有空继续. |
|