Merge pull request #2557 from dumganhar/iss2151-customttf

fixed #2151: Custom font can't be loaded correctly if using full path of filename.
This commit is contained in:
James Chen 2013-05-13 07:14:08 -07:00
commit f80bce9b6d
3 changed files with 24 additions and 4 deletions

View File

@ -636,8 +636,8 @@ std::string CCFileUtils::fullPathForFilename(const char* pszFileName)
}
}
CCLOG("cocos2d: fullPathForFilename: No file found at %s. Possible missing file.", 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("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);

View File

@ -57,7 +57,15 @@ public class Cocos2dxTypefaces {
public static synchronized Typeface get(final Context pContext, final String pAssetName) {
if (!Cocos2dxTypefaces.sTypefaceCache.containsKey(pAssetName)) {
final Typeface typeface = Typeface.createFromAsset(pContext.getAssets(), pAssetName);
Typeface typeface = null;
if (pAssetName.startsWith("/"))
{
typeface = Typeface.createFromFile(pAssetName);
}
else
{
typeface = Typeface.createFromAsset(pContext.getAssets(), pAssetName);
}
Cocos2dxTypefaces.sTypefaceCache.put(pAssetName, typeface);
}