OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkTypes.h" | 8 #include "SkTypes.h" |
9 | 9 |
10 #include "SkData.h" | 10 #include "SkData.h" |
(...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
294 (custom && SkFontMgr_Android_CustomFonts::kOnlyCustom != custom->fSy
stemFontUse)) | 294 (custom && SkFontMgr_Android_CustomFonts::kOnlyCustom != custom->fSy
stemFontUse)) |
295 { | 295 { |
296 SkFontMgr_Android_Parser::GetSystemFontFamilies(families); | 296 SkFontMgr_Android_Parser::GetSystemFontFamilies(families); |
297 } | 297 } |
298 if (custom && SkFontMgr_Android_CustomFonts::kPreferSystem == custom->fS
ystemFontUse) { | 298 if (custom && SkFontMgr_Android_CustomFonts::kPreferSystem == custom->fS
ystemFontUse) { |
299 SkString base(custom->fBasePath); | 299 SkString base(custom->fBasePath); |
300 SkFontMgr_Android_Parser::GetCustomFontFamilies( | 300 SkFontMgr_Android_Parser::GetCustomFontFamilies( |
301 families, base, custom->fFontsXml, custom->fFallbackFontsXml); | 301 families, base, custom->fFontsXml, custom->fFallbackFontsXml); |
302 } | 302 } |
303 this->buildNameToFamilyMap(families, custom ? custom->fIsolated : false)
; | 303 this->buildNameToFamilyMap(families, custom ? custom->fIsolated : false)
; |
304 this->findDefaultFont(); | 304 this->findDefaultStyleSet(); |
305 families.deleteAll(); | 305 families.deleteAll(); |
306 } | 306 } |
307 | 307 |
308 protected: | 308 protected: |
309 /** Returns not how many families we have, but how many unique names | 309 /** Returns not how many families we have, but how many unique names |
310 * exist among the families. | 310 * exist among the families. |
311 */ | 311 */ |
312 int onCountFamilies() const override { | 312 int onCountFamilies() const override { |
313 return fNameToFamilyMap.count(); | 313 return fNameToFamilyMap.count(); |
314 } | 314 } |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
348 } | 348 } |
349 | 349 |
350 virtual SkTypeface* onMatchFamilyStyle(const char familyName[], | 350 virtual SkTypeface* onMatchFamilyStyle(const char familyName[], |
351 const SkFontStyle& style) const overr
ide { | 351 const SkFontStyle& style) const overr
ide { |
352 SkAutoTUnref<SkFontStyleSet> sset(this->matchFamily(familyName)); | 352 SkAutoTUnref<SkFontStyleSet> sset(this->matchFamily(familyName)); |
353 return sset->matchStyle(style); | 353 return sset->matchStyle(style); |
354 } | 354 } |
355 | 355 |
356 virtual SkTypeface* onMatchFaceStyle(const SkTypeface* typeface, | 356 virtual SkTypeface* onMatchFaceStyle(const SkTypeface* typeface, |
357 const SkFontStyle& style) const overrid
e { | 357 const SkFontStyle& style) const overrid
e { |
358 for (int i = 0; i < fFontStyleSets.count(); ++i) { | 358 for (int i = 0; i < fStyleSets.count(); ++i) { |
359 for (int j = 0; j < fFontStyleSets[i]->fStyles.count(); ++j) { | 359 for (int j = 0; j < fStyleSets[i]->fStyles.count(); ++j) { |
360 if (fFontStyleSets[i]->fStyles[j] == typeface) { | 360 if (fStyleSets[i]->fStyles[j] == typeface) { |
361 return fFontStyleSets[i]->matchStyle(style); | 361 return fStyleSets[i]->matchStyle(style); |
362 } | 362 } |
363 } | 363 } |
364 } | 364 } |
365 return nullptr; | 365 return nullptr; |
366 } | 366 } |
367 | 367 |
368 static sk_sp<SkTypeface_AndroidSystem> find_family_style_character( | 368 static sk_sp<SkTypeface_AndroidSystem> find_family_style_character( |
369 const SkTDArray<NameToFamily>& fallbackNameToFamilyMap, | 369 const SkTArray<NameToFamily, true>& fallbackNameToFamilyMap, |
370 const SkFontStyle& style, bool elegant, | 370 const SkFontStyle& style, bool elegant, |
371 const SkString& langTag, SkUnichar character) | 371 const SkString& langTag, SkUnichar character) |
372 { | 372 { |
373 for (int i = 0; i < fallbackNameToFamilyMap.count(); ++i) { | 373 for (int i = 0; i < fallbackNameToFamilyMap.count(); ++i) { |
374 SkFontStyleSet_Android* family = fallbackNameToFamilyMap[i].styleSet
; | 374 SkFontStyleSet_Android* family = fallbackNameToFamilyMap[i].styleSet
; |
375 sk_sp<SkTypeface_AndroidSystem> face(family->matchStyle(style)); | 375 sk_sp<SkTypeface_AndroidSystem> face(family->matchStyle(style)); |
376 | 376 |
377 if (!langTag.isEmpty() && !face->fLang.getTag().startsWith(langTag.c
_str())) { | 377 if (!langTag.isEmpty() && !face->fLang.getTag().startsWith(langTag.c
_str())) { |
378 continue; | 378 continue; |
379 } | 379 } |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
489 } | 489 } |
490 | 490 |
491 SkTypeface* onLegacyCreateTypeface(const char familyName[], SkFontStyle styl
e) const override { | 491 SkTypeface* onLegacyCreateTypeface(const char familyName[], SkFontStyle styl
e) const override { |
492 if (familyName) { | 492 if (familyName) { |
493 // On Android, we must return nullptr when we can't find the request
ed | 493 // On Android, we must return nullptr when we can't find the request
ed |
494 // named typeface so that the system/app can provide their own recov
ery | 494 // named typeface so that the system/app can provide their own recov
ery |
495 // mechanism. On other platforms we'd provide a typeface from the | 495 // mechanism. On other platforms we'd provide a typeface from the |
496 // default family instead. | 496 // default family instead. |
497 return this->onMatchFamilyStyle(familyName, style); | 497 return this->onMatchFamilyStyle(familyName, style); |
498 } | 498 } |
499 return fDefaultFamily->matchStyle(style); | 499 return fDefaultStyleSet->matchStyle(style); |
500 } | 500 } |
501 | 501 |
502 | 502 |
503 private: | 503 private: |
504 | 504 |
505 SkTypeface_FreeType::Scanner fScanner; | 505 SkTypeface_FreeType::Scanner fScanner; |
506 | 506 |
507 SkTArray<SkAutoTUnref<SkFontStyleSet_Android>, true> fFontStyleSets; | 507 SkTArray<sk_sp<SkFontStyleSet_Android>, true> fStyleSets; |
508 SkFontStyleSet* fDefaultFamily; | 508 sk_sp<SkFontStyleSet> fDefaultStyleSet; |
509 SkTypeface* fDefaultTypeface; | |
510 | 509 |
511 SkTDArray<NameToFamily> fNameToFamilyMap; | 510 SkTArray<NameToFamily, true> fNameToFamilyMap; |
512 SkTDArray<NameToFamily> fFallbackNameToFamilyMap; | 511 SkTArray<NameToFamily, true> fFallbackNameToFamilyMap; |
513 | 512 |
514 void buildNameToFamilyMap(SkTDArray<FontFamily*> families, const bool isolat
ed) { | 513 void buildNameToFamilyMap(SkTDArray<FontFamily*> families, const bool isolat
ed) { |
515 for (int i = 0; i < families.count(); i++) { | 514 for (int i = 0; i < families.count(); i++) { |
516 FontFamily& family = *families[i]; | 515 FontFamily& family = *families[i]; |
517 | 516 |
518 SkTDArray<NameToFamily>* nameToFamily = &fNameToFamilyMap; | 517 SkTArray<NameToFamily, true>* nameToFamily = &fNameToFamilyMap; |
519 if (family.fIsFallbackFont) { | 518 if (family.fIsFallbackFont) { |
520 nameToFamily = &fFallbackNameToFamilyMap; | 519 nameToFamily = &fFallbackNameToFamilyMap; |
521 | 520 |
522 if (0 == family.fNames.count()) { | 521 if (0 == family.fNames.count()) { |
523 SkString& fallbackName = family.fNames.push_back(); | 522 SkString& fallbackName = family.fNames.push_back(); |
524 fallbackName.printf("%.2x##fallback", i); | 523 fallbackName.printf("%.2x##fallback", i); |
525 } | 524 } |
526 } | 525 } |
527 | 526 |
528 SkFontStyleSet_Android* newSet = new SkFontStyleSet_Android(family,
fScanner, isolated); | 527 sk_sp<SkFontStyleSet_Android> newSet = |
| 528 sk_make_sp<SkFontStyleSet_Android>(family, fScanner, isolated); |
529 if (0 == newSet->count()) { | 529 if (0 == newSet->count()) { |
530 delete newSet; | |
531 continue; | 530 continue; |
532 } | 531 } |
533 fFontStyleSets.push_back().reset(newSet); | |
534 | 532 |
535 for (int j = 0; j < family.fNames.count(); j++) { | 533 for (const SkString& name : family.fNames) { |
536 NameToFamily* nextEntry = nameToFamily->append(); | 534 nameToFamily->emplace_back(NameToFamily{name, newSet.get()}); |
537 new (&nextEntry->name) SkString(family.fNames[j]); | |
538 nextEntry->styleSet = newSet; | |
539 } | 535 } |
| 536 fStyleSets.emplace_back(std::move(newSet)); |
540 } | 537 } |
541 } | 538 } |
542 | 539 |
543 void findDefaultFont() { | 540 void findDefaultStyleSet() { |
544 SkASSERT(!fFontStyleSets.empty()); | 541 SkASSERT(!fStyleSets.empty()); |
545 | 542 |
546 static const char* gDefaultNames[] = { "sans-serif" }; | 543 static const char* defaultNames[] = { "sans-serif" }; |
547 for (size_t i = 0; i < SK_ARRAY_COUNT(gDefaultNames); ++i) { | 544 for (const char* defaultName : defaultNames) { |
548 SkFontStyleSet* set = this->onMatchFamily(gDefaultNames[i]); | 545 fDefaultStyleSet.reset(this->onMatchFamily(defaultName)); |
549 if (nullptr == set) { | 546 if (fDefaultStyleSet) { |
550 continue; | 547 break; |
551 } | 548 } |
552 SkTypeface* tf = set->matchStyle(SkFontStyle()); | |
553 if (nullptr == tf) { | |
554 continue; | |
555 } | |
556 fDefaultFamily = set; | |
557 fDefaultTypeface = tf; | |
558 break; | |
559 } | 549 } |
560 if (nullptr == fDefaultTypeface) { | 550 if (nullptr == fDefaultStyleSet) { |
561 fDefaultFamily = fFontStyleSets[0]; | 551 fDefaultStyleSet = fStyleSets[0]; |
562 fDefaultTypeface = fDefaultFamily->createTypeface(0); | |
563 } | 552 } |
564 SkASSERT(fDefaultFamily); | 553 SkASSERT(fDefaultStyleSet); |
565 SkASSERT(fDefaultTypeface); | |
566 } | 554 } |
567 | 555 |
568 typedef SkFontMgr INHERITED; | 556 typedef SkFontMgr INHERITED; |
569 }; | 557 }; |
570 | 558 |
571 #ifdef SK_DEBUG | 559 #ifdef SK_DEBUG |
572 static char const * const gSystemFontUseStrings[] = { | 560 static char const * const gSystemFontUseStrings[] = { |
573 "OnlyCustom", "PreferCustom", "PreferSystem" | 561 "OnlyCustom", "PreferCustom", "PreferSystem" |
574 }; | 562 }; |
575 #endif | 563 #endif |
576 SkFontMgr* SkFontMgr_New_Android(const SkFontMgr_Android_CustomFonts* custom) { | 564 SkFontMgr* SkFontMgr_New_Android(const SkFontMgr_Android_CustomFonts* custom) { |
577 if (custom) { | 565 if (custom) { |
578 SkASSERT(0 <= custom->fSystemFontUse); | 566 SkASSERT(0 <= custom->fSystemFontUse); |
579 SkASSERT(custom->fSystemFontUse < SK_ARRAY_COUNT(gSystemFontUseStrings))
; | 567 SkASSERT(custom->fSystemFontUse < SK_ARRAY_COUNT(gSystemFontUseStrings))
; |
580 SkDEBUGF(("SystemFontUse: %s BasePath: %s Fonts: %s FallbackFonts: %s\n"
, | 568 SkDEBUGF(("SystemFontUse: %s BasePath: %s Fonts: %s FallbackFonts: %s\n"
, |
581 gSystemFontUseStrings[custom->fSystemFontUse], | 569 gSystemFontUseStrings[custom->fSystemFontUse], |
582 custom->fBasePath, | 570 custom->fBasePath, |
583 custom->fFontsXml, | 571 custom->fFontsXml, |
584 custom->fFallbackFontsXml)); | 572 custom->fFallbackFontsXml)); |
585 } | 573 } |
586 | 574 |
587 return new SkFontMgr_Android(custom); | 575 return new SkFontMgr_Android(custom); |
588 } | 576 } |
OLD | NEW |