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