字体层由fonts_manager.c来管理,
下面有3个文件:ascii.c , gbk.c ,freetype .c
ascii字体opriation
static T_FontOpr g_tASCIIFontOpr = {
.name = "ascii",
.FontInit = ASCIIFontInit,
.GetFontBitmap = ASCIIGetFontBitmap,
};
ascii字体初始化函数
static int ASCIIFontInit(char *pcFontFile, unsigned int dwFontSize)
{
if (dwFontSize != 16)
{
//DBG_PRINTF("ASCII can't support %d font size\n", dwFontSize);
return -1;
}
return 0;
}
ascii获得字体的位图
static int ASCIIGetFontBitmap(unsigned int dwCode, PT_FontBitMap ptFontBitMap)
{
int iPenX = ptFontBitMap->iCurOriginX;
int iPenY = ptFontBitMap->iCurOriginY;
if (dwCode > (unsigned int)0x80)
{
//DBG_PRINTF("don't support this code : 0x%x\n", dwCode);
return -1;
}
ptFontBitMap->iXLeft = iPenX;
ptFontBitMap->iYTop = iPenY - 16;
ptFontBitMap->iXMax = iPenX + 8;
ptFontBitMap->iYMax = iPenY;
ptFontBitMap->iBpp = 1;
ptFontBitMap->iPitch = 1;
ptFontBitMap->pucBuffer = (unsigned char *)&fontdata_8x16[dwCode * 16];;
ptFontBitMap->iNextOriginX = iPenX + 8;
ptFontBitMap->iNextOriginY = iPenY;
return 0;
}
向上层注册结构体
int ASCIIInit(void)
{
return RegisterFontOpr(&g_tASCIIFontOpr);
}
gbk.c文件
定义一个gbk字体结构体
static T_FontOpr g_tGBKFontOpr = {
.name = "gbk",
.FontInit = GBKFontInit,
.GetFontBitmap = GBKGetFontBitmap,
};
static int GBKFontInit(char *pcFontFile, unsigned int dwFontSize)
{
struct stat tStat;
if (16 != dwFontSize)
{
DBG_PRINTF("GBK can't support %d fontsize\n", dwFontSize);
return -1;
}
g_iFdHZK = open(pcFontFile, O_RDONLY);
if (g_iFdHZK < 0)
{
DBG_PRINTF("can't open %s\n", pcFontFile);
return -1;
}
if(fstat(g_iFdHZK, &tStat))
{
DBG_PRINTF("can't get fstat\n");
return -1;
}
g_pucHZKMem = (unsigned char *)mmap(NULL , tStat.st_size, PROT_READ, MAP_SHARED, g_iFdHZK, 0);
if (g_pucHZKMem == (unsigned char *)-1)
{
DBG_PRINTF("can't mmap for hzk16\n");
return -1;
}
g_pucHZKMemEnd = g_pucHZKMem + tStat.st_size;
return 0;
}
获取GBK字体位图
static int GBKGetFontBitmap(unsigned int dwCode, PT_FontBitMap ptFontBitMap)
{
int iArea;
int iWhere;
int iPenX = ptFontBitMap->iCurOriginX;
int iPenY = ptFontBitMap->iCurOriginY;
DBG_PRINTF("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
if (dwCode & 0xffff0000)
{
DBG_PRINTF("don't support this code : 0x%x\n", dwCode);
return -1;
}
iArea = (int)(dwCode & 0xff) - 0xA1;
iWhere = (int)((dwCode >> 8) & 0xff) - 0xA1;
if ((iArea < 0) || (iWhere < 0))
{
DBG_PRINTF("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
return -1;
}
ptFontBitMap->iXLeft = iPenX;
ptFontBitMap->iYTop = iPenY - 16;
ptFontBitMap->iXMax = iPenX + 16;
ptFontBitMap->iYMax = iPenY;
ptFontBitMap->iBpp = 1;
ptFontBitMap->iPitch = 2; //gbk字体两行间的像素跨度为2
ptFontBitMap->pucBuffer = g_pucHZKMem + (iArea * 94 + iWhere)*32;;
if (ptFontBitMap->pucBuffer >= g_pucHZKMemEnd)
{
return -1;
}
ptFontBitMap->iNextOriginX = iPenX + 16;
ptFontBitMap->iNextOriginY = iPenY;
DBG_PRINTF("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
return 0;
}
向上层注册bgk结构体
int GBKInit(void)
{
return RegisterFontOpr(&g_tGBKFontOpr);
}
freetype.c字体文件
首先定义freetype字体结构体
static T_FontOpr g_tFreeTypeFontOpr = {
.name = "freetype",
.FontInit = FreeTypeFontInit,
.GetFontBitmap = FreeTypeGetFontBitmap,
};
freet字体初始化
static int FreeTypeFontInit(char *pcFontFile, unsigned int dwFontSize)//传入的是字体文件 还有字体大小
{
int iError;
/* 显示矢量字体 */
iError = FT_Init_FreeType(&g_tLibrary ); /* initialize library */
/* error handling omitted */
if (iError)
{
DBG_PRINTF("FT_Init_FreeType failed\n");
return -1;
}
iError = FT_New_Face(g_tLibrary, pcFontFile, 0, &g_tFace); /* create face object */
/* error handling omitted */
if (iError)
{
DBG_PRINTF("FT_Init_FreeType failed\n");
return -1;
}
g_tSlot = g_tFace->glyph;
iError = FT_Set_Pixel_Sizes(g_tFace, dwFontSize, 0);
if (iError)
{
DBG_PRINTF("FT_Set_Pixel_Sizes failed : %d\n", dwFontSize);
return -1;
}
return 0;
}
获得freetype字体位图
static int FreeTypeGetFontBitmap(unsigned int dwCode, PT_FontBitMap ptFontBitMap)
{
int iError;
int iPenX = ptFontBitMap->iCurOriginX;
int iPenY = ptFontBitMap->iCurOriginY;
/* load glyph image into the slot (erase previous one) */
//iError = FT_Load_Char(g_tFace, dwCode, FT_LOAD_RENDER );
iError = FT_Load_Char(g_tFace, dwCode, FT_LOAD_RENDER | FT_LOAD_MONOCHROME);
if (iError)
{
DBG_PRINTF("FT_Load_Char error for code : 0x%x\n", dwCode);
return -1;
}
//DBG_PRINTF("iPenX = %d, iPenY = %d, bitmap_left = %d, bitmap_top = %d, width = %d, rows = %d\n", iPenX, iPenY, g_tSlot->bitmap_left, g_tSlot->bitmap_top, g_tSlot->bitmap.width, g_tSlot->bitmap.rows);
ptFontBitMap->iXLeft = iPenX + g_tSlot->bitmap_left;
ptFontBitMap->iYTop = iPenY - g_tSlot->bitmap_top;
ptFontBitMap->iXMax = ptFontBitMap->iXLeft + g_tSlot->bitmap.width;
ptFontBitMap->iYMax = ptFontBitMap->iYTop + g_tSlot->bitmap.rows;
ptFontBitMap->iBpp = 1;
ptFontBitMap->iPitch = g_tSlot->bitmap.pitch; //矢量字体不是固定的
ptFontBitMap->pucBuffer = g_tSlot->bitmap.buffer;
ptFontBitMap->iNextOriginX = iPenX + g_tSlot->advance.x / 64;
ptFontBitMap->iNextOriginY = iPenY;
//DBG_PRINTF("iXLeft = %d, iYTop = %d, iXMax = %d, iYMax = %d, iNextOriginX = %d, iNextOriginY = %d\n", ptFontBitMap->iXLeft, ptFontBitMap->iYTop, ptFontBitMap->iXMax, ptFontBitMap->iYMax, ptFontBitMap->iNextOriginX, ptFontBitMap->iNextOriginY);
return 0;
}
最后向上注册结构体
int FreeTypeInit(void)
{
return RegisterFontOpr(&g_tFreeTypeFontOpr);
}
三个字体文件都向上层fonts_manager.c注册了结构体,并放在链表中
int RegisterFontOpr(PT_FontOpr ptFontOpr)
{
PT_FontOpr ptTmp;
if (!g_ptFontOprHead) //如果链表头为空
{
g_ptFontOprHead = ptFontOpr; //链表头等于传入的结构体
ptFontOpr->ptNext = NULL; //结构体的下一项为空
}
else
{
ptTmp = g_ptFontOprHead; //如果不为空,把链表头赋给结构体,
while (ptTmp->ptNext) //判断结构体是否有下一项
{
ptTmp = ptTmp->ptNext; //变成下一个结构体
}
ptTmp->ptNext = ptFontOpr;//直到结构体没有下一项,结构体的下一项为传入的结构体
ptFontOpr->ptNext = NULL; //把结构体的下一项为空
}
return 0;
}
在字体管理文件fonts-manager.c中,
有fontsinit函数,供上次调用,用来初始化
int FontsInit(void)
{
int iError;
iError = ASCIIInit();
if (iError)
{
DBG_PRINTF("ASCIIInit error!\n");
return -1;
}
iError = GBKInit();
if (iError)
{
DBG_PRINTF("GBKInit error!\n");
return -1;
}
iError = FreeTypeInit();
if (iError)
{
DBG_PRINTF("FreeTypeInit error!\n");
return -1;
}
return 0;
}