• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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)->getAPIStyle();
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)->getFontStyle().weight();
108 }
109 
110 // Critical Native
Typeface_isVariationInstance(CRITICAL_JNI_PARAMS_COMMA jlong faceHandle)111 static jboolean Typeface_isVariationInstance(CRITICAL_JNI_PARAMS_COMMA jlong faceHandle) {
112     return toTypeface(faceHandle)->isVariationInstance();
113 }
114 
Typeface_createFromArray(JNIEnv * env,jobject,jlongArray familyArray,jlong fallbackPtr,int weight,int italic)115 static jlong Typeface_createFromArray(JNIEnv *env, jobject, jlongArray familyArray,
116                                       jlong fallbackPtr, int weight, int italic) {
117     ScopedLongArrayRO families(env, familyArray);
118     Typeface* typeface = (fallbackPtr == 0) ? nullptr : toTypeface(fallbackPtr);
119     std::vector<std::shared_ptr<minikin::FontFamily>> familyVec;
120     familyVec.reserve(families.size());
121     for (size_t i = 0; i < families.size(); i++) {
122         FontFamilyWrapper* family = reinterpret_cast<FontFamilyWrapper*>(families[i]);
123         familyVec.emplace_back(family->family);
124     }
125     return toJLong(Typeface::createFromFamilies(std::move(familyVec), weight, italic, typeface));
126 }
127 
128 // CriticalNative
Typeface_setDefault(CRITICAL_JNI_PARAMS_COMMA jlong faceHandle)129 static void Typeface_setDefault(CRITICAL_JNI_PARAMS_COMMA jlong faceHandle) {
130     Typeface::setDefault(toTypeface(faceHandle));
131     minikin::SystemFonts::registerDefault(toTypeface(faceHandle)->getFontCollection());
132 }
133 
Typeface_getSupportedAxes(JNIEnv * env,jobject,jlong faceHandle)134 static jobject Typeface_getSupportedAxes(JNIEnv *env, jobject, jlong faceHandle) {
135     Typeface* face = toTypeface(faceHandle);
136     const size_t length = face->getFontCollection()->getSupportedAxesCount();
137     if (length == 0) {
138         return nullptr;
139     }
140     std::vector<jint> tagVec(length);
141     for (size_t i = 0; i < length; i++) {
142         tagVec[i] = face->getFontCollection()->getSupportedAxisAt(i);
143     }
144     std::sort(tagVec.begin(), tagVec.end());
145     const jintArray result = env->NewIntArray(length);
146     env->SetIntArrayRegion(result, 0, length, tagVec.data());
147     return result;
148 }
149 
Typeface_registerGenericFamily(JNIEnv * env,jobject,jstring familyName,jlong ptr)150 static void Typeface_registerGenericFamily(JNIEnv *env, jobject, jstring familyName, jlong ptr) {
151     ScopedUtfChars familyNameChars(env, familyName);
152     minikin::SystemFonts::registerFallback(familyNameChars.c_str(),
153                                            toTypeface(ptr)->getFontCollection());
154 }
155 
156 #ifdef __ANDROID__
157 
getVerity(const std::string & path)158 static bool getVerity(const std::string& path) {
159     struct statx out = {};
160     if (statx(AT_FDCWD, path.c_str(), 0 /* flags */, STATX_ALL, &out) != 0) {
161         ALOGE("statx failed for %s, errno = %d", path.c_str(), errno);
162         return false;
163     }
164 
165     // Validity check.
166     if ((out.stx_attributes_mask & STATX_ATTR_VERITY) == 0) {
167         // STATX_ATTR_VERITY not supported by kernel.
168         return false;
169     }
170 
171     return (out.stx_attributes & STATX_ATTR_VERITY) != 0;
172 }
173 
174 #else
175 
getVerity(const std::string &)176 static bool getVerity(const std::string&) {
177     // verity check is not enabled on desktop.
178     return false;
179 }
180 
181 #endif  // __ANDROID__
182 
makeSkDataCached(const std::string & path,bool hasVerity)183 static sk_sp<SkData> makeSkDataCached(const std::string& path, bool hasVerity) {
184     // We don't clear cache as Typeface objects created by Typeface_readTypefaces() will be stored
185     // in a static field and will not be garbage collected.
186     static std::unordered_map<std::string, sk_sp<SkData>> cache;
187     static std::mutex mutex;
188     ALOG_ASSERT(!path.empty());
189     if (hasVerity && !getVerity(path)) {
190         LOG_ALWAYS_FATAL("verity bit was removed from %s", path.c_str());
191         return nullptr;
192     }
193     std::lock_guard lock{mutex};
194     sk_sp<SkData>& entry = cache[path];
195     if (entry.get() == nullptr) {
196         entry = SkData::MakeFromFileName(path.c_str());
197     }
198     return entry;
199 }
200 
201 class MinikinFontSkiaFactory : minikin::MinikinFontFactory {
202 private:
MinikinFontSkiaFactory()203     MinikinFontSkiaFactory() : MinikinFontFactory() { MinikinFontFactory::setInstance(this); }
204 
205 public:
init()206     static void init() { static MinikinFontSkiaFactory factory; }
207     void skip(minikin::BufferReader* reader) const override;
208     std::shared_ptr<minikin::MinikinFont> create(minikin::BufferReader reader) const override;
209     void write(minikin::BufferWriter* writer, const minikin::MinikinFont* typeface) const override;
210 };
211 
skip(minikin::BufferReader * reader) const212 void MinikinFontSkiaFactory::skip(minikin::BufferReader* reader) const {
213     // Advance reader's position.
214     reader->skipString(); // fontPath
215     reader->skip<int>(); // fontIndex
216     reader->skipArray<minikin::FontVariation>(); // axesPtr, axesCount
217     bool hasVerity = static_cast<bool>(reader->read<int8_t>());
218     if (hasVerity) {
219         reader->skip<uint32_t>(); // expectedFontRevision
220         reader->skipString(); // expectedPostScriptName
221     }
222 }
223 
create(minikin::BufferReader reader) const224 std::shared_ptr<minikin::MinikinFont> MinikinFontSkiaFactory::create(
225         minikin::BufferReader reader) const {
226     std::string_view fontPath = reader.readString();
227     std::string path(fontPath.data(), fontPath.size());
228     ATRACE_FORMAT("Loading font %s", path.c_str());
229     int fontIndex = reader.read<int>();
230     const minikin::FontVariation* axesPtr;
231     uint32_t axesCount;
232     std::tie(axesPtr, axesCount) = reader.readArray<minikin::FontVariation>();
233     bool hasVerity = static_cast<bool>(reader.read<int8_t>());
234     uint32_t expectedFontRevision;
235     std::string_view expectedPostScriptName;
236     if (hasVerity) {
237         expectedFontRevision = reader.read<uint32_t>();
238         expectedPostScriptName = reader.readString();
239     }
240     sk_sp<SkData> data = makeSkDataCached(path, hasVerity);
241     if (data.get() == nullptr) {
242         // This may happen if:
243         // 1. When the process failed to open the file (e.g. invalid path or permission).
244         // 2. When the process failed to map the file (e.g. hitting max_map_count limit).
245         ALOGE("Failed to make SkData from file name: %s", path.c_str());
246         return nullptr;
247     }
248     const void* fontPtr = data->data();
249     size_t fontSize = data->size();
250     if (hasVerity) {
251         // Verify font metadata if verity is enabled.
252         minikin::FontFileParser parser(fontPtr, fontSize, fontIndex);
253         std::optional<uint32_t> revision = parser.getFontRevision();
254         if (!revision.has_value() || revision.value() != expectedFontRevision) {
255             LOG_ALWAYS_FATAL("Wrong font revision: %s", path.c_str());
256             return nullptr;
257         }
258         std::optional<std::string> psName = parser.getPostScriptName();
259         if (!psName.has_value() || psName.value() != expectedPostScriptName) {
260             LOG_ALWAYS_FATAL("Wrong PostScript name: %s", path.c_str());
261             return nullptr;
262         }
263     }
264     std::vector<minikin::FontVariation> axes(axesPtr, axesPtr + axesCount);
265     std::shared_ptr<minikin::MinikinFont> minikinFont = fonts::createMinikinFontSkia(
266             std::move(data), fontPath, fontPtr, fontSize, fontIndex, axes);
267     if (minikinFont == nullptr) {
268         ALOGE("Failed to create MinikinFontSkia: %s", path.c_str());
269         return nullptr;
270     }
271     return minikinFont;
272 }
273 
write(minikin::BufferWriter * writer,const minikin::MinikinFont * typeface) const274 void MinikinFontSkiaFactory::write(minikin::BufferWriter* writer,
275                                    const minikin::MinikinFont* typeface) const {
276     // When you change the format of font metadata, please update code to parse
277     // typefaceMetadataReader() in
278     // frameworks/base/libs/hwui/jni/fonts/Font.cpp too.
279     const std::string& path = typeface->GetFontPath();
280     writer->writeString(path);
281     writer->write<int>(typeface->GetFontIndex());
282     const minikin::VariationSettings& axes = typeface->GetAxes();
283     writer->writeArray<minikin::FontVariation>(axes.data(), axes.size());
284     bool hasVerity = getVerity(path);
285     writer->write<int8_t>(static_cast<int8_t>(hasVerity));
286     if (hasVerity) {
287         // Write font metadata for verification only when verity is enabled.
288         minikin::FontFileParser parser(typeface->GetFontData(), typeface->GetFontSize(),
289                                        typeface->GetFontIndex());
290         std::optional<uint32_t> revision = parser.getFontRevision();
291         LOG_ALWAYS_FATAL_IF(!revision.has_value());
292         writer->write<uint32_t>(revision.value());
293         std::optional<std::string> psName = parser.getPostScriptName();
294         LOG_ALWAYS_FATAL_IF(!psName.has_value());
295         writer->writeString(psName.value());
296     }
297 }
298 
Typeface_writeTypefaces(JNIEnv * env,jobject,jobject buffer,jint position,jlongArray faceHandles)299 static jint Typeface_writeTypefaces(JNIEnv* env, jobject, jobject buffer, jint position,
300                                     jlongArray faceHandles) {
301     MinikinFontSkiaFactory::init();
302     ScopedLongArrayRO faces(env, faceHandles);
303     std::vector<Typeface*> typefaces;
304     typefaces.reserve(faces.size());
305     for (size_t i = 0; i < faces.size(); i++) {
306         typefaces.push_back(toTypeface(faces[i]));
307     }
308     void* addr = buffer == nullptr ? nullptr : env->GetDirectBufferAddress(buffer);
309     if (addr != nullptr &&
310         reinterpret_cast<intptr_t>(addr) % minikin::BufferReader::kMaxAlignment != 0) {
311         ALOGE("addr (%p) must be aligned at kMaxAlignment, but it was not.", addr);
312         return 0;
313     }
314     minikin::BufferWriter writer(addr, position);
315     std::vector<std::shared_ptr<minikin::FontCollection>> fontCollections;
316     std::unordered_map<std::shared_ptr<minikin::FontCollection>, size_t> fcToIndex;
317     for (Typeface* typeface : typefaces) {
318         bool inserted =
319                 fcToIndex.emplace(typeface->getFontCollection(), fontCollections.size()).second;
320         if (inserted) {
321             fontCollections.push_back(typeface->getFontCollection());
322         }
323     }
324     minikin::FontCollection::writeVector(&writer, fontCollections);
325     writer.write<uint32_t>(typefaces.size());
326     for (Typeface* typeface : typefaces) {
327         writer.write<uint32_t>(fcToIndex.find(typeface->getFontCollection())->second);
328         typeface->getFontStyle().writeTo(&writer);
329         writer.write<Typeface::Style>(typeface->getAPIStyle());
330         writer.write<int>(typeface->getBaseWeight());
331     }
332     return static_cast<jint>(writer.size());
333 }
334 
Typeface_readTypefaces(JNIEnv * env,jobject,jobject buffer,jint position)335 static jlongArray Typeface_readTypefaces(JNIEnv* env, jobject, jobject buffer, jint position) {
336     MinikinFontSkiaFactory::init();
337     void* addr = buffer == nullptr ? nullptr : env->GetDirectBufferAddress(buffer);
338     if (addr == nullptr) {
339         ALOGE("Passed a null buffer.");
340         return nullptr;
341     }
342     if (reinterpret_cast<intptr_t>(addr) % minikin::BufferReader::kMaxAlignment != 0) {
343         ALOGE("addr (%p) must be aligned at kMaxAlignment, but it was not.", addr);
344         return nullptr;
345     }
346     minikin::BufferReader reader(addr, position);
347     std::vector<std::shared_ptr<minikin::FontCollection>> fontCollections =
348             minikin::FontCollection::readVector(&reader);
349     uint32_t typefaceCount = reader.read<uint32_t>();
350     std::vector<jlong> faceHandles;
351     faceHandles.reserve(typefaceCount);
352     for (uint32_t i = 0; i < typefaceCount; i++) {
353         Typeface* typeface =
354                 new Typeface(fontCollections[reader.read<uint32_t>()], minikin::FontStyle(&reader),
355                              reader.read<Typeface::Style>(), reader.read<int>(),
356                              false /* isVariationInstance */);
357         faceHandles.push_back(toJLong(typeface));
358     }
359     const jlongArray result = env->NewLongArray(typefaceCount);
360     env->SetLongArrayRegion(result, 0, typefaceCount, faceHandles.data());
361     return result;
362 }
363 
Typeface_forceSetStaticFinalField(JNIEnv * env,jclass cls,jstring fieldName,jobject typeface)364 static void Typeface_forceSetStaticFinalField(JNIEnv *env, jclass cls, jstring fieldName,
365         jobject typeface) {
366     ScopedUtfChars fieldNameChars(env, fieldName);
367     jfieldID fid =
368             env->GetStaticFieldID(cls, fieldNameChars.c_str(), "Landroid/graphics/Typeface;");
369     if (fid == 0) {
370         jniThrowRuntimeException(env, "Unable to find field");
371         return;
372     }
373     env->SetStaticObjectField(cls, fid, typeface);
374 }
375 
376 // Regular JNI
Typeface_warmUpCache(JNIEnv * env,jobject,jstring jFilePath)377 static void Typeface_warmUpCache(JNIEnv* env, jobject, jstring jFilePath) {
378     ScopedUtfChars filePath(env, jFilePath);
379     makeSkDataCached(filePath.c_str(), false /* fs verity */);
380 }
381 
382 // Critical Native
Typeface_addFontCollection(CRITICAL_JNI_PARAMS_COMMA jlong faceHandle)383 static void Typeface_addFontCollection(CRITICAL_JNI_PARAMS_COMMA jlong faceHandle) {
384     std::shared_ptr<minikin::FontCollection> collection =
385             toTypeface(faceHandle)->getFontCollection();
386     minikin::SystemFonts::addFontMap(std::move(collection));
387 }
388 
389 // Fast Native
Typeface_registerLocaleList(JNIEnv * env,jobject,jstring jLocales)390 static void Typeface_registerLocaleList(JNIEnv* env, jobject, jstring jLocales) {
391     ScopedUtfChars locales(env, jLocales);
392     minikin::registerLocaleList(locales.c_str());
393 }
394 
395 ///////////////////////////////////////////////////////////////////////////////
396 
397 static const JNINativeMethod gTypefaceMethods[] = {
nativeCreateFromTypeface(JI)398         {"nativeCreateFromTypeface", "(JI)J", (void*)Typeface_createFromTypeface},
nativeCreateFromTypefaceWithExactStyle(JIZ)399         {"nativeCreateFromTypefaceWithExactStyle", "(JIZ)J",
400          (void*)Typeface_createFromTypefaceWithExactStyle},
nativeCreateFromTypefaceWithVariation(JLjava/util/List;)401         {"nativeCreateFromTypefaceWithVariation", "(JLjava/util/List;)J",
402          (void*)Typeface_createFromTypefaceWithVariation},
nativeCreateWeightAlias(JI)403         {"nativeCreateWeightAlias", "(JI)J", (void*)Typeface_createWeightAlias},
nativeGetReleaseFunc()404         {"nativeGetReleaseFunc", "()J", (void*)Typeface_getReleaseFunc},
nativeGetStyle(J)405         {"nativeGetStyle", "(J)I", (void*)Typeface_getStyle},
nativeGetWeight(J)406         {"nativeGetWeight", "(J)I", (void*)Typeface_getWeight},
nativeIsVariationInstance(J)407         {"nativeIsVariationInstance", "(J)Z", (void*)Typeface_isVariationInstance},
nativeCreateFromArray([JJII)408         {"nativeCreateFromArray", "([JJII)J", (void*)Typeface_createFromArray},
nativeSetDefault(J)409         {"nativeSetDefault", "(J)V", (void*)Typeface_setDefault},
nativeGetSupportedAxes(J)410         {"nativeGetSupportedAxes", "(J)[I", (void*)Typeface_getSupportedAxes},
nativeRegisterGenericFamily(Ljava/lang/String;J)411         {"nativeRegisterGenericFamily", "(Ljava/lang/String;J)V",
412          (void*)Typeface_registerGenericFamily},
nativeWriteTypefaces(Ljava/nio/ByteBuffer;I[J)413         {"nativeWriteTypefaces", "(Ljava/nio/ByteBuffer;I[J)I", (void*)Typeface_writeTypefaces},
nativeReadTypefaces(Ljava/nio/ByteBuffer;I)414         {"nativeReadTypefaces", "(Ljava/nio/ByteBuffer;I)[J", (void*)Typeface_readTypefaces},
nativeForceSetStaticFinalField(Ljava/lang/String;Landroid/graphics/Typeface;)415         {"nativeForceSetStaticFinalField", "(Ljava/lang/String;Landroid/graphics/Typeface;)V",
416          (void*)Typeface_forceSetStaticFinalField},
nativeWarmUpCache(Ljava/lang/String;)417         {"nativeWarmUpCache", "(Ljava/lang/String;)V", (void*)Typeface_warmUpCache},
nativeAddFontCollections(J)418         {"nativeAddFontCollections", "(J)V", (void*)Typeface_addFontCollection},
nativeRegisterLocaleList(Ljava/lang/String;)419         {"nativeRegisterLocaleList", "(Ljava/lang/String;)V", (void*)Typeface_registerLocaleList},
420 };
421 
register_android_graphics_Typeface(JNIEnv * env)422 int register_android_graphics_Typeface(JNIEnv* env)
423 {
424     return RegisterMethodsOrDie(env, "android/graphics/Typeface", gTypefaceMethods,
425                                 NELEM(gTypefaceMethods));
426 }
427