Fix custom font loading on Android when using fonts that are in resource mapped directories/paths.

1: Use the CCFileUtils::fullPathForFilename function to lookup the resolved path for the font in the Android specific function, CCImage::getBitmapFromJava.

2: Remove a warning previously added in pull request 2422 (https://github.com/cocos2d/cocos2d-x/pull/2422) in CCFileUtils::fullPathForFilename in order to support change (1) above. If we pass just a system font name to CCFileUtils::fullPathForFilename such as 'Arial' (instead of a path) then it should just return 'Arial' and not generate a warning. We can't assume that when a path (or handle) given to CCFileUtils::fullPathForFilename isn't found within Cocos2dx that it doesn't exist or isn't valid. Depending on the platform there might be files or folders or handles that could be accessible to the app which might exist outside of the knowledge of Cocos2dx. The correct place to warn about missing resource files would be at the point where the resource loading itself fails; e.g if a texture or sound fails to load for example.
This commit is contained in:
Darragh Coy 2013-05-09 14:29:42 +01:00
parent c25c89e5a8
commit 032aed4499
2 changed files with 13 additions and 3 deletions

View File

@ -636,8 +636,6 @@ std::string CCFileUtils::fullPathForFilename(const char* pszFileName)
}
}
CCLOG("cocos2d: fullPathForFilename: No file found at %s. Possible missing file.", pszFileName);
// The file wasn't found, return the file name passed in.
return pszFileName;
}

View File

@ -28,6 +28,7 @@ THE SOFTWARE.
#include "platform/CCImageCommon_cpp.h"
#include "platform/CCPlatformMacros.h"
#include "platform/CCImage.h"
#include "platform/CCFileUtils.h"
#include "jni/JniHelper.h"
#include <android/log.h>
@ -65,6 +66,17 @@ public:
return false;
}
// Do a full lookup for the font path using CCFileUtils 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 = CCFileUtils::sharedFileUtils()->fullPathForFilename(pFontName);
// 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_first_of("assets/") == 0)
{
fullPathOrFontName = fullPathOrFontName.substr(strlen("assets/")); // Chop out the 'assets/' portion of the path.
}
/**create bitmap
* this method call Cococs2dx.createBitmap()(java code) to create the bitmap, the java code
* will call Java_org_cocos2dx_lib_Cocos2dxBitmap_nativeInitBitmapDC() to init the width, height
@ -72,7 +84,7 @@ public:
* use this approach to decrease the jni call number
*/
jstring jstrText = methodInfo.env->NewStringUTF(text);
jstring jstrFont = methodInfo.env->NewStringUTF(pFontName);
jstring jstrFont = methodInfo.env->NewStringUTF(fullPathOrFontName.c_str());
methodInfo.env->CallStaticVoidMethod(methodInfo.classID, methodInfo.methodID, jstrText,
jstrFont, (int)fontSize, eAlignMask, nWidth, nHeight);