| 1 |
/* |
| 2 |
* Copyright 2024 Google Inc. |
| 3 |
* |
| 4 |
* Use of this source code is governed by a BSD-style license that can be |
| 5 |
* found in the LICENSE file. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "include/core/SkFontArguments.h" |
| 9 |
#include "include/core/SkFontMgr.h" |
| 10 |
#include "include/core/SkFontScanner.h" |
| 11 |
#include "include/core/SkStream.h" |
| 12 |
#include "include/core/SkTypeface.h" |
| 13 |
#include "include/core/SkTypes.h" |
| 14 |
#include "include/ports/SkFontMgr_android_ndk.h" |
| 15 |
#include "include/private/base/SkAssert.h" |
| 16 |
#include "include/private/base/SkFeatures.h" |
| 17 |
#include "include/private/base/SkFloatingPoint.h" |
| •18 |
#include "include/private/base/SkMutex.h" |
| 19 |
#include "include/private/base/SkTArray.h" |
| 20 |
#include "include/private/base/SkTemplates.h" |
| 21 |
#include "src/base/SkTSort.h" |
| 22 |
#include "src/base/SkUTF.h" |
| 23 |
#include "src/core/SkChecksum.h" |
| 24 |
#include "src/core/SkFontDescriptor.h" |
| 25 |
#include "src/core/SkLRUCache.h" |
| 26 |
#include "src/core/SkTHash.h" |
| 27 |
#include "src/ports/SkFontMgr_android_parser.h" |
| 28 |
#include "src/ports/SkTypeface_proxy.h" |
| 29 |
|
| 30 |
#include <unicode/uchar.h> |
| 31 |
#include <unicode/ustring.h> |
| 32 |
|
| 33 |
#include <algorithm> |
| 34 |
#include <cmath> |
| 35 |
|
| 36 |
#if defined(SK_BUILD_FOR_ANDROID) |
| 37 |
#include <android/api-level.h> |
| 38 |
#else |
| 39 |
#define __ANDROID_API__ 0 |
| 40 |
#define __ANDROID_API_Q__ 29 |
| 41 |
#define __ANDROID_API_R__ 30 |
| 42 |
#define __ANDROID_API_S__ 31 |
| 43 |
int android_get_device_api_level() { return __ANDROID_API__; }; |
| 44 |
#endif |
| 45 |
|
| 46 |
using namespace skia_private; |
| 47 |
|
| 48 |
/** |
| 49 |
* Technically, the AFont API was introduced in Android 10 (Q, API 29). However... |
| 50 |
* |
| 51 |
* The AFontMatcher API implementation is broken from its introduction until at least API 33. What |
| 52 |
* is desired is to find a font for the given locale which contains the given character. However, |
| 53 |
* the implementation actually attempts to shape the string passed to it with the default font and |
| 54 |
* then returns the font chosen for the first run. However, this produces undesireable results, as |
| 55 |
* it will always prefer the default font over the locale, so any code points covered by the default |
| 56 |
* font will always come from the default font regardless of the requested locale. In addition, this |
| 57 |
* will claim coverage for code points "made up" by the shaper through normalization, |
| 58 |
* denormalization, whitespace synthesis, no-draw synthesis, etc, for the default font, when there |
| 59 |
* may be better choices later in fallback. |
| 60 |
* |
| 61 |
* On Android 10 (Q, API 29) AFont_getLocale always returns nullptr (if there is a locale set) or |
| 62 |
* whatever std::unique_ptr<std::string>()->c_str() returns, which happens to be 0x1. As a result, |
| 63 |
* AFont_getLocale cannot be used until Android 11 (R, API 30). This is b/139201432 and fixed with |
| 64 |
* "Make AFont_getLocale work" [0]. This change is in Android 11 (API 30) but does not appear to |
| 65 |
* have been cherry-picked into Android 10 (Q, API 29). |
| 66 |
* [0] https://cs.android.com/android/_/android/platform/frameworks/base/+/01709c7469b59e451f064c266bbe442e9bef0ab4 |
| 67 |
* |
| 68 |
* As a result, there is no correct way to use locale information from the Android 10 NDK. So this |
| 69 |
* font manager only works with Android 11 (R, API 30) and above. |
| 70 |
*/ |
| 71 |
#define SK_ANDROID_NDK_FONT_API_EXISTS __ANDROID_API_Q__ |
| 72 |
#define SK_ANDROID_NDK_FONT_API_LOCALE_WORKS __ANDROID_API_R__ |
| 73 |
|
| 74 |
#if __ANDROID_API__ >= SK_ANDROID_NDK_FONT_API_EXISTS |
| 75 |
#include <android/font.h> |
| 76 |
#include <android/font_matcher.h> |
| 77 |
#include <android/system_fonts.h> |
| 78 |
#endif |
| 79 |
|
| 80 |
#include <cinttypes> |
| 81 |
#include <memory> |
| 82 |
#include <vector> |
| 83 |
|
| 84 |
#include <dlfcn.h> |
| 85 |
|
| 86 |
struct ASystemFontIterator; |
| 87 |
struct AFont; |
| 88 |
|
| 89 |
namespace { |
| 90 |
|
| 91 |
[[maybe_unused]] static inline const constexpr bool kSkFontMgrVerbose = false; |
| 92 |
|
| 93 |
namespace variation { |
| 94 |
using Coordinate = SkFontArguments::VariationPosition::Coordinate; |
| 95 |
using Storage = AutoSTArray<4, Coordinate>; |
| 96 |
|
| 97 |
static constexpr SkFourByteTag wghtTag = SkSetFourByteTag('w','g','h','t'); |
| 98 |
static constexpr SkFourByteTag wdthTag = SkSetFourByteTag('w','d','t','h'); |
| 99 |
static constexpr SkFourByteTag slntTag = SkSetFourByteTag('s','l','n','t'); |
| 100 |
static constexpr SkFourByteTag italTag = SkSetFourByteTag('i','t','a','l'); |
| 101 |
|
| 102 |
static bool coordinateLess(const Coordinate& a, const Coordinate& b) { |
| 103 |
return a.axis != b.axis ? a.axis < b.axis : a.value < b.value; |
| 104 |
} |
| 105 |
|
| 106 |
static bool coordinateEqual(const Coordinate& a, const Coordinate& b) { |
| 107 |
return a.axis == b.axis && a.value == b.value; |
| 108 |
} |
| 109 |
|
| 110 |
static SkSpan<Coordinate> Get(const SkTypeface& typeface, Storage& storage) { |
| 111 |
if (storage.size() < Storage::kCount) { |
| 112 |
storage.reset(Storage::kCount); |
| 113 |
} |
| 114 |
int numAxes = typeface.getVariationDesignPosition(SkSpan(storage)); |
| 115 |
if (SkToInt(storage.size()) < numAxes) { |
| 116 |
storage.reset(numAxes); |
| 117 |
numAxes = typeface.getVariationDesignPosition(SkSpan(storage)); |
| 118 |
} |
| 119 |
if (numAxes < 0) { |
| 120 |
numAxes = 0; |
| 121 |
} |
| 122 |
|
| 123 |
return SkSpan(storage.data(), numAxes); |
| 124 |
} |
| 125 |
|
| 126 |
/* Normalize the values. NaN and -0.0 => 0. |
| 127 |
* Should normalize before sorting or comparing. |
| 128 |
*/ |
| 129 |
static SkSpan<Coordinate> Normalize(SkSpan<Coordinate> variation) { |
| 130 |
for (auto&& coord : variation) { |
| 131 |
if (coord.value == 0 || SkIsNaN(coord.value)) { |
| 132 |
coord.value = 0.0f; |
| 133 |
} |
| 134 |
} |
| 135 |
return variation; |
| 136 |
} |
| 137 |
|
| 138 |
static SkSpan<Coordinate> Sort(SkSpan<Coordinate> variation) { |
| 139 |
SkTQSort(variation.begin(), variation.end(), variation::coordinateLess); |
| 140 |
return variation; |
| 141 |
} |
| 142 |
|
| 143 |
} // namespace variation |
| 144 |
|
| 145 |
struct AndroidFontAPI { |
| 146 |
ASystemFontIterator* (*ASystemFontIterator_open)(); |
| 147 |
void (*ASystemFontIterator_close)(ASystemFontIterator*); |
| 148 |
AFont* (*ASystemFontIterator_next)(ASystemFontIterator*); |
| 149 |
|
| 150 |
void (*AFont_close)(AFont*); |
| 151 |
const char* (*AFont_getFontFilePath)(const AFont*); |
| 152 |
uint16_t (*AFont_getWeight)(const AFont*); |
| 153 |
bool (*AFont_isItalic)(const AFont*); |
| 154 |
const char* (*AFont_getLocale)(const AFont*); |
| 155 |
size_t (*AFont_getCollectionIndex)(const AFont*); |
| 156 |
size_t (*AFont_getAxisCount)(const AFont*); |
| 157 |
uint32_t (*AFont_getAxisTag)(const AFont*, uint32_t axisIndex); |
| 158 |
float (*AFont_getAxisValue)(const AFont*, uint32_t axisIndex); |
| 159 |
|
| 160 |
#if __ANDROID_API__ >= SK_ANDROID_NDK_FONT_API_EXISTS |
| 161 |
|
| 162 |
static std::optional<AndroidFontAPI> Make() { |
| 163 |
static AndroidFontAPI api { |
| 164 |
::ASystemFontIterator_open, |
| 165 |
::ASystemFontIterator_close, |
| 166 |
::ASystemFontIterator_next, |
| 167 |
|
| 168 |
::AFont_close, |
| 169 |
::AFont_getFontFilePath, |
| 170 |
::AFont_getWeight, |
| 171 |
::AFont_isItalic, |
| 172 |
::AFont_getLocale, |
| 173 |
::AFont_getCollectionIndex, |
| 174 |
::AFont_getAxisCount, |
| 175 |
::AFont_getAxisTag, |
| 176 |
::AFont_getAxisValue, |
| 177 |
}; |
| 178 |
if (android_get_device_api_level() < SK_ANDROID_NDK_FONT_API_LOCALE_WORKS) { |
| 179 |
return std::nullopt; |
| 180 |
} |
| 181 |
if constexpr (kSkFontMgrVerbose) { SkDebugf("SKIA: GetAndroidFontAPI direct\n"); } |
| 182 |
return api; |
| 183 |
} |
| 184 |
|
| 185 |
#else |
| 186 |
|
| 187 |
private: |
| 188 |
AndroidFontAPI() {} |
| 189 |
std::unique_ptr<void, SkFunctionObject<dlclose>> self; |
| 190 |
public: |
| 191 |
AndroidFontAPI(const AndroidFontAPI&) = delete; |
| 192 |
AndroidFontAPI& operator=(const AndroidFontAPI&) = delete; |
| 193 |
AndroidFontAPI(AndroidFontAPI&&) = default; |
| 194 |
AndroidFontAPI& operator=(AndroidFontAPI&&) = default; |
| 195 |
|
| 196 |
static std::optional<AndroidFontAPI> Make() { |
| 197 |
if (android_get_device_api_level() < SK_ANDROID_NDK_FONT_API_LOCALE_WORKS) { |
| 198 |
return std::nullopt; |
| 199 |
} |
| 200 |
|
| 201 |
AndroidFontAPI api; |
| 202 |
api.self.reset(dlopen("libandroid.so", RTLD_LAZY | RTLD_LOCAL)); |
| 203 |
if (!api.self) { |
| 204 |
return std::nullopt; |
| 205 |
} |
| 206 |
|
| 207 |
#define SK_DLSYM_ANDROID_FONT_API(NAME) \ |
| 208 |
do { \ |
| 209 |
*(void**)(&api.NAME) = dlsym(api.self.get(), #NAME); \ |
| 210 |
if (!api.NAME) { \ |
| 211 |
if constexpr (kSkFontMgrVerbose) { \ |
| 212 |
SkDebugf("SKIA: Failed to load: " #NAME "\n");\ |
| 213 |
} \ |
| 214 |
return std::nullopt; \ |
| 215 |
} \ |
| 216 |
} while (0) |
| 217 |
|
| 218 |
SK_DLSYM_ANDROID_FONT_API(ASystemFontIterator_open); |
| 219 |
SK_DLSYM_ANDROID_FONT_API(ASystemFontIterator_close); |
| 220 |
SK_DLSYM_ANDROID_FONT_API(ASystemFontIterator_next); |
| 221 |
|
| 222 |
SK_DLSYM_ANDROID_FONT_API(AFont_close); |
| 223 |
SK_DLSYM_ANDROID_FONT_API(AFont_getFontFilePath); |
| 224 |
SK_DLSYM_ANDROID_FONT_API(AFont_getWeight); |
| 225 |
SK_DLSYM_ANDROID_FONT_API(AFont_isItalic); |
| 226 |
SK_DLSYM_ANDROID_FONT_API(AFont_getLocale); |
| 227 |
SK_DLSYM_ANDROID_FONT_API(AFont_getCollectionIndex); |
| 228 |
SK_DLSYM_ANDROID_FONT_API(AFont_getAxisCount); |
| 229 |
SK_DLSYM_ANDROID_FONT_API(AFont_getAxisTag); |
| 230 |
SK_DLSYM_ANDROID_FONT_API(AFont_getAxisValue); |
| 231 |
#undef SK_DLSYM_ANDROID_FONT_API |
| 232 |
|
| 233 |
if constexpr (kSkFontMgrVerbose) { SkDebugf("SKIA: GetAndroidFontAPI dlsym\n"); } |
| 234 |
return api; |
| 235 |
} |
| 236 |
|
| 237 |
#endif |
| 238 |
}; |
| 239 |
|
| 240 |
struct AndroidIcuAPI { |
| 241 |
/* The result is case folded UTF-8. This is normalized case, it isn't upper or lower case. */ |
| 242 |
SkString casefold(SkSpan<const char> s) const { |
| 243 |
return fHasICU ? this->icuCaseFold(s) : this->cToWLower(s); |
| 244 |
} |
| 245 |
|
| 246 |
private: |
| 247 |
int32_t (*u_strFoldCase)(UChar *dest, int32_t destCapacity, const UChar *src, int32_t srcLength, |
| 248 |
uint32_t options, UErrorCode *pErrorCode) = nullptr; |
| 249 |
UChar* (*u_strFromUTF8)(UChar *dest, int32_t destCapacity, int32_t *pDestLength, |
| 250 |
const char *src, int32_t srcLength, UErrorCode *pErrorCode) = nullptr; |
| 251 |
char * (*u_strToUTF8)(char *dest, int32_t destCapacity, int32_t *pDestLength, |
| 252 |
const UChar *src, int32_t srcLength, UErrorCode *pErrorCode) = nullptr; |
| 253 |
bool fHasICU = false; |
| 254 |
|
| 255 |
SkString cToWLower(SkSpan<const char> s) const { |
| 256 |
// Find length of result |
| 257 |
size_t retLen = 0; |
| 258 |
bool isDifferent = false; |
| 259 |
const char* src = s.begin(); |
| 260 |
while (src != s.end()) { |
| 261 |
SkUnichar uni = SkUTF::NextUTF8(&src, s.end()); |
| 262 |
if (uni < 0) { |
| 263 |
return SkString(s.data(), s.size()); |
| 264 |
} |
| 265 |
// On Android 2.3 (API 9) and later wchar_t is 32 bit, UTF-32 like. |
| 266 |
// towlower only provides simple case folding, but this should be fine for Android 11. |
| 267 |
wint_t wlow = towlower(uni); |
| 268 |
char buffer[SkUTF::kMaxBytesInUTF8Sequence]; |
| 269 |
size_t buflen = SkUTF::ToUTF8(wlow, buffer); |
| 270 |
if (buflen == 0) { |
| 271 |
return SkString(s.data(), s.size()); |
| 272 |
} |
| 273 |
isDifferent |= wlow != SkToU32(uni); |
| 274 |
retLen += buflen; |
| 275 |
} |
| 276 |
|
| 277 |
// No change needed |
| 278 |
if (!isDifferent) { |
| 279 |
if constexpr (kSkFontMgrVerbose) { SkDebugf("SKIA: TL \"%s\" unchanged\n", s.data()); } |
| 280 |
return SkString(s.data(), s.size()); |
| 281 |
} |
| 282 |
|
| 283 |
// Create result |
| 284 |
SkString ret(retLen); |
| 285 |
src = s.begin(); |
| 286 |
char* dst = ret.begin(); |
| 287 |
while (src != s.end()) { |
| 288 |
SkUnichar uni = SkUTF::NextUTF8(&src, s.end()); |
| 289 |
wint_t wlow = towlower(uni); |
| 290 |
char buffer[SkUTF::kMaxBytesInUTF8Sequence]; |
| 291 |
size_t buflen = SkUTF::ToUTF8(wlow, buffer); |
| 292 |
memcpy(dst, buffer, buflen); |
| 293 |
dst += buflen; |
| 294 |
} |
| 295 |
|
| 296 |
if constexpr (kSkFontMgrVerbose) { |
| 297 |
SkDebugf("SKIA: TL \"%s\" to \"%s\"\n", s.data(), ret.c_str()); |
| 298 |
} |
| 299 |
return ret; |
| 300 |
} |
| 301 |
|
| 302 |
SkString icuCaseFold(SkSpan<const char> s) const { |
| 303 |
if (!SkTFitsIn<int32_t>(s.size())) { |
| 304 |
return SkString(s.data(), s.size()); |
| 305 |
} |
| 306 |
|
| 307 |
using Storage = AutoSTMalloc<32, UChar>; |
| 308 |
UErrorCode error = U_ZERO_ERROR; |
| 309 |
|
| 310 |
// Convert to UTF-16 |
| 311 |
int32_t uStrSize = 0; |
| 312 |
Storage uStr(Storage::kCount); |
| 313 |
this->u_strFromUTF8(uStr, Storage::kCount, &uStrSize, s.data(), s.size(), &error); |
| 314 |
if (error == U_BUFFER_OVERFLOW_ERROR) { |
| 315 |
error = U_ZERO_ERROR; |
| 316 |
} |
| 317 |
if (U_FAILURE(error)) { |
| 318 |
if constexpr (kSkFontMgrVerbose) { SkDebugf("SKIA: CF UTF-16 length\n"); } |
| 319 |
return SkString(s.data(), s.size()); |
| 320 |
} |
| 321 |
if (SkTo<int32_t>(Storage::kCount) < uStrSize) { |
| 322 |
uStr.reset(uStrSize); |
| 323 |
int32_t uStrSizePrev = uStrSize; |
| 324 |
this->u_strFromUTF8(uStr, uStrSize, &uStrSize, s.data(), s.size(), &error); |
| 325 |
if (U_FAILURE(error) || uStrSizePrev < uStrSize) { |
| 326 |
if constexpr (kSkFontMgrVerbose) { SkDebugf("SKIA: CF UTF-16 wrong length\n"); } |
| 327 |
return SkString(s.data(), s.size()); |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
// Case fold |
| 332 |
Storage uStrFolded(Storage::kCount); |
| 333 |
int32_t uStrFoldedSize = this->u_strFoldCase(uStrFolded, Storage::kCount, uStr, uStrSize, |
| 334 |
U_FOLD_CASE_EXCLUDE_SPECIAL_I, &error); |
| 335 |
if (error == U_BUFFER_OVERFLOW_ERROR) { |
| 336 |
error = U_ZERO_ERROR; |
| 337 |
} |
| 338 |
if (U_FAILURE(error)) { |
| 339 |
if constexpr (kSkFontMgrVerbose) { SkDebugf("SKIA: CF folded length\n"); } |
| 340 |
return SkString(s.data(), s.size()); |
| 341 |
} |
| 342 |
if (SkTo<int32_t>(Storage::kCount) < uStrFoldedSize) { |
| 343 |
uStrFolded.reset(uStrFoldedSize); |
| 344 |
int32_t uStrFoldedSizePrev = uStrFoldedSize; |
| 345 |
uStrFoldedSize = this->u_strFoldCase(uStrFolded, uStrFoldedSize, uStr, uStrSize, |
| 346 |
U_FOLD_CASE_EXCLUDE_SPECIAL_I, &error); |
| 347 |
if (U_FAILURE(error) || uStrFoldedSizePrev < uStrFoldedSize) { |
| 348 |
if constexpr (kSkFontMgrVerbose) { SkDebugf("SKIA: CF folded wrong length\n"); } |
| 349 |
return SkString(s.data(), s.size()); |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
// Convert to UTF-8 |
| 354 |
SkString ret; |
| 355 |
int32_t retSize = 0; |
| 356 |
this->u_strToUTF8(nullptr, 0, &retSize, uStrFolded, uStrFoldedSize, &error); |
| 357 |
if (error == U_BUFFER_OVERFLOW_ERROR) { |
| 358 |
error = U_ZERO_ERROR; |
| 359 |
} |
| 360 |
if (U_FAILURE(error)) { |
| 361 |
if constexpr (kSkFontMgrVerbose) { SkDebugf("SKIA: CF UTF-8 length\n"); } |
| 362 |
return SkString(s.data(), s.size()); |
| 363 |
} |
| 364 |
ret.resize(retSize); |
| 365 |
int32_t retSizePrev = retSize; |
| 366 |
this->u_strToUTF8(ret.data(), retSize, &retSize, uStrFolded, uStrFoldedSize, &error); |
| 367 |
if (U_FAILURE(error) || retSizePrev != retSize) { |
| 368 |
if constexpr (kSkFontMgrVerbose) { SkDebugf("SKIA: CF UTF-8 wrong length\n"); } |
| 369 |
return SkString(s.data(), s.size()); |
| 370 |
} |
| 371 |
|
| 372 |
// Return result |
| 373 |
if constexpr (kSkFontMgrVerbose) { |
| 374 |
SkDebugf("SKIA: CF \"%s\" to \"%s\"\n", s.data(), ret.c_str()); |
| 375 |
} |
| 376 |
return ret; |
| 377 |
} |
| 378 |
|
| 379 |
#if __ANDROID_API__ >= __ANDROID_API_S__ |
| 380 |
public: |
| 381 |
AndroidIcuAPI() |
| 382 |
: u_strFoldCase(::u_strFoldCase) |
| 383 |
, u_strFromUTF8(::u_strFromUTF8) |
| 384 |
, u_strToUTF8(::u_strToUTF8) |
| 385 |
, fHasICU(true) |
| 386 |
{ |
| 387 |
if constexpr (kSkFontMgrVerbose) { SkDebugf("SKIA: GetAndroidIcuAPI direct\n"); } |
| 388 |
} |
| 389 |
|
| 390 |
#else |
| 391 |
|
| 392 |
private: |
| 393 |
std::unique_ptr<void, SkFunctionObject<dlclose>> fSelf; |
| 394 |
public: |
| 395 |
AndroidIcuAPI(const AndroidIcuAPI&) = delete; |
| 396 |
AndroidIcuAPI& operator=(const AndroidIcuAPI&) = delete; |
| 397 |
AndroidIcuAPI(AndroidIcuAPI&&) = default; |
| 398 |
AndroidIcuAPI& operator=(AndroidIcuAPI&&) = default; |
| 399 |
|
| 400 |
AndroidIcuAPI() { |
| 401 |
fSelf.reset(dlopen("libicu.so", RTLD_LAZY | RTLD_LOCAL)); |
| 402 |
if (!fSelf) { |
| 403 |
return; |
| 404 |
} |
| 405 |
|
| 406 |
#define SK_DLSYM_ANDROID_ICU_API(NAME) \ |
| 407 |
do { \ |
| 408 |
*(void**)(&NAME) = dlsym(fSelf.get(), #NAME); \ |
| 409 |
if (!NAME) { \ |
| 410 |
if constexpr (kSkFontMgrVerbose) { \ |
| 411 |
SkDebugf("SKIA: Failed to load: " #NAME "\n");\ |
| 412 |
} \ |
| 413 |
return; \ |
| 414 |
} \ |
| 415 |
} while (0) |
| 416 |
|
| 417 |
SK_DLSYM_ANDROID_ICU_API(u_strFoldCase); |
| 418 |
SK_DLSYM_ANDROID_ICU_API(u_strFromUTF8); |
| 419 |
SK_DLSYM_ANDROID_ICU_API(u_strToUTF8); |
| 420 |
#undef SK_DLSYM_ANDROID_ICU_API |
| 421 |
|
| 422 |
fHasICU = true; |
| 423 |
if constexpr (kSkFontMgrVerbose) { SkDebugf("SKIA: GetAndroidIcuAPI dlsym\n"); } |
| 424 |
} |
| 425 |
#endif |
| 426 |
}; |
| 427 |
|
| 428 |
// bcp47 is ascii only and only does 1:1 replacements for case folding. |
| 429 |
static void normalizeAsciiCase(SkSpan<char> s) { |
| 430 |
std::transform(s.begin(), s.end(), s.begin(), |
| 431 |
[](char c){ return (c < 'A' || 'Z' < c) ? c : c + 'a' - 'A'; }); |
| 432 |
} |
| 433 |
|
| 434 |
struct SkAFont { |
| 435 |
SkAFont(const AndroidFontAPI& api, AFont* font) : fAPI(api), fFont(font) {} |
| 436 |
SkAFont(SkAFont&& that) : fAPI(that.fAPI), fFont(that.fFont) { |
| 437 |
that.fFont = nullptr; |
| 438 |
} |
| 439 |
~SkAFont() { |
| 440 |
if (fFont) { |
| 441 |
if constexpr (kSkFontMgrVerbose) { SkDebugf("SKIA: AFont_close\n"); } |
| 442 |
fAPI.AFont_close(fFont); |
| 443 |
} |
| 444 |
} |
| 445 |
explicit operator bool() { return fFont; } |
| 446 |
|
| 447 |
const char* getFontFilePath() const { return fAPI.AFont_getFontFilePath(fFont); } |
| 448 |
uint16_t getWeight() const { return fAPI.AFont_getWeight(fFont); } |
| 449 |
bool isItalic() const { return fAPI.AFont_isItalic(fFont); } |
| 450 |
const char* getLocale() const { return fAPI.AFont_getLocale(fFont); } |
| 451 |
size_t getCollectionIndex() const { return fAPI.AFont_getCollectionIndex(fFont); } |
| 452 |
size_t getAxisCount() const { return fAPI.AFont_getAxisCount(fFont); } |
| 453 |
uint32_t getAxisTag(uint32_t index) const { return fAPI.AFont_getAxisTag(fFont, index); } |
| 454 |
float getAxisValue(uint32_t index) const { return fAPI.AFont_getAxisValue(fFont, index); } |
| 455 |
|
| 456 |
private: |
| 457 |
const AndroidFontAPI& fAPI; |
| 458 |
AFont* fFont; |
| 459 |
}; |
| 460 |
|
| 461 |
struct SkASystemFontIterator { |
| 462 |
SkASystemFontIterator(const AndroidFontAPI& api) |
| 463 |
: fAPI(api) |
| 464 |
, fIterator(fAPI.ASystemFontIterator_open()) |
| 465 |
{ |
| 466 |
if constexpr (kSkFontMgrVerbose) { SkDebugf("SKIA: ASystemFontIterator_open\n"); } |
| 467 |
} |
| 468 |
SkASystemFontIterator(SkASystemFontIterator&&) = default; |
| 469 |
~SkASystemFontIterator() { |
| 470 |
if constexpr (kSkFontMgrVerbose) { SkDebugf("SKIA: ASystemFontIterator_close\n"); } |
| 471 |
fAPI.ASystemFontIterator_close(fIterator); |
| 472 |
} |
| 473 |
explicit operator bool() { return fIterator; } |
| 474 |
|
| 475 |
SkAFont next() { return SkAFont(fAPI, fAPI.ASystemFontIterator_next(fIterator)); } |
| 476 |
|
| 477 |
private: |
| 478 |
const AndroidFontAPI& fAPI; |
| 479 |
ASystemFontIterator* const fIterator; |
| 480 |
}; |
| 481 |
|
| 482 |
class SkALanguage { |
| 483 |
public: |
| 484 |
SkALanguage() {} |
| 485 |
SkALanguage(const char* tag) : SkALanguage(SkSpan<const char>(tag, tag ? strlen(tag) : 0)) {} |
| 486 |
SkALanguage(SkSpan<const char> tag) { |
| 487 |
fLanguage = consumeSubtag(tag); |
| 488 |
if (fLanguage.equals("und", 3)) { |
| 489 |
fLanguage = SkString(); |
| 490 |
} |
| 491 |
fScript = consumeSubtag(tag); |
| 492 |
fRegion = consumeSubtag(tag); |
| 493 |
} |
| 494 |
SkALanguage(const SkALanguage&) = default; |
| 495 |
SkALanguage& operator=(const SkALanguage& b) = default; |
| 496 |
|
| 497 |
SkALanguage lessSpecific() const { |
| 498 |
SkALanguage lessSpecific(*this); |
| 499 |
if (!lessSpecific.fRegion.isEmpty()) { |
| 500 |
lessSpecific.fRegion = SkString(); |
| 501 |
return lessSpecific; |
| 502 |
} |
| 503 |
if (!lessSpecific.fScript.isEmpty()) { |
| 504 |
lessSpecific.fScript = SkString(); |
| 505 |
return lessSpecific; |
| 506 |
} |
| 507 |
|
| 508 |
if (!lessSpecific.fLanguage.isEmpty()) { |
| 509 |
lessSpecific.fLanguage = SkString(); |
| 510 |
return lessSpecific; |
| 511 |
} |
| 512 |
return lessSpecific; |
| 513 |
} |
| 514 |
|
| 515 |
bool isEmpty() const { |
| 516 |
return fLanguage.isEmpty() && fScript.isEmpty() && fRegion.isEmpty(); |
| 517 |
} |
| 518 |
|
| 519 |
bool startsWith(const SkALanguage& that) { |
| 520 |
return (that.fLanguage.isEmpty() || fLanguage == that.fLanguage) && |
| 521 |
(that.fScript.isEmpty() || fScript == that.fScript) && |
| 522 |
(that.fRegion.isEmpty() || fRegion == that.fRegion); |
| 523 |
} |
| 524 |
|
| 525 |
const SkString& getLanguage() const { return fLanguage; } |
| 526 |
const SkString& getScript() const { return fScript; } |
| 527 |
const SkString& getRegion() const { return fRegion; } |
| 528 |
|
| 529 |
|
| 530 |
using sk_is_trivially_relocatable = std::true_type; |
| 531 |
private: |
| 532 |
SkString consumeSubtag(SkSpan<const char>& tag) { |
| 533 |
if (tag.size() == 0) { |
| 534 |
return SkString(); |
| 535 |
} |
| 536 |
const char* subtagEnd = static_cast<const char*>(memchr(tag.data(), '-', tag.size())); |
| 537 |
if (!subtagEnd) { |
| 538 |
SkString ret(tag.data(), tag.size()); |
| 539 |
tag = SkSpan<const char>(); |
| 540 |
return ret; |
| 541 |
} |
| 542 |
size_t subtagLength = subtagEnd - tag.data(); |
| 543 |
SkString ret(tag.data(), subtagLength); |
| 544 |
normalizeAsciiCase({ret.begin(), ret.size()}); |
| 545 |
tag = tag.subspan(subtagLength + 1); |
| 546 |
return ret; |
| 547 |
} |
| 548 |
SkString fLanguage; |
| 549 |
SkString fScript; |
| 550 |
SkString fRegion; |
| 551 |
// variants, extensions, and privateuses are ignored as no known font configuration uses them. |
| 552 |
static_assert(::sk_is_trivially_relocatable<decltype(fLanguage)>::value); |
| 553 |
static_assert(::sk_is_trivially_relocatable<decltype(fScript)>::value); |
| 554 |
static_assert(::sk_is_trivially_relocatable<decltype(fRegion)>::value); |
| 555 |
}; |
| 556 |
|
| 557 |
class SkTypeface_AndroidNDK : public SkTypeface_proxy { |
| 558 |
public: |
| 559 |
struct AutoAxis { |
| 560 |
static constexpr struct KnownAxis { |
| 561 |
SkFourByteTag tag; |
| 562 |
int flag; |
| 563 |
} kKnownAxis[] = { |
| 564 |
{ variation::wghtTag, 1 << 0 }, |
| 565 |
{ variation::wdthTag, 1 << 1 }, |
| 566 |
{ variation::slntTag, 1 << 2 }, |
| 567 |
{ variation::italTag, 1 << 3 }, |
| 568 |
}; |
| 569 |
|
| 570 |
AutoAxis() = default; |
| 571 |
AutoAxis(const AutoAxis&) = default; |
| 572 |
AutoAxis& operator=(const AutoAxis&) = default; |
| 573 |
AutoAxis(SkSpan<const SkFontArguments::VariationPosition::Coordinate> pos) { |
| 574 |
for (auto&& coord : pos) { |
| 575 |
for (auto&& axis : kKnownAxis) { |
| 576 |
if (coord.axis == axis.tag) { |
| 577 |
fFlags |= axis.flag; |
| 578 |
} |
| 579 |
} |
| 580 |
} |
| 581 |
} |
| 582 |
|
| 583 |
void remove(const AutoAxis& that) { |
| 584 |
fFlags &= ~that.fFlags; |
| 585 |
} |
| 586 |
|
| 587 |
bool weight() const { return SkToBool(fFlags & kKnownAxis[0].flag); } |
| 588 |
bool width() const { return SkToBool(fFlags & kKnownAxis[1].flag); } |
| 589 |
bool slant() const { return SkToBool(fFlags & kKnownAxis[2].flag); } |
| 590 |
bool italic() const { return SkToBool(fFlags & kKnownAxis[3].flag); } |
| 591 |
bool none() const { return fFlags == 0; } |
| 592 |
uint8_t fFlags = 0; |
| 593 |
}; |
| 594 |
static sk_sp<SkTypeface_AndroidNDK> Make(sk_sp<SkTypeface> realTypeface, |
| 595 |
const SkFontStyle& style, |
| 596 |
bool isFixedPitch, |
| 597 |
const SkString& familyName, |
| 598 |
TArray<SkString>&& extraFamilyNames, |
| 599 |
TArray<SkALanguage>&& lang, |
| 600 |
const AutoAxis& autoAxis) { |
| 601 |
SkASSERT(realTypeface); |
| 602 |
return sk_sp<SkTypeface_AndroidNDK>(new SkTypeface_AndroidNDK(std::move(realTypeface), |
| 603 |
style, |
| 604 |
isFixedPitch, |
| 605 |
familyName, |
| 606 |
std::move(extraFamilyNames), |
| 607 |
std::move(lang), |
| 608 |
autoAxis)); |
| 609 |
} |
| 610 |
|
| 611 |
private: |
| 612 |
SkTypeface_AndroidNDK(sk_sp<SkTypeface> realTypeface, |
| 613 |
const SkFontStyle& style, |
| 614 |
bool isFixedPitch, |
| 615 |
const SkString& familyName, |
| 616 |
TArray<SkString>&& extraFamilyNames, |
| 617 |
TArray<SkALanguage>&& lang, |
| 618 |
const AutoAxis& autoAxis) |
| 619 |
: SkTypeface_proxy(std::move(realTypeface), style, isFixedPitch) |
| 620 |
, fFamilyName(familyName) |
| 621 |
, fExtraFamilyNames(std::move(extraFamilyNames)) |
| 622 |
, fLang(std::move(lang)) |
| 623 |
, fAutoAxis(autoAxis) |
| 624 |
{ } |
| 625 |
|
| 626 |
void onGetFamilyName(SkString* familyName) const override { |
| 627 |
*familyName = fFamilyName; |
| 628 |
} |
| 629 |
|
| 630 |
void onGetFontDescriptor(SkFontDescriptor* desc, bool* serialize) const override { |
| 631 |
SkASSERT(desc); |
| 632 |
SkASSERT(serialize); |
| 633 |
SkTypeface_proxy::onGetFontDescriptor(desc, serialize); |
| 634 |
desc->setFamilyName(fFamilyName.c_str()); |
| 635 |
desc->setStyle(this->fontStyle()); |
| 636 |
*serialize = false; |
| 637 |
} |
| 638 |
|
| 639 |
sk_sp<SkTypeface> onMakeClone(const SkFontArguments& args) const override { |
| 640 |
auto proxy = SkTypeface_proxy::onMakeClone(args); |
| 641 |
if (proxy == nullptr) { |
| 642 |
return nullptr; |
| 643 |
} |
| 644 |
SkFontStyle style = proxy->fontStyle(); |
| 645 |
bool fixedPitch = proxy->isFixedPitch(); |
| 646 |
return SkTypeface_AndroidNDK::Make( |
| 647 |
std::move(proxy), |
| 648 |
style, |
| 649 |
fixedPitch, |
| 650 |
fFamilyName, |
| 651 |
TArray<SkString>(fExtraFamilyNames), |
| 652 |
TArray<SkALanguage>(), |
| 653 |
AutoAxis()); |
| 654 |
} |
| 655 |
|
| 656 |
SkFontStyle onGetFontStyle() const override { |
| 657 |
return SkTypeface::onGetFontStyle(); |
| 658 |
} |
| 659 |
|
| 660 |
bool onGetFixedPitch() const override { |
| 661 |
return SkTypeface::onGetFixedPitch(); |
| 662 |
} |
| 663 |
|
| 664 |
SkTypeface::LocalizedStrings* onCreateFamilyNameIterator() const override { |
| 665 |
class ALocalizedStrings : public SkTypeface::LocalizedStrings { |
| 666 |
public: |
| 667 |
ALocalizedStrings(sk_sp<SkTypeface_AndroidNDK> typeface, |
| 668 |
sk_sp<SkTypeface::LocalizedStrings> base) |
| 669 |
: fTypeface(std::move(typeface)) |
| 670 |
, fBase(std::move(base)) |
| 671 |
, fExtraFamilyName(nullptr) {} |
| 672 |
private: |
| 673 |
sk_sp<SkTypeface_AndroidNDK> fTypeface; |
| 674 |
sk_sp<SkTypeface::LocalizedStrings> fBase; |
| 675 |
const SkString* fExtraFamilyName; |
| 676 |
|
| 677 |
bool next(LocalizedString* localizedString) override { |
| 678 |
if (!fExtraFamilyName) { |
| 679 |
if (fBase->next(localizedString)) { |
| 680 |
return true; |
| 681 |
} |
| 682 |
fExtraFamilyName = fTypeface->fExtraFamilyNames.begin(); |
| 683 |
} |
| 684 |
if (fExtraFamilyName == fTypeface->fExtraFamilyNames.end()) { |
| 685 |
return false; |
| 686 |
} |
| 687 |
*localizedString = {*fExtraFamilyName, SkString()}; |
| 688 |
++fExtraFamilyName; |
| 689 |
return true; |
| 690 |
} |
| 691 |
}; |
| 692 |
sk_sp<SkTypeface::LocalizedStrings> base(SkTypeface_proxy::onCreateFamilyNameIterator()); |
| 693 |
return new ALocalizedStrings(sk_ref_sp(this), std::move(base)); |
| 694 |
} |
| 695 |
|
| 696 |
public: |
| 697 |
const SkString fFamilyName; |
| 698 |
const TArray<SkString> fExtraFamilyNames; |
| 699 |
const STArray<4, SkALanguage> fLang; |
| 700 |
const AutoAxis fAutoAxis; |
| 701 |
}; |
| 702 |
|
| 703 |
class TypefaceCache : public SkRefCnt { |
| 704 |
public: |
| 705 |
TypefaceCache() : fRequests(64), fMatches(64) {} |
| 706 |
~TypefaceCache() override {} |
| 707 |
|
| 708 |
class Request { |
| 709 |
public: |
| 710 |
Request(SkTypefaceID id, SkFontStyle style) : fId(id), fStyle(style) { |
| 711 |
SkGoodHash hasher; |
| 712 |
fHash = hasher(fId); |
| 713 |
fHash ^= hasher(fStyle); |
| 714 |
} |
| 715 |
bool operator==(const Request& that) const { |
| 716 |
return fId == that.fId && fStyle == that.fStyle; |
| 717 |
} |
| 718 |
struct Hash { uint32_t operator()(const Request& a) { return a.fHash; } }; |
| 719 |
private: |
| 720 |
const SkTypefaceID fId; |
| 721 |
const SkFontStyle fStyle; |
| 722 |
uint32_t fHash; |
| 723 |
}; |
| 724 |
sk_sp<SkTypeface> find(const Request& request) { |
| •725 |
SkAutoMutexExclusive lock(fMutex); // SkLRUCache::find() is mutating |
| 726 |
sk_sp<SkTypeface>* typeface = fRequests.find(request); |
| 727 |
if (typeface) { |
| 728 |
return *typeface; |
| 729 |
} |
| 730 |
return nullptr; |
| 731 |
} |
| 732 |
void add(const Request& request, sk_sp<SkTypeface> typeface) { |
| •733 |
SkAutoMutexExclusive lock(fMutex); |
| 734 |
fRequests.insert_or_update(request, std::move(typeface)); |
| 735 |
} |
| 736 |
|
| 737 |
class Match { |
| 738 |
public: |
| 739 |
/* variation is expected to be normalized and sorted. */ |
| 740 |
Match(SkTypefaceID id, variation::Storage&& variation) |
| 741 |
: fId(id) |
| 742 |
, fVariation(std::move(variation)) |
| 743 |
{ |
| 744 |
SkGoodHash hasher; |
| 745 |
fHash = hasher(id); |
| 746 |
for (auto&& coord : fVariation) { |
| 747 |
fHash ^= hasher(coord.axis); |
| 748 |
fHash ^= hasher(FloatBits(coord.value)); |
| 749 |
} |
| 750 |
} |
| 751 |
Match(const Match&) = delete; |
| 752 |
Match& operator=(const Match&) = delete; |
| 753 |
Match(Match&& that) : fId(std::move(that.fId)) |
| 754 |
, fVariation(std::move(that.fVariation)) |
| 755 |
, fHash(std::move(that.fHash)) {} |
| 756 |
Match& operator=(Match&&) = delete; |
| 757 |
bool operator==(const Match& that) const { |
| 758 |
return fId == that.fId && |
| 759 |
fVariation.size() == that.fVariation.size() && |
| 760 |
std::equal(fVariation.begin(), fVariation.end(), |
| 761 |
that.fVariation.begin(), variation::coordinateEqual); |
| 762 |
} |
| 763 |
struct Hash { uint32_t operator()(const Match& a) { return a.fHash; } }; |
| 764 |
private: |
| 765 |
static uint32_t FloatBits(float f) { |
| 766 |
static_assert(sizeof(uint32_t) == sizeof(float)); |
| 767 |
uint32_t bits; |
| 768 |
std::memcpy(&bits, &f, sizeof(uint32_t)); |
| 769 |
return bits; |
| 770 |
} |
| 771 |
const SkTypefaceID fId; |
| 772 |
variation::Storage fVariation; |
| 773 |
uint32_t fHash; |
| 774 |
}; |
| 775 |
sk_sp<SkTypeface> find(const Match& match) { |
| •776 |
SkAutoMutexExclusive lock(fMutex); |
| 777 |
sk_sp<SkTypeface>* typeface = fMatches.find(match); |
| 778 |
if (typeface) { |
| 779 |
return *typeface; |
| 780 |
} |
| 781 |
return nullptr; |
| 782 |
} |
| 783 |
void add(Match&& match, sk_sp<SkTypeface> typeface) { |
| •784 |
SkAutoMutexExclusive lock(fMutex); |
| 785 |
fMatches.insert_or_update(std::move(match), typeface); |
| 786 |
} |
| 787 |
private: |
| 788 |
SkLRUCache<Request, sk_sp<SkTypeface>, Request::Hash> fRequests; |
| 789 |
SkLRUCache<Match, sk_sp<SkTypeface>, Match::Hash> fMatches; |
| •790 |
SkMutex fMutex; |
| 791 |
}; |
| 792 |
|
| 793 |
sk_sp<SkTypeface> adjustForStyle(sk_sp<SkTypeface_AndroidNDK>&& typeface, SkFontStyle style, |
| 794 |
TypefaceCache& cache) { |
| 795 |
if (!typeface) { |
| 796 |
return std::move(typeface); |
| 797 |
} |
| 798 |
|
| 799 |
SkFontStyle typefaceStyle = typeface->fontStyle(); |
| 800 |
if (typefaceStyle == style || typeface->fAutoAxis.none()) { |
| 801 |
return std::move(typeface); |
| 802 |
} |
| 803 |
|
| 804 |
SkFontArguments::VariationPosition::Coordinate coord[4]; |
| 805 |
int numCoords = 0; |
| 806 |
if (typefaceStyle.weight() != style.weight() && typeface->fAutoAxis.weight()) { |
| 807 |
coord[numCoords++] = {variation::wghtTag, static_cast<float>(style.weight())}; |
| 808 |
} |
| 809 |
if (typefaceStyle.width() != style.width() && typeface->fAutoAxis.width()) { |
| 810 |
coord[numCoords++] = {variation::wdthTag, |
| 811 |
SkFontDescriptor::SkFontWidthAxisValueForStyleWidth(style.width())}; |
| 812 |
} |
| 813 |
if (typefaceStyle.slant() != style.slant()) { |
| 814 |
switch (style.slant()) { |
| 815 |
case SkFontStyle::Slant::kItalic_Slant: |
| 816 |
if (typeface->fAutoAxis.italic()) { |
| 817 |
coord[numCoords++] = {variation::italTag, 1.0}; |
| 818 |
} |
| 819 |
break; |
| 820 |
case SkFontStyle::Slant::kOblique_Slant: |
| 821 |
if (typeface->fAutoAxis.slant()) { |
| 822 |
coord[numCoords++] = {variation::slntTag, -7.0}; |
| 823 |
} |
| 824 |
break; |
| 825 |
case SkFontStyle::Slant::kUpright_Slant: |
| 826 |
if (typeface->fAutoAxis.italic()) { |
| 827 |
coord[numCoords++] = {variation::italTag, 0.0}; |
| 828 |
} |
| 829 |
if (typeface->fAutoAxis.slant()) { |
| 830 |
coord[numCoords++] = {variation::slntTag, 0.0}; |
| 831 |
} |
| 832 |
break; |
| 833 |
} |
| 834 |
} |
| 835 |
if (numCoords == 0) { |
| 836 |
return std::move(typeface); |
| 837 |
} |
| 838 |
|
| 839 |
TypefaceCache::Request request(typeface->uniqueID(), style); |
| 840 |
if (sk_sp<SkTypeface> cachedTypeface = cache.find(request)) { |
| 841 |
if constexpr (kSkFontMgrVerbose) { |
| 842 |
SkString familyName; |
| 843 |
typeface->getFamilyName(&familyName); |
| 844 |
SkFontStyle s = cachedTypeface->fontStyle(); |
| 845 |
SkDebugf("Cached request of \"%s\" weight:%d width: %d slant %d\n", |
| 846 |
familyName.c_str(), s.weight(), s.width(), s.slant()); |
| 847 |
} |
| 848 |
return cachedTypeface; |
| 849 |
} |
| 850 |
|
| 851 |
sk_sp<SkTypeface> newTypeface = typeface->makeClone( |
| 852 |
SkFontArguments().setVariationDesignPosition({coord, numCoords})); |
| 853 |
if (!newTypeface) { |
| 854 |
if constexpr (kSkFontMgrVerbose) { |
| 855 |
SkString familyName; |
| 856 |
typeface->getFamilyName(&familyName); |
| 857 |
SkDebugf("Failed to clone \"%s\"\n", familyName.c_str()); |
| 858 |
} |
| 859 |
return std::move(typeface); |
| 860 |
} |
| 861 |
|
| 862 |
variation::Storage variationStorage; |
| 863 |
SkSpan<variation::Coordinate> newVariation = variation::Get(*newTypeface, variationStorage); |
| 864 |
variation::Sort(variation::Normalize(newVariation)); |
| 865 |
variationStorage.trimTo(newVariation.size()); |
| 866 |
TypefaceCache::Match match(typeface->uniqueID(), std::move(variationStorage)); |
| 867 |
if (sk_sp<SkTypeface> cachedTypeface = cache.find(match)) { |
| 868 |
if constexpr (kSkFontMgrVerbose) { |
| 869 |
SkString familyName; |
| 870 |
typeface->getFamilyName(&familyName); |
| 871 |
SkFontStyle s = cachedTypeface->fontStyle(); |
| 872 |
SkDebugf("Cached match of \"%s\" weight:%d width: %d slant %d\n", |
| 873 |
familyName.c_str(), s.weight(), s.width(), s.slant()); |
| 874 |
} |
| 875 |
cache.add(std::move(request), cachedTypeface); |
| 876 |
return cachedTypeface; |
| 877 |
} |
| 878 |
|
| 879 |
if constexpr (kSkFontMgrVerbose) { |
| 880 |
SkString familyName; |
| 881 |
typeface->getFamilyName(&familyName); |
| 882 |
SkFontStyle s = newTypeface->fontStyle(); |
| 883 |
SkDebugf("New variant of \"%s\" weight:%d width: %d slant %d\n", |
| 884 |
familyName.c_str(), s.weight(), s.width(), s.slant()); |
| 885 |
} |
| 886 |
cache.add(std::move(match), newTypeface); |
| 887 |
cache.add(std::move(request), newTypeface); |
| 888 |
return newTypeface; |
| 889 |
} |
| 890 |
|
| 891 |
class SkFontStyleSet_AndroidNDK : public SkFontStyleSet { |
| 892 |
public: |
| 893 |
explicit SkFontStyleSet_AndroidNDK(sk_sp<TypefaceCache> cache) : fCache(std::move(cache)) {} |
| 894 |
|
| 895 |
int count() override { |
| 896 |
return fStyles.size(); |
| 897 |
} |
| 898 |
void getStyle(int index, SkFontStyle* style, SkString* name) override { |
| 899 |
if (index < 0 || fStyles.size() <= index) { |
| 900 |
return; |
| 901 |
} |
| 902 |
if (style) { |
| 903 |
*style = fStyles[index]->fontStyle(); |
| 904 |
} |
| 905 |
if (name) { |
| 906 |
name->reset(); |
| 907 |
} |
| 908 |
} |
| 909 |
sk_sp<SkTypeface_AndroidNDK> createATypeface(int index) { |
| 910 |
if (index < 0 || fStyles.size() <= index) { |
| 911 |
return nullptr; |
| 912 |
} |
| 913 |
return fStyles[index]; |
| 914 |
} |
| 915 |
sk_sp<SkTypeface> createTypeface(int index) override { |
| 916 |
return createATypeface(index); |
| 917 |
} |
| 918 |
|
| 919 |
sk_sp<SkTypeface_AndroidNDK> matchAStyle(const SkFontStyle& pattern) { |
| 920 |
sk_sp<SkTypeface> match = this->matchStyleCSS3(pattern); |
| 921 |
sk_sp<SkTypeface_AndroidNDK> amatch(static_cast<SkTypeface_AndroidNDK*>(match.release())); |
| 922 |
if constexpr (kSkFontMgrVerbose) { |
| 923 |
SkString name; |
| 924 |
amatch->getFamilyName(&name); |
| 925 |
SkString resourceName; |
| 926 |
amatch->getResourceName(&resourceName); |
| 927 |
SkFontStyle fontStyle = amatch->fontStyle(); |
| 928 |
SkDebugf("SKIA: Search for [%d, %d, %d] matched %s [%d, %d, %d] %s\n", |
| 929 |
pattern.weight(), pattern.width(), pattern.slant(), |
| 930 |
name.c_str(), fontStyle.weight(), fontStyle.width(), fontStyle.slant(), |
| 931 |
resourceName.c_str()); |
| 932 |
} |
| 933 |
return amatch; |
| 934 |
} |
| 935 |
sk_sp<SkTypeface> matchStyle(const SkFontStyle& pattern) override { |
| 936 |
return adjustForStyle(this->matchAStyle(pattern), pattern, *fCache); |
| 937 |
} |
| 938 |
|
| 939 |
private: |
| 940 |
TArray<sk_sp<SkTypeface_AndroidNDK>> fStyles; |
| 941 |
sk_sp<TypefaceCache> fCache; |
| 942 |
friend class SkFontMgr_AndroidNDK; |
| 943 |
}; |
| 944 |
|
| 945 |
struct NameToFamily { |
| 946 |
SkString name; |
| 947 |
SkString normalizedName; |
| 948 |
SkFontStyleSet_AndroidNDK* styleSet; |
| 949 |
|
| 950 |
using sk_is_trivially_relocatable = std::true_type; |
| 951 |
static_assert(::sk_is_trivially_relocatable<decltype(name)>::value); |
| 952 |
static_assert(::sk_is_trivially_relocatable<decltype(normalizedName)>::value); |
| 953 |
static_assert(::sk_is_trivially_relocatable<decltype(styleSet)>::value); |
| 954 |
}; |
| 955 |
|
| 956 |
class SkFontMgr_AndroidNDK : public SkFontMgr { |
| 957 |
void addSystemTypeface(sk_sp<SkTypeface_AndroidNDK> typeface, const SkString& name, |
| 958 |
bool isFallback) { |
| 959 |
NameToFamily* nameToFamily = nullptr; |
| 960 |
SkString normalizedName(fICU.casefold({name.data(), name.size()})); |
| 961 |
size_t index = 0; |
| 962 |
for (NameToFamily& current : fNameToFamilyMap) { |
| 963 |
if (current.normalizedName == normalizedName) { |
| 964 |
nameToFamily = ¤t; |
| 965 |
index = ¤t - fNameToFamilyMap.data(); |
| 966 |
break; |
| 967 |
} |
| 968 |
} |
| 969 |
if (!nameToFamily) { |
| 970 |
sk_sp<SkFontStyleSet_AndroidNDK> newSet(new SkFontStyleSet_AndroidNDK(fCache)); |
| 971 |
nameToFamily = &fNameToFamilyMap.emplace_back( |
| 972 |
NameToFamily{name, normalizedName, newSet.get()}); |
| 973 |
index = fFallbackStyleSets.size(); |
| 974 |
fFallbackStyleSets.push_back(nullptr); |
| 975 |
fStyleSets.push_back(std::move(newSet)); |
| 976 |
} |
| 977 |
if constexpr (kSkFontMgrVerbose) { SkDebugf("SKIA: Adding member to %s\n", name.c_str()); } |
| 978 |
if (isFallback) { |
| 979 |
if constexpr (kSkFontMgrVerbose) { SkDebugf("SKIA: Adding it to fallback\n"); } |
| 980 |
fFallbackStyleSets[index] = nameToFamily->styleSet; |
| 981 |
} |
| 982 |
nameToFamily->styleSet->fStyles.push_back(std::move(typeface)); |
| 983 |
} |
| 984 |
|
| 985 |
public: |
| 986 |
SkFontMgr_AndroidNDK(AndroidFontAPI&& fontAPI, bool const cacheFontFiles, |
| 987 |
std::unique_ptr<SkFontScanner> scanner) |
| 988 |
: fAPI(std::move(fontAPI)) |
| 989 |
, fScanner(std::move(scanner)) |
| 990 |
, fCache(new TypefaceCache()) |
| 991 |
{ |
| 992 |
SkASystemFontIterator fontIter(fAPI); |
| 993 |
if (!fontIter) { |
| 994 |
if constexpr (kSkFontMgrVerbose) { SkDebugf("SKIA: No ASystemFontIterator"); } |
| 995 |
return; |
| 996 |
} |
| 997 |
|
| 998 |
std::vector<std::unique_ptr<FontFamily>> xmlFamilies; |
| 999 |
SkFontMgr_Android_Parser::GetSystemFontFamilies(xmlFamilies); |
| 1000 |
|
| 1001 |
skia_private::THashMap<SkString, std::unique_ptr<SkStreamAsset>> streamForPath; |
| 1002 |
|
| 1003 |
if constexpr (kSkFontMgrVerbose) { SkDebugf("SKIA: Iterating over AFonts\n"); } |
| 1004 |
while (SkAFont font = fontIter.next()) { |
| 1005 |
sk_sp<SkTypeface_AndroidNDK> typeface = this->make(font, xmlFamilies, streamForPath); |
| 1006 |
if (!typeface) { |
| 1007 |
continue; |
| 1008 |
} |
| 1009 |
|
| 1010 |
SkString name; |
| 1011 |
typeface->getFamilyName(&name); |
| 1012 |
this->addSystemTypeface(typeface, name, true); |
| 1013 |
|
| 1014 |
// A font may have many localized family names. |
| 1015 |
sk_sp<SkTypeface::LocalizedStrings> names(typeface->createFamilyNameIterator()); |
| 1016 |
SkTypeface::LocalizedString localeName; |
| 1017 |
while (names->next(&localeName)) { |
| 1018 |
if (localeName.fString != name) { |
| 1019 |
this->addSystemTypeface(typeface, localeName.fString, false); |
| 1020 |
} |
| 1021 |
} |
| 1022 |
} |
| 1023 |
|
| 1024 |
// xmlFamilies may have an entry with "sans-serif" but there may be no matches (even for |
| 1025 |
// just file name, like on Xiaomi HyperOS devices). In such cases assume Roboto (if it |
| 1026 |
// exists) should be used for "sans-serif" and aliases. |
| 1027 |
[&]() { |
| 1028 |
if (this->onMatchAFamily("sans-serif")) { |
| 1029 |
return; |
| 1030 |
} |
| 1031 |
if constexpr (kSkFontMgrVerbose) { SkDebugf("SKIA: Found no sans-serif matches\n"); } |
| 1032 |
|
| 1033 |
auto&& matchXmlFamily = [&xmlFamilies](std::string_view familyName) -> FontFamily* { |
| 1034 |
for (const std::unique_ptr<FontFamily>& xmlFamily : xmlFamilies) { |
| 1035 |
for (auto&& xmlName : xmlFamily->fNames) { |
| 1036 |
if (xmlName.equals(familyName.data(), familyName.size())) { |
| 1037 |
return &*xmlFamily; |
| 1038 |
} |
| 1039 |
} |
| 1040 |
} |
| 1041 |
return nullptr; |
| 1042 |
}; |
| 1043 |
FontFamily* xmlSansSerif = matchXmlFamily("sans-serif"); |
| 1044 |
if (!xmlSansSerif) { |
| 1045 |
return; |
| 1046 |
} |
| 1047 |
if constexpr (kSkFontMgrVerbose) { SkDebugf("SKIA: Found xml sans-serif\n"); } |
| 1048 |
|
| 1049 |
sk_sp<SkFontStyleSet_AndroidNDK> roboto = this->onMatchAFamily("Roboto"); |
| 1050 |
if (!roboto) { |
| 1051 |
return; |
| 1052 |
} |
| 1053 |
if constexpr (kSkFontMgrVerbose) { SkDebugf("SKIA: Found Roboto\n"); } |
| 1054 |
|
| 1055 |
for (auto&& xmlName : xmlSansSerif->fNames) { |
| 1056 |
if constexpr (kSkFontMgrVerbose) { SkDebugf("SKIA: Alias %s\n", xmlName.c_str()); } |
| 1057 |
SkString normalizedName(fICU.casefold({xmlName.data(), xmlName.size()})); |
| 1058 |
fNameToFamilyMap.emplace_back(NameToFamily{xmlName, normalizedName, roboto.get()}); |
| 1059 |
} |
| 1060 |
}(); |
| 1061 |
|
| 1062 |
// fFallbackStyleSets was populated by addSystemTypeface, remove all nullptr but keep order. |
| 1063 |
SkFontStyleSet_AndroidNDK** newEnd = |
| 1064 |
std::remove(fFallbackStyleSets.begin(), fFallbackStyleSets.end(), nullptr); |
| 1065 |
fFallbackStyleSets.resize_back(newEnd - fFallbackStyleSets.data()); |
| 1066 |
if constexpr (kSkFontMgrVerbose) { |
| 1067 |
SkDebugf("SKIA: Fallback list size %d out of %d\n", |
| 1068 |
fFallbackStyleSets.size(), fNameToFamilyMap.size()); |
| 1069 |
} |
| 1070 |
|
| 1071 |
if (fStyleSets.empty()) { |
| 1072 |
if constexpr (kSkFontMgrVerbose) { SkDebugf("SKIA: No fonts!"); } |
| 1073 |
} else { |
| 1074 |
this->findDefaultStyleSet(); |
| 1075 |
} |
| 1076 |
} |
| 1077 |
|
| 1078 |
protected: |
| 1079 |
/** Returns not how many families we have, but how many unique names |
| 1080 |
* exist among the families. |
| 1081 |
*/ |
| 1082 |
int onCountFamilies() const override { |
| 1083 |
return fNameToFamilyMap.size(); |
| 1084 |
} |
| 1085 |
|
| 1086 |
void onGetFamilyName(int index, SkString* familyName) const override { |
| 1087 |
if (index < 0 || fNameToFamilyMap.size() <= index) { |
| 1088 |
familyName->reset(); |
| 1089 |
return; |
| 1090 |
} |
| 1091 |
familyName->set(fNameToFamilyMap[index].name); |
| 1092 |
} |
| 1093 |
|
| 1094 |
sk_sp<SkFontStyleSet> onCreateStyleSet(int index) const override { |
| 1095 |
if (index < 0 || fNameToFamilyMap.size() <= index) { |
| 1096 |
return nullptr; |
| 1097 |
} |
| 1098 |
return sk_ref_sp(fNameToFamilyMap[index].styleSet); |
| 1099 |
} |
| 1100 |
|
| 1101 |
sk_sp<SkFontStyleSet_AndroidNDK> onMatchAFamily(const char familyName[]) const { |
| 1102 |
if (!familyName) { |
| 1103 |
return nullptr; |
| 1104 |
} |
| 1105 |
SkString normalizedFamilyName(fICU.casefold({familyName, strlen(familyName)})); |
| 1106 |
for (const NameToFamily& nameToFamily : fNameToFamilyMap) { |
| 1107 |
if (nameToFamily.normalizedName == normalizedFamilyName) { |
| 1108 |
return sk_ref_sp(nameToFamily.styleSet); |
| 1109 |
} |
| 1110 |
} |
| 1111 |
return nullptr; |
| 1112 |
} |
| 1113 |
sk_sp<SkFontStyleSet> onMatchFamily(const char familyName[]) const override { |
| 1114 |
return this->onMatchAFamily(familyName); |
| 1115 |
} |
| 1116 |
|
| 1117 |
sk_sp<SkTypeface> onMatchFamilyStyle(const char familyName[], |
| 1118 |
const SkFontStyle& style) const override |
| 1119 |
{ |
| 1120 |
sk_sp<SkFontStyleSet_AndroidNDK> sset(this->onMatchAFamily(familyName)); |
| 1121 |
if (!sset) { |
| 1122 |
return nullptr; |
| 1123 |
} |
| 1124 |
return sset->matchStyle(style); |
| 1125 |
} |
| 1126 |
|
| 1127 |
static TArray<SkString> GetExtraFamilyNames( |
| 1128 |
const SkAFont& font, const SkTypeface& typeface, |
| 1129 |
const SkSpan<const std::unique_ptr<FontFamily>> xmlFamilies) |
| 1130 |
{ |
| 1131 |
// The NDK does not report aliases like 'serif', 'sans-serif`, 'monospace', etc. |
| 1132 |
// If a font matches an entry in fonts.xml, add the fonts.xml family name as well. |
| 1133 |
|
| 1134 |
// In Android <= 14 AFont reports the variation as specified in fonts.xml. |
| 1135 |
// In Android >= 15 AFont only reports fixed axes. |
| 1136 |
variation::Storage variationStorage; |
| 1137 |
SkSpan<variation::Coordinate> variation = variation::Get(typeface, variationStorage); |
| 1138 |
variation::Sort(variation::Normalize(variation)); |
| 1139 |
|
| 1140 |
variation::Storage xmlVariationStorage; |
| 1141 |
|
| 1142 |
TArray<SkString> extraFamilyNames; |
| 1143 |
for (const std::unique_ptr<FontFamily>& xmlFamily : xmlFamilies) { |
| 1144 |
if (xmlFamily->fNames.empty()) { |
| 1145 |
continue; |
| 1146 |
} |
| 1147 |
|
| 1148 |
for (const FontFileInfo& xmlFont : xmlFamily->fFonts) { |
| 1149 |
SkString pathName(xmlFamily->fBasePath); |
| 1150 |
pathName.append(xmlFont.fFileName); |
| 1151 |
if (!pathName.equals(font.getFontFilePath())) { |
| 1152 |
continue; |
| 1153 |
} |
| 1154 |
|
| 1155 |
if (font.getCollectionIndex() != static_cast<size_t>(xmlFont.fIndex)) { |
| 1156 |
continue; |
| 1157 |
} |
| 1158 |
|
| 1159 |
if (!xmlFont.fTypeface) { |
| 1160 |
xmlFont.fTypeface = typeface.makeClone(SkFontArguments() |
| 1161 |
.setCollectionIndex(xmlFont.fIndex) |
| 1162 |
.setVariationDesignPosition(SkFontArguments::VariationPosition{ |
| 1163 |
xmlFont.fVariationDesignPosition.data(), |
| 1164 |
xmlFont.fVariationDesignPosition.size() |
| 1165 |
}) |
| 1166 |
); |
| 1167 |
} |
| 1168 |
if (!xmlFont.fTypeface) { |
| 1169 |
SkDEBUGFAIL("Cannot create clone."); |
| 1170 |
continue; |
| 1171 |
} |
| 1172 |
|
| 1173 |
SkSpan<variation::Coordinate> xmlVariation = |
| 1174 |
variation::Get(*xmlFont.fTypeface, xmlVariationStorage); |
| 1175 |
if (variation.size() != xmlVariation.size()) { |
| 1176 |
SkDEBUGFAIL("Clone does not have same number of axes."); |
| 1177 |
continue; |
| 1178 |
} |
| 1179 |
|
| 1180 |
variation::Sort(variation::Normalize(xmlVariation)); |
| 1181 |
if (!std::equal(variation.begin(), variation.end(), |
| 1182 |
xmlVariation.begin(), variation::coordinateEqual)) |
| 1183 |
{ |
| 1184 |
continue; |
| 1185 |
} |
| 1186 |
|
| 1187 |
if (xmlFont.fWeight != 0 && xmlFont.fWeight != font.getWeight()) { |
| 1188 |
continue; |
| 1189 |
} |
| 1190 |
|
| 1191 |
for (auto&& xmlName : xmlFamily->fNames) { |
| 1192 |
extraFamilyNames.push_back(xmlName); |
| 1193 |
} |
| 1194 |
} |
| 1195 |
} |
| 1196 |
return extraFamilyNames; |
| 1197 |
} |
| 1198 |
|
| 1199 |
sk_sp<SkTypeface_AndroidNDK> make( |
| 1200 |
const SkAFont& font, |
| 1201 |
const SkSpan<const std::unique_ptr<FontFamily>> xmlFamilies, |
| 1202 |
skia_private::THashMap<SkString, std::unique_ptr<SkStreamAsset>>& streamForPath) const |
| 1203 |
{ |
| 1204 |
SkString filePath(font.getFontFilePath()); |
| 1205 |
|
| 1206 |
std::unique_ptr<SkStreamAsset>* streamPtr = streamForPath.find(filePath); |
| 1207 |
if (!streamPtr) { |
| 1208 |
streamPtr = streamForPath.set(filePath, SkStream::MakeFromFile(filePath.c_str())); |
| 1209 |
} |
| 1210 |
if (!*streamPtr) { |
| 1211 |
if constexpr (kSkFontMgrVerbose) { |
| 1212 |
SkDebugf("SKIA: Font file %s cannot be opened.\n", filePath.c_str()); |
| 1213 |
} |
| 1214 |
return nullptr; |
| 1215 |
} |
| 1216 |
std::unique_ptr<SkStreamAsset> stream = (*streamPtr)->duplicate(); |
| 1217 |
if (!stream) { |
| 1218 |
if constexpr (kSkFontMgrVerbose) { |
| 1219 |
SkDebugf("SKIA: Font file %s could not be duplicated.\n", filePath.c_str()); |
| 1220 |
} |
| 1221 |
return nullptr; |
| 1222 |
} |
| 1223 |
|
| 1224 |
size_t collectionIndex = font.getCollectionIndex(); |
| 1225 |
if constexpr (kSkFontMgrVerbose) { |
| 1226 |
SkDebugf("SKIA: Making font from %s#%zu\n", filePath.c_str(), collectionIndex); |
| 1227 |
} |
| 1228 |
if (!SkTFitsIn<int>(collectionIndex)) { |
| 1229 |
if constexpr (kSkFontMgrVerbose) { SkDebugf("SKIA: Collection index invalid!"); } |
| 1230 |
return nullptr; |
| 1231 |
} |
| 1232 |
|
| 1233 |
size_t requestAxisCount = font.getAxisCount(); |
| 1234 |
if (!SkTFitsIn<int>(requestAxisCount)) { |
| 1235 |
if constexpr (kSkFontMgrVerbose) { SkDebugf("SKIA: Axis count unreasonable!"); } |
| 1236 |
return nullptr; |
| 1237 |
} |
| 1238 |
variation::Storage requestAxisValues(requestAxisCount); |
| 1239 |
std::optional<int> requestedWidth; |
| 1240 |
for (size_t i = 0; i < requestAxisCount; ++i) { |
| 1241 |
uint32_t tag = font.getAxisTag(i); |
| 1242 |
float value = font.getAxisValue(i); |
| 1243 |
requestAxisValues[i] = { tag, value }; |
| 1244 |
if (tag == variation::wdthTag) { |
| 1245 |
// Set the width based on the requested `wdth` axis value. |
| 1246 |
requestedWidth = SkFontDescriptor::SkFontStyleWidthForWidthAxisValue(value); |
| 1247 |
} |
| 1248 |
} |
| 1249 |
SkFontArguments::VariationPosition requestedPosition = { |
| 1250 |
requestAxisValues.get(), SkTo<int>(requestAxisCount) |
| 1251 |
}; |
| 1252 |
// TODO: this creates the proxy with the given stream, so always cacheFontFiles. |
| 1253 |
auto proxy = fScanner->MakeFromStream( |
| 1254 |
std::move(stream), |
| 1255 |
SkFontArguments() |
| 1256 |
.setCollectionIndex(collectionIndex) |
| 1257 |
.setVariationDesignPosition(requestedPosition)); |
| 1258 |
if (!proxy) { |
| 1259 |
if constexpr (kSkFontMgrVerbose) { |
| 1260 |
SkDebugf("SKIA: Font file %s exists, but is not a valid font.\n", filePath.c_str()); |
| 1261 |
} |
| 1262 |
return nullptr; |
| 1263 |
} |
| 1264 |
variation::Storage variationStorage; |
| 1265 |
SkSpan<variation::Coordinate> variation = variation::Get(*proxy, variationStorage); |
| 1266 |
SkTypeface_AndroidNDK::AutoAxis autoAxis(variation); |
| 1267 |
autoAxis.remove(SkTypeface_AndroidNDK::AutoAxis(SkSpan(requestedPosition.coordinates, |
| 1268 |
requestedPosition.coordinateCount))); |
| 1269 |
|
| 1270 |
SkFontStyle style = proxy->fontStyle(); |
| 1271 |
int weight = SkTo<int>(font.getWeight()); |
| 1272 |
SkFontStyle::Slant slant = style.slant(); |
| 1273 |
if (font.isItalic()) { |
| 1274 |
slant = SkFontStyle::kItalic_Slant; |
| 1275 |
} |
| 1276 |
int width = requestedWidth.value_or(style.width()); |
| 1277 |
style = SkFontStyle(weight, width, slant); |
| 1278 |
|
| 1279 |
// The family name(s) are not reported. |
| 1280 |
// This would be very helpful for aliases, like "sans-serif", "Arial", etc. |
| 1281 |
TArray<SkString> extraFamilyNames = GetExtraFamilyNames(font, *proxy, xmlFamilies); |
| 1282 |
|
| 1283 |
SkString familyName; |
| 1284 |
proxy->getFamilyName(&familyName); |
| 1285 |
|
| 1286 |
STArray<4, SkALanguage> skLangs; |
| 1287 |
const char* aLangs = font.getLocale(); |
| 1288 |
{ |
| 1289 |
SkString postscriptName; |
| 1290 |
proxy->getPostScriptName(&postscriptName); |
| 1291 |
|
| 1292 |
// HACK: For backwards compatibility NotoSansSymbols-Regular-Subsetted needs "und-Zsym". |
| 1293 |
// Base Android appears to hack this into its fallback list for similar reasons. |
| 1294 |
static constexpr char kNotoSansSymbols[] = "NotoSansSymbols-Regular-Subsetted"; |
| 1295 |
if (postscriptName.equals(kNotoSansSymbols, std::size(kNotoSansSymbols)-1) && |
| 1296 |
(!aLangs || aLangs[0] == '\0') && |
| 1297 |
proxy->unicharToGlyph(0x2603) != 0) |
| 1298 |
{ |
| 1299 |
if constexpr (kSkFontMgrVerbose) { |
| 1300 |
SkDebugf("SKIA: Hacking in und-Zsym for NotoSansSymbols-Regular-Subsetted\n"); |
| 1301 |
} |
| 1302 |
aLangs = "und-Zsym"; |
| 1303 |
} |
| 1304 |
|
| 1305 |
// HACK: Some Android versions have a variable Roboto font named Roboto but also use |
| 1306 |
// a font named RobotoStatic (which does not claim to be Roboto) for the 400 weight. |
| 1307 |
// If RobotoStatic is found but does not have the name "Roboto", add it. |
| 1308 |
// Fixed in U "[2nd attempt] Revive use of VF font for regular style of roboto font" |
| 1309 |
// https://android.googlesource.com/platform/frameworks/base/+/89abe560d722a6f4136b7a05d80f23b269413aad |
| 1310 |
static constexpr char kRobotoStatic[] = "RobotoStatic-Regular"; |
| 1311 |
static constexpr char kRoboto[] = "Roboto"; |
| 1312 |
if (postscriptName.equals(kRobotoStatic, std::size(kRobotoStatic)-1) && |
| 1313 |
std::none_of(extraFamilyNames.begin(), extraFamilyNames.end(), |
| 1314 |
[](SkString& n){return n.equals(kRoboto, std::size(kRoboto)-1);})) |
| 1315 |
{ |
| 1316 |
if constexpr (kSkFontMgrVerbose) { |
| 1317 |
SkDebugf("SKIA: Hacking in Roboto for RobotoStatic-Regular\n"); |
| 1318 |
} |
| 1319 |
extraFamilyNames.push_back(SkString(kRoboto, std::size(kRoboto)-1)); |
| 1320 |
} |
| 1321 |
} |
| 1322 |
if (aLangs) { |
| 1323 |
if constexpr (kSkFontMgrVerbose) { |
| 1324 |
SkDebugf("SKIA: %s ALangs %s\n", familyName.c_str(), aLangs); |
| 1325 |
} |
| 1326 |
// Format: ',' or '\0' are terminators, '\0' is the final terminator. |
| 1327 |
const char* begin = aLangs; |
| 1328 |
const char* end = aLangs; |
| 1329 |
while (true) { |
| 1330 |
while (*end != '\0' && *end != ',') { |
| 1331 |
++end; |
| 1332 |
} |
| 1333 |
const size_t size = end - begin; |
| 1334 |
if (size) { |
| 1335 |
skLangs.emplace_back(SkSpan(begin, size)); |
| 1336 |
} |
| 1337 |
if (*end == '\0') { |
| 1338 |
break; |
| 1339 |
} |
| 1340 |
++end; |
| 1341 |
begin = end; |
| 1342 |
} |
| 1343 |
} |
| 1344 |
if constexpr (kSkFontMgrVerbose) { |
| 1345 |
for (auto&& lang : skLangs) { |
| 1346 |
SkDebugf("SKIA: %s Lang %s Script %s Region %s\n", |
| 1347 |
familyName.c_str(), |
| 1348 |
lang.getLanguage().c_str(), |
| 1349 |
lang.getScript().c_str(), |
| 1350 |
lang.getRegion().c_str()); |
| 1351 |
} |
| 1352 |
} |
| 1353 |
|
| 1354 |
if constexpr (kSkFontMgrVerbose) { |
| 1355 |
SkDebugf("SKIA: New typeface %s [%d %d %d]\n", familyName.c_str(), style.weight(), |
| 1356 |
style.width(), style.slant()); |
| 1357 |
} |
| 1358 |
|
| 1359 |
return SkTypeface_AndroidNDK::Make( |
| 1360 |
proxy, style, proxy->isFixedPitch(), |
| 1361 |
familyName, std::move(extraFamilyNames), std::move(skLangs), autoAxis); |
| 1362 |
} |
| 1363 |
|
| 1364 |
|
| 1365 |
static bool has_locale_and_character(SkTypeface_AndroidNDK* face, |
| 1366 |
const SkALanguage& langTag, |
| 1367 |
SkUnichar character, |
| 1368 |
const char* scope, size_t* step) { |
| 1369 |
++*step; |
| 1370 |
if (!langTag.isEmpty() && |
| 1371 |
std::none_of(face->fLang.begin(), face->fLang.end(), [&](SkALanguage lang) { |
| 1372 |
return lang.startsWith(langTag); |
| 1373 |
})) |
| 1374 |
{ |
| 1375 |
return false; |
| 1376 |
} |
| 1377 |
|
| 1378 |
if (face->unicharToGlyph(character) == 0) { |
| 1379 |
return false; |
| 1380 |
} |
| 1381 |
|
| 1382 |
if constexpr (kSkFontMgrVerbose) { |
| 1383 |
SkString foundName; |
| 1384 |
face->getFamilyName(&foundName); |
| 1385 |
using SkUnicharUnsigned = std::make_unsigned_t<decltype(character)>; |
| 1386 |
SkDebugf("SKIA: Found U%c%" PRIx32 " in \"%s\" lang \"%s\" " |
| 1387 |
"script \"%s\" region \"%s\" scope %s step %zu.\n", |
| 1388 |
"+-"[character < 0], static_cast<SkUnicharUnsigned>(std::abs(character)), |
| 1389 |
foundName.c_str(), langTag.getLanguage().c_str(), langTag.getScript().c_str(), |
| 1390 |
langTag.getRegion().c_str(), scope, *step); |
| 1391 |
|
| 1392 |
} |
| 1393 |
return true; |
| 1394 |
} |
| 1395 |
|
| 1396 |
sk_sp<SkTypeface> findByCharacterLocaleFamily( |
| 1397 |
SkTypeface_AndroidNDK* familyFace, |
| 1398 |
const SkFontStyle& style, |
| 1399 |
const SkALanguage& langTag, |
| 1400 |
SkUnichar character) const |
| 1401 |
{ |
| 1402 |
size_t step = 0; |
| 1403 |
// First look at the familyFace |
| 1404 |
if (familyFace && has_locale_and_character(familyFace, langTag, character, "face", &step)) { |
| 1405 |
return sk_ref_sp(familyFace); |
| 1406 |
} |
| 1407 |
|
| 1408 |
// Look through the styles that match in each family. |
| 1409 |
for (SkFontStyleSet_AndroidNDK* styleSet : fFallbackStyleSets) { |
| 1410 |
sk_sp<SkTypeface_AndroidNDK> face(styleSet->matchAStyle(style)); |
| 1411 |
if (has_locale_and_character(face.get(), langTag, character, "style", &step)) { |
| 1412 |
return adjustForStyle(std::move(face), style, *fCache); |
| 1413 |
} |
| 1414 |
} |
| 1415 |
|
| 1416 |
// Look through everything. |
| 1417 |
|
| 1418 |
// Android by default has a setup like |
| 1419 |
// /system/fonts/NotoSansSymbols-Regular-Subsetted.ttf#0 |
| 1420 |
// /system/fonts/NotoSansSymbols-Regular-Subsetted2.ttf#0 |
| 1421 |
// Which are both "Noto Sans Symbols" so end up in a "family" together. However, these |
| 1422 |
// are not in the same family, these are two different fonts in different families and |
| 1423 |
// should have been given different names. Internally this works because these are |
| 1424 |
// in separate <family> tags, but the NDK API doesn't provide that information. |
| 1425 |
// While Android internally depends on all fonts in a family having the same characters |
| 1426 |
// mapped, this cannot be relied upon when guessing at the families by name. |
| 1427 |
|
| 1428 |
for (SkFontStyleSet_AndroidNDK* styleSet : fFallbackStyleSets) { |
| 1429 |
for (const sk_sp<SkTypeface_AndroidNDK>& face : styleSet->fStyles) { |
| 1430 |
if (has_locale_and_character(face.get(), langTag, character, "anything", &step)) { |
| 1431 |
return adjustForStyle(sk_sp(face), style, *fCache); |
| 1432 |
} |
| 1433 |
} |
| 1434 |
} |
| 1435 |
|
| 1436 |
return nullptr; |
| 1437 |
} |
| 1438 |
|
| 1439 |
sk_sp<SkTypeface> onMatchFamilyStyleCharacter(const char familyName[], |
| 1440 |
const SkFontStyle& style, |
| 1441 |
const char* bcp47[], |
| 1442 |
int bcp47Count, |
| 1443 |
SkUnichar character) const override { |
| 1444 |
// If at some point AFontMatcher becomes usable, the code for using it is at |
| 1445 |
// https://skia-review.googlesource.com/c/skia/+/585970/13/src/ports/SkFontMgr_android_ndk.cpp#766 |
| 1446 |
|
| 1447 |
sk_sp<SkTypeface> familyFace; |
| 1448 |
SkTypeface_AndroidNDK* afamilyFace = nullptr; |
| 1449 |
if (familyName) { |
| 1450 |
familyFace = this->onMatchFamilyStyle(familyName, style); |
| 1451 |
afamilyFace = static_cast<SkTypeface_AndroidNDK*>(familyFace.get()); |
| 1452 |
} |
| 1453 |
|
| 1454 |
SkSpan langtags(bcp47, bcp47Count); |
| 1455 |
for (auto&& langtag = langtags.rbegin(); langtag != langtags.rend(); ++langtag) { |
| 1456 |
SkALanguage lang(*langtag); |
| 1457 |
if constexpr (kSkFontMgrVerbose) { |
| 1458 |
SkDebugf("SKIA: Matching against %s Lang %s Script %s Region %s\n", |
| 1459 |
familyName ? familyName : "", |
| 1460 |
lang.getLanguage().c_str(), |
| 1461 |
lang.getScript().c_str(), |
| 1462 |
lang.getRegion().c_str()); |
| 1463 |
} |
| 1464 |
while (!lang.isEmpty()) { |
| 1465 |
sk_sp<SkTypeface> typeface = |
| 1466 |
findByCharacterLocaleFamily(afamilyFace, style, lang, character); |
| 1467 |
if (typeface) { |
| 1468 |
return typeface; |
| 1469 |
} |
| 1470 |
lang = lang.lessSpecific(); |
| 1471 |
} |
| 1472 |
} |
| 1473 |
|
| 1474 |
sk_sp<SkTypeface> typeface = |
| 1475 |
findByCharacterLocaleFamily(afamilyFace, style, SkALanguage(), character); |
| 1476 |
if (typeface) { |
| 1477 |
return typeface; |
| 1478 |
} |
| 1479 |
|
| 1480 |
if constexpr (kSkFontMgrVerbose) { |
| 1481 |
using SkUnicharUnsigned = std::make_unsigned_t<decltype(character)>; |
| 1482 |
SkDebugf("SKIA: No font had U%c%" PRIx32 "\n", |
| 1483 |
"+-"[character < 0], static_cast<SkUnicharUnsigned>(std::abs(character))); |
| 1484 |
} |
| 1485 |
return nullptr; |
| 1486 |
} |
| 1487 |
|
| 1488 |
sk_sp<SkTypeface> onMakeFromData(sk_sp<SkData> data, int ttcIndex) const override { |
| 1489 |
return this->makeFromStream( |
| 1490 |
std::unique_ptr<SkStreamAsset>(new SkMemoryStream(std::move(data))), ttcIndex); |
| 1491 |
} |
| 1492 |
|
| 1493 |
sk_sp<SkTypeface> onMakeFromFile(const char path[], int ttcIndex) const override { |
| 1494 |
std::unique_ptr<SkStreamAsset> stream = SkStream::MakeFromFile(path); |
| 1495 |
return stream ? this->makeFromStream(std::move(stream), ttcIndex) : nullptr; |
| 1496 |
} |
| 1497 |
|
| 1498 |
sk_sp<SkTypeface> onMakeFromStreamIndex(std::unique_ptr<SkStreamAsset> stream, |
| 1499 |
int ttcIndex) const override { |
| 1500 |
return this->makeFromStream(std::move(stream), |
| 1501 |
SkFontArguments().setCollectionIndex(ttcIndex)); |
| 1502 |
} |
| 1503 |
|
| 1504 |
sk_sp<SkTypeface> onMakeFromStreamArgs(std::unique_ptr<SkStreamAsset> stream, |
| 1505 |
const SkFontArguments& args) const override { |
| 1506 |
return fScanner->MakeFromStream(std::move(stream), args); |
| 1507 |
} |
| 1508 |
|
| 1509 |
sk_sp<SkTypeface> onLegacyMakeTypeface(const char name[], SkFontStyle style) const override { |
| 1510 |
if (name) { |
| 1511 |
// On Android, we must return nullptr when we can't find the requested |
| 1512 |
// named typeface so that the system/app can provide their own recovery |
| 1513 |
// mechanism. On other platforms we'd provide a typeface from the |
| 1514 |
// default family instead. |
| 1515 |
return sk_sp<SkTypeface>(this->onMatchFamilyStyle(name, style)); |
| 1516 |
} |
| 1517 |
if (fDefaultStyleSet) { |
| 1518 |
return sk_sp<SkTypeface>(fDefaultStyleSet->matchStyle(style)); |
| 1519 |
} |
| 1520 |
return nullptr; |
| 1521 |
} |
| 1522 |
|
| 1523 |
|
| 1524 |
private: |
| 1525 |
AndroidFontAPI fAPI; |
| 1526 |
AndroidIcuAPI fICU; |
| 1527 |
std::unique_ptr<SkFontScanner> fScanner; |
| 1528 |
|
| 1529 |
TArray<sk_sp<SkFontStyleSet_AndroidNDK>> fStyleSets; |
| 1530 |
TArray<SkFontStyleSet_AndroidNDK*> fFallbackStyleSets; |
| 1531 |
TArray<NameToFamily> fNameToFamilyMap; |
| 1532 |
sk_sp<SkFontStyleSet> fDefaultStyleSet; |
| 1533 |
|
| 1534 |
sk_sp<TypefaceCache> fCache; |
| 1535 |
|
| 1536 |
void findDefaultStyleSet() { |
| 1537 |
SkASSERT(!fStyleSets.empty()); |
| 1538 |
|
| 1539 |
static constexpr const char* kDefaultNames[] = { "sans-serif", "Roboto" }; |
| 1540 |
for (const char* defaultName : kDefaultNames) { |
| 1541 |
fDefaultStyleSet = this->onMatchFamily(defaultName); |
| 1542 |
if (fDefaultStyleSet) { |
| 1543 |
break; |
| 1544 |
} |
| 1545 |
} |
| 1546 |
if (nullptr == fDefaultStyleSet) { |
| 1547 |
fDefaultStyleSet = fStyleSets[0]; |
| 1548 |
} |
| 1549 |
SkASSERT(fDefaultStyleSet); |
| 1550 |
} |
| 1551 |
}; |
| 1552 |
|
| 1553 |
} // namespace |
| 1554 |
|
| 1555 |
sk_sp<SkFontMgr> SkFontMgr_New_AndroidNDK(bool cacheFontFiles, |
| 1556 |
std::unique_ptr<SkFontScanner> scanner) |
| 1557 |
{ |
| 1558 |
std::optional<AndroidFontAPI> fontAPI = AndroidFontAPI::Make(); |
| 1559 |
if (!fontAPI) { |
| 1560 |
return nullptr; |
| 1561 |
} |
| 1562 |
return sk_sp(new SkFontMgr_AndroidNDK(*std::move(fontAPI), cacheFontFiles, std::move(scanner))); |
| 1563 |
} |