Raph Levien | 1a73f732 | 2014-01-30 16:06:28 -0800 | [diff] [blame] | 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" |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 20 | #include <core_jni_helpers.h> |
Raph Levien | 1a73f732 | 2014-01-30 16:06:28 -0800 | [diff] [blame] | 21 | |
Ben Wagner | f462c24 | 2015-01-12 14:26:14 -0500 | [diff] [blame] | 22 | #include "SkData.h" |
| 23 | #include "SkRefCnt.h" |
Raph Levien | 1a73f732 | 2014-01-30 16:06:28 -0800 | [diff] [blame] | 24 | #include "SkTypeface.h" |
| 25 | #include "GraphicsJNI.h" |
| 26 | #include <ScopedPrimitiveArray.h> |
| 27 | #include <ScopedUtfChars.h> |
Raph Levien | d573794 | 2014-06-01 22:52:12 -0700 | [diff] [blame] | 28 | #include <android_runtime/android_util_AssetManager.h> |
| 29 | #include <androidfw/AssetManager.h> |
| 30 | #include "Utils.h" |
Raph Levien | 1a73f732 | 2014-01-30 16:06:28 -0800 | [diff] [blame] | 31 | |
Raph Levien | 3d528c40 | 2014-06-26 09:04:54 -0700 | [diff] [blame] | 32 | #include "TypefaceImpl.h" |
Raph Levien | 1a73f732 | 2014-01-30 16:06:28 -0800 | [diff] [blame] | 33 | #include <minikin/FontFamily.h> |
| 34 | #include "MinikinSkia.h" |
Raph Levien | 1a73f732 | 2014-01-30 16:06:28 -0800 | [diff] [blame] | 35 | |
| 36 | namespace android { |
| 37 | |
Raph Levien | f9e3d31 | 2014-05-27 16:33:11 -0700 | [diff] [blame] | 38 | static jlong FontFamily_create(JNIEnv* env, jobject clazz, jstring lang, jint variant) { |
Raph Levien | f9e3d31 | 2014-05-27 16:33:11 -0700 | [diff] [blame] | 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); |
Raph Levien | 1a73f732 | 2014-01-30 16:06:28 -0800 | [diff] [blame] | 45 | } |
| 46 | |
Raph Levien | 15cf475 | 2014-05-05 16:08:07 -0700 | [diff] [blame] | 47 | static void FontFamily_unref(JNIEnv* env, jobject clazz, jlong familyPtr) { |
Raph Levien | 15cf475 | 2014-05-05 16:08:07 -0700 | [diff] [blame] | 48 | FontFamily* fontFamily = reinterpret_cast<FontFamily*>(familyPtr); |
| 49 | fontFamily->Unref(); |
Raph Levien | 1a73f732 | 2014-01-30 16:06:28 -0800 | [diff] [blame] | 50 | } |
| 51 | |
Raph Levien | d573794 | 2014-06-01 22:52:12 -0700 | [diff] [blame] | 52 | static jboolean addSkTypeface(FontFamily* family, SkTypeface* face) { |
| 53 | MinikinFont* minikinFont = new MinikinFontSkia(face); |
| 54 | bool result = family->addFont(minikinFont); |
| 55 | minikinFont->Unref(); |
| 56 | return result; |
| 57 | } |
Raph Levien | d573794 | 2014-06-01 22:52:12 -0700 | [diff] [blame] | 58 | |
Raph Levien | 1a73f732 | 2014-01-30 16:06:28 -0800 | [diff] [blame] | 59 | static jboolean FontFamily_addFont(JNIEnv* env, jobject clazz, jlong familyPtr, jstring path) { |
Raph Levien | 1a73f732 | 2014-01-30 16:06:28 -0800 | [diff] [blame] | 60 | NPE_CHECK_RETURN_ZERO(env, path); |
| 61 | ScopedUtfChars str(env, path); |
Raph Levien | 1a73f732 | 2014-01-30 16:06:28 -0800 | [diff] [blame] | 62 | SkTypeface* face = SkTypeface::CreateFromFile(str.c_str()); |
Raph Levien | 9a5b61c | 2014-04-29 18:26:48 -0700 | [diff] [blame] | 63 | if (face == NULL) { |
| 64 | ALOGE("addFont failed to create font %s", str.c_str()); |
| 65 | return false; |
| 66 | } |
Raph Levien | 117cbeb | 2014-08-25 13:47:16 -0700 | [diff] [blame] | 67 | FontFamily* fontFamily = reinterpret_cast<FontFamily*>(familyPtr); |
Raph Levien | d573794 | 2014-06-01 22:52:12 -0700 | [diff] [blame] | 68 | return addSkTypeface(fontFamily, face); |
Raph Levien | d573794 | 2014-06-01 22:52:12 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Raph Levien | 117cbeb | 2014-08-25 13:47:16 -0700 | [diff] [blame] | 71 | static jboolean FontFamily_addFontWeightStyle(JNIEnv* env, jobject clazz, jlong familyPtr, |
| 72 | jstring path, jint weight, jboolean isItalic) { |
| 73 | NPE_CHECK_RETURN_ZERO(env, path); |
| 74 | ScopedUtfChars str(env, path); |
| 75 | SkTypeface* face = SkTypeface::CreateFromFile(str.c_str()); |
| 76 | if (face == NULL) { |
| 77 | ALOGE("addFont failed to create font %s", str.c_str()); |
| 78 | return false; |
| 79 | } |
| 80 | FontFamily* fontFamily = reinterpret_cast<FontFamily*>(familyPtr); |
| 81 | MinikinFont* minikinFont = new MinikinFontSkia(face); |
| 82 | fontFamily->addFont(minikinFont, FontStyle(weight / 100, isItalic)); |
| 83 | minikinFont->Unref(); |
| 84 | return true; |
| 85 | } |
| 86 | |
Ben Wagner | f462c24 | 2015-01-12 14:26:14 -0500 | [diff] [blame] | 87 | static void releaseAsset(const void* ptr, size_t length, void* context) { |
| 88 | delete static_cast<Asset*>(context); |
| 89 | } |
| 90 | |
Raph Levien | d573794 | 2014-06-01 22:52:12 -0700 | [diff] [blame] | 91 | static jboolean FontFamily_addFontFromAsset(JNIEnv* env, jobject, jlong familyPtr, |
| 92 | jobject jassetMgr, jstring jpath) { |
Raph Levien | d573794 | 2014-06-01 22:52:12 -0700 | [diff] [blame] | 93 | NPE_CHECK_RETURN_ZERO(env, jassetMgr); |
| 94 | NPE_CHECK_RETURN_ZERO(env, jpath); |
| 95 | |
| 96 | AssetManager* mgr = assetManagerForJavaObject(env, jassetMgr); |
| 97 | if (NULL == mgr) { |
| 98 | return false; |
| 99 | } |
| 100 | |
| 101 | ScopedUtfChars str(env, jpath); |
| 102 | Asset* asset = mgr->open(str.c_str(), Asset::ACCESS_BUFFER); |
| 103 | if (NULL == asset) { |
| 104 | return false; |
| 105 | } |
| 106 | |
Ben Wagner | f462c24 | 2015-01-12 14:26:14 -0500 | [diff] [blame] | 107 | const void* buf = asset->getBuffer(false); |
| 108 | if (NULL == buf) { |
| 109 | delete asset; |
| 110 | return false; |
| 111 | } |
| 112 | |
| 113 | SkAutoTUnref<SkData> data(SkData::NewWithProc(buf, asset->getLength(), releaseAsset, asset)); |
Leon Scroggins III | 3449789 | 2015-01-20 15:52:43 -0500 | [diff] [blame] | 114 | SkMemoryStream* stream = new SkMemoryStream(data); |
| 115 | // CreateFromStream takes ownership of stream. |
Raph Levien | d573794 | 2014-06-01 22:52:12 -0700 | [diff] [blame] | 116 | SkTypeface* face = SkTypeface::CreateFromStream(stream); |
Raph Levien | d573794 | 2014-06-01 22:52:12 -0700 | [diff] [blame] | 117 | if (face == NULL) { |
| 118 | ALOGE("addFontFromAsset failed to create font %s", str.c_str()); |
| 119 | return false; |
| 120 | } |
Raph Levien | 117cbeb | 2014-08-25 13:47:16 -0700 | [diff] [blame] | 121 | FontFamily* fontFamily = reinterpret_cast<FontFamily*>(familyPtr); |
Raph Levien | d573794 | 2014-06-01 22:52:12 -0700 | [diff] [blame] | 122 | return addSkTypeface(fontFamily, face); |
Raph Levien | 1a73f732 | 2014-01-30 16:06:28 -0800 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | /////////////////////////////////////////////////////////////////////////////// |
| 126 | |
| 127 | static JNINativeMethod gFontFamilyMethods[] = { |
Raph Levien | 117cbeb | 2014-08-25 13:47:16 -0700 | [diff] [blame] | 128 | { "nCreateFamily", "(Ljava/lang/String;I)J", (void*)FontFamily_create }, |
| 129 | { "nUnrefFamily", "(J)V", (void*)FontFamily_unref }, |
| 130 | { "nAddFont", "(JLjava/lang/String;)Z", (void*)FontFamily_addFont }, |
| 131 | { "nAddFontWeightStyle", "(JLjava/lang/String;IZ)Z", (void*)FontFamily_addFontWeightStyle }, |
| 132 | { "nAddFontFromAsset", "(JLandroid/content/res/AssetManager;Ljava/lang/String;)Z", |
Raph Levien | d573794 | 2014-06-01 22:52:12 -0700 | [diff] [blame] | 133 | (void*)FontFamily_addFontFromAsset }, |
Raph Levien | 1a73f732 | 2014-01-30 16:06:28 -0800 | [diff] [blame] | 134 | }; |
| 135 | |
| 136 | int register_android_graphics_FontFamily(JNIEnv* env) |
| 137 | { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 138 | return RegisterMethodsOrDie(env, "android/graphics/FontFamily", gFontFamilyMethods, |
| 139 | NELEM(gFontFamilyMethods)); |
Raph Levien | 1a73f732 | 2014-01-30 16:06:28 -0800 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | } |