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 #undef LOG_TAG
18 #define LOG_TAG "Minikin"
19
20 #include <nativehelper/ScopedPrimitiveArray.h>
21 #include <nativehelper/ScopedUtfChars.h>
22 #include "FontUtils.h"
23 #include "GraphicsJNI.h"
24 #include "SkData.h"
25 #include "SkFontMgr.h"
26 #include "SkRefCnt.h"
27 #include "SkStream.h"
28 #include "SkTypeface.h"
29 #include "Utils.h"
30 #include "fonts/Font.h"
31
32 #include <hwui/MinikinSkia.h>
33 #include <hwui/Typeface.h>
34 #include <minikin/FontFamily.h>
35 #include <minikin/LocaleList.h>
36 #include <ui/FatVector.h>
37
38 #include <memory>
39
40 ///////////////////////////////////////////////////////////////////////////////////////////////////
41 //
42 // The following JNI methods are kept only for compatibility reasons due to hidden API accesses.
43 //
44 ///////////////////////////////////////////////////////////////////////////////////////////////////
45
46 namespace android {
47
48 struct NativeFamilyBuilder {
NativeFamilyBuilderandroid::NativeFamilyBuilder49 NativeFamilyBuilder(uint32_t langId, int variant)
50 : langId(langId), variant(static_cast<minikin::FamilyVariant>(variant)) {}
51 uint32_t langId;
52 minikin::FamilyVariant variant;
53 std::vector<std::shared_ptr<minikin::Font>> fonts;
54 std::vector<minikin::FontVariation> axes;
55 };
56
toNativeBuilder(jlong ptr)57 static inline NativeFamilyBuilder* toNativeBuilder(jlong ptr) {
58 return reinterpret_cast<NativeFamilyBuilder*>(ptr);
59 }
60
toFamily(jlong ptr)61 static inline FontFamilyWrapper* toFamily(jlong ptr) {
62 return reinterpret_cast<FontFamilyWrapper*>(ptr);
63 }
64
toJLong(Ptr ptr)65 template<typename Ptr> static inline jlong toJLong(Ptr ptr) {
66 return reinterpret_cast<jlong>(ptr);
67 }
68
FontFamily_initBuilder(JNIEnv * env,jobject clazz,jstring langs,jint variant)69 static jlong FontFamily_initBuilder(JNIEnv* env, jobject clazz, jstring langs, jint variant) {
70 NativeFamilyBuilder* builder;
71 if (langs != nullptr) {
72 ScopedUtfChars str(env, langs);
73 builder = new NativeFamilyBuilder(minikin::registerLocaleList(str.c_str()), variant);
74 } else {
75 builder = new NativeFamilyBuilder(minikin::registerLocaleList(""), variant);
76 }
77 return toJLong(builder);
78 }
79
FontFamily_create(CRITICAL_JNI_PARAMS_COMMA jlong builderPtr)80 static jlong FontFamily_create(CRITICAL_JNI_PARAMS_COMMA jlong builderPtr) {
81 if (builderPtr == 0) {
82 return 0;
83 }
84 NativeFamilyBuilder* builder = toNativeBuilder(builderPtr);
85 if (builder->fonts.empty()) {
86 return 0;
87 }
88 std::shared_ptr<minikin::FontFamily> family = minikin::FontFamily::create(
89 builder->langId, builder->variant, std::move(builder->fonts),
90 true /* isCustomFallback */, false /* isDefaultFallback */);
91 if (family->getCoverage().length() == 0) {
92 return 0;
93 }
94 return toJLong(new FontFamilyWrapper(std::move(family)));
95 }
96
releaseBuilder(jlong builderPtr)97 static void releaseBuilder(jlong builderPtr) {
98 delete toNativeBuilder(builderPtr);
99 }
100
FontFamily_getBuilderReleaseFunc(CRITICAL_JNI_PARAMS)101 static jlong FontFamily_getBuilderReleaseFunc(CRITICAL_JNI_PARAMS) {
102 return toJLong(&releaseBuilder);
103 }
104
releaseFamily(jlong familyPtr)105 static void releaseFamily(jlong familyPtr) {
106 delete toFamily(familyPtr);
107 }
108
FontFamily_getFamilyReleaseFunc(CRITICAL_JNI_PARAMS)109 static jlong FontFamily_getFamilyReleaseFunc(CRITICAL_JNI_PARAMS) {
110 return toJLong(&releaseFamily);
111 }
112
addSkTypeface(NativeFamilyBuilder * builder,sk_sp<SkData> && data,int ttcIndex,jint weight,jint italic)113 static bool addSkTypeface(NativeFamilyBuilder* builder, sk_sp<SkData>&& data, int ttcIndex,
114 jint weight, jint italic) {
115 FatVector<SkFontArguments::VariationPosition::Coordinate, 2> skVariation;
116 for (const auto& axis : builder->axes) {
117 skVariation.push_back({axis.axisTag, axis.value});
118 }
119
120 const size_t fontSize = data->size();
121 const void* fontPtr = data->data();
122 std::unique_ptr<SkStreamAsset> fontData(new SkMemoryStream(std::move(data)));
123
124 SkFontArguments args;
125 args.setCollectionIndex(ttcIndex);
126 args.setVariationDesignPosition({skVariation.data(), static_cast<int>(skVariation.size())});
127
128 sk_sp<SkFontMgr> fm(SkFontMgr::RefDefault());
129 sk_sp<SkTypeface> face(fm->makeFromStream(std::move(fontData), args));
130 if (face == NULL) {
131 ALOGE("addFont failed to create font, invalid request");
132 builder->axes.clear();
133 return false;
134 }
135 std::shared_ptr<minikin::MinikinFont> minikinFont =
136 std::make_shared<MinikinFontSkia>(std::move(face), fonts::getNewSourceId(), fontPtr,
137 fontSize, "", ttcIndex, builder->axes);
138 minikin::Font::Builder fontBuilder(minikinFont);
139
140 if (weight != RESOLVE_BY_FONT_TABLE) {
141 fontBuilder.setWeight(weight);
142 }
143 if (italic != RESOLVE_BY_FONT_TABLE) {
144 fontBuilder.setSlant(static_cast<minikin::FontStyle::Slant>(italic != 0));
145 }
146 builder->fonts.push_back(fontBuilder.build());
147 builder->axes.clear();
148 return true;
149 }
150
release_global_ref(const void *,void * context)151 static void release_global_ref(const void* /*data*/, void* context) {
152 JNIEnv* env = GraphicsJNI::getJNIEnv();
153 bool needToAttach = (env == NULL);
154 if (needToAttach) {
155 env = GraphicsJNI::attachJNIEnv("release_font_data");
156 if (env == nullptr) {
157 ALOGE("failed to attach to thread to release global ref.");
158 return;
159 }
160 }
161
162 jobject obj = reinterpret_cast<jobject>(context);
163 env->DeleteGlobalRef(obj);
164
165 if (needToAttach) {
166 GraphicsJNI::detachJNIEnv();
167 }
168 }
169
FontFamily_addFont(JNIEnv * env,jobject clazz,jlong builderPtr,jobject bytebuf,jint ttcIndex,jint weight,jint isItalic)170 static jboolean FontFamily_addFont(JNIEnv* env, jobject clazz, jlong builderPtr, jobject bytebuf,
171 jint ttcIndex, jint weight, jint isItalic) {
172 NPE_CHECK_RETURN_ZERO(env, bytebuf);
173 NativeFamilyBuilder* builder = reinterpret_cast<NativeFamilyBuilder*>(builderPtr);
174 const void* fontPtr = env->GetDirectBufferAddress(bytebuf);
175 if (fontPtr == NULL) {
176 ALOGE("addFont failed to create font, buffer invalid");
177 builder->axes.clear();
178 return false;
179 }
180 jlong fontSize = env->GetDirectBufferCapacity(bytebuf);
181 if (fontSize < 0) {
182 ALOGE("addFont failed to create font, buffer size invalid");
183 builder->axes.clear();
184 return false;
185 }
186 jobject fontRef = MakeGlobalRefOrDie(env, bytebuf);
187 sk_sp<SkData> data(SkData::MakeWithProc(fontPtr, fontSize,
188 release_global_ref, reinterpret_cast<void*>(fontRef)));
189 return addSkTypeface(builder, std::move(data), ttcIndex, weight, isItalic);
190 }
191
FontFamily_addFontWeightStyle(JNIEnv * env,jobject clazz,jlong builderPtr,jobject font,jint ttcIndex,jint weight,jint isItalic)192 static jboolean FontFamily_addFontWeightStyle(JNIEnv* env, jobject clazz, jlong builderPtr,
193 jobject font, jint ttcIndex, jint weight, jint isItalic) {
194 NPE_CHECK_RETURN_ZERO(env, font);
195 NativeFamilyBuilder* builder = toNativeBuilder(builderPtr);
196 const void* fontPtr = env->GetDirectBufferAddress(font);
197 if (fontPtr == NULL) {
198 ALOGE("addFont failed to create font, buffer invalid");
199 builder->axes.clear();
200 return false;
201 }
202 jlong fontSize = env->GetDirectBufferCapacity(font);
203 if (fontSize < 0) {
204 ALOGE("addFont failed to create font, buffer size invalid");
205 builder->axes.clear();
206 return false;
207 }
208 jobject fontRef = MakeGlobalRefOrDie(env, font);
209 sk_sp<SkData> data(SkData::MakeWithProc(fontPtr, fontSize,
210 release_global_ref, reinterpret_cast<void*>(fontRef)));
211 return addSkTypeface(builder, std::move(data), ttcIndex, weight, isItalic);
212 }
213
FontFamily_addAxisValue(CRITICAL_JNI_PARAMS_COMMA jlong builderPtr,jint tag,jfloat value)214 static void FontFamily_addAxisValue(CRITICAL_JNI_PARAMS_COMMA jlong builderPtr, jint tag, jfloat value) {
215 NativeFamilyBuilder* builder = toNativeBuilder(builderPtr);
216 builder->axes.push_back({static_cast<minikin::AxisTag>(tag), value});
217 }
218
219 ///////////////////////////////////////////////////////////////////////////////
220
221 static const JNINativeMethod gFontFamilyMethods[] = {
222 { "nInitBuilder", "(Ljava/lang/String;I)J", (void*)FontFamily_initBuilder },
223 { "nCreateFamily", "(J)J", (void*)FontFamily_create },
224 { "nGetBuilderReleaseFunc", "()J", (void*)FontFamily_getBuilderReleaseFunc },
225 { "nGetFamilyReleaseFunc", "()J", (void*)FontFamily_getFamilyReleaseFunc },
226 { "nAddFont", "(JLjava/nio/ByteBuffer;III)Z", (void*)FontFamily_addFont },
227 { "nAddFontWeightStyle", "(JLjava/nio/ByteBuffer;III)Z",
228 (void*)FontFamily_addFontWeightStyle },
229 { "nAddAxisValue", "(JIF)V", (void*)FontFamily_addAxisValue },
230 };
231
register_android_graphics_FontFamily(JNIEnv * env)232 int register_android_graphics_FontFamily(JNIEnv* env)
233 {
234 int err = RegisterMethodsOrDie(env, "android/graphics/FontFamily", gFontFamilyMethods,
235 NELEM(gFontFamilyMethods));
236
237 init_FontUtils(env);
238 return err;
239 }
240
241 }
242