//========================================================================
//TITLE:
// 获取TTF文件的字体名
//AUTHOR:
// norains
//DATE:
// Monday 22-December-2008
//Environment:
// VS2005 + ARM BSP
//========================================================================
地球人都知道,字体文件有一种叫TTF的东西;同样地球上做WINCE的程序员也都知道,wince5.0里是没有API函数获取TTF的FONT NAME的。还好CODEPROJECT上有不少牛人,为我们写好了相应的代码:http://www.codeguru.com/cpp/g-m/gdi/fonthandlinganddetection/article.php/c3659/
而我这函数的代码也是基于其思路,只是将之从PC的MFC环境移植到wince的API环境下,并修改了一些bug。
函数很简单,代码如下:
- //----------------------------------------------------------------------
- //Description:
- //Getthefontname
- //
- //Parameters:
- //strFilePath:[in]Thefontfilename.
- //
- //----------------------------------------------------------------------
- TSTRINGGetFontNameFromFile(TSTRINGstrFilePath)
- {
- std::stringstrFontName;
- HANDLEhFile=CreateFile(strFilePath.c_str(),GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
- if(hFile==INVALID_HANDLE_VALUE)
- {
- returnTSTRING();
- }
- DWORDdwRead=0;
- TT_OFFSET_TABLEttOffsetTable;
- ReadFile(hFile,&ttOffsetTable,sizeof(TT_OFFSET_TABLE),&dwRead,NULL);
- ttOffsetTable.uNumOfTables=SWAPWORD(ttOffsetTable.uNumOfTables);
- ttOffsetTable.uMajorVersion=SWAPWORD(ttOffsetTable.uMajorVersion);
- ttOffsetTable.uMinorVersion=SWAPWORD(ttOffsetTable.uMinorVersion);
- //checkisthisisatruetypefontandtheversionis1.0
- if(ttOffsetTable.uMajorVersion!=1||ttOffsetTable.uMinorVersion!=0)
- {
- returnTSTRING();
- }
- TT_TABLE_DIRECTORYtblDir;
- BOOLbFound=FALSE;
- std::vector<char>vtBuf;
- for(inti=0;i<ttOffsetTable.uNumOfTables;i++)
- {
- ReadFile(hFile,&tblDir,sizeof(TT_TABLE_DIRECTORY),&dwRead,NULL);
- vtBuf.resize(5);
- memset(&vtBuf[0],0,vtBuf.size());
- strncpy(&vtBuf[0],tblDir.szTag,4);
- if(strcmp(_strlwr(&vtBuf[0]),"name")==0)
- {
- bFound=TRUE;
- tblDir.uLength=SWAPLONG(tblDir.uLength);
- tblDir.uOffset=SWAPLONG(tblDir.uOffset);
- break;
- }
- }
- if(bFound)
- {
- SetFilePointer(hFile,tblDir.uOffset,NULL,FILE_BEGIN);
- TT_NAME_TABLE_HEADERttNTHeader;
- ReadFile(hFile,&ttNTHeader,sizeof(TT_NAME_TABLE_HEADER),&dwRead,NULL);
- ttNTHeader.uNRCount=SWAPWORD(ttNTHeader.uNRCount);
- ttNTHeader.uStorageOffset=SWAPWORD(ttNTHeader.uStorageOffset);
- TT_NAME_RECORDttRecord;
- bFound=FALSE;
- for(inti=0;i<ttNTHeader.uNRCount;i++)
- {
- ReadFile(hFile,&ttRecord,sizeof(TT_NAME_RECORD),&dwRead,NULL);
- ttRecord.uNameID=SWAPWORD(ttRecord.uNameID);
- if(ttRecord.uNameID==1)
- {
- ttRecord.uStringLength=SWAPWORD(ttRecord.uStringLength);
- ttRecord.uStringOffset=SWAPWORD(ttRecord.uStringOffset);
- //Getcurrentpointerposition
- DWORDdwPos=SetFilePointer(hFile,0,NULL,FILE_CURRENT);
- SetFilePointer(hFile,tblDir.uOffset+ttRecord.uStringOffset+ttNTHeader.uStorageOffset,NULL,FILE_BEGIN);
- vtBuf.resize(ttRecord.uStringLength+1);
- memset(&vtBuf[0],0,vtBuf.size());
- ReadFile(hFile,&vtBuf[0],ttRecord.uStringLength,&dwRead,NULL);
- if(strlen(&vtBuf[0])>0)
- {
- strFontName=&vtBuf[0];
- break;
- }
- SetFilePointer(hFile,dwPos,NULL,FILE_BEGIN);
- }
- }
- }
- CloseHandle(hFile);
- #ifdefUNICODE
- returnMByteToWChar(strFontName);
- #else
- returnstrFontName;
- #endif
- }
想获取某个TTF文件的字体名,只需要简单如此:
TSTRINGstrFont=GetFontNameFromFile(TEXT("/SONGTI.TTF"));
另外,在该函数体中用到的MByteToWChar函数,具体实现请参考我blog上的CCommon类,在此不再赘述。