1 /*
2  * Copyright (C) 2018 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 #undef LOG_TAG
18 #define LOG_TAG "Minikin"
19 
20 #include "graphics_jni_helpers.h"
21 #include <nativehelper/ScopedUtfChars.h>
22 
23 #include "FontUtils.h"
24 
25 #include <minikin/FontFamily.h>
26 #include <minikin/LocaleList.h>
27 
28 #include <memory>
29 
30 namespace android {
31 
32 struct NativeFamilyBuilder {
33     std::vector<std::shared_ptr<minikin::Font>> fonts;
34 };
35 
toBuilder(jlong ptr)36 static inline NativeFamilyBuilder* toBuilder(jlong ptr) {
37     return reinterpret_cast<NativeFamilyBuilder*>(ptr);
38 }
39 
toFontWrapper(jlong ptr)40 static inline FontWrapper* toFontWrapper(jlong ptr) {
41     return reinterpret_cast<FontWrapper*>(ptr);
42 }
43 
releaseFontFamily(jlong family)44 static void releaseFontFamily(jlong family) {
45     delete reinterpret_cast<FontFamilyWrapper*>(family);
46 }
47 
48 // Regular JNI
FontFamily_Builder_initBuilder(JNIEnv *,jobject)49 static jlong FontFamily_Builder_initBuilder(JNIEnv*, jobject) {
50     return reinterpret_cast<jlong>(new NativeFamilyBuilder());
51 }
52 
53 // Critical Native
FontFamily_Builder_addFont(CRITICAL_JNI_PARAMS_COMMA jlong builderPtr,jlong fontPtr)54 static void FontFamily_Builder_addFont(CRITICAL_JNI_PARAMS_COMMA jlong builderPtr, jlong fontPtr) {
55     toBuilder(builderPtr)->fonts.push_back(toFontWrapper(fontPtr)->font);
56 }
57 
58 // Regular JNI
FontFamily_Builder_build(JNIEnv * env,jobject clazz,jlong builderPtr,jstring langTags,jint variant,jboolean isCustomFallback,jboolean isDefaultFallback)59 static jlong FontFamily_Builder_build(JNIEnv* env, jobject clazz, jlong builderPtr,
60                                       jstring langTags, jint variant, jboolean isCustomFallback,
61                                       jboolean isDefaultFallback) {
62     std::unique_ptr<NativeFamilyBuilder> builder(toBuilder(builderPtr));
63     uint32_t localeId;
64     if (langTags == nullptr) {
65         localeId = minikin::registerLocaleList("");
66     } else {
67         ScopedUtfChars str(env, langTags);
68         localeId = minikin::registerLocaleList(str.c_str());
69     }
70     std::shared_ptr<minikin::FontFamily> family = minikin::FontFamily::create(
71             localeId, static_cast<minikin::FamilyVariant>(variant), std::move(builder->fonts),
72             isCustomFallback, isDefaultFallback);
73     if (family->getCoverage().length() == 0) {
74         // No coverage means minikin rejected given font for some reasons.
75         jniThrowException(env, "java/lang/IllegalArgumentException",
76                           "Failed to create internal object. maybe invalid font data");
77         return 0;
78     }
79     return reinterpret_cast<jlong>(new FontFamilyWrapper(std::move(family)));
80 }
81 
82 // CriticalNative
FontFamily_Builder_GetReleaseFunc(CRITICAL_JNI_PARAMS)83 static jlong FontFamily_Builder_GetReleaseFunc(CRITICAL_JNI_PARAMS) {
84     return reinterpret_cast<jlong>(releaseFontFamily);
85 }
86 
87 // FastNative
FontFamily_getLangTags(JNIEnv * env,jobject,jlong familyPtr)88 static jstring FontFamily_getLangTags(JNIEnv* env, jobject, jlong familyPtr) {
89     FontFamilyWrapper* family = reinterpret_cast<FontFamilyWrapper*>(familyPtr);
90     uint32_t localeListId = family->family->localeListId();
91     if (localeListId == 0) {
92         return nullptr;
93     }
94     std::string langTags = minikin::getLocaleString(localeListId);
95     return env->NewStringUTF(langTags.c_str());
96 }
97 
98 // CriticalNative
FontFamily_getVariant(CRITICAL_JNI_PARAMS_COMMA jlong familyPtr)99 static jint FontFamily_getVariant(CRITICAL_JNI_PARAMS_COMMA jlong familyPtr) {
100     FontFamilyWrapper* family = reinterpret_cast<FontFamilyWrapper*>(familyPtr);
101     return static_cast<jint>(family->family->variant());
102 }
103 
104 // CriticalNative
FontFamily_getFontSize(CRITICAL_JNI_PARAMS_COMMA jlong familyPtr)105 static jint FontFamily_getFontSize(CRITICAL_JNI_PARAMS_COMMA jlong familyPtr) {
106     FontFamilyWrapper* family = reinterpret_cast<FontFamilyWrapper*>(familyPtr);
107     return family->family->getNumFonts();
108 }
109 
110 // CriticalNative
FontFamily_getFont(CRITICAL_JNI_PARAMS_COMMA jlong familyPtr,jint index)111 static jlong FontFamily_getFont(CRITICAL_JNI_PARAMS_COMMA jlong familyPtr, jint index) {
112     FontFamilyWrapper* family = reinterpret_cast<FontFamilyWrapper*>(familyPtr);
113     std::shared_ptr<minikin::Font> font = family->family->getFontRef(index);
114     return reinterpret_cast<jlong>(new FontWrapper(std::move(font)));
115 }
116 
117 ///////////////////////////////////////////////////////////////////////////////
118 
119 static const JNINativeMethod gFontFamilyBuilderMethods[] = {
120         {"nInitBuilder", "()J", (void*)FontFamily_Builder_initBuilder},
121         {"nAddFont", "(JJ)V", (void*)FontFamily_Builder_addFont},
122         {"nBuild", "(JLjava/lang/String;IZZ)J", (void*)FontFamily_Builder_build},
123         {"nGetReleaseNativeFamily", "()J", (void*)FontFamily_Builder_GetReleaseFunc},
124 };
125 
126 static const JNINativeMethod gFontFamilyMethods[] = {
127         {"nGetFontSize", "(J)I", (void*)FontFamily_getFontSize},
128         {"nGetFont", "(JI)J", (void*)FontFamily_getFont},
129         {"nGetLangTags", "(J)Ljava/lang/String;", (void*)FontFamily_getLangTags},
130         {"nGetVariant", "(J)I", (void*)FontFamily_getVariant},
131 };
132 
register_android_graphics_fonts_FontFamily(JNIEnv * env)133 int register_android_graphics_fonts_FontFamily(JNIEnv* env) {
134     return RegisterMethodsOrDie(env, "android/graphics/fonts/FontFamily$Builder",
135                                 gFontFamilyBuilderMethods, NELEM(gFontFamilyBuilderMethods)) +
136            RegisterMethodsOrDie(env, "android/graphics/fonts/FontFamily", gFontFamilyMethods,
137                                 NELEM(gFontFamilyMethods));
138 }
139 
140 }
141