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