Merge change 1478 into donut

* changes:
  Fixes #1847219. Add a new API to load fonts from arbitrary files: Typeface.createFromFile(String/File).
This commit is contained in:
Android (Google) Code Review
2009-05-12 13:33:07 -07:00
3 changed files with 65 additions and 9 deletions

View File

@@ -53318,6 +53318,32 @@
<parameter name="path" type="java.lang.String"> <parameter name="path" type="java.lang.String">
</parameter> </parameter>
</method> </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" <method name="defaultFromStyle"
return="android.graphics.Typeface" return="android.graphics.Typeface"
abstract="false" abstract="false"

View File

@@ -133,6 +133,14 @@ static SkTypeface* Typeface_createFromAsset(JNIEnv* env, jobject,
return SkTypeface::CreateFromStream(new AssetStream(asset, true)); 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[] = { static JNINativeMethod gTypefaceMethods[] = {
@@ -140,9 +148,10 @@ static JNINativeMethod gTypefaceMethods[] = {
{ "nativeCreateFromTypeface", "(II)I", (void*)Typeface_createFromTypeface }, { "nativeCreateFromTypeface", "(II)I", (void*)Typeface_createFromTypeface },
{ "nativeUnref", "(I)V", (void*)Typeface_unref }, { "nativeUnref", "(I)V", (void*)Typeface_unref },
{ "nativeGetStyle", "(I)I", (void*)Typeface_getStyle }, { "nativeGetStyle", "(I)I", (void*)Typeface_getStyle },
{ "nativeCreateFromAsset", { "nativeCreateFromAsset", "(Landroid/content/res/AssetManager;Ljava/lang/String;)I",
"(Landroid/content/res/AssetManager;Ljava/lang/String;)I", (void*)Typeface_createFromAsset },
(void*)Typeface_createFromAsset } { "nativeCreateFromFile", "(Ljava/lang/String)I",
(void*)Typeface_createFromFile }
}; };
int register_android_graphics_Typeface(JNIEnv* env); int register_android_graphics_Typeface(JNIEnv* env);
@@ -153,4 +162,3 @@ int register_android_graphics_Typeface(JNIEnv* env)
gTypefaceMethods, gTypefaceMethods,
SK_ARRAY_COUNT(gTypefaceMethods)); SK_ARRAY_COUNT(gTypefaceMethods));
} }

View File

@@ -18,6 +18,8 @@ package android.graphics;
import android.content.res.AssetManager; import android.content.res.AssetManager;
import java.io.File;
/** /**
* The Typeface class specifies the typeface and intrinsic style of a font. * The Typeface class specifies the typeface and intrinsic style of a font.
* This is used in the paint, along with optionally Paint settings like * 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)); 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 // don't allow clients to call this directly
private Typeface(int ni) { private Typeface(int ni) {
native_instance = ni; native_instance = ni;
@@ -140,14 +162,14 @@ public class Typeface {
} }
protected void finalize() throws Throwable { protected void finalize() throws Throwable {
super.finalize();
nativeUnref(native_instance); nativeUnref(native_instance);
} }
private static native int nativeCreate(String familyName, int style); private static native int nativeCreate(String familyName, int style);
private static native int nativeCreateFromTypeface(int native_instance, private static native int nativeCreateFromTypeface(int native_instance, int style);
int style);
private static native void nativeUnref(int native_instance); private static native void nativeUnref(int native_instance);
private static native int nativeGetStyle(int native_instance); private static native int nativeGetStyle(int native_instance);
private static native int nativeCreateFromAsset(AssetManager mgr, private static native int nativeCreateFromAsset(AssetManager mgr, String path);
String path); private static native int nativeCreateFromFile(String path);
} }