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