Typeface.cpp revision 1a73f732f91e97c9c66b808c245ddda36a10e987
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
17#include "jni.h"
18#include <android_runtime/AndroidRuntime.h>
19
20#include "GraphicsJNI.h"
21#include <ScopedPrimitiveArray.h>
22#include "SkStream.h"
23#include "SkTypeface.h"
24#include "TypefaceImpl.h"
25#include <android_runtime/android_util_AssetManager.h>
26#include <androidfw/AssetManager.h>
27
28using namespace android;
29
30class AutoJavaStringToUTF8 {
31public:
32    AutoJavaStringToUTF8(JNIEnv* env, jstring str) : fEnv(env), fJStr(str)
33    {
34        fCStr = env->GetStringUTFChars(str, NULL);
35    }
36    ~AutoJavaStringToUTF8()
37    {
38        fEnv->ReleaseStringUTFChars(fJStr, fCStr);
39    }
40    const char* c_str() const { return fCStr; }
41
42private:
43    JNIEnv*     fEnv;
44    jstring     fJStr;
45    const char* fCStr;
46};
47
48static jlong Typeface_create(JNIEnv* env, jobject, jstring name,
49                             jint styleHandle) {
50    SkTypeface::Style style = static_cast<SkTypeface::Style>(styleHandle);
51    TypefaceImpl* face = NULL;
52
53    if (NULL != name) {
54        AutoJavaStringToUTF8    str(env, name);
55        face = TypefaceImpl_createFromName(str.c_str(), style);
56    }
57
58    // return the default font at the best style if no exact match exists
59    if (NULL == face) {
60        face = TypefaceImpl_createFromName(NULL, style);
61    }
62    return reinterpret_cast<jlong>(face);
63}
64
65static jlong Typeface_createFromTypeface(JNIEnv* env, jobject, jlong familyHandle, jint style) {
66    TypefaceImpl* family = reinterpret_cast<TypefaceImpl*>(familyHandle);
67    TypefaceImpl* face = TypefaceImpl_createFromTypeface(family, (SkTypeface::Style)style);
68    // Try to find the closest matching font, using the standard heuristic
69    if (NULL == face) {
70        face = TypefaceImpl_createFromTypeface(family, (SkTypeface::Style)(style ^ SkTypeface::kItalic));
71    }
72    for (int i = 0; NULL == face && i < 4; i++) {
73        face = TypefaceImpl_createFromTypeface(family, (SkTypeface::Style)i);
74    }
75    if (NULL == face) {
76        face = TypefaceImpl_createFromName(NULL, (SkTypeface::Style)style);
77    }
78    return reinterpret_cast<jlong>(face);
79}
80
81static void Typeface_unref(JNIEnv* env, jobject obj, jlong faceHandle) {
82    TypefaceImpl* face = reinterpret_cast<TypefaceImpl*>(faceHandle);
83    TypefaceImpl_unref(face);
84}
85
86static jint Typeface_getStyle(JNIEnv* env, jobject obj, jlong faceHandle) {
87    TypefaceImpl* face = reinterpret_cast<TypefaceImpl*>(faceHandle);
88    return TypefaceImpl_getStyle(face);
89}
90
91static jlong Typeface_createFromAsset(JNIEnv* env, jobject,
92                                      jobject jassetMgr,
93                                      jstring jpath) {
94    NPE_CHECK_RETURN_ZERO(env, jassetMgr);
95    NPE_CHECK_RETURN_ZERO(env, jpath);
96
97    AssetManager* mgr = assetManagerForJavaObject(env, jassetMgr);
98    if (NULL == mgr) {
99        return NULL;
100    }
101
102    AutoJavaStringToUTF8 str(env, jpath);
103    Asset* asset = mgr->open(str.c_str(), Asset::ACCESS_BUFFER);
104    if (NULL == asset) {
105        return NULL;
106    }
107
108    return reinterpret_cast<jlong>(TypefaceImpl_createFromAsset(asset));
109}
110
111static jlong Typeface_createFromFile(JNIEnv* env, jobject, jstring jpath) {
112    NPE_CHECK_RETURN_ZERO(env, jpath);
113
114    AutoJavaStringToUTF8 str(env, jpath);
115    return reinterpret_cast<jlong>(TypefaceImpl_createFromFile(str.c_str()));
116}
117
118static jlong Typeface_createFromArray(JNIEnv *env, jobject, jlongArray familyArray) {
119    ScopedLongArrayRO families(env, familyArray);
120    return reinterpret_cast<jlong>(TypefaceImpl_createFromFamilies(families.get(), families.size()));
121}
122
123///////////////////////////////////////////////////////////////////////////////
124
125static JNINativeMethod gTypefaceMethods[] = {
126    { "nativeCreate",        "(Ljava/lang/String;I)J", (void*)Typeface_create },
127    { "nativeCreateFromTypeface", "(JI)J", (void*)Typeface_createFromTypeface },
128    { "nativeUnref",              "(J)V",  (void*)Typeface_unref },
129    { "nativeGetStyle",           "(J)I",  (void*)Typeface_getStyle },
130    { "nativeCreateFromAsset",    "(Landroid/content/res/AssetManager;Ljava/lang/String;)J",
131                                           (void*)Typeface_createFromAsset },
132    { "nativeCreateFromFile",     "(Ljava/lang/String;)J",
133                                           (void*)Typeface_createFromFile },
134    { "nativeCreateFromArray",    "([J)J",
135                                           (void*)Typeface_createFromArray },
136};
137
138int register_android_graphics_Typeface(JNIEnv* env)
139{
140    return android::AndroidRuntime::registerNativeMethods(env,
141                                                       "android/graphics/Typeface",
142                                                       gTypefaceMethods,
143                                                       SK_ARRAY_COUNT(gTypefaceMethods));
144}
145