Home | History | Annotate | Download | only in graphics
      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 "TypefaceImpl.h"
     24 #include <android_runtime/android_util_AssetManager.h>
     25 #include <androidfw/AssetManager.h>
     26 
     27 using namespace android;
     28 
     29 static 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 
     43 static 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 
     49 static void Typeface_unref(JNIEnv* env, jobject obj, jlong faceHandle) {
     50     TypefaceImpl* face = reinterpret_cast<TypefaceImpl*>(faceHandle);
     51     TypefaceImpl_unref(face);
     52 }
     53 
     54 static jint Typeface_getStyle(JNIEnv* env, jobject obj, jlong faceHandle) {
     55     TypefaceImpl* face = reinterpret_cast<TypefaceImpl*>(faceHandle);
     56     return TypefaceImpl_getStyle(face);
     57 }
     58 
     59 static jlong Typeface_createFromArray(JNIEnv *env, jobject, jlongArray familyArray) {
     60     ScopedLongArrayRO families(env, familyArray);
     61     return reinterpret_cast<jlong>(TypefaceImpl_createFromFamilies(families.get(), families.size()));
     62 }
     63 
     64 static void Typeface_setDefault(JNIEnv *env, jobject, jlong faceHandle) {
     65     TypefaceImpl* face = reinterpret_cast<TypefaceImpl*>(faceHandle);
     66     return TypefaceImpl_setDefault(face);
     67 }
     68 
     69 ///////////////////////////////////////////////////////////////////////////////
     70 
     71 static JNINativeMethod gTypefaceMethods[] = {
     72     { "nativeCreateFromTypeface", "(JI)J", (void*)Typeface_createFromTypeface },
     73     { "nativeCreateWeightAlias",  "(JI)J", (void*)Typeface_createWeightAlias },
     74     { "nativeUnref",              "(J)V",  (void*)Typeface_unref },
     75     { "nativeGetStyle",           "(J)I",  (void*)Typeface_getStyle },
     76     { "nativeCreateFromArray",    "([J)J",
     77                                            (void*)Typeface_createFromArray },
     78     { "nativeSetDefault",         "(J)V",   (void*)Typeface_setDefault },
     79 };
     80 
     81 int register_android_graphics_Typeface(JNIEnv* env)
     82 {
     83     return RegisterMethodsOrDie(env, "android/graphics/Typeface", gTypefaceMethods,
     84                                 NELEM(gTypefaceMethods));
     85 }
     86