赞
踩
问题描述:字库 在ios端 不生效 oc 报错信息 = NULL
需求: ios端除了cocos相关的操作 还需要原生页面的支持 ,但是修改完项目 字库在ios端 不生效
解决方案:
修改jsb_platform_apple.mm文件
static bool JSB_loadFont(se::State& s) { const auto& args = s.args(); size_t argc = args.size(); CC_UNUSED bool ok = true; if (argc >= 1) { s.rval().setNull(); std::string originalFamilyName; ok &= seval_to_std_string(args[0], &originalFamilyName); SE_PRECONDITION2(ok, false, "JSB_loadFont : Error processing argument: originalFamilyName"); std::string source; ok &= seval_to_std_string(args[1], &source); SE_PRECONDITION2(ok, false, "JSB_loadFont : Error processing argument: source"); std::string fontFilePath; std::regex re("url\\(\\s*'\\s*(.*?)\\s*'\\s*\\)"); std::match_results<std::string::const_iterator> results; if (std::regex_search(source.cbegin(), source.cend(), results, re)) { fontFilePath = results[1].str(); } fontFilePath = FileUtils::getInstance()->fullPathForFilename(fontFilePath); if (fontFilePath.empty()) { SE_LOGE("Font (%s) doesn't exist!", fontFilePath.c_str()); return true; } NSURL* url = [NSURL fileURLWithPath: [NSString stringWithUTF8String:fontFilePath.c_str()]]; NSData* dynamicFontData = [NSData dataWithContentsOfURL:url]; if (!dynamicFontData) { SE_LOGE("load font (%s) failed!", source.c_str()); return true; } const auto& familyNamesBeforeRegister = getAvailableFontFamilyNames(); bool succeed = true; CFErrorRef error; CGDataProviderRef providerRef = CGDataProviderCreateWithCFData((CFDataRef)dynamicFontData); CGFontRef font = CGFontCreateWithDataProvider(providerRef); if (!CTFontManagerRegisterGraphicsFont(font, &error)) { CFStringRef errorDescription = CFErrorCopyDescription(error); // 捕获报错信息代码修改.新代码可以捕获到报错信息.旧代码捕获不到 CFIndex length = CFStringGetLength(errorDescription); CFIndex maxSize = CFStringGetMaximumSizeForEncoding(length, kCFStringEncodingUTF8) + 1; char *buffer = (char *)malloc(maxSize); if (CFStringGetCString(errorDescription, buffer, maxSize, kCFStringEncodingUTF8)) { SE_LOGE("Failed to load font: %s", buffer); } // 原始代码注释. 捕获不到报错信息 // const char* cErrorStr = CFStringGetCStringPtr(errorDescription, kCFStringEncodingUTF8); // SE_LOGE("Failed to load font: %s", cErrorStr); CFRelease(errorDescription); // 由于报错信息是105 重复注册 .表示 现在已经有字库 强制 success succeed = true; } succeed = true; if (succeed) { const auto& familyNamesAfterRegister = getAvailableFontFamilyNames(); std::string familyName = getFontFamilyByCompareAvailableFontFamilyNames(familyNamesBeforeRegister, familyNamesAfterRegister); // 强制引用项目中的字库 名字如下 然后 就能走 字库的正常逻辑 familyName = "FZCuYuan-M03S"; if (!familyName.empty()) { _fontFamilyNameMap.emplace(originalFamilyName, familyName); s.rval().setString(familyName); } } CFRelease(font); CFRelease(providerRef); return true; } SE_REPORT_ERROR("wrong number of arguments: %d, was expecting %d", (int)argc, 1); return false; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。