Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit bae1ca31 authored by Android (Google) Code Review's avatar Android (Google) Code Review Committed by The Android Open Source Project
Browse files

am 3e3439d5: Merge change 1478 into donut

Merge commit '3e3439d5'

* commit '3e3439d5':
  Fixes #1847219. Add a new API to load fonts from arbitrary files: Typeface.createFromFile(String/File).
parents 06f429ed 3e3439d5
Loading
Loading
Loading
Loading
+26 −0
Original line number Diff line number Diff line
@@ -55625,6 +55625,32 @@
<parameter name="path" type="java.lang.String">
</parameter>
</method>
<method name="createFromFile"
 return="android.graphics.Typeface"
 abstract="false"
 native="false"
 synchronized="false"
 static="true"
 final="false"
 deprecated="not deprecated"
 visibility="public"
>
<parameter name="path" type="java.io.File">
</parameter>
</method>
<method name="createFromFile"
 return="android.graphics.Typeface"
 abstract="false"
 native="false"
 synchronized="false"
 static="true"
 final="false"
 deprecated="not deprecated"
 visibility="public"
>
<parameter name="path" type="java.lang.String">
</parameter>
</method>
<method name="defaultFromStyle"
 return="android.graphics.Typeface"
 abstract="false"
+12 −4
Original line number Diff line number Diff line
@@ -133,6 +133,14 @@ static SkTypeface* Typeface_createFromAsset(JNIEnv* env, jobject,
    return SkTypeface::CreateFromStream(new AssetStream(asset, true));
}

static SkTypeface* Typeface_createFromFile(JNIEnv* env, jobject, jstring jpath) {
    NPE_CHECK_RETURN_ZERO(env, jpath);

    AutoJavaStringToUTF8 str(env, jpath);

    return SkTypeface::CreateFromFile(str.c_str());
}

///////////////////////////////////////////////////////////////////////////////

static JNINativeMethod gTypefaceMethods[] = {
@@ -140,9 +148,10 @@ static JNINativeMethod gTypefaceMethods[] = {
    { "nativeCreateFromTypeface", "(II)I", (void*)Typeface_createFromTypeface },
    { "nativeUnref",              "(I)V",  (void*)Typeface_unref },
    { "nativeGetStyle",           "(I)I",  (void*)Typeface_getStyle },
    { "nativeCreateFromAsset",
                        "(Landroid/content/res/AssetManager;Ljava/lang/String;)I",
                                            (void*)Typeface_createFromAsset }
    { "nativeCreateFromAsset",    "(Landroid/content/res/AssetManager;Ljava/lang/String;)I",
                                           (void*)Typeface_createFromAsset },
    { "nativeCreateFromFile",     "(Ljava/lang/String)I",
                                           (void*)Typeface_createFromFile }
};

int register_android_graphics_Typeface(JNIEnv* env);
@@ -153,4 +162,3 @@ int register_android_graphics_Typeface(JNIEnv* env)
                                                       gTypefaceMethods,
                                                       SK_ARRAY_COUNT(gTypefaceMethods));
}
+27 −5
Original line number Diff line number Diff line
@@ -18,6 +18,8 @@ package android.graphics;

import android.content.res.AssetManager;

import java.io.File;

/**
 * The Typeface class specifies the typeface and intrinsic style of a font.
 * This is used in the paint, along with optionally Paint settings like
@@ -119,6 +121,26 @@ public class Typeface {
        return new Typeface(nativeCreateFromAsset(mgr, path));
    }

    /**
     * Create a new typeface from the specified font file.
     *
     * @param path The path to the font data. 
     * @return The new typeface.
     */
    public static Typeface createFromFile(File path) {
        return new Typeface(nativeCreateFromFile(path.getAbsolutePath()));
    }

    /**
     * Create a new typeface from the specified font file.
     *
     * @param path The full path to the font data. 
     * @return The new typeface.
     */
    public static Typeface createFromFile(String path) {
        return new Typeface(nativeCreateFromFile(path));
    }

    // don't allow clients to call this directly
    private Typeface(int ni) {
        native_instance = ni;
@@ -140,14 +162,14 @@ public class Typeface {
    }

    protected void finalize() throws Throwable {
        super.finalize();
        nativeUnref(native_instance);
    }

    private static native int  nativeCreate(String familyName, int style);
    private static native int  nativeCreateFromTypeface(int native_instance,
                                                        int style);
    private static native int  nativeCreateFromTypeface(int native_instance, int style); 
    private static native void nativeUnref(int native_instance);
    private static native int  nativeGetStyle(int native_instance);
    private static native int  nativeCreateFromAsset(AssetManager mgr,
                                                     String path);
    private static native int  nativeCreateFromAsset(AssetManager mgr, String path);
    private static native int nativeCreateFromFile(String path);
}