0

I have old MFC based application. Recently we moved from Trébuchent to Roboto font. From that time onwards on some machines we are finding weird characters in the application.

I am using the CreateFontIndirect function to create the font. The IDE that I am using is Visual studio 2008.

This issue is not affecting the entire application. It is only affecting a few places.

I have observed a pattern in those junk characters. Is appears that it is adding 2 to every ASCII value. For example: if I want to display ABCD it is showing CDEF.

Please help me out in finding this. Thanks in Advance.

Here is my code sample :

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;
}
Prasanna
  • 11
  • 2
  • 2
    Please clarify your question. What does GetLogFont return? Please show us appropriate code. Maybe the Font selected by the font mapper is something complete different! Try to give us minimal reproducible sample. – xMRi Oct 24 '17 at 14:05
  • 1
    Screendumps with the old font and the new font could useful too. – Jabberwocky Oct 24 '17 at 18:52
  • If the error occurs some of the time but not all of the time, it could be undefined behavior. You forgot to initialize a stack variable or something. Otherwise you should be able to reproduce the problem. – Barmak Shemirani Oct 24 '17 at 20:17
  • GetLogFont is returning Roboto with -ve font height – Prasanna Oct 25 '17 at 10:00

0 Answers0