cocos2d-x 中 TTF 字体文件的位置


cocos2d-x 中,字体文件需要保存在 fonts 文件夹中,如果字体路径中没有 fonts/ 会自动添加上这个文件夹。

如果字体名称没有 .ttf 后缀,也会自动加上这个后缀。

unsigned char* CCFreeTypeFont::loadFont(const char *pFontName, ssize_t *size) 
{
    std::string lowerCase(pFontName);
    std::string path(pFontName);

    for (unsigned int i = 0; i < lowerCase.length(); ++i)
    {
        lowerCase[i] = tolower(lowerCase[i]);
    }

    if (std::string::npos == lowerCase.find("fonts/"))
    {
        path = "fonts/";
        path += pFontName;
    }

    if (std::string::npos == lowerCase.find(".ttf"))
    {
        path += ".ttf";
    }

    std::string fullpath  = FileUtils::getInstance()->fullPathForFilename(path.c_str());
    return FileUtils::sharedFileUtils()->getFileData(fullpath.c_str(), "rb", size);
}

string::npos 就是 -1, 表示没有找到子串,find 方法用来查找字体名称中是否包含了 fonts.


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM