xref: /aosp_15_r20/frameworks/base/libs/hwui/jni/Typeface.cpp (revision d57664e9bc4670b3ecf6748a746a57c557b6bc9e)
1 /*
2  * Copyright (C) 2013 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 #define ATRACE_TAG ATRACE_TAG_VIEW
18 #include <gui/TraceUtils.h>
19 #include <hwui/Typeface.h>
20 #include <minikin/FontCollection.h>
21 #include <minikin/FontFamily.h>
22 #include <minikin/FontFileParser.h>
23 #include <minikin/LocaleList.h>
24 #include <minikin/MinikinFontFactory.h>
25 #include <minikin/SystemFonts.h>
26 #include <nativehelper/ScopedPrimitiveArray.h>
27 #include <nativehelper/ScopedUtfChars.h>
28 
29 #include <mutex>
30 #include <unordered_map>
31 
32 #include "FontUtils.h"
33 #include "GraphicsJNI.h"
34 #include "SkData.h"
35 #include "SkTypeface.h"
36 #include "fonts/Font.h"
37 
38 #ifdef __ANDROID__
39 #include <sys/stat.h>
40 #endif
41 
42 using namespace android;
43 
toTypeface(jlong ptr)44 static inline Typeface* toTypeface(jlong ptr) {
45     return reinterpret_cast<Typeface*>(ptr);
46 }
47 
toJLong(Ptr ptr)48 template<typename Ptr> static inline jlong toJLong(Ptr ptr) {
49     return reinterpret_cast<jlong>(ptr);
50 }
51 
Typeface_createFromTypeface(JNIEnv * env,jobject,jlong familyHandle,jint style)52 static jlong Typeface_createFromTypeface(JNIEnv* env, jobject, jlong familyHandle, jint style) {
53     Typeface* family = toTypeface(familyHandle);
54     Typeface* face = Typeface::createRelative(family, (Typeface::Style)style);
55     // TODO: the following logic shouldn't be necessary, the above should always succeed.
56     // Try to find the closest matching font, using the standard heuristic
57     if (NULL == face) {
58         face = Typeface::createRelative(family, (Typeface::Style)(style ^ Typeface::kItalic));
59     }
60     for (int i = 0; NULL == face && i < 4; i++) {
61         face = Typeface::createRelative(family, (Typeface::Style)i);
62     }
63     return toJLong(face);
64 }
65 
Typeface_createFromTypefaceWithExactStyle(JNIEnv * env,jobject,jlong nativeInstance,jint weight,jboolean italic)66 static jlong Typeface_createFromTypefaceWithExactStyle(JNIEnv* env, jobject, jlong nativeInstance,
67         jint weight, jboolean italic) {
68     return toJLong(Typeface::createAbsolute(toTypeface(nativeInstance), weight, italic));
69 }
70 
Typeface_createFromTypefaceWithVariation(JNIEnv * env,jobject,jlong familyHandle,jobject listOfAxis)71 static jlong Typeface_createFromTypefaceWithVariation(JNIEnv* env, jobject, jlong familyHandle,
72         jobject listOfAxis) {
73     std::vector<minikin::FontVariation> variations;
74     ListHelper list(env, listOfAxis);
75     for (jint i = 0; i < list.size(); i++) {
76         jobject axisObject = list.get(i);
77         if (axisObject == nullptr) {
78             continue;
79         }
80         AxisHelper axis(env, axisObject);
81         variations.push_back(minikin::FontVariation(axis.getTag(), axis.getStyleValue()));
82     }
83     return toJLong(Typeface::createFromTypefaceWithVariation(
84             toTypeface(familyHandle), minikin::VariationSettings(variations, false /* sorted */)));
85 }
86 
Typeface_createWeightAlias(JNIEnv * env,jobject,jlong familyHandle,jint weight)87 static jlong Typeface_createWeightAlias(JNIEnv* env, jobject, jlong familyHandle, jint weight) {
88     return toJLong(Typeface::createWithDifferentBaseWeight(toTypeface(familyHandle), weight));
89 }
90 
releaseFunc(jlong ptr)91 static void releaseFunc(jlong ptr) {
92     delete toTypeface(ptr);
93 }
94 
95 // CriticalNative
Typeface_getReleaseFunc(CRITICAL_JNI_PARAMS)96 static jlong Typeface_getReleaseFunc(CRITICAL_JNI_PARAMS) {
97     return toJLong(&releaseFunc);
98 }
99 
100 // CriticalNative
Typeface_getStyle(CRITICAL_JNI_PARAMS_COMMA jlong faceHandle)101 static jint Typeface_getStyle(CRITICAL_JNI_PARAMS_COMMA jlong faceHandle) {
102     return toTypeface(faceHandle)->fAPIStyle;
103 }
104 
105 // CriticalNative
Typeface_getWeight(CRITICAL_JNI_PARAMS_COMMA jlong faceHandle)106 static jint Typeface_getWeight(CRITICAL_JNI_PARAMS_COMMA jlong faceHandle) {
107     return toTypeface(faceHandle)->fStyle.weight();
108 }
109 
Typeface_createFromArray(JNIEnv * env,jobject,jlongArray familyArray,jlong fallbackPtr,int weight,int italic)110 static jlong Typeface_createFromArray(JNIEnv *env, jobject, jlongArray familyArray,
111                                       jlong fallbackPtr, int weight, int italic) {
112     ScopedLongArrayRO families(env, familyArray);
113     Typeface* typeface = (fallbackPtr == 0) ? nullptr : toTypeface(fallbackPtr);
114     std::vector<std::shared_ptr<minikin::FontFamily>> familyVec;
115     familyVec.reserve(families.size());
116     for (size_t i = 0; i < families.size(); i++) {
117         FontFamilyWrapper* family = reinterpret_cast<FontFamilyWrapper*>(families[i]);
118         familyVec.emplace_back(family->family);
119     }
120     return toJLong(Typeface::createFromFamilies(std::move(familyVec), weight, italic, typeface));
121 }
122 
123 // CriticalNative
Typeface_setDefault(CRITICAL_JNI_PARAMS_COMMA jlong faceHandle)124 static void Typeface_setDefault(CRITICAL_JNI_PARAMS_COMMA jlong faceHandle) {
125     Typeface::setDefault(toTypeface(faceHandle));
126     minikin::SystemFonts::registerDefault(toTypeface(faceHandle)->fFontCollection);
127 }
128 
Typeface_getSupportedAxes(JNIEnv * env,jobject,jlong faceHandle)129 static jobject Typeface_getSupportedAxes(JNIEnv *env, jobject, jlong faceHandle) {
130     Typeface* face = toTypeface(faceHandle);
131     const size_t length = face->fFontCollection->getSupportedAxesCount();
132     if (length == 0) {
133         return nullptr;
134     }
135     std::vector<jint> tagVec(length);
136     for (size_t i = 0; i < length; i++) {
137         tagVec[i] = face->fFontCollection->getSupportedAxisAt(i);
138     }
139     std::sort(tagVec.begin(), tagVec.end());
140     const jintArray result = env->NewIntArray(length);
141     env->SetIntArrayRegion(result, 0, length, tagVec.data());
142     return result;
143 }
144 
Typeface_registerGenericFamily(JNIEnv * env,jobject,jstring familyName,jlong ptr)145 static void Typeface_registerGenericFamily(JNIEnv *env, jobject, jstring familyName, jlong ptr) {
146     ScopedUtfChars familyNameChars(env, familyName);
147     minikin::SystemFonts::registerFallback(familyNameChars.c_str(),
148                                            toTypeface(ptr)->fFontCollection);
149 }
150 
151 #ifdef __ANDROID__
152 
getVerity(const std::string & path)153 static bool getVerity(const std::string& path) {
154     struct statx out = {};
155     if (statx(AT_FDCWD, path.c_str(), 0 /* flags */, STATX_ALL, &out) != 0) {
156         ALOGE("statx failed for %s, errno = %d", path.c_str(), errno);
157         return false;
158     }
159 
160     // Validity check.
161     if ((out.stx_attributes_mask & STATX_ATTR_VERITY) == 0) {
162         // STATX_ATTR_VERITY not supported by kernel.
163         return false;
164     }
165 
166     return (out.stx_attributes & STATX_ATTR_VERITY) != 0;
167 }
168 
169 #else
170 
getVerity(const std::string &)171 static bool getVerity(const std::string&) {
172     // verity check is not enabled on desktop.
173     return false;
174 }
175 
176 #endif  // __ANDROID__
177 
makeSkDataCached(const std::string & path,bool hasVerity)178 static sk_sp<SkData> makeSkDataCached(const std::string& path, bool hasVerity) {
179     // We don't clear cache as Typeface objects created by Typeface_readTypefaces() will be stored
180     // in a static field and will not be garbage collected.
181     static std::unordered_map<std::string, sk_sp<SkData>> cache;
182     static std::mutex mutex;
183     ALOG_ASSERT(!path.empty());
184     if (hasVerity && !getVerity(path)) {
185         LOG_ALWAYS_FATAL("verity bit was removed from %s", path.c_str());
186         return nullptr;
187     }
188     std::lock_guard lock{mutex};
189     sk_sp<SkData>& entry = cache[path];
190     if (entry.get() == nullptr) {
191         entry = SkData::MakeFromFileName(path.c_str());
192     }
193     return entry;
194 }
195 
196 class MinikinFontSkiaFactory : minikin::MinikinFontFactory {
197 private:
MinikinFontSkiaFactory()198     MinikinFontSkiaFactory() : MinikinFontFactory() { MinikinFontFactory::setInstance(this); }
199 
200 public:
init()201     static void init() { static MinikinFontSkiaFactory factory; }
202     void skip(minikin::BufferReader* reader) const override;
203     std::shared_ptr<minikin::MinikinFont> create(minikin::BufferReader reader) const override;
204     void write(minikin::BufferWriter* writer, const minikin::MinikinFont* typeface) const override;
205 };
206 
skip(minikin::BufferReader * reader) const207 void MinikinFontSkiaFactory::skip(minikin::BufferReader* reader) const {
208     // Advance reader's position.
209     reader->skipString(); // fontPath
210     reader->skip<int>(); // fontIndex
211     reader->skipArray<minikin::FontVariation>(); // axesPtr, axesCount
212     bool hasVerity = static_cast<bool>(reader->read<int8_t>());
213     if (hasVerity) {
214         reader->skip<uint32_t>(); // expectedFontRevision
215         reader->skipString(); // expectedPostScriptName
216     }
217 }
218 
create(minikin::BufferReader reader) const219 std::shared_ptr<minikin::MinikinFont> MinikinFontSkiaFactory::create(
220         minikin::BufferReader reader) const {
221     std::string_view fontPath = reader.readString();
222     std::string path(fontPath.data(), fontPath.size());
223     ATRACE_FORMAT("Loading font %s", path.c_str());
224     int fontIndex = reader.read<int>();
225     const minikin::FontVariation* axesPtr;
226     uint32_t axesCount;
227     std::tie(axesPtr, axesCount) = reader.readArray<minikin::FontVariation>();
228     bool hasVerity = static_cast<bool>(reader.read<int8_t>());
229     uint32_t expectedFontRevision;
230     std::string_view expectedPostScriptName;
231     if (hasVerity) {
232         expectedFontRevision = reader.read<uint32_t>();
233         expectedPostScriptName = reader.readString();
234     }
235     sk_sp<SkData> data = makeSkDataCached(path, hasVerity);
236     if (data.get() == nullptr) {
237         // This may happen if:
238         // 1. When the process failed to open the file (e.g. invalid path or permission).
239         // 2. When the process failed to map the file (e.g. hitting max_map_count limit).
240         ALOGE("Failed to make SkData from file name: %s", path.c_str());
241         return nullptr;
242     }
243     const void* fontPtr = data->data();
244     size_t fontSize = data->size();
245     if (hasVerity) {
246         // Verify font metadata if verity is enabled.
247         minikin::FontFileParser parser(fontPtr, fontSize, fontIndex);
248         std::optional<uint32_t> revision = parser.getFontRevision();
249         if (!revision.has_value() || revision.value() != expectedFontRevision) {
250             LOG_ALWAYS_FATAL("Wrong font revision: %s", path.c_str());
251             return nullptr;
252         }
253         std::optional<std::string> psName = parser.getPostScriptName();
254         if (!psName.has_value() || psName.value() != expectedPostScriptName) {
255             LOG_ALWAYS_FATAL("Wrong PostScript name: %s", path.c_str());
256             return nullptr;
257         }
258     }
259     std::vector<minikin::FontVariation> axes(axesPtr, axesPtr + axesCount);
260     std::shared_ptr<minikin::MinikinFont> minikinFont = fonts::createMinikinFontSkia(
261             std::move(data), fontPath, fontPtr, fontSize, fontIndex, axes);
262     if (minikinFont == nullptr) {
263         ALOGE("Failed to create MinikinFontSkia: %s", path.c_str());
264         return nullptr;
265     }
266     return minikinFont;
267 }
268 
write(minikin::BufferWriter * writer,const minikin::MinikinFont * typeface) const269 void MinikinFontSkiaFactory::write(minikin::BufferWriter* writer,
270                                    const minikin::MinikinFont* typeface) const {
271     // When you change the format of font metadata, please update code to parse
272     // typefaceMetadataReader() in
273     // frameworks/base/libs/hwui/jni/fonts/Font.cpp too.
274     const std::string& path = typeface->GetFontPath();
275     writer->writeString(path);
276     writer->write<int>(typeface->GetFontIndex());
277     const minikin::VariationSettings& axes = typeface->GetAxes();
278     writer->writeArray<minikin::FontVariation>(axes.data(), axes.size());
279     bool hasVerity = getVerity(path);
280     writer->write<int8_t>(static_cast<int8_t>(hasVerity));
281     if (hasVerity) {
282         // Write font metadata for verification only when verity is enabled.
283         minikin::FontFileParser parser(typeface->GetFontData(), typeface->GetFontSize(),
284                                        typeface->GetFontIndex());
285         std::optional<uint32_t> revision = parser.getFontRevision();
286         LOG_ALWAYS_FATAL_IF(!revision.has_value());
287         writer->write<uint32_t>(revision.value());
288         std::optional<std::string> psName = parser.getPostScriptName();
289         LOG_ALWAYS_FATAL_IF(!psName.has_value());
290         writer->writeString(psName.value());
291     }
292 }
293 
Typeface_writeTypefaces(JNIEnv * env,jobject,jobject buffer,jint position,jlongArray faceHandles)294 static jint Typeface_writeTypefaces(JNIEnv* env, jobject, jobject buffer, jint position,
295                                     jlongArray faceHandles) {
296     MinikinFontSkiaFactory::init();
297     ScopedLongArrayRO faces(env, faceHandles);
298     std::vector<Typeface*> typefaces;
299     typefaces.reserve(faces.size());
300     for (size_t i = 0; i < faces.size(); i++) {
301         typefaces.push_back(toTypeface(faces[i]));
302     }
303     void* addr = buffer == nullptr ? nullptr : env->GetDirectBufferAddress(buffer);
304     if (addr != nullptr &&
305         reinterpret_cast<intptr_t>(addr) % minikin::BufferReader::kMaxAlignment != 0) {
306         ALOGE("addr (%p) must be aligned at kMaxAlignment, but it was not.", addr);
307         return 0;
308     }
309     minikin::BufferWriter writer(addr, position);
310     std::vector<std::shared_ptr<minikin::FontCollection>> fontCollections;
311     std::unordered_map<std::shared_ptr<minikin::FontCollection>, size_t> fcToIndex;
312     for (Typeface* typeface : typefaces) {
313         bool inserted = fcToIndex.emplace(typeface->fFontCollection, fontCollections.size()).second;
314         if (inserted) {
315             fontCollections.push_back(typeface->fFontCollection);
316         }
317     }
318     minikin::FontCollection::writeVector(&writer, fontCollections);
319     writer.write<uint32_t>(typefaces.size());
320     for (Typeface* typeface : typefaces) {
321       writer.write<uint32_t>(fcToIndex.find(typeface->fFontCollection)->second);
322       typeface->fStyle.writeTo(&writer);
323       writer.write<Typeface::Style>(typeface->fAPIStyle);
324       writer.write<int>(typeface->fBaseWeight);
325     }
326     return static_cast<jint>(writer.size());
327 }
328 
Typeface_readTypefaces(JNIEnv * env,jobject,jobject buffer,jint position)329 static jlongArray Typeface_readTypefaces(JNIEnv* env, jobject, jobject buffer, jint position) {
330     MinikinFontSkiaFactory::init();
331     void* addr = buffer == nullptr ? nullptr : env->GetDirectBufferAddress(buffer);
332     if (addr == nullptr) {
333         ALOGE("Passed a null buffer.");
334         return nullptr;
335     }
336     if (reinterpret_cast<intptr_t>(addr) % minikin::BufferReader::kMaxAlignment != 0) {
337         ALOGE("addr (%p) must be aligned at kMaxAlignment, but it was not.", addr);
338         return nullptr;
339     }
340     minikin::BufferReader reader(addr, position);
341     std::vector<std::shared_ptr<minikin::FontCollection>> fontCollections =
342             minikin::FontCollection::readVector(&reader);
343     uint32_t typefaceCount = reader.read<uint32_t>();
344     std::vector<jlong> faceHandles;
345     faceHandles.reserve(typefaceCount);
346     for (uint32_t i = 0; i < typefaceCount; i++) {
347         Typeface* typeface = new Typeface;
348         typeface->fFontCollection = fontCollections[reader.read<uint32_t>()];
349         typeface->fStyle = minikin::FontStyle(&reader);
350         typeface->fAPIStyle = reader.read<Typeface::Style>();
351         typeface->fBaseWeight = reader.read<int>();
352         faceHandles.push_back(toJLong(typeface));
353     }
354     const jlongArray result = env->NewLongArray(typefaceCount);
355     env->SetLongArrayRegion(result, 0, typefaceCount, faceHandles.data());
356     return result;
357 }
358 
Typeface_forceSetStaticFinalField(JNIEnv * env,jclass cls,jstring fieldName,jobject typeface)359 static void Typeface_forceSetStaticFinalField(JNIEnv *env, jclass cls, jstring fieldName,
360         jobject typeface) {
361     ScopedUtfChars fieldNameChars(env, fieldName);
362     jfieldID fid =
363             env->GetStaticFieldID(cls, fieldNameChars.c_str(), "Landroid/graphics/Typeface;");
364     if (fid == 0) {
365         jniThrowRuntimeException(env, "Unable to find field");
366         return;
367     }
368     env->SetStaticObjectField(cls, fid, typeface);
369 }
370 
371 // Regular JNI
Typeface_warmUpCache(JNIEnv * env,jobject,jstring jFilePath)372 static void Typeface_warmUpCache(JNIEnv* env, jobject, jstring jFilePath) {
373     ScopedUtfChars filePath(env, jFilePath);
374     makeSkDataCached(filePath.c_str(), false /* fs verity */);
375 }
376 
377 // Critical Native
Typeface_addFontCollection(CRITICAL_JNI_PARAMS_COMMA jlong faceHandle)378 static void Typeface_addFontCollection(CRITICAL_JNI_PARAMS_COMMA jlong faceHandle) {
379     std::shared_ptr<minikin::FontCollection> collection = toTypeface(faceHandle)->fFontCollection;
380     minikin::SystemFonts::addFontMap(std::move(collection));
381 }
382 
383 // Fast Native
Typeface_registerLocaleList(JNIEnv * env,jobject,jstring jLocales)384 static void Typeface_registerLocaleList(JNIEnv* env, jobject, jstring jLocales) {
385     ScopedUtfChars locales(env, jLocales);
386     minikin::registerLocaleList(locales.c_str());
387 }
388 
389 ///////////////////////////////////////////////////////////////////////////////
390 
391 static const JNINativeMethod gTypefaceMethods[] = {
392         {"nativeCreateFromTypeface", "(JI)J", (void*)Typeface_createFromTypeface},
393         {"nativeCreateFromTypefaceWithExactStyle", "(JIZ)J",
394          (void*)Typeface_createFromTypefaceWithExactStyle},
395         {"nativeCreateFromTypefaceWithVariation", "(JLjava/util/List;)J",
396          (void*)Typeface_createFromTypefaceWithVariation},
397         {"nativeCreateWeightAlias", "(JI)J", (void*)Typeface_createWeightAlias},
398         {"nativeGetReleaseFunc", "()J", (void*)Typeface_getReleaseFunc},
399         {"nativeGetStyle", "(J)I", (void*)Typeface_getStyle},
400         {"nativeGetWeight", "(J)I", (void*)Typeface_getWeight},
401         {"nativeCreateFromArray", "([JJII)J", (void*)Typeface_createFromArray},
402         {"nativeSetDefault", "(J)V", (void*)Typeface_setDefault},
403         {"nativeGetSupportedAxes", "(J)[I", (void*)Typeface_getSupportedAxes},
404         {"nativeRegisterGenericFamily", "(Ljava/lang/String;J)V",
405          (void*)Typeface_registerGenericFamily},
406         {"nativeWriteTypefaces", "(Ljava/nio/ByteBuffer;I[J)I", (void*)Typeface_writeTypefaces},
407         {"nativeReadTypefaces", "(Ljava/nio/ByteBuffer;I)[J", (void*)Typeface_readTypefaces},
408         {"nativeForceSetStaticFinalField", "(Ljava/lang/String;Landroid/graphics/Typeface;)V",
409          (void*)Typeface_forceSetStaticFinalField},
410         {"nativeWarmUpCache", "(Ljava/lang/String;)V", (void*)Typeface_warmUpCache},
411         {"nativeAddFontCollections", "(J)V", (void*)Typeface_addFontCollection},
412         {"nativeRegisterLocaleList", "(Ljava/lang/String;)V", (void*)Typeface_registerLocaleList},
413 };
414 
register_android_graphics_Typeface(JNIEnv * env)415 int register_android_graphics_Typeface(JNIEnv* env)
416 {
417     return RegisterMethodsOrDie(env, "android/graphics/Typeface", gTypefaceMethods,
418                                 NELEM(gTypefaceMethods));
419 }
420