FontFamily.cpp revision f9e3d311275c37fe5f2562993687a1627780a6d0
1/*
2 * Copyright (C) 2014 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#define LOG_TAG "Minikin"
18
19#include "JNIHelp.h"
20#include <android_runtime/AndroidRuntime.h>
21
22#include "SkTypeface.h"
23#include "GraphicsJNI.h"
24#include <ScopedPrimitiveArray.h>
25#include <ScopedUtfChars.h>
26
27#ifdef USE_MINIKIN
28#include <minikin/FontFamily.h>
29#include "MinikinSkia.h"
30#endif
31
32namespace android {
33
34static jlong FontFamily_create(JNIEnv* env, jobject clazz, jstring lang, jint variant) {
35#ifdef USE_MINIKIN
36    FontLanguage fontLanguage;
37    if (lang != NULL) {
38        ScopedUtfChars str(env, lang);
39        fontLanguage = FontLanguage(str.c_str(), str.size());
40    }
41    return (jlong)new FontFamily(fontLanguage, variant);
42#else
43    return 0;
44#endif
45}
46
47static void FontFamily_unref(JNIEnv* env, jobject clazz, jlong familyPtr) {
48#ifdef USE_MINIKIN
49    FontFamily* fontFamily = reinterpret_cast<FontFamily*>(familyPtr);
50    fontFamily->Unref();
51#endif
52}
53
54static jboolean FontFamily_addFont(JNIEnv* env, jobject clazz, jlong familyPtr, jstring path) {
55#ifdef USE_MINIKIN
56    NPE_CHECK_RETURN_ZERO(env, path);
57    ScopedUtfChars str(env, path);
58    ALOGD("addFont %s", str.c_str());
59    SkTypeface* face = SkTypeface::CreateFromFile(str.c_str());
60    if (face == NULL) {
61        ALOGE("addFont failed to create font %s", str.c_str());
62        return false;
63    }
64    MinikinFont* minikinFont = new MinikinFontSkia(face);
65    FontFamily* fontFamily = (FontFamily*)familyPtr;
66    return fontFamily->addFont(minikinFont);
67#else
68    return false;
69#endif
70}
71
72///////////////////////////////////////////////////////////////////////////////
73
74static JNINativeMethod gFontFamilyMethods[] = {
75    { "nCreateFamily",            "(Ljava/lang/String;I)J", (void*)FontFamily_create },
76    { "nUnrefFamily",             "(J)V", (void*)FontFamily_unref },
77    { "nAddFont",                 "(JLjava/lang/String;)Z", (void*)FontFamily_addFont },
78};
79
80int register_android_graphics_FontFamily(JNIEnv* env)
81{
82    return android::AndroidRuntime::registerNativeMethods(env,
83        "android/graphics/FontFamily",
84        gFontFamilyMethods, NELEM(gFontFamilyMethods));
85}
86
87}
88