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
28 using namespace android;
29
Typeface_createFromTypeface(JNIEnv * env,jobject,jlong familyHandle,jint style)30 static jlong Typeface_createFromTypeface(JNIEnv* env, jobject, jlong familyHandle, jint style) {
31 TypefaceImpl* family = reinterpret_cast<TypefaceImpl*>(familyHandle);
32 TypefaceImpl* face = TypefaceImpl_createFromTypeface(family, (SkTypeface::Style)style);
33 // TODO: the following logic shouldn't be necessary, the above should always succeed.
34 // Try to find the closest matching font, using the standard heuristic
35 if (NULL == face) {
36 face = TypefaceImpl_createFromTypeface(family, (SkTypeface::Style)(style ^ SkTypeface::kItalic));
37 }
38 for (int i = 0; NULL == face && i < 4; i++) {
39 face = TypefaceImpl_createFromTypeface(family, (SkTypeface::Style)i);
40 }
41 return reinterpret_cast<jlong>(face);
42 }
43
Typeface_createWeightAlias(JNIEnv * env,jobject,jlong familyHandle,jint weight)44 static jlong Typeface_createWeightAlias(JNIEnv* env, jobject, jlong familyHandle, jint weight) {
45 TypefaceImpl* family = reinterpret_cast<TypefaceImpl*>(familyHandle);
46 TypefaceImpl* face = TypefaceImpl_createWeightAlias(family, weight);
47 return reinterpret_cast<jlong>(face);
48 }
49
Typeface_unref(JNIEnv * env,jobject obj,jlong faceHandle)50 static void Typeface_unref(JNIEnv* env, jobject obj, jlong faceHandle) {
51 TypefaceImpl* face = reinterpret_cast<TypefaceImpl*>(faceHandle);
52 TypefaceImpl_unref(face);
53 }
54
Typeface_getStyle(JNIEnv * env,jobject obj,jlong faceHandle)55 static jint Typeface_getStyle(JNIEnv* env, jobject obj, jlong faceHandle) {
56 TypefaceImpl* face = reinterpret_cast<TypefaceImpl*>(faceHandle);
57 return TypefaceImpl_getStyle(face);
58 }
59
Typeface_createFromArray(JNIEnv * env,jobject,jlongArray familyArray)60 static jlong Typeface_createFromArray(JNIEnv *env, jobject, jlongArray familyArray) {
61 ScopedLongArrayRO families(env, familyArray);
62 return reinterpret_cast<jlong>(TypefaceImpl_createFromFamilies(families.get(), families.size()));
63 }
64
Typeface_setDefault(JNIEnv * env,jobject,jlong faceHandle)65 static void Typeface_setDefault(JNIEnv *env, jobject, jlong faceHandle) {
66 TypefaceImpl* face = reinterpret_cast<TypefaceImpl*>(faceHandle);
67 return TypefaceImpl_setDefault(face);
68 }
69
70 ///////////////////////////////////////////////////////////////////////////////
71
72 static JNINativeMethod gTypefaceMethods[] = {
73 { "nativeCreateFromTypeface", "(JI)J", (void*)Typeface_createFromTypeface },
74 { "nativeCreateWeightAlias", "(JI)J", (void*)Typeface_createWeightAlias },
75 { "nativeUnref", "(J)V", (void*)Typeface_unref },
76 { "nativeGetStyle", "(J)I", (void*)Typeface_getStyle },
77 { "nativeCreateFromArray", "([J)J",
78 (void*)Typeface_createFromArray },
79 { "nativeSetDefault", "(J)V", (void*)Typeface_setDefault },
80 };
81
register_android_graphics_Typeface(JNIEnv * env)82 int register_android_graphics_Typeface(JNIEnv* env)
83 {
84 return android::AndroidRuntime::registerNativeMethods(env,
85 "android/graphics/Typeface",
86 gTypefaceMethods,
87 SK_ARRAY_COUNT(gTypefaceMethods));
88 }
89