非常简陋的一段小程序,演示怎样显示点阵字库。有时间的时候再详解。
#include <stdio.h> #include <stdlib.h> struct HzkInfoStruct { int HzkSelect; int HzkSquare; char * fileName; FILE * file; int martixBytesCount; unsigned char *pMatrix; }; typedef struct HzkInfoStruct HzkInfo; void SelectHZK(HzkInfo * pHzkInfo); void LoadHzkInfo(HzkInfo * pHzkInfo); void CleanupHzkInfo(HzkInfo hzkInfo); void GetQuWh(unsigned char *chineaseWord, int * qu, int * wh); void FillMartix(HzkInfo * pHzkInfo, const int qu, const int wh); void PrintMartix(HzkInfo hzkInfo); int _tmain(int argc, _TCHAR* argv[]) { unsigned char chinease[3] = "京"; HzkInfo hzkInfo; int qu; int wh; SelectHZK(&hzkInfo); if(hzkInfo.file == NULL || hzkInfo.pMatrix == NULL) { printf("HZK loading failed. Will exit now. "); goto EXIT; } GetQuWh(chinease, &qu, &wh); FillMartix(&hzkInfo, qu, wh); PrintMartix(hzkInfo); EXIT: CleanupHzkInfo(hzkInfo); return 0; } void GetQuWh(unsigned char *chineaseWord, int * qu, int * wh) { *qu = chineaseWord[0] - 0xa0; *wh = chineaseWord[1] - 0xa0; } void FillMartix(HzkInfo * pHzkInfo, const int qu, const int wh) { fseek(pHzkInfo->file, (94 * (qu - 1) + (wh - 1)) * pHzkInfo->martixBytesCount, SEEK_SET); fread(pHzkInfo->pMatrix, pHzkInfo->martixBytesCount, 1, pHzkInfo->file); } void PrintMartix(HzkInfo hzkInfo) { int i, j, k; int i_steps = hzkInfo.HzkSquare / 8; unsigned char * pMatrix = hzkInfo.pMatrix; printf(" "); for (j = 0; j < hzkInfo.HzkSquare; j++) { for (i = 0; i < i_steps; i++) { for (k = 0; k < 8; k++) { if (pMatrix[i] & (0x80 >> k)) { printf("%c ", '*'); } else { printf(" "); } } } pMatrix += i_steps; printf(" "); } printf(" "); } void SelectHZK(HzkInfo * pHzkInfo) { SELECT: printf("Please select [0: Exit; 1: HZK16; 2: HZK32;] : "); scanf_s("%d", &(pHzkInfo->HzkSelect)); if(pHzkInfo->HzkSelect == 0) { exit(0); } if(pHzkInfo->HzkSelect < 1 || pHzkInfo->HzkSelect > 2) { goto SELECT; } switch(pHzkInfo->HzkSelect) { case 1: pHzkInfo->fileName = "Hzk16"; pHzkInfo->HzkSquare = 16; break; case 2: pHzkInfo->fileName = "HZK32"; pHzkInfo->HzkSquare = 32; break; } LoadHzkInfo(pHzkInfo); } void CleanupHzkInfo(HzkInfo hzkInfo) { if(hzkInfo.pMatrix != NULL) { fclose(hzkInfo.file); free(hzkInfo.pMatrix); hzkInfo.pMatrix = NULL; } } void LoadHzkInfo(HzkInfo * pHzkInfo) { printf("Loading %s ... ", pHzkInfo->fileName); if ((fopen_s(&(pHzkInfo->file), pHzkInfo->fileName, "rb")) != 0) { printf("Failed. "); pHzkInfo->file = NULL; pHzkInfo->martixBytesCount = 0; pHzkInfo->pMatrix = NULL; } else { pHzkInfo->martixBytesCount = pHzkInfo->HzkSquare * pHzkInfo->HzkSquare / 8; pHzkInfo->pMatrix = (unsigned char *)malloc(pHzkInfo->martixBytesCount); if(pHzkInfo->pMatrix == NULL) { printf("Malloc failed. "); fclose(pHzkInfo->file); pHzkInfo->file = NULL; pHzkInfo->martixBytesCount = 0; pHzkInfo->pMatrix = NULL; return; } printf("Done. "); } }
演示效果:
使用16*16 的字库:
使用32*32 的字库: