1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
| /*************************************************** 字符集编码统一为gb2312,即源代码文件保存格式为gb2312(notepad++下显示为“ANSI”), 编译环境的字符集编码为gb2312,如果不是,可能得不到预期效果 多个汉字 & 中英文混合显示 编译:$ gcc font-test-ncurses.c -lncurses 运行:$ ./a.out 源代码文件编码:ANSI 测试环境编码:zh_CN.UTF-8、zh_CN.gd2312
log: 中文使用字库HZK16,英文使用font_8x16.h头文件中的数组,寻址方式为a[i]*16,与ASCII字库一样 而font_8x16-me.h头文件的数组,寻址方式为(a[i] - 0x20) * 16。 *16与<<4暂时未作比较。 *************************************************/
#include <stdio.h> #include <unistd.h> #include <sys/stat.h> #include <sys/types.h> #include <fcntl.h> #include <stdlib.h> #include <errno.h> #include <string.h> #include <ncurses.h> /* ncurses库头文件 */
//#include "font_8x16.h" #include "font_8x16-me.h"
#define font_size 32 #define ascii_size 16 //#define ascii_code fontdata_8x16 /* font_8x16.h */ #define ascii_code fontdata_8x16_me
/* for debug */ //#define DEBUG #ifdef DEBUG #define debug(fmt, ...) printw(fmt, ##__VA_ARGS__) #else #define debug(fmt, ...) #endif
void display_font(int y, int x, unsigned char *mat) { int i, j, k; for(i=0;i<16;i++) { for(j=0;j<2;j++) /* 汉字占2个字节 */ { for(k=0;k<8;k++) /* 输出一个字节*/ { /* 逐位相与,为1者,输出“*” */ if(mat[i*2+j] & (0x80>>k)) mvprintw(y+i, x+j*8+k, "*"); else mvprintw(y+i, x+j*8+k, " "); } } } refresh(); }
void display_font_ascii(int y, int x, unsigned char *ascii) { int i, j; debug("=================/n"); for(i=0;i<16;i++) { for(j=0;j<8;j++) { if(ascii[i] & (0x80>>j)) mvprintw(y+i, x+j, "*"); else mvprintw(y+i, x+j, " "); } } debug("=================/n"); refresh(); }
int main() { int i; //unsigned char incode[] = "我Az你个pf"; /* 全部是中文字符,英文为全角状态下输入 */ //unsigned char incode[] = "人生如梦"; /* 全部中文 */ //unsigned char incode[] = "I'm Late Lee"; /* 全部英文 */ unsigned char incode[] = "我A你个BCD"; /* 中文、英文 */ unsigned char *p; unsigned char *p_ascii; int qh,wh; unsigned long offset; FILE *HZK; unsigned char mat[32]={0};
int y = 5; /* 行 */ int x = 0; /* 列 */
initscr(); /* init screen */
if((HZK=fopen("HZK16","rb"))==NULL) { perror("Can't Open hzk16"); exit(0); } /* 中英混合显示 */ #if 0 /* 中英文混合显示时,这种方法似乎不好处理,暂时舍弃 */ for (i = 0; i < sizeof(incode)-1; i+=2) { qh = incode[i] - 0xa0; wh = incode[i+1] - 0xa0; if (qh > 0 && wh > 0) { debug("code : %x %x/n", incode[i], incode[i+1]); offset = ( 94*(qh-1) + (wh-1) ) * 32; // 计算偏移 fseek(HZK,offset,SEEK_SET); fread(mat,32,1,HZK); display_font(y, x, mat); x += 16; } else { int offset1, offset2; offset1 = (incode[i]-0x20) * 16; /*16*/ offset2 = (incode[i+1]-0x20) * 16; /*16*/ p_ascii = ascii_code + offset1; display_font_ascii(y, x, p_ascii); x += 8; p_ascii = ascii_code + offset2; display_font_ascii(y, x, p_ascii); x += 8; } } #endif /* 另一种方法 */ #if 01 p = incode; while (*p != 0) { qh = *p - 0xa0; wh = *(p+1) - 0xa0; if (qh > 0 && wh > 0){ debug("code : %x %x/n", *p, *(p+1)); offset = ( 94*(qh-1) + (wh-1) ) * 32; debug("qh: %x wh: %x offset: %x/n", qh, wh, offset); fseek(HZK,offset,SEEK_SET); fread(mat,32,1,HZK); display_font(y, x, mat); x += 16; p+=2; /* 中文字符,移动2个字节 */ } else { int offset1; offset1 = (*p - 0x20 ) * 16; p_ascii = ascii_code + offset1; display_font_ascii(y, x, p_ascii); x += 8; p+=1; /* 英文字符,移动1个字节 */ } } #endif
fclose(HZK); getch(); /*暂停*/ endwin(); /* close it */ return 0; }
|