Raph Levien | a033630 | 2013-05-22 16:16:59 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 17 | #include "jni.h" |
| 18 | #include <android_runtime/AndroidRuntime.h> |
| 19 | |
| 20 | #include "GraphicsJNI.h" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 | #include "SkStream.h" |
| 22 | #include "SkTypeface.h" |
Raph Levien | a033630 | 2013-05-22 16:16:59 -0700 | [diff] [blame] | 23 | #include "TypefaceImpl.h" |
Mathias Agopian | b13b9bd | 2012-02-17 18:27:36 -0800 | [diff] [blame] | 24 | #include <android_runtime/android_util_AssetManager.h> |
| 25 | #include <androidfw/AssetManager.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | |
| 27 | using namespace android; |
| 28 | |
| 29 | class AutoJavaStringToUTF8 { |
| 30 | public: |
| 31 | AutoJavaStringToUTF8(JNIEnv* env, jstring str) : fEnv(env), fJStr(str) |
| 32 | { |
| 33 | fCStr = env->GetStringUTFChars(str, NULL); |
| 34 | } |
| 35 | ~AutoJavaStringToUTF8() |
| 36 | { |
| 37 | fEnv->ReleaseStringUTFChars(fJStr, fCStr); |
| 38 | } |
| 39 | const char* c_str() const { return fCStr; } |
| 40 | |
| 41 | private: |
| 42 | JNIEnv* fEnv; |
| 43 | jstring fJStr; |
| 44 | const char* fCStr; |
| 45 | }; |
| 46 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 47 | static jlong Typeface_create(JNIEnv* env, jobject, jstring name, |
| 48 | jint styleHandle) { |
| 49 | SkTypeface::Style style = static_cast<SkTypeface::Style>(styleHandle); |
Raph Levien | a033630 | 2013-05-22 16:16:59 -0700 | [diff] [blame] | 50 | TypefaceImpl* face = NULL; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | |
Derek Sollenberger | 89ec829 | 2013-09-18 15:07:46 -0400 | [diff] [blame] | 52 | if (NULL != name) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 53 | AutoJavaStringToUTF8 str(env, name); |
Raph Levien | a033630 | 2013-05-22 16:16:59 -0700 | [diff] [blame] | 54 | face = TypefaceImpl_createFromName(str.c_str(), style); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 | } |
Derek Sollenberger | 89ec829 | 2013-09-18 15:07:46 -0400 | [diff] [blame] | 56 | |
| 57 | // return the default font at the best style if no exact match exists |
| 58 | if (NULL == face) { |
Raph Levien | a033630 | 2013-05-22 16:16:59 -0700 | [diff] [blame] | 59 | face = TypefaceImpl_createFromName(NULL, style); |
Derek Sollenberger | 89ec829 | 2013-09-18 15:07:46 -0400 | [diff] [blame] | 60 | } |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 61 | return reinterpret_cast<jlong>(face); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 62 | } |
| 63 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 64 | static jlong Typeface_createFromTypeface(JNIEnv* env, jobject, jlong familyHandle, jint style) { |
| 65 | SkTypeface* family = reinterpret_cast<SkTypeface*>(familyHandle); |
Raph Levien | a033630 | 2013-05-22 16:16:59 -0700 | [diff] [blame] | 66 | TypefaceImpl* face = TypefaceImpl_createFromTypeface(family, (SkTypeface::Style)style); |
Raph Levien | 4f0064f | 2013-11-06 11:12:11 -0800 | [diff] [blame] | 67 | // Try to find the closest matching font, using the standard heuristic |
| 68 | if (NULL == face) { |
Raph Levien | a033630 | 2013-05-22 16:16:59 -0700 | [diff] [blame] | 69 | face = TypefaceImpl_createFromTypeface(family, (SkTypeface::Style)(style ^ SkTypeface::kItalic)); |
Raph Levien | 4f0064f | 2013-11-06 11:12:11 -0800 | [diff] [blame] | 70 | } |
| 71 | for (int i = 0; NULL == face && i < 4; i++) { |
Raph Levien | a033630 | 2013-05-22 16:16:59 -0700 | [diff] [blame] | 72 | face = TypefaceImpl_createFromTypeface(family, (SkTypeface::Style)i); |
Raph Levien | 4f0064f | 2013-11-06 11:12:11 -0800 | [diff] [blame] | 73 | } |
Derek Sollenberger | fe8e21f | 2013-09-23 08:32:16 -0400 | [diff] [blame] | 74 | if (NULL == face) { |
Raph Levien | a033630 | 2013-05-22 16:16:59 -0700 | [diff] [blame] | 75 | face = TypefaceImpl_createFromName(NULL, (SkTypeface::Style)style); |
Derek Sollenberger | fe8e21f | 2013-09-23 08:32:16 -0400 | [diff] [blame] | 76 | } |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 77 | return reinterpret_cast<jlong>(face); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 78 | } |
Elliott Hughes | 4cb1753 | 2011-04-12 16:10:26 -0700 | [diff] [blame] | 79 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 80 | static void Typeface_unref(JNIEnv* env, jobject obj, jlong faceHandle) { |
| 81 | TypefaceImpl* face = reinterpret_cast<TypefaceImpl*>(faceHandle); |
Raph Levien | a033630 | 2013-05-22 16:16:59 -0700 | [diff] [blame] | 82 | TypefaceImpl_unref(face); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 83 | } |
| 84 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 85 | static jint Typeface_getStyle(JNIEnv* env, jobject obj, jlong faceHandle) { |
| 86 | TypefaceImpl* face = reinterpret_cast<TypefaceImpl*>(faceHandle); |
Raph Levien | a033630 | 2013-05-22 16:16:59 -0700 | [diff] [blame] | 87 | return TypefaceImpl_getStyle(face); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 88 | } |
| 89 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 90 | static jlong Typeface_createFromAsset(JNIEnv* env, jobject, |
| 91 | jobject jassetMgr, |
| 92 | jstring jpath) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 93 | NPE_CHECK_RETURN_ZERO(env, jassetMgr); |
| 94 | NPE_CHECK_RETURN_ZERO(env, jpath); |
Elliott Hughes | 4cb1753 | 2011-04-12 16:10:26 -0700 | [diff] [blame] | 95 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 96 | AssetManager* mgr = assetManagerForJavaObject(env, jassetMgr); |
| 97 | if (NULL == mgr) { |
| 98 | return NULL; |
| 99 | } |
Elliott Hughes | 4cb1753 | 2011-04-12 16:10:26 -0700 | [diff] [blame] | 100 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 101 | AutoJavaStringToUTF8 str(env, jpath); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 102 | Asset* asset = mgr->open(str.c_str(), Asset::ACCESS_BUFFER); |
| 103 | if (NULL == asset) { |
| 104 | return NULL; |
| 105 | } |
Elliott Hughes | 4cb1753 | 2011-04-12 16:10:26 -0700 | [diff] [blame] | 106 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 107 | return reinterpret_cast<jlong>(TypefaceImpl_createFromAsset(asset)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 108 | } |
| 109 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 110 | static jlong Typeface_createFromFile(JNIEnv* env, jobject, jstring jpath) { |
Romain Guy | a87a132 | 2009-05-12 13:22:18 -0700 | [diff] [blame] | 111 | NPE_CHECK_RETURN_ZERO(env, jpath); |
| 112 | |
| 113 | AutoJavaStringToUTF8 str(env, jpath); |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 114 | return reinterpret_cast<jlong>(TypefaceImpl_createFromFile(str.c_str())); |
Romain Guy | a87a132 | 2009-05-12 13:22:18 -0700 | [diff] [blame] | 115 | } |
| 116 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 117 | /////////////////////////////////////////////////////////////////////////////// |
| 118 | |
| 119 | static JNINativeMethod gTypefaceMethods[] = { |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 120 | { "nativeCreate", "(Ljava/lang/String;I)J", (void*)Typeface_create }, |
| 121 | { "nativeCreateFromTypeface", "(JI)J", (void*)Typeface_createFromTypeface }, |
| 122 | { "nativeUnref", "(J)V", (void*)Typeface_unref }, |
| 123 | { "nativeGetStyle", "(J)I", (void*)Typeface_getStyle }, |
| 124 | { "nativeCreateFromAsset", "(Landroid/content/res/AssetManager;Ljava/lang/String;)J", |
Romain Guy | a87a132 | 2009-05-12 13:22:18 -0700 | [diff] [blame] | 125 | (void*)Typeface_createFromAsset }, |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 126 | { "nativeCreateFromFile", "(Ljava/lang/String;)J", |
Mike Reed | dbade9d | 2009-08-25 13:20:19 -0400 | [diff] [blame] | 127 | (void*)Typeface_createFromFile }, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 128 | }; |
| 129 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 130 | int register_android_graphics_Typeface(JNIEnv* env) |
| 131 | { |
| 132 | return android::AndroidRuntime::registerNativeMethods(env, |
| 133 | "android/graphics/Typeface", |
| 134 | gTypefaceMethods, |
| 135 | SK_ARRAY_COUNT(gTypefaceMethods)); |
| 136 | } |