FontFamily.cpp revision 15cf4757dc0099301662f8a26da561434cc07cfa
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) {
35#ifdef USE_MINIKIN
36    return (jlong)new FontFamily();
37#else
38    return 0;
39#endif
40}
41
42static void FontFamily_unref(JNIEnv* env, jobject clazz, jlong familyPtr) {
43#ifdef USE_MINIKIN
44    FontFamily* fontFamily = reinterpret_cast<FontFamily*>(familyPtr);
45    fontFamily->Unref();
46#endif
47}
48
49static jboolean FontFamily_addFont(JNIEnv* env, jobject clazz, jlong familyPtr, jstring path) {
50#ifdef USE_MINIKIN
51    NPE_CHECK_RETURN_ZERO(env, path);
52    ScopedUtfChars str(env, path);
53    ALOGD("addFont %s", str.c_str());
54    SkTypeface* face = SkTypeface::CreateFromFile(str.c_str());
55    if (face == NULL) {
56        ALOGE("addFont failed to create font %s", str.c_str());
57        return false;
58    }
59    MinikinFont* minikinFont = new MinikinFontSkia(face);
60    FontFamily* fontFamily = (FontFamily*)familyPtr;
61    return fontFamily->addFont(minikinFont);
62#else
63    return false;
64#endif
65}
66
67///////////////////////////////////////////////////////////////////////////////
68
69static JNINativeMethod gFontFamilyMethods[] = {
70    { "nCreateFamily",            "()J", (void*)FontFamily_create },
71    { "nUnrefFamily",             "(J)V", (void*)FontFamily_unref },
72    { "nAddFont",                 "(JLjava/lang/String;)Z", (void*)FontFamily_addFont },
73};
74
75int register_android_graphics_FontFamily(JNIEnv* env)
76{
77    return android::AndroidRuntime::registerNativeMethods(env,
78        "android/graphics/FontFamily",
79        gFontFamilyMethods, NELEM(gFontFamilyMethods));
80}
81
82}
83