基于MFC的应用程序中的Roboto字体问题

时间:2017-10-24 13:56:19

标签: mfc roboto

我有旧的基于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;
}

0 个答案:

没有答案