我有旧的基于 MFC 的应用程序。最近我们从 Trébuchent 迁移到 Roboto 字体。从那时起,在某些机器上,我们在应用程序中发现了奇怪的字符。
我正在使用该CreateFontIndirect
功能来创建字体。我使用的 IDE 是Visual Studio 2008。
此问题不会影响整个应用程序。它只影响少数地方。
我在那些垃圾字符中观察到了一种模式。似乎它为每个ASCII值加了 2。例如:如果我想显示ABCD它正在显示CDEF。
请帮我找到这个。提前致谢。
这是我的代码示例:
void Render (CDC *pDC)
{
CString strText;
CFont *pFont = GetLocalFontStore()->LoadFont(_T("Roboto"), (int)(26 *fSizeFactor) , FW_EXTRABOLD, false, false);
COLORREF txtColor =RGB(255,0,0);
CRect rcTobeDrawn = CRect( (CPoint(177,172)), (CPoint(636,215)));
strText = _T(“This is my string”);
CFont *pOldfont = pDC->SelectObject(pFont);
COLORREF oldTxtColor = pDC->SetTextColor(txtColor);
int oldBkMode = pDC->SetBkMode(TRANSPARENT);
pDC->DrawText(strText, rcTobeDrawn, DT_CENTER|DT_NOCLIP|DT_VCENTER);
pDC->SelectObject(pOldfont);
pDC->SetBkMode(oldBkMode);
pDC->SetTextColor(oldTxtColor);
}
CFont* CLocalFontStore::LoadFont(CString strFaceName,int height,int weight/*=FW_REGULAR*/,
bool bUnderLine/*=false*/,bool bItalics/* = false*/, BYTE charSet /*= DEFAULT_CHARSET*/,bool bHeightFlag/* = false*/)
{
if(m_nCharSet == -1)
m_nCharSet = GetCharset();
LOGFONT lf;
CFont* pFont = NULL;
CString strFontText;
memset(&lf, 0, sizeof(LOGFONT));
swprintf_s(lf.lfFaceName,GetCommonLobby()->GetSupportedFontName(strFaceName));
double d72DPIHeight = (STANDARD_DPI * (float)height) / 72.0;
lf.lfHeight = (long) (d72DPIHeight < 0.0 ? ceil(d72DPIHeight - 0.5) : floor(d72DPIHeight + 0.5));
lf.lfWeight = weight >0 ? weight : FW_DONTCARE;
lf.lfUnderline = bUnderLine ? true :false;
lf.lfItalic = bItalics;
lf.lfCharSet = m_nCharSet;
strFontText.Format(_T("%s~%d~%d~%d~%d~%d"), lf.lfFaceName, lf.lfHeight, lf.lfWeight, lf.lfUnderline, lf.lfItalic, m_nCharSet);
if(m_FontMap.size() != 0)
pFont = GetFont(strFontText);
if(pFont == NULL)
{
HFONT hFont = GetIFontStore()->LoadFont(strFaceName, height, weight, bUnderLine, bItalics, charSet,bHeightFlag);
if(hFont != NULL)
{
pFont = new CFont;
pFont->Attach(hFont);
m_FontMap[strFontText] = pFont;
}
}
return pFont;
}
CFont* CLocalFontStore::LoadFont(CString strFaceName,int height,int weight/*=FW_REGULAR*/,
bool bUnderLine/*=false*/,bool bItalics/* = false*/, BYTE charSet /*= DEFAULT_CHARSET*/,bool bHeightFlag/* = false*/)
{
if(m_nCharSet == -1)
m_nCharSet = GetCharset();
LOGFONT lf;
CFont* pFont = NULL;
CString strFontText;
memset(&lf, 0, sizeof(LOGFONT));
swprintf_s(lf.lfFaceName,GetCommonLobby()->GetSupportedFontName(strFaceName));
double d72DPIHeight = (STANDARD_DPI * (float)height) / 72.0;
lf.lfHeight = (long) (d72DPIHeight < 0.0 ? ceil(d72DPIHeight - 0.5) : floor(d72DPIHeight + 0.5));
lf.lfWeight = weight >0 ? weight : FW_DONTCARE;
lf.lfUnderline = bUnderLine ? true :false;
lf.lfItalic = bItalics;
lf.lfCharSet = m_nCharSet;
strFontText.Format(_T("%s~%d~%d~%d~%d~%d"), lf.lfFaceName, lf.lfHeight, lf.lfWeight, lf.lfUnderline, lf.lfItalic, m_nCharSet);
if(m_FontMap.size() != 0)
pFont = GetFont(strFontText);
if(pFont == NULL)
{
HFONT hFont = GetIFontStore()->LoadFont(strFaceName, height, weight, bUnderLine, bItalics, charSet,bHeightFlag);
if(hFont != NULL)
{
pFont = new CFont;
pFont->Attach(hFont);
m_FontMap[strFontText] = pFont;
}
}
return pFont;
}
HFONT CFontStore::LoadPointFont(CString strFaceName,int height,int weight/*=FW_REGULAR*/,
bool bUnderLine/*=false*/,bool bItalics/* = false*/, BYTE charSet /*= DEFAULT_CHARSET*/,bool bHeightFlag/* = false*/, bool bIsEnglish/* = false*/)
{
LOGFONT lf;
CString strFontText;
memset(&lf, 0, sizeof(LOGFONT));
if (!_tcsicmp(strFaceName, _T("Small Fonts")))
{
_stprintf(lf.lfFaceName, strFaceName);
}
else
{
_stprintf(lf.lfFaceName,GetCommonLobby()->GetSupportedFontName(strFaceName));
}
double d72DPIHeight = (STANDARD_DPI * (float)height) / 72.0;
lf.lfHeight = (long) (d72DPIHeight < 0.0 ? ceil(d72DPIHeight - 0.5) : floor(d72DPIHeight + 0.5));
lf.lfWeight = weight >0 ? weight : FW_DONTCARE;
lf.lfUnderline = bUnderLine ? true :false;
lf.lfItalic = bItalics;
lf.lfCharSet = GetCharset();
if(!bIsEnglish)
{
ChangeFont(lf, 10);
}
strFontText.Format(_T("%s~%d~%d~%d~%d~%d"),lf.lfFaceName,lf.lfHeight,lf.lfWeight,lf.lfUnderline,lf.lfItalic, GetCharset());
HFONT hFont = NULL;
if(m_FontMap.size() != 0)
{
hFont = GetFont(strFontText);
}
if(hFont == NULL)
{
lf.lfHeight = -lf.lfHeight;
HFONT hf = CreateFontIndirect(&lf);
if(NULL != hf)
{
hFont = hf;
m_FontMap[strFontText] = hFont;
}
}
return hFont;
}