• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "graphics_jni_helpers.h"
18 #include <nativehelper/ScopedUtfChars.h>
19 
20 #include "FontUtils.h"
21 
22 #include <minikin/FontFamily.h>
23 #include <minikin/LocaleList.h>
24 
25 #include <memory>
26 
27 namespace android {
28 
29 namespace {
30 struct NativeFamilyBuilder {
31     std::vector<std::shared_ptr<minikin::Font>> fonts;
32 };
33 }  // namespace
34 
toBuilder(jlong ptr)35 static inline NativeFamilyBuilder* toBuilder(jlong ptr) {
36     return reinterpret_cast<NativeFamilyBuilder*>(ptr);
37 }
38 
toFontWrapper(jlong ptr)39 static inline FontWrapper* toFontWrapper(jlong ptr) {
40     return reinterpret_cast<FontWrapper*>(ptr);
41 }
42 
releaseFontFamily(jlong family)43 static void releaseFontFamily(jlong family) {
44     delete reinterpret_cast<FontFamilyWrapper*>(family);
45 }
46 
47 // Regular JNI
FontFamily_Builder_initBuilder(JNIEnv *,jobject)48 static jlong FontFamily_Builder_initBuilder(JNIEnv*, jobject) {
49     return reinterpret_cast<jlong>(new NativeFamilyBuilder());
50 }
51 
52 // Critical Native
FontFamily_Builder_addFont(CRITICAL_JNI_PARAMS_COMMA jlong builderPtr,jlong fontPtr)53 static void FontFamily_Builder_addFont(CRITICAL_JNI_PARAMS_COMMA jlong builderPtr, jlong fontPtr) {
54     toBuilder(builderPtr)->fonts.push_back(toFontWrapper(fontPtr)->font);
55 }
56 
57 // Regular JNI
FontFamily_Builder_build(JNIEnv * env,jobject clazz,jlong builderPtr,jstring langTags,jint variant,jboolean isCustomFallback,jboolean isDefaultFallback,jint variationFamilyType)58 static jlong FontFamily_Builder_build(JNIEnv* env, jobject clazz, jlong builderPtr,
59                                       jstring langTags, jint variant, jboolean isCustomFallback,
60                                       jboolean isDefaultFallback, jint variationFamilyType) {
61     std::unique_ptr<NativeFamilyBuilder> builder(toBuilder(builderPtr));
62     uint32_t localeId;
63     if (langTags == nullptr) {
64         localeId = minikin::registerLocaleList("");
65     } else {
66         ScopedUtfChars str(env, langTags);
67         localeId = minikin::registerLocaleList(str.c_str());
68     }
69     std::shared_ptr<minikin::FontFamily> family = minikin::FontFamily::create(
70             localeId, static_cast<minikin::FamilyVariant>(variant), std::move(builder->fonts),
71             isCustomFallback, isDefaultFallback,
72             static_cast<minikin::VariationFamilyType>(variationFamilyType));
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;IZZI)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