转 [cocos2d-x使用ttf字体时,字体库中不存在的文字用框框代替]

cocos2d-x版本:3.2

使用ttf字体时,由于字体库不全,没有包含所有的字符,导致有些字符无法显示,修改代码让无法显示的字符用囗代替,能够明确看到有字符显示不了,代码修改如下:

unsigned char* FontFreeType::getGlyphBitmap(unsigned short theChar, long &outWidth, long &outHeight, Rect &outRect,int &xAdvance)  
{  
    bool invalidChar = true;  
    unsigned char * ret = nullptr;  
  
    do   
    {  
        if (!_fontRef)  
            break;  
  
        auto glyphIndex = FT_Get_Char_Index(_fontRef, theChar);  
        if(!glyphIndex)  
        {  
            // 字符找不到时默认用囗代替(not cocos)  
            static int defIndex = FT_Get_Char_Index(_fontRef, 22231);  
            glyphIndex = defIndex;  
            if (!glyphIndex)  
                break;  
        }  
  
        // ...  
    } while (0);  
}  
unsigned char * FontFreeType::getGlyphBitmapWithOutline(unsigned short theChar, FT_BBox &bbox)  
{     
    unsigned char* ret = nullptr;  
  
    FT_UInt gindex = FT_Get_Char_Index(_fontRef, theChar);  
    if (!gindex)  
    {  
        // 字符找不到时默认用囗代替(not cocos)  
        static int defIndex = FT_Get_Char_Index(_fontRef, 22231);  
        gindex = defIndex;  
    }  
    // ...  
  
    return ret;  
}  

这样修改之后又有另外一个问题,EditBox只支持系统字体,使用EditBox做输入框时,比如聊天输入时正常,发出来就变成了囗。解决方法:修改EditBox各平台代码的setFont和setPlaceholderFont接口,使其支持ttf字体,代码如下:

// CCEditBoxImplWin.h  
#define SET_EDIT_FONT(label, fontName, fontSize) \  
if (FileUtils::getInstance()->isFileExist(fontName)) \  
{ \  
    TTFConfig config = label->getTTFConfig(); \  
    config.fontFilePath = fontName; \  
    config.fontSize = fontSize; \  
    label->setTTFConfig(config); \  
} \  
    else \  
{ \  
    label->setSystemFontName(fontName); \  
    label->setSystemFontSize(fontSize); \  
}  
// CCEditBoxImplWin.cpp  
void EditBoxImplWin::setFont(const char* pFontName, int fontSize)  
{  
    if(_label != nullptr) {  
        SET_EDIT_FONT(_label, pFontName, fontSize)  
    }  
  
    if(_labelPlaceHolder != nullptr) {  
        SET_EDIT_FONT(_labelPlaceHolder, pFontName, fontSize)  
    }  
}  
  
void EditBoxImplWin::setPlaceholderFont(const char* pFontName, int fontSize)  
{  
    if(_labelPlaceHolder != nullptr) {  
        SET_EDIT_FONT(_labelPlaceHolder, pFontName, fontSize)  
    }  
}
// EditBoxImplAndroid.cpp  
void EditBoxImplAndroid::setFont(const char* pFontName, int fontSize)  
{  
    if(_label != NULL) {  
        SET_EDIT_FONT(_label, pFontName, fontSize)  
    }  
      
    if(_labelPlaceHolder != NULL) {  
        SET_EDIT_FONT(_labelPlaceHolder, pFontName, fontSize)  
    }  
}  
  
void EditBoxImplAndroid::setPlaceholderFont(const char* pFontName, int fontSize)  
{  
    if(_labelPlaceHolder != NULL) {  
        SET_EDIT_FONT(_labelPlaceHolder, pFontName, fontSize)  
    }  
}  
// EditBoxImplIOS.mm  
void EditBoxImplIOS::setFont(const char* pFontName, int fontSize)  
{  
    bool isValidFontName = true;  
    if(pFontName == NULL || strlen(pFontName) == 0) {  
        isValidFontName = false;  
    }  
  
    float retinaFactor = _inRetinaMode ? 2.0f : 1.0f;  
    NSString * fntName = [NSString stringWithUTF8String:pFontName];  
  
    auto glview = cocos2d::Director::getInstance()->getOpenGLView();  
  
    float scaleFactor = glview->getScaleX();  
    UIFont *textFont = nil;  
    if (isValidFontName) {  
        textFont = [UIFont fontWithName:fntName size:fontSize * scaleFactor / retinaFactor];  
    }  
      
    if (!isValidFontName || textFont == nil){  
        textFont = [UIFont systemFontOfSize:fontSize * scaleFactor / retinaFactor];  
    }  
  
    if(textFont != nil) {  
        [_systemControl.textField setFont:textFont];  
    }  
  
    SET_EDIT_FONT(_label, pFontName, fontSize)  
    SET_EDIT_FONT(_labelPlaceHolder, pFontName, fontSize)  
}  
  
void EditBoxImplIOS::setPlaceholderFont(const char* pFontName, int fontSize)  
{  
    if(_labelPlaceHolder != NULL) {  
        SET_EDIT_FONT(_labelPlaceHolder, pFontName, fontSize)  
    }  
}  

你可能感兴趣的:(转 [cocos2d-x使用ttf字体时,字体库中不存在的文字用框框代替])