FontFamily.cpp revision d573794d83a049fe59e289944f0cd77406dd776a
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#ifdef USE_MINIKIN 31#include <minikin/FontFamily.h> 32#include "MinikinSkia.h" 33#endif 34 35namespace android { 36 37static jlong FontFamily_create(JNIEnv* env, jobject clazz, jstring lang, jint variant) { 38#ifdef USE_MINIKIN 39 FontLanguage fontLanguage; 40 if (lang != NULL) { 41 ScopedUtfChars str(env, lang); 42 fontLanguage = FontLanguage(str.c_str(), str.size()); 43 } 44 return (jlong)new FontFamily(fontLanguage, variant); 45#else 46 return 0; 47#endif 48} 49 50static void FontFamily_unref(JNIEnv* env, jobject clazz, jlong familyPtr) { 51#ifdef USE_MINIKIN 52 FontFamily* fontFamily = reinterpret_cast<FontFamily*>(familyPtr); 53 fontFamily->Unref(); 54#endif 55} 56 57#ifdef USE_MINIKIN 58static jboolean addSkTypeface(FontFamily* family, SkTypeface* face) { 59 MinikinFont* minikinFont = new MinikinFontSkia(face); 60 bool result = family->addFont(minikinFont); 61 minikinFont->Unref(); 62 return result; 63} 64#endif 65 66static jboolean FontFamily_addFont(JNIEnv* env, jobject clazz, jlong familyPtr, jstring path) { 67#ifdef USE_MINIKIN 68 NPE_CHECK_RETURN_ZERO(env, path); 69 ScopedUtfChars str(env, path); 70 SkTypeface* face = SkTypeface::CreateFromFile(str.c_str()); 71 if (face == NULL) { 72 ALOGE("addFont failed to create font %s", str.c_str()); 73 return false; 74 } 75 FontFamily* fontFamily = (FontFamily*)familyPtr; 76 return addSkTypeface(fontFamily, face); 77#else 78 return false; 79#endif 80} 81 82static jboolean FontFamily_addFontFromAsset(JNIEnv* env, jobject, jlong familyPtr, 83 jobject jassetMgr, jstring jpath) { 84#ifdef USE_MINIKIN 85 NPE_CHECK_RETURN_ZERO(env, jassetMgr); 86 NPE_CHECK_RETURN_ZERO(env, jpath); 87 88 AssetManager* mgr = assetManagerForJavaObject(env, jassetMgr); 89 if (NULL == mgr) { 90 return false; 91 } 92 93 ScopedUtfChars str(env, jpath); 94 Asset* asset = mgr->open(str.c_str(), Asset::ACCESS_BUFFER); 95 if (NULL == asset) { 96 return false; 97 } 98 99 SkStream* stream = new AssetStreamAdaptor(asset, 100 AssetStreamAdaptor::kYes_OwnAsset, 101 AssetStreamAdaptor::kYes_HasMemoryBase); 102 SkTypeface* face = SkTypeface::CreateFromStream(stream); 103 // Note: SkTypeface::CreateFromStream holds its own reference to the stream 104 stream->unref(); 105 if (face == NULL) { 106 ALOGE("addFontFromAsset failed to create font %s", str.c_str()); 107 return false; 108 } 109 FontFamily* fontFamily = (FontFamily*)familyPtr; 110 return addSkTypeface(fontFamily, face); 111#else 112 return false; 113#endif 114} 115 116/////////////////////////////////////////////////////////////////////////////// 117 118static JNINativeMethod gFontFamilyMethods[] = { 119 { "nCreateFamily", "(Ljava/lang/String;I)J", (void*)FontFamily_create }, 120 { "nUnrefFamily", "(J)V", (void*)FontFamily_unref }, 121 { "nAddFont", "(JLjava/lang/String;)Z", (void*)FontFamily_addFont }, 122 { "nAddFontFromAsset", "(JLandroid/content/res/AssetManager;Ljava/lang/String;)Z", 123 (void*)FontFamily_addFontFromAsset }, 124}; 125 126int register_android_graphics_FontFamily(JNIEnv* env) 127{ 128 return android::AndroidRuntime::registerNativeMethods(env, 129 "android/graphics/FontFamily", 130 gFontFamilyMethods, NELEM(gFontFamilyMethods)); 131} 132 133} 134