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/Typeface.h>
26
27using namespace android;
28
29static jlong Typeface_createFromTypeface(JNIEnv* env, jobject, jlong familyHandle, jint style) {
30    Typeface* family = reinterpret_cast<Typeface*>(familyHandle);
31    Typeface* face = Typeface::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 = Typeface::createFromTypeface(family, (SkTypeface::Style)(style ^ SkTypeface::kItalic));
36    }
37    for (int i = 0; NULL == face && i < 4; i++) {
38        face = Typeface::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    Typeface* family = reinterpret_cast<Typeface*>(familyHandle);
45    Typeface* face = Typeface::createWeightAlias(family, weight);
46    return reinterpret_cast<jlong>(face);
47}
48
49static void Typeface_unref(JNIEnv* env, jobject obj, jlong faceHandle) {
50    Typeface* face = reinterpret_cast<Typeface*>(faceHandle);
51    if (face != NULL) {
52        face->unref();
53    }
54}
55
56static jint Typeface_getStyle(JNIEnv* env, jobject obj, jlong faceHandle) {
57    Typeface* face = reinterpret_cast<Typeface*>(faceHandle);
58    return face->fSkiaStyle;
59}
60
61static jlong Typeface_createFromArray(JNIEnv *env, jobject, jlongArray familyArray) {
62    ScopedLongArrayRO families(env, familyArray);
63    std::vector<FontFamily*> familyVec;
64    for (size_t i = 0; i < families.size(); i++) {
65        FontFamily* family = reinterpret_cast<FontFamily*>(families[i]);
66        familyVec.push_back(family);
67    }
68    return reinterpret_cast<jlong>(Typeface::createFromFamilies(familyVec));
69}
70
71static void Typeface_setDefault(JNIEnv *env, jobject, jlong faceHandle) {
72    Typeface* face = reinterpret_cast<Typeface*>(faceHandle);
73    return Typeface::setDefault(face);
74}
75
76///////////////////////////////////////////////////////////////////////////////
77
78static const JNINativeMethod gTypefaceMethods[] = {
79    { "nativeCreateFromTypeface", "(JI)J", (void*)Typeface_createFromTypeface },
80    { "nativeCreateWeightAlias",  "(JI)J", (void*)Typeface_createWeightAlias },
81    { "nativeUnref",              "(J)V",  (void*)Typeface_unref },
82    { "nativeGetStyle",           "(J)I",  (void*)Typeface_getStyle },
83    { "nativeCreateFromArray",    "([J)J",
84                                           (void*)Typeface_createFromArray },
85    { "nativeSetDefault",         "(J)V",   (void*)Typeface_setDefault },
86};
87
88int register_android_graphics_Typeface(JNIEnv* env)
89{
90    return RegisterMethodsOrDie(env, "android/graphics/Typeface", gTypefaceMethods,
91                                NELEM(gTypefaceMethods));
92}
93