fix android editbox custom font file (#15924)

* fix android editbox custom font file

* simplify get real font name on Android
This commit is contained in:
子龙山人 2016-06-23 11:26:32 +08:00 committed by minggo
parent 597fb61a78
commit bac1870d16
4 changed files with 37 additions and 11 deletions

View File

@ -96,13 +96,15 @@ public:
// Do a full lookup for the font path using FileUtils in case the given font name is a relative path to a font file asset,
// or the path has been mapped to a different location in the app package:
std::string fullPathOrFontName = FileUtils::getInstance()->fullPathForFilename(textDefinition._fontName);
// If the path name returned includes the 'assets' dir then that needs to be removed, because the android.content.Context
// requires this portion of the path to be omitted for assets inside the app package.
if (fullPathOrFontName.find("assets/") == 0)
{
fullPathOrFontName = fullPathOrFontName.substr(strlen("assets/")); // Chop out the 'assets/' portion of the path.
std::string fullPathOrFontName = textDefinition._fontName;
if(FileUtils::getInstance()->isFileExist(fullPathOrFontName)) {
fullPathOrFontName = FileUtils::getInstance()->fullPathForFilename(textDefinition._fontName);
// If the path name returned includes the 'assets' dir then that needs to be removed, because the android.content.Context
// requires this portion of the path to be omitted for assets inside the app package.
if (fullPathOrFontName.find("assets/") == 0)
{
fullPathOrFontName = fullPathOrFontName.substr(strlen("assets/")); // Chop out the 'assets/' portion of the path.
}
}
/**create bitmap
@ -114,7 +116,7 @@ public:
int count = strlen(text);
jbyteArray strArray = methodInfo.env->NewByteArray(count);
methodInfo.env->SetByteArrayRegion(strArray, 0, count, reinterpret_cast<const jbyte*>(text));
jstring jstrFont = methodInfo.env->NewStringUTF(fullPathOrFontName.empty() ? textDefinition._fontName.c_str() : fullPathOrFontName.c_str());
jstring jstrFont = methodInfo.env->NewStringUTF(fullPathOrFontName.c_str());
if(!methodInfo.env->CallStaticBooleanMethod(methodInfo.classID, methodInfo.methodID, strArray,
jstrFont, textDefinition._fontSize, textDefinition._fontFillColor.r, textDefinition._fontFillColor.g,

View File

@ -243,7 +243,19 @@ public class Cocos2dxEditBoxHelper {
if (editBox != null) {
Typeface tf;
if (!fontName.isEmpty()) {
tf = Typeface.create(fontName, Typeface.NORMAL);
if (fontName.endsWith(".ttf")) {
try {
tf = Cocos2dxTypefaces.get(mCocos2dxActivity.getContext(), fontName);
} catch (final Exception e) {
Log.e("Cocos2dxEditBoxHelper", "error to create ttf type face: "
+ fontName);
// The file may not find, use system font.
tf = Typeface.create(fontName, Typeface.NORMAL);
}
} else {
tf = Typeface.create(fontName, Typeface.NORMAL);
}
}else{
tf = Typeface.DEFAULT;
}

View File

@ -36,6 +36,7 @@
#include "math/Vec2.h"
#include "ui/UIHelper.h"
#include "base/CCDirector.h"
#include "platform/CCFileUtils.h"
NS_CC_BEGIN
@ -112,8 +113,17 @@ void EditBoxImplAndroid::setNativeFont(const char* pFontName, int fontSize)
{
auto director = cocos2d::Director::getInstance();
auto glView = director->getOpenGLView();
JniHelper::callStaticVoidMethod(editBoxClassName, "setFont",
_editBoxIndex, pFontName,
auto isFontFileExists = cocos2d::FileUtils::getInstance()->isFileExist(pFontName);
std::string realFontPath = pFontName;
if(isFontFileExists) {
realFontPath = cocos2d::FileUtils::getInstance()->fullPathForFilename(pFontName);
if (realFontPath.find("assets/") == 0)
{
realFontPath = realFontPath.substr(strlen("assets/")); // Chop out the 'assets/' portion of the path.
}
}
JniHelper::callStaticVoidMethod(editBoxClassName, "setFont",
_editBoxIndex, realFontPath,
(float)fontSize * glView->getScaleX());
}

View File

@ -2,6 +2,8 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>ATSApplicationFontsPath</key>
<string>fonts</string>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleExecutable</key>