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