📄 font.cpp
字号:
#include<windows.h>
#include<fstream>
#include<assert.h>
#include"Font.h"
#include"GFileLoader.h"
#include"GScreen.h"
const unsigned long GFont::C_FONT_MASK[]=
{
1,1<<1,1<<2,1<<3,1<<4,1<<5,1<<6,1<<7,
1<<8,1<<9,1<<10,1<<11,1<<12,1<<13,1<<14,1<<15
};
GFont::GFont()
{
m_pFontData=NULL;
memset(&m_ffh,0,sizeof(m_ffh));
}
GFont::~GFont()
{
if(m_pFontData)
{
delete [] m_pFontData;
m_pFontData=NULL;
}
}
bool GFont::LoadFontFile(GFileLoader*pFileLoader,const string &szFileName)
{
assert(pFileLoader);
unsigned long dwFileSize;
char*pBuffer;
if(!pFileLoader->LoadFile(szFileName,&pBuffer,dwFileSize))
return false;
memcpy(&m_ffh,pBuffer,sizeof(m_ffh));
if(m_ffh.wID!=GPP_FONT_FILE_ID)
{
delete [] pBuffer;
return false;
}
if(m_pFontData)
{
delete [] m_pFontData;
}
m_pFontData=new unsigned short[m_ffh.dwFontDataSize];
if(NULL==m_pFontData)
{
delete [] pBuffer;
return false;
}
memcpy((char*)m_pFontData,pBuffer+sizeof(m_ffh),m_ffh.dwFontDataSize*sizeof(unsigned short));
delete [] pBuffer;
return true;
}
bool GFont::SaveFontDataToFile(const string &strFontFile,const GFontFileHeader & fh,unsigned char*pData)
{
ofstream ofs(strFontFile.data(),ios::out|ios::binary);
if(!ofs.is_open())
{
return false;
}
ofs.write((const char*)&fh,sizeof(fh));
ofs.write((const char*)pData,fh.dwFontDataSize*sizeof(unsigned short));
ofs.close();
return true;
}
void GFont::DrawTextFastToScreen(GScreen*pScreen,int x,int y,int nLineChar,unsigned char byColorIndex,char*szText)
{
assert(pScreen);
assert(szText);
int nTextIndex;
int i=0;
int nAsciiWidth=m_ffh.byFontWidth>>1;
int OrgX=x;
while(szText[i]!='\0')
{
nTextIndex=0;
while(szText[i]!='\0' && nTextIndex< nLineChar)
{
if(szText[i]<0) //如果是汉字
{
unsigned short*pFontUnicode=GetUnicode(szText[i],szText[i+1]);
for(register int m=0;m<GPP_FONT_MAX_HEIGHT;m++)
{
for(register int n=0;n<m_ffh.byFontHeight;n++)
{
if(*pFontUnicode & GFont::C_FONT_MASK[n])
{
pScreen->DrawPixel(x+n,y+m,byColorIndex);
}
}
pFontUnicode++;
}
x+=m_ffh.byFontWidth+1;
nTextIndex+=2;
i+=2;
}
else
{
unsigned short*pFontChar=GetAscIIChar(szText[i]);
for(register int m=0;m<GPP_FONT_MAX_HEIGHT;m++)
{
for(register int n=0;n<nAsciiWidth;n++)
{
if(*pFontChar & GFont::C_FONT_MASK[n])
{
pScreen->DrawPixel(x+n,y+m,byColorIndex);
}
}
pFontChar++;
}
x+=nAsciiWidth+1;
nTextIndex++;
i++;
}
}
x=OrgX;
y+=m_ffh.byFontHeight+2;
}
}
void GFont::DrawTextFormatToScreen(GScreen*pScreen,int x,int y,int nLineChar,unsigned char byColorIndex,char*szText,...)
{
char txt[1024];
va_list vl;
va_start(vl,szText);
vsprintf(txt,szText,vl);
va_end(vl);
DrawTextFastToScreen(pScreen,x,y,nLineChar,byColorIndex,txt);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -