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)59 static jlong FontFamily_Builder_build(JNIEnv* env, jobject clazz, jlong builderPtr,
60 jstring langTags, jint variant, jboolean isCustomFallback) {
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 = std::make_shared<minikin::FontFamily>(
70 localeId, static_cast<minikin::FamilyVariant>(variant), std::move(builder->fonts),
71 isCustomFallback);
72 if (family->getCoverage().length() == 0) {
73 // No coverage means minikin rejected given font for some reasons.
74 jniThrowException(env, "java/lang/IllegalArgumentException",
75 "Failed to create internal object. maybe invalid font data");
76 return 0;
77 }
78 return reinterpret_cast<jlong>(new FontFamilyWrapper(std::move(family)));
79 }
80
81 // CriticalNative
FontFamily_Builder_GetReleaseFunc(CRITICAL_JNI_PARAMS)82 static jlong FontFamily_Builder_GetReleaseFunc(CRITICAL_JNI_PARAMS) {
83 return reinterpret_cast<jlong>(releaseFontFamily);
84 }
85
86 // FastNative
FontFamily_getLangTags(JNIEnv * env,jobject,jlong familyPtr)87 static jstring FontFamily_getLangTags(JNIEnv* env, jobject, jlong familyPtr) {
88 FontFamilyWrapper* family = reinterpret_cast<FontFamilyWrapper*>(familyPtr);
89 uint32_t localeListId = family->family->localeListId();
90 if (localeListId == 0) {
91 return nullptr;
92 }
93 std::string langTags = minikin::getLocaleString(localeListId);
94 return env->NewStringUTF(langTags.c_str());
95 }
96
97 // CriticalNative
FontFamily_getVariant(CRITICAL_JNI_PARAMS_COMMA jlong familyPtr)98 static jint FontFamily_getVariant(CRITICAL_JNI_PARAMS_COMMA jlong familyPtr) {
99 FontFamilyWrapper* family = reinterpret_cast<FontFamilyWrapper*>(familyPtr);
100 return static_cast<jint>(family->family->variant());
101 }
102
103 // CriticalNative
FontFamily_getFontSize(CRITICAL_JNI_PARAMS_COMMA jlong familyPtr)104 static jint FontFamily_getFontSize(CRITICAL_JNI_PARAMS_COMMA jlong familyPtr) {
105 FontFamilyWrapper* family = reinterpret_cast<FontFamilyWrapper*>(familyPtr);
106 return family->family->getNumFonts();
107 }
108
109 // CriticalNative
FontFamily_getFont(CRITICAL_JNI_PARAMS_COMMA jlong familyPtr,jint index)110 static jlong FontFamily_getFont(CRITICAL_JNI_PARAMS_COMMA jlong familyPtr, jint index) {
111 FontFamilyWrapper* family = reinterpret_cast<FontFamilyWrapper*>(familyPtr);
112 std::shared_ptr<minikin::Font> font = family->family->getFontRef(index);
113 return reinterpret_cast<jlong>(new FontWrapper(std::move(font)));
114 }
115
116 ///////////////////////////////////////////////////////////////////////////////
117
118 static const JNINativeMethod gFontFamilyBuilderMethods[] = {
119 { "nInitBuilder", "()J", (void*) FontFamily_Builder_initBuilder },
120 { "nAddFont", "(JJ)V", (void*) FontFamily_Builder_addFont },
121 { "nBuild", "(JLjava/lang/String;IZ)J", (void*) FontFamily_Builder_build },
122 { "nGetReleaseNativeFamily", "()J", (void*) FontFamily_Builder_GetReleaseFunc },
123 };
124
125 static const JNINativeMethod gFontFamilyMethods[] = {
126 {"nGetFontSize", "(J)I", (void*)FontFamily_getFontSize},
127 {"nGetFont", "(JI)J", (void*)FontFamily_getFont},
128 {"nGetLangTags", "(J)Ljava/lang/String;", (void*)FontFamily_getLangTags},
129 {"nGetVariant", "(J)I", (void*)FontFamily_getVariant},
130 };
131
register_android_graphics_fonts_FontFamily(JNIEnv * env)132 int register_android_graphics_fonts_FontFamily(JNIEnv* env) {
133 return RegisterMethodsOrDie(env, "android/graphics/fonts/FontFamily$Builder",
134 gFontFamilyBuilderMethods, NELEM(gFontFamilyBuilderMethods)) +
135 RegisterMethodsOrDie(env, "android/graphics/fonts/FontFamily", gFontFamilyMethods,
136 NELEM(gFontFamilyMethods));
137 }
138
139 }
140