最近一直在做PXA270开发板做实验。。现将显示汉字的程序,整理一下。
首先有汉字库这个东西,这个东西类似一个二维数组,有94行,94列。行号叫区号,列号叫位号。这样可以有94x94个汉字。每一个汉字可以由区号+位号来得到。因为94化成二进制数要7位,所以要表示一个汉字就得14位。这样的编码就叫做区位码。所以区位码是14位。然而大家都知道一个汉字是由两个字节表示的。是16位。多了两位。由16位表示编码方式叫内码。内码与区位码的换算方式如下:
区号=内码第一个字节-A1H
位号=内码第二个字节-A1H
那么汉字的点阵的起始位置=(区号x94+位号)x点阵的字节数
有了上面的知识,来看开发板显示汉字的实例
1:下载汉字库hzk16
2:安装ascii.lib库
3:源码如下
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "ascii.lib"
#define SYS_BLACK 0x00000000
#define SYS_WHITE 0xffffffff
file *hzkfile=null;
unsigned char * chsTxt = "青梅不爱竹马";
short x,y;
short i;
unsigned char bufferTxt[2];
typedef unsigned long ColorType;
ColorType color = SYS_WHITE;
void DrawCharCHS(int x, int y, unsigned char c[2], ColorType color);
void fb_Text_16x16(int x, int y, unsigned char * dotCodes, ColorType color);
void fb_PutPixel(short x, short y, ColorType color/*, int xorm */);
void main()
{
hzkFile = fopen("./hz16", "rb");
x = 10, y = 40;
for (i = 0; i < strlen(chsTxt); i += 2)
{
bufferTxt[0] = chsTxt[i];
bufferTxt[1] = chsTxt[i + 1];
DrawCharCHS(x, y, bufferTxt, color);
x += 16;
}
fclose(hzkFile);
}
void DrawCharCHS(int x, int y, unsigned char c[2], ColorType color)
{
unsigned char codes[32];
short i;
unsigned char ch, cl;
unsigned long offset;
if (hzkFile == NULL)
{
printf("No Chinese Character Library opened.\n");
exit(1);
}
ch = c[0];
cl = c[1];
offset = ((ch - 0xa1) * 94L + (cl - 0xa1)) * 32L;
fseek(hzkFile, offset, SEEK_SET);//通过区位号找到汉字
fread(codes, 32, 1, hzkFile);
fb_Text_16x16(x, y, codes, color);
}
void fb_Text_16x16(int x, int y, unsigned char * dotCodes, ColorType color)
{
int i, j, k;
if (x < 0 || x >= SCREEN_WIDTH ||
y < 0 || y >= SCREEN_HEIGHT)
{
#ifdef ERR_DEBUG
printf("DEBUG_INFO: Pixel out of screen range.\n");
printf("DEBUG_INFO: x = %d, y = %d\n", x, y);
#endif
return;
}
if (dotCodes == NULL)
{
#ifdef ERR_DEBUG
printf("DEBUG_INFO: Dot codes NULL.\n");
#endif
return;
}
for(i = 0; i < 16; i++)
{
for (j = 0; j < 2; j++)
{
x += 8 * ( j + 1);
for (k = 0; k < 8; k++)
{
x--;
if ((dotCodes[2 * i + j] >> k) & 0x1)
{
fb_PutPixel(x, y, color);
}
}
}
x -= 8;
++y;
}
return;
}
void fb_PutPixel(short x, short y, ColorType color/*, int xorm */)
{
void * currPoint;
if (x < 0 || x >= SCREEN_WIDTH ||
y < 0 || y >= SCREEN_HEIGHT)
{
#ifdef ERR_DEBUG
printf("DEBUG_INFO: Pixel out of screen range.\n");
printf("DEBUG_INFO: x = %d, y = %d\n", x, y);
#endif
return;
}
// Calculate address of specified point
currPoint = (ByteType *)frame_base + y * LINE_SIZE + x * PIXEL_SIZE;
#ifdef DEBUG
printf("DEBUG_INFO: x = %d, y= %d, currPoint = 0x%x\n", x, y, currPoint);
#endif
switch (BPP)
{
case 8:
*((ByteType *)currPoint) = (color & 0xff);
break;
case 16:
*((WordType *)currPoint) = (color & 0xffff);
break;
}
return;
}
阅读(2233) | 评论(0) | 转发(0) |