のねのBlog

パソコンの問題や、ソフトウェアの開発で起きた問題など書いていきます。よろしくお願いします^^。

createFromAsset

CreateFromAssetの流れ

    148     public static Typeface createFromAsset(AssetManager mgr, String path) {
    149         return new Typeface(nativeCreateFromAsset(mgr, path));
    150     }


niは、nativeCreateFromAsset(mgr, path)の返り値。

    172     // don't allow clients to call this directly
    173     private Typeface(int ni) {
    174         if (ni == 0) {
    175             throw new RuntimeException("native typeface cannot be made");
    176         }
    177 
    178         native_instance = ni;
    179         mStyle = nativeGetStyle(ni);
    180     }


nativeCreateFromAssetは、JNIでTypeface_createFromAssetになる。

    172 static JNINativeMethod gTypefaceMethods[] = {
    177     { "nativeCreateFromAsset", 
              "(Landroid/content/res/AssetManager;Ljava/lang/String;)I",
    178       (void*)Typeface_createFromAsset },
    181 };
    135 static SkTypeface* Typeface_createFromAsset(JNIEnv* env, jobject,
    136                                             jobject jassetMgr,
    137                                             jstring jpath) {
    138 
    139     NPE_CHECK_RETURN_ZERO(env, jassetMgr);
    140     NPE_CHECK_RETURN_ZERO(env, jpath);
    141 
    142     AssetManager* mgr = assetManagerForJavaObject(env, jassetMgr);
    143     if (NULL == mgr) {
    144         return NULL;
    145     }
    146 
    147     AutoJavaStringToUTF8    str(env, jpath);
    148     Asset* asset = mgr->open(str.c_str(), Asset::ACCESS_BUFFER);
    149     if (NULL == asset) {
    150         return NULL;
    151     }
    152 
    153     SkStream* stream = new AssetStream(asset, true);
    154     SkTypeface* face = SkTypeface::CreateFromStream(stream);
    155     // SkTypeFace::CreateFromStream calls ref() on the stream, so we
    156     // need to unref it here or it won't be freed later on
    157     stream->unref();
    158 
    159     return face;
    160 }