diff --git a/core/jni/android/graphics/FontFamily.cpp b/core/jni/android/graphics/FontFamily.cpp index 8a85d625e0513..3e6327ba4de9c 100644 --- a/core/jni/android/graphics/FontFamily.cpp +++ b/core/jni/android/graphics/FontFamily.cpp @@ -19,6 +19,8 @@ #include "JNIHelp.h" #include +#include "SkData.h" +#include "SkRefCnt.h" #include "SkTypeface.h" #include "GraphicsJNI.h" #include @@ -82,6 +84,10 @@ static jboolean FontFamily_addFontWeightStyle(JNIEnv* env, jobject clazz, jlong return true; } +static void releaseAsset(const void* ptr, size_t length, void* context) { + delete static_cast(context); +} + static jboolean FontFamily_addFontFromAsset(JNIEnv* env, jobject, jlong familyPtr, jobject jassetMgr, jstring jpath) { NPE_CHECK_RETURN_ZERO(env, jassetMgr); @@ -98,12 +104,15 @@ static jboolean FontFamily_addFontFromAsset(JNIEnv* env, jobject, jlong familyPt return false; } - SkStream* stream = new AssetStreamAdaptor(asset, - AssetStreamAdaptor::kYes_OwnAsset, - AssetStreamAdaptor::kYes_HasMemoryBase); + const void* buf = asset->getBuffer(false); + if (NULL == buf) { + delete asset; + return false; + } + + SkAutoTUnref data(SkData::NewWithProc(buf, asset->getLength(), releaseAsset, asset)); + SkAutoTUnref stream(new SkMemoryStream(data)); SkTypeface* face = SkTypeface::CreateFromStream(stream); - // Note: SkTypeface::CreateFromStream holds its own reference to the stream - stream->unref(); if (face == NULL) { ALOGE("addFontFromAsset failed to create font %s", str.c_str()); return false;