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