FontFamily.cpp revision 3660789f06c5fbcb81e6c7c79612048bff8f0f66
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#include <android_runtime/android_util_AssetManager.h> 27#include <androidfw/AssetManager.h> 28#include "Utils.h" 29 30#include <minikin/FontFamily.h> 31#include "MinikinSkia.h" 32 33namespace android { 34 35static jlong FontFamily_create(JNIEnv* env, jobject clazz, jstring lang, jint variant) { 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} 43 44static void FontFamily_unref(JNIEnv* env, jobject clazz, jlong familyPtr) { 45 FontFamily* fontFamily = reinterpret_cast<FontFamily*>(familyPtr); 46 fontFamily->Unref(); 47} 48 49static jboolean addSkTypeface(FontFamily* family, SkTypeface* face) { 50 MinikinFont* minikinFont = new MinikinFontSkia(face); 51 bool result = family->addFont(minikinFont); 52 minikinFont->Unref(); 53 return result; 54} 55 56static jboolean FontFamily_addFont(JNIEnv* env, jobject clazz, jlong familyPtr, jstring path) { 57 NPE_CHECK_RETURN_ZERO(env, path); 58 ScopedUtfChars str(env, path); 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 FontFamily* fontFamily = (FontFamily*)familyPtr; 65 return addSkTypeface(fontFamily, face); 66} 67 68static jboolean FontFamily_addFontFromAsset(JNIEnv* env, jobject, jlong familyPtr, 69 jobject jassetMgr, jstring jpath) { 70 NPE_CHECK_RETURN_ZERO(env, jassetMgr); 71 NPE_CHECK_RETURN_ZERO(env, jpath); 72 73 AssetManager* mgr = assetManagerForJavaObject(env, jassetMgr); 74 if (NULL == mgr) { 75 return false; 76 } 77 78 ScopedUtfChars str(env, jpath); 79 Asset* asset = mgr->open(str.c_str(), Asset::ACCESS_BUFFER); 80 if (NULL == asset) { 81 return false; 82 } 83 84 SkStream* stream = new AssetStreamAdaptor(asset, 85 AssetStreamAdaptor::kYes_OwnAsset, 86 AssetStreamAdaptor::kYes_HasMemoryBase); 87 SkTypeface* face = SkTypeface::CreateFromStream(stream); 88 // Note: SkTypeface::CreateFromStream holds its own reference to the stream 89 stream->unref(); 90 if (face == NULL) { 91 ALOGE("addFontFromAsset failed to create font %s", str.c_str()); 92 return false; 93 } 94 FontFamily* fontFamily = (FontFamily*)familyPtr; 95 return addSkTypeface(fontFamily, face); 96} 97 98/////////////////////////////////////////////////////////////////////////////// 99 100static JNINativeMethod gFontFamilyMethods[] = { 101 { "nCreateFamily", "(Ljava/lang/String;I)J", (void*)FontFamily_create }, 102 { "nUnrefFamily", "(J)V", (void*)FontFamily_unref }, 103 { "nAddFont", "(JLjava/lang/String;)Z", (void*)FontFamily_addFont }, 104 { "nAddFontFromAsset", "(JLandroid/content/res/AssetManager;Ljava/lang/String;)Z", 105 (void*)FontFamily_addFontFromAsset }, 106}; 107 108int register_android_graphics_FontFamily(JNIEnv* env) 109{ 110 return android::AndroidRuntime::registerNativeMethods(env, 111 "android/graphics/FontFamily", 112 gFontFamilyMethods, NELEM(gFontFamilyMethods)); 113} 114 115} 116