Typeface.cpp revision 3660789f06c5fbcb81e6c7c79612048bff8f0f66
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 30static 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 44static void Typeface_unref(JNIEnv* env, jobject obj, jlong faceHandle) { 45 TypefaceImpl* face = reinterpret_cast<TypefaceImpl*>(faceHandle); 46 TypefaceImpl_unref(face); 47} 48 49static jint Typeface_getStyle(JNIEnv* env, jobject obj, jlong faceHandle) { 50 TypefaceImpl* face = reinterpret_cast<TypefaceImpl*>(faceHandle); 51 return TypefaceImpl_getStyle(face); 52} 53 54static jlong Typeface_createFromArray(JNIEnv *env, jobject, jlongArray familyArray) { 55 ScopedLongArrayRO families(env, familyArray); 56 return reinterpret_cast<jlong>(TypefaceImpl_createFromFamilies(families.get(), families.size())); 57} 58 59static void Typeface_setDefault(JNIEnv *env, jobject, jlong faceHandle) { 60 TypefaceImpl* face = reinterpret_cast<TypefaceImpl*>(faceHandle); 61 return TypefaceImpl_setDefault(face); 62} 63 64/////////////////////////////////////////////////////////////////////////////// 65 66static JNINativeMethod gTypefaceMethods[] = { 67 { "nativeCreateFromTypeface", "(JI)J", (void*)Typeface_createFromTypeface }, 68 { "nativeUnref", "(J)V", (void*)Typeface_unref }, 69 { "nativeGetStyle", "(J)I", (void*)Typeface_getStyle }, 70 { "nativeCreateFromArray", "([J)J", 71 (void*)Typeface_createFromArray }, 72 { "nativeSetDefault", "(J)V", (void*)Typeface_setDefault }, 73}; 74 75int register_android_graphics_Typeface(JNIEnv* env) 76{ 77 return android::AndroidRuntime::registerNativeMethods(env, 78 "android/graphics/Typeface", 79 gTypefaceMethods, 80 SK_ARRAY_COUNT(gTypefaceMethods)); 81} 82