📄 binfilefactory_font.cpp
字号:
// BinFileFactory_Font.cpp: implementation of the CBinFileFactory class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "ResourceEditor.h"
#include "BinFileFactory.h"
#include "MMIRes.h"
#include "DirFileInfo.h"
#include "Converter.h"
#include "mmi_font_def.h"
void CBinFileFactory::CalcFontInfo()
{
_ASSERTE( m_dwFontOffset > 0 );
CMMIRes &mmires = g_theApp.m_MMIRes;
MMI_RESOURCE_T &res = mmires.m_Resource;
MMI_RES_DATA_T &FontData = res.mmi_data_table[MMI_RT_FONT];
FONT_MAP &fontMap = mmires.m_mapFont;
DWORD dwBase = res.ToolUsedInfo.nStartAddress;
BOOL bIsOffset = res.ToolUsedInfo.bAddrIsOffset;
FontData.pData = (void *)CalcAddr(dwBase, m_dwFontOffset, bIsOffset);
// 保持4字节对齐
_ASSERTE((uint32)FontData.pData % sizeof(uint32) == 0);
FontData.uCount = fontMap.GetCount();
FontData.uSize = 0;
PGUI_FONT_INFO_T pFontInfo = NULL;
CString strID;
POSITION pos = fontMap.GetStartPosition();
while( pos != NULL )
{
fontMap.GetNextAssoc( pos, strID, pFontInfo );
FontData.uSize += Addr4ByteAlign(pFontInfo->ngb_db_size) +
Addr4ByteAlign(pFontInfo->nascii_db_size) +
sizeof(GUI_FONT_INFO_T);
}
}
//x
void CBinFileFactory::CalcFontInfoEx()
{
_ASSERTE( m_dwFontOffset > 0 );
CMMIRes &mmires = g_theApp.m_MMIRes;
MMI_RESOURCE_T &res = mmires.m_Resource;
MMI_RES_DATA_T &FontData = res.mmi_data_table[MMI_RT_FONT];
FONTEX_MAP &fontExMap= mmires.m_mapFontEx;
DWORD dwBase = res.ToolUsedInfo.nStartAddress;
BOOL bIsOffset = res.ToolUsedInfo.bAddrIsOffset;
FontData.pData = (void *)CalcAddr(dwBase, m_dwFontOffset, bIsOffset);
// 保持4字节对齐
_ASSERTE((uint32)FontData.pData % sizeof(uint32) == 0);
FontData.uCount = fontExMap.GetCount();
FontData.uSize = 0;
PGUI_FONT_INFO_EX_T pFontInfo = NULL;
CString strID;
POSITION pos = fontExMap.GetStartPosition();
while( pos != NULL )
{
fontExMap.GetNextAssoc( pos, strID, pFontInfo );
FontData.uSize +=( sizeof(GUI_FONT_INFO_EX_T)+sizeof(GUI_FONT_DB_T)*g_theApp.m_nUsedLangNum);
PGUI_FONT_DB_T pDBT = (PGUI_FONT_DB_T)pFontInfo->pDbtt;
for(int i = 0; i < g_theApp.m_nUsedLangNum; i++)
{
FontData.uSize += Addr4ByteAlign(pDBT->db_size);
pDBT++;
}
}
}
//xe
BOOL CBinFileFactory::WriteFontInfo( FILE * pFile )
{
_ASSERTE( pFile != NULL );
CMMIRes &mmires = g_theApp.m_MMIRes;
MMI_RESOURCE_T &res = mmires.m_Resource;
MMI_RES_DATA_T &FontData = res.mmi_data_table[MMI_RT_FONT];
FONT_MAP &fontMap = mmires.m_mapFont;
CDirFileInfo * pInfo = g_theApp.m_pFontInfo;
int nCount = FontData.uCount;
if( nCount == 0 )
return TRUE;
//if( g_theApp.m_MMIRes.m_iVersion == 1 )//xe*/
//{
// _ASSERTE(( (uint32)ftell(pFile) - g_theApp.m_MMIRes.m_iReset == ((uint32)FontData.pData+g_theApp.prj_arm_addr ))||
// ( (uint32)ftell(pFile) - g_theApp.m_MMIRes.m_iReset == ((uint32)FontData.pData+g_theApp.prj_win_addr )));
// }
// else
// {
_ASSERTE(( (uint32)ftell(pFile) == ((uint32)FontData.pData+g_theApp.prj_arm_addr ))||
( (uint32)ftell(pFile) == ((uint32)FontData.pData+g_theApp.prj_win_addr )));
// }
///////////////////////////////////////////////////////////////////////////////
#define WRITE_FILE(des, st, num, file) \
if( fwrite(des, st, num, file) != num ) \
{ \
_ASSERTE(0); \
_tcscpy(m_szErrMsg, _T("Write file error!")); \
return FALSE; \
}
///////////////////////////////////////////////////////////////////////////////
CStringArray arrID;
pInfo->CalcAllLeafID(arrID);
_ASSERTE( nCount == arrID.GetSize() );
_ASSERTE( nCount == fontMap.GetCount() );
int i = 0;
PGUI_FONT_INFO_T pFontInfo = NULL;
// 计算所有ascii字库的长度
uint32 nAsciiTotal = 0;
for( i = 0; i < nCount; ++i )
{
VERIFY( fontMap.Lookup(arrID[i], pFontInfo) );
nAsciiTotal += pFontInfo->nascii_db_size;
nAsciiTotal = Addr4ByteAlign(nAsciiTotal);
}
const BOOL bBigEdn = res.ToolUsedInfo.bBigEndian;
CConverter conv;
uint8 * pAscii = NULL;
uint8 * pGb = NULL;
uint32 nAsciiAddr = (uint32)FontData.pData + nCount * sizeof(GUI_FONT_INFO_T);
uint32 nGbAddr = nAsciiAddr + nAsciiTotal;
nGbAddr = Addr4ByteAlign(nGbAddr);
for( i = 0; i < nCount; ++i )
{
VERIFY( fontMap.Lookup(arrID[i], pFontInfo) );
pAscii = pFontInfo->pascii_ptr;
pGb = pFontInfo->gb_database_ptr;
pFontInfo->pascii_ptr = (uint8 *)nAsciiAddr;
if( pAscii == pGb )
pFontInfo->gb_database_ptr = (uint8 *)nAsciiAddr;
else
pFontInfo->gb_database_ptr = (uint8 *)nGbAddr;
if( bBigEdn )
{
pFontInfo->pascii_ptr = (uint8 *)conv.ConvEdn_INT((DWORD)pFontInfo->pascii_ptr);
pFontInfo->nascii_db_size = conv.ConvEdn_INT(pFontInfo->nascii_db_size);
pFontInfo->gb_database_ptr = (uint8 *)conv.ConvEdn_INT((DWORD)pFontInfo->gb_database_ptr);
pFontInfo->ngb_db_size = conv.ConvEdn_INT(pFontInfo->ngb_db_size);
}
if( fwrite(pFontInfo, sizeof(GUI_FONT_INFO_T), 1, pFile) != 1 )
{
_ASSERTE(0);
pFontInfo->pascii_ptr = pAscii;
pFontInfo->gb_database_ptr = pGb;
_tcscpy(m_szErrMsg, _T("Write file error!"));
return FALSE;
}
pFontInfo->pascii_ptr = pAscii;
pFontInfo->gb_database_ptr = pGb;
if( bBigEdn )
{
pFontInfo->nascii_db_size = conv.ConvEdn_INT(pFontInfo->nascii_db_size);
pFontInfo->ngb_db_size = conv.ConvEdn_INT(pFontInfo->ngb_db_size);
}
nAsciiAddr += pFontInfo->nascii_db_size;
nAsciiAddr = Addr4ByteAlign(nAsciiAddr);
if(pGb != pAscii)
{
nGbAddr += pFontInfo->ngb_db_size;
nGbAddr = Addr4ByteAlign(nGbAddr);
}
}
int nAlign = 0;
BYTE arrAlign[] = { 0xdd, 0xdd, 0xdd, 0xdd };
// 写ascii字库
for( i = 0; i < nCount; ++i )
{
VERIFY( fontMap.Lookup(arrID[i], pFontInfo) );
WRITE_FILE(pFontInfo->pascii_ptr, pFontInfo->nascii_db_size, 1, pFile);
nAlign = Addr4ByteAlign(pFontInfo->nascii_db_size);
fwrite(arrAlign, nAlign - pFontInfo->nascii_db_size, 1, pFile);
}
for( i = 0; i < nCount; ++i )
{
VERIFY( fontMap.Lookup(arrID[i], pFontInfo) );
if( pFontInfo->gb_database_ptr != pFontInfo->pascii_ptr )
{
WRITE_FILE(pFontInfo->gb_database_ptr, pFontInfo->ngb_db_size, 1, pFile);
nAlign = Addr4ByteAlign(pFontInfo->ngb_db_size);
fwrite(arrAlign, nAlign - pFontInfo->ngb_db_size, 1, pFile);
}
}
return TRUE;
}
//x
BOOL CBinFileFactory::WriteFontInfoEx( FILE * pFile )
{
_ASSERTE( pFile != NULL );
CMMIRes &mmires = g_theApp.m_MMIRes;
MMI_RESOURCE_T &res = mmires.m_Resource;
MMI_RES_DATA_T &FontData = res.mmi_data_table[MMI_RT_FONT];
FONTEX_MAP &fontExMap = mmires.m_mapFontEx;
CDirFileInfo * pInfo = g_theApp.m_pFontInfo;
int nLangNum = g_theApp.m_nUsedLangNum;
int nCount = FontData.uCount;
if( nCount == 0 )
return TRUE;
_ASSERTE(( (uint32)ftell(pFile) == ((uint32)FontData.pData+g_theApp.prj_arm_addr ))||
( (uint32)ftell(pFile) == ((uint32)FontData.pData+g_theApp.prj_win_addr )));
///////////////////////////////////////////////////////////////////////////////
#define WRITE_FILE(des, st, num, file) \
if( fwrite(des, st, num, file) != num ) \
{ \
_ASSERTE(0); \
_tcscpy(m_szErrMsg, _T("Write file error!")); \
return FALSE; \
}
///////////////////////////////////////////////////////////////////////////////
CStringArray arrID;
pInfo->CalcAllLeafID(arrID);
_ASSERTE( nCount == arrID.GetSize() );
_ASSERTE( nCount == fontExMap.GetCount() );
int i = 0;
int j = 0;
PGUI_FONT_INFO_EX_T pFIT = (PGUI_FONT_INFO_EX_T)new GUI_FONT_INFO_EX_T[nCount];
if(pFIT == NULL)
return FALSE;
memset(pFIT,0,sizeof(GUI_FONT_INFO_EX_T)*nCount);
PGUI_FONT_INFO_EX_T pFontInfo = NULL;
VERIFY( fontExMap.Lookup(arrID[0], pFontInfo) );
pFIT[0].pDbtt = (uint8 *)FontData.pData + nCount * sizeof(GUI_FONT_INFO_EX_T);
//pFIT[0].pDbtt = (uint8 *)Addr4ByteAlign(pFIT[0].pDbtt);
pFIT[0].type = pFontInfo->type;
for(i=1;i<nCount;i++)
{
VERIFY( fontExMap.Lookup(arrID[i], pFontInfo) );
pFIT[i].pDbtt = pFIT[i-1].pDbtt + nLangNum * sizeof(GUI_FONT_DB_T);
//pFIT[i].pDbtt = Addr4ByteAlign(pFIT[i].pDbtt);
pFIT[i].type = pFontInfo->type;
}
const BOOL bBigEdn = res.ToolUsedInfo.bBigEndian;
CConverter conv;
if( bBigEdn )
{
for(i=0;i<nCount;i++)
{
pFIT[i].pDbtt = (uint8 *)conv.ConvEdn_INT((DWORD)pFIT[i].pDbtt);
pFIT[i].type = conv.ConvEdn_INT((DWORD)pFIT[i].type);
}
}
WRITE_FILE(pFIT, sizeof(GUI_FONT_INFO_EX_T)*nCount, 1, pFile);
if( bBigEdn )
{
for(i=0;i<nCount;i++)
{
pFIT[i].pDbtt = (uint8 *)conv.ConvEdn_INT((DWORD)pFIT[i].pDbtt);
pFIT[i].type = conv.ConvEdn_INT((DWORD)pFIT[i].type);
}
}
PGUI_FONT_DB_T pDbt = NULL,pDbt0 = NULL;
int *pSize = new int[nLangNum];
if(pSize == NULL)
return FALSE;
memset(pSize,0,sizeof(int)*nLangNum);
for( i =0;i<nLangNum;i++)
{
for( j = 0; j < nCount; j++)
{
VERIFY( fontExMap.Lookup(arrID[j], pFontInfo) );
pDbt = (PGUI_FONT_DB_T)(pFontInfo->pDbtt);
if(i == 0 ||(i!=0 && pDbt[i].pdb != pDbt[0].pdb))
pSize[i]+=Addr4ByteAlign(pDbt[i].db_size);
}
}
uint32 * nAddr = new uint32[nLangNum];
nAddr[0] =(uint32)pFIT[nCount-1].pDbtt + sizeof(GUI_FONT_DB_T) * nLangNum;
nAddr[0] = Addr4ByteAlign(nAddr[0]);
for(i =1;i<nLangNum;i++)
{
nAddr[i] = nAddr[i-1] + pSize[i-1];
nAddr[i] = Addr4ByteAlign(nAddr[i]);
}
uint8 * pdb = NULL;
uint32 * pdb0 = (uint32 *)new uint32[nCount];
uint32 nAddrEx = 0;
int nSize = 0;
for( i = 0; i < nCount; ++i )
{
VERIFY( fontExMap.Lookup(arrID[i], pFontInfo) );
pDbt = (PGUI_FONT_DB_T)(pFontInfo->pDbtt);
for(j=0; j < nLangNum; j++)
{
pdb = pDbt[j].pdb;
nSize = pDbt[j].db_size;
nAddrEx = nAddr[j];
if( bBigEdn )
{
nAddrEx = conv.ConvEdn_INT(nAddr[j]);
pDbt[j].db_size = conv.ConvEdn_INT(nSize);
}
if(j!=0 && pDbt[j].pdb == pDbt[0].pdb)
pDbt[j].pdb = (uint8*)pdb0[i];
else
{
pDbt[j].pdb = (uint8*)nAddrEx;
}
WRITE_FILE(pDbt+j, sizeof(GUI_FONT_DB_T), 1, pFile);
if(j==0)
{
pdb0[i] = (uint32)(pDbt->pdb);
}
pDbt[j].pdb = pdb;
pDbt[j].db_size = nSize;
if(j== 0 || (j!=0 && pDbt[j].pdb != pDbt[0].pdb))
{
nAddr[j] += nSize;
nAddr[j] = Addr4ByteAlign(nAddr[j]);
}
}
}
int nAlign = 0;
BYTE arrAlign[] = { 0xdd, 0xdd, 0xdd, 0xdd };
// 写字库
for( j = 0; j < nLangNum; j++)
{
for( i = 0; i < nCount; ++i )
{
VERIFY( fontExMap.Lookup(arrID[i], pFontInfo) );
pDbt = (PGUI_FONT_DB_T)(pFontInfo->pDbtt);
if(j==0 || (j != 0 && pDbt[j].pdb != pDbt[0].pdb))
{
WRITE_FILE(pDbt[j].pdb, pDbt[j].db_size, 1, pFile);
nAlign = Addr4ByteAlign(pDbt[j].db_size);
fwrite(arrAlign, nAlign - pDbt[j].db_size, 1, pFile);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -