• 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 #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<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 ///////////////////////////////////////////////////////////////////////////////
87 
88 static const JNINativeMethod gFontFamilyBuilderMethods[] = {
89     { "nInitBuilder", "()J", (void*) FontFamily_Builder_initBuilder },
90     { "nAddFont", "(JJ)V", (void*) FontFamily_Builder_addFont },
91     { "nBuild", "(JLjava/lang/String;IZ)J", (void*) FontFamily_Builder_build },
92 
93     { "nGetReleaseNativeFamily", "()J", (void*) FontFamily_Builder_GetReleaseFunc },
94 };
95 
register_android_graphics_fonts_FontFamily(JNIEnv * env)96 int register_android_graphics_fonts_FontFamily(JNIEnv* env) {
97     return RegisterMethodsOrDie(env, "android/graphics/fonts/FontFamily$Builder",
98             gFontFamilyBuilderMethods, NELEM(gFontFamilyBuilderMethods));
99 }
100 
101 }
102