码迷,mamicode.com
首页 > 其他好文 > 详细

C Tips:显示点阵汉字的小样例

时间:2014-07-26 00:46:46      阅读:327      评论:0      收藏:0      [点我收藏+]

标签:blog   http   使用   os   io   2014   for   art   

非常简陋的一段小程序,演示怎样显示点阵字库。有时间的时候再详解。

#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.\n");
		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("\n");

	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("\n");
	}

	printf("\n");
}

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.\n");
		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.\n");
			fclose(pHzkInfo->file);
			pHzkInfo->file = NULL;
			pHzkInfo->martixBytesCount = 0;
			pHzkInfo->pMatrix = NULL;

			return;
		}

		printf("Done.\n");
	}
}

演示效果:


使用16*16 的字库:

bubuko.com,布布扣


使用32*32 的字库:

bubuko.com,布布扣

C Tips:显示点阵汉字的小样例,布布扣,bubuko.com

C Tips:显示点阵汉字的小样例

标签:blog   http   使用   os   io   2014   for   art   

原文地址:http://www.cnblogs.com/mengfanrong/p/3868647.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!