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