打开文件 ```c int OpenTextFile(char *pcFileName) { struct stat tStat;
g_iFdTextFile = open(pcFileName, O_RDONLY); / open / if (0 > g_iFdTextFile) {
DBG_PRINTF("can't open text file %s\n", pcFileName);
return -1;
}
if(fstat(g_iFdTextFile, &tStat)) / fstat函数获取文件信息(大小等属性) / {
DBG_PRINTF("can't get fstat\n"); return -1;
}
/ 内存映射 / g_pucTextFileMem = (unsigned char )mmap(NULL , tStat.st_size, PROT_READ, MAP_SHARED, g_iFdTextFile, 0); if (g_pucTextFileMem == (unsigned char )-1) {
DBG_PRINTF("can't mmap for text file\n"); return -1;
}
/ 文件末尾位置 / g_pucTextFileMemEnd = g_pucTextFileMem + tStat.st_size;
/ 选择文件对应的编码方式 / g_ptEncodingOprForFile = SelectEncodingOprForFile(g_pucTextFileMem);
if (g_ptEncodingOprForFile) {
/* 如果找到对应支持的编码方式,就将frame buffer的起始位置和文件内容实际的其实位置对应上 */ g_pucLcdFirstPosAtFile = g_pucTextFileMem + g_ptEncodingOprForFile->iHeadLen; return 0;
} else {
/* 没有支持的编码,直接退出 */ return -1;
}
}
代码跟踪:
```c
OpenTextFile() /* 打开文件操作, draw.c */
open(pcFileName, O_RDONLY) /* 只读方式打开 */
fstat(g_iFdTextFile, &tStat) /* 获取文件状态 */
mmap(NULL , tStat.st_size, PROT_READ, MAP_SHARED, g_iFdTextFile, 0) /* 内存映射 */
SelectEncodingOprForFile() /* 选择文件编码方式, encoding_manager.c */
isSupport() /* 不断判断该编码是否支持这个文件 */
设置文件属性 ```c int SetTextDetail(char pcHZKFile, char pcFileFreetype, unsigned int dwFontSize) { int iError = 0; PT_FontOpr ptFontOpr; PT_FontOpr ptTmp; int iRet = -1;
g_dwFontSize = dwFontSize;
ptFontOpr = g_ptEncodingOprForFile->ptFontOprSupportedHead;
while (ptFontOpr)
{
if (strcmp(ptFontOpr->name, "ascii") == 0)
{
iError = ptFontOpr->FontInit(NULL, dwFontSize);
}
else if (strcmp(ptFontOpr->name, "gbk") == 0)
{
iError = ptFontOpr->FontInit(pcHZKFile, dwFontSize);
}
else
{
iError = ptFontOpr->FontInit(pcFileFreetype, dwFontSize);
}
DBG_PRINTF("%s, %d\n", ptFontOpr->name, iError);
ptTmp = ptFontOpr->ptNext;
if (iError == 0)
{
/* 比如对于ascii编码的文件, 可能用ascii字体也可能用gbk字体,
* 所以只要有一个FontInit成功, SetTextDetail最终就返回成功
*/
iRet = 0;
}
else
{
DelFontOprFrmEncoding(g_ptEncodingOprForFile, ptFontOpr);
}
ptFontOpr = ptTmp;
}
return iRet;
}
代码跟踪:
```c
SetTextDetail()
ptFontOpr = g_ptEncodingOprForFile->ptFontOprSupportedHead; /* draw.c */
AddFontOprForEncoding() /* encodingmanager.c 这一步在编码初始化时进行 */
ptFontOpr->FontInit() /* draw.c 进行对应的初始化 */
DelFontOprFrmEncoding() /* 如果不支持,则从编码中删除字体 */