Easy way to tell if unicode character is supported in current font?

我的梦境 提交于 2019-12-30 02:25:06

问题


I'm using Borland C++ Builder 2009 and I display the right and left pointing arrows like so:

Button2->Hint = L"Ctrl+\u2190" ;
Button3->Hint = L"Ctrl+\u2192" ; 

This works fine on Windows 7, the application uses font 'Segoe UI'.

On XP I get a square instead of the arrows, I use font 'Tahoma' on XP. In other words mentioned Unicode characters are not present in Tahoma on XP.

Is there an easy and fast way to simply check if the requested Unicode character is supported in the currently used font ? If so I could, for instance, replace the arrow with '>' or '<'. Not perfect, but good enough. I don't want to start changing fonts at this stage.

Your help appreciated.


回答1:


You can use GetFontUnicodeRanges() to see which characters are supported by the font currently selected into the DC. Note that this API requires you to call it once to find out how big the buffer needs to be, and a second time to actually get the data.

DWORD dwSize = GetFontUnicodeRanges(hDC, nullptr);
BYTE* bBuffer = new BYTE[dwSize];
GLYPHSET* pGlyphSet = reinterpret_cast<GLYPHSET*>(bBuffer);
GetFontUnicodeRanges(hDC, pGlyphSet);
// use data in pGlyphSet, then free the buffer
delete[] bBuffer;

The GLYPHSET structure has a member array called ranges which lets you determine the range of characters supported by the font.




回答2:


Just for reference and the Google Gods:

bool UnicodeCharSupported(HWND Handle, wchar_t Char)
{
if (Handle)
    {
    DWORD dwSize = GetFontUnicodeRanges(Handle, NULL);
    if (dwSize)
        {
        bool Supported = false ;
        BYTE* bBuffer = new BYTE[dwSize];
        GLYPHSET* pGlyphSet = reinterpret_cast<GLYPHSET*>(bBuffer);
        if (GetFontUnicodeRanges(Handle, pGlyphSet))
            {
            for (DWORD x = 0 ; x < pGlyphSet->cRanges && !Supported ; x++)
                {
                Supported = (Char >= pGlyphSet->ranges[x].wcLow &&
                             Char < (pGlyphSet->ranges[x].wcLow + pGlyphSet->ranges[x].cGlyphs)) ;
                }
            }
        delete[] bBuffer;
        return Supported ;
        }
    }
return false ;
}

Example, relating to my Question:

if (!UnicodeCharSupported(Canvas->Handle, 0x2190))
    { /* Character not supported in current Font, use different character */ }


来源:https://stackoverflow.com/questions/34278340/easy-way-to-tell-if-unicode-character-is-supported-in-current-font

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!