C语言实现汉字的输出以及字库的应用
我用的是HZK16字库,可以自行网上下载
代码如下:
#include <stdio.h> #include<windows.h>#define FONT_SIZE (16) /* 字体大小 */ /* 获取汉字在汉字库中的索引位置 根据GB2312/GBK编码规则,汉字的低字节hz[0]是区码,高字节hz[1]是位码,汉字库从区位码0xa1a1开始存放汉字 */ #define HZ_INDEX(hz) ((hz[0] - 0xa1) * 94 + (hz[1] - 0xa1)) #define DOTS_BYTES (FONT_SIZE * FONT_SIZE / 8) /* 汉字点阵数据所占的字节数 */ void DisplayChinese(unsigned char dots[DOTS_BYTES],const char *tabl);int main(int argc, char* argv[]) {FILE* hzk; unsigned char dots[DOTS_BYTES]; //unsigned char b; //int i, j, k,time; unsigned char incode[3][3] = {"王","老","五"};unsigned char qh[3],wh[3];unsigned long offset[3];qh[0] = incode[0][0] - 0Xa0;//获取区码wh[0] = incode[0][1] - 0Xa0;//获取位码offset[0] = (94*(qh[0]-1)+(wh[0]-1))*32;//得到偏移位置qh[1] = incode[1][0] - 0Xa0;//获取区码wh[1] = incode[1][1] - 0Xa0;//获取位码offset[1] = (94*(qh[1]-1)+(wh[1]-1))*32;//得到偏移位置qh[2] = incode[2][0] - 0Xa0;//获取区码wh[2] = incode[2][1] - 0Xa0;//获取位码offset[2] = (94*(qh[2]-1)+(wh[2]-1))*32;//得到偏移位置/* 打开汉字库hzk16,并从中提取“宋”字的点阵数据 */ if((hzk = fopen("hzk16", "rb")) == NULL) {return -1; }fseek(hzk, offset[0], SEEK_SET); fread(dots, sizeof(unsigned char), DOTS_BYTES, hzk);DisplayChinese(dots,NULL);memset(dots,0,sizeof(dots));fseek(hzk, offset[1], SEEK_SET); fread(dots, sizeof(unsigned char), DOTS_BYTES, hzk);DisplayChinese(dots,"\t\t\t\t\t\t\t\t\t\t\t\t\t\t");memset(dots,0,sizeof(dots));fseek(hzk, offset[2], SEEK_SET); fread(dots, sizeof(unsigned char), DOTS_BYTES, hzk);DisplayChinese(dots,"\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t");memset(dots,0,sizeof(dots));fclose(hzk); return 0; } void DisplayChinese(unsigned char dots[DOTS_BYTES],const char *tabl){unsigned char b; int i, j, k; for(i = 0; i < FONT_SIZE; i++) {/* 每行共FONT_SIZE / 8个字节 */ for(j = 0; j < FONT_SIZE / 8; j++) {b = dots[i * 2 + j]; /* 从左至右的点分别对应字节从高到低的位值 */ for(k = 0; k < 8; k++) {if(b & 0x80) printf("%s%c ", tabl,'*'); else printf(" "); b <<= 1; } } printf("\n"); } }