cocos2dx判断TTF字体库是否有不支持的字体

来源:互联网 发布:淘宝商品信用卡套现 编辑:程序博客网 时间:2024/05/30 04:48

在FontAtlasCache类里面加了方法,直接上代码:

bool FontAtlasCache::hasInvalidChar(const std::string& fontFile, const std::string& text)

{
TTFConfig ttfConfig(fontFile.c_str(), 20, GlyphCollection::DYNAMIC);
FontAtlas * font =  getFontAtlasTTF(ttfConfig);
if(font == nullptr)
{
return true;
}


Font* _font = const_cast<Font*>(font->getFont());
FontFreeType* fontTTf = dynamic_cast<FontFreeType*>(_font);
if(fontTTf ==nullptr)
{
font->release();
return true;
}
    std::u16string utf16String;
    long bitmapWidth;
    long bitmapHeight;
    Rect tempRect;
    int xAdvance = 0;
bool invalid = false;
    if (StringUtils::UTF8ToUTF16(text, utf16String))
    {
        size_t length = utf16String.length();
        for (size_t i = 0; i < length; ++i)
        {
            auto bitmap =  fontTTf->getGlyphBitmap(utf16String[i], bitmapWidth, bitmapHeight, tempRect, xAdvance);
            if(bitmap == nullptr && xAdvance == 0)
            {
invalid = true;
break;
}
else
{
xAdvance = 0;
}
        }
    }


font->release();
return invalid;
}
阅读全文
0 0