OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2011 The Android Open Source Project | 2 * Copyright 2011 The Android Open Source Project |
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 "SkFontConfigParser_android.h" | 8 #include "SkFontConfigParser_android.h" |
9 #include "SkFontMgr_android.h" | 9 #include "SkFontMgr_android.h" |
10 #include "SkStream.h" | 10 #include "SkStream.h" |
(...skipping 484 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
495 */ | 495 */ |
496 static void append_fallback_font_families_for_locale(SkTDArray<FontFamily*>& fal
lbackFonts, | 496 static void append_fallback_font_families_for_locale(SkTDArray<FontFamily*>& fal
lbackFonts, |
497 const char* dir, | 497 const char* dir, |
498 const SkString& basePath) | 498 const SkString& basePath) |
499 { | 499 { |
500 #if defined(SK_BUILD_FOR_ANDROID_FRAMEWORK) | 500 #if defined(SK_BUILD_FOR_ANDROID_FRAMEWORK) |
501 // The framework is beyond Android 4.2 and can therefore skip this function | 501 // The framework is beyond Android 4.2 and can therefore skip this function |
502 return; | 502 return; |
503 #endif | 503 #endif |
504 | 504 |
505 DIR* fontDirectory = opendir(dir); | 505 SkAutoTCallIProc<DIR, closedir> fontDirectory(opendir(dir)); |
506 if (fontDirectory != NULL){ | 506 if (NULL == fontDirectory) { |
507 struct dirent* dirEntry = readdir(fontDirectory); | 507 return; |
508 while (dirEntry) { | 508 } |
509 | 509 |
510 // The size of both the prefix, suffix, and a minimum valid language
code | 510 for (struct dirent* dirEntry; (dirEntry = readdir(fontDirectory));) { |
511 static const size_t minSize = strlen(LOCALE_FALLBACK_FONTS_PREFIX) + | 511 // The size of both the prefix, suffix, and a minimum valid language cod
e |
512 strlen(LOCALE_FALLBACK_FONTS_SUFFIX) +
2; | 512 static const size_t minSize = sizeof(LOCALE_FALLBACK_FONTS_PREFIX) - 1 |
| 513 + sizeof(LOCALE_FALLBACK_FONTS_SUFFIX) - 1 |
| 514 + 2; |
513 | 515 |
514 SkString fileName(dirEntry->d_name); | 516 SkString fileName(dirEntry->d_name); |
515 if (fileName.size() >= minSize && | 517 if (fileName.size() < minSize || |
516 fileName.startsWith(LOCALE_FALLBACK_FONTS_PREFIX) && | 518 !fileName.startsWith(LOCALE_FALLBACK_FONTS_PREFIX) || |
517 fileName.endsWith(LOCALE_FALLBACK_FONTS_SUFFIX)) { | 519 !fileName.endsWith(LOCALE_FALLBACK_FONTS_SUFFIX)) |
| 520 { |
| 521 continue; |
| 522 } |
518 | 523 |
519 static const size_t fixedLen = strlen(LOCALE_FALLBACK_FONTS_PREF
IX) - | 524 static const size_t fixedLen = sizeof(LOCALE_FALLBACK_FONTS_PREFIX) - 1 |
520 strlen(LOCALE_FALLBACK_FONTS_SUFF
IX); | 525 + sizeof(LOCALE_FALLBACK_FONTS_SUFFIX) - 1; |
521 | 526 |
522 SkString locale(fileName.c_str() - strlen(LOCALE_FALLBACK_FONTS_
PREFIX), | 527 SkString locale(fileName.c_str() + sizeof(LOCALE_FALLBACK_FONTS_PREFIX)
- 1, |
523 fileName.size() - fixedLen); | 528 fileName.size() - fixedLen); |
524 | 529 |
525 SkString absoluteFilename; | 530 SkString absoluteFilename; |
526 absoluteFilename.printf("%s/%s", dir, fileName.c_str()); | 531 absoluteFilename.printf("%s/%s", dir, fileName.c_str()); |
527 | 532 |
528 SkTDArray<FontFamily*> langSpecificFonts; | 533 SkTDArray<FontFamily*> langSpecificFonts; |
529 parse_config_file(absoluteFilename.c_str(), langSpecificFonts, b
asePath, true); | 534 parse_config_file(absoluteFilename.c_str(), langSpecificFonts, basePath,
true); |
530 | 535 |
531 for (int i = 0; i < langSpecificFonts.count(); ++i) { | 536 for (int i = 0; i < langSpecificFonts.count(); ++i) { |
532 FontFamily* family = langSpecificFonts[i]; | 537 FontFamily* family = langSpecificFonts[i]; |
533 family->fLanguage = SkLanguage(locale); | 538 family->fLanguage = SkLanguage(locale); |
534 *fallbackFonts.append() = family; | 539 *fallbackFonts.append() = family; |
535 } | |
536 } | |
537 | |
538 // proceed to the next entry in the directory | |
539 dirEntry = readdir(fontDirectory); | |
540 } | 540 } |
541 // cleanup the directory reference | |
542 closedir(fontDirectory); | |
543 } | 541 } |
544 } | 542 } |
545 | 543 |
546 static void append_system_fallback_font_families(SkTDArray<FontFamily*>& fallbac
kFonts, | 544 static void append_system_fallback_font_families(SkTDArray<FontFamily*>& fallbac
kFonts, |
547 const SkString& basePath) | 545 const SkString& basePath) |
548 { | 546 { |
549 parse_config_file(FALLBACK_FONTS_FILE, fallbackFonts, basePath, true); | 547 parse_config_file(FALLBACK_FONTS_FILE, fallbackFonts, basePath, true); |
550 append_fallback_font_families_for_locale(fallbackFonts, | 548 append_fallback_font_families_for_locale(fallbackFonts, |
551 LOCALE_FALLBACK_FONTS_SYSTEM_DIR, | 549 LOCALE_FALLBACK_FONTS_SYSTEM_DIR, |
552 basePath); | 550 basePath); |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
597 // Append all the fallback fonts to system fonts | 595 // Append all the fallback fonts to system fonts |
598 SkTDArray<FontFamily*> fallbackFonts; | 596 SkTDArray<FontFamily*> fallbackFonts; |
599 append_system_fallback_font_families(fallbackFonts, basePath); | 597 append_system_fallback_font_families(fallbackFonts, basePath); |
600 mixin_vendor_fallback_font_families(fallbackFonts, basePath); | 598 mixin_vendor_fallback_font_families(fallbackFonts, basePath); |
601 fontFamilies.append(fallbackFonts.count(), fallbackFonts.begin()); | 599 fontFamilies.append(fallbackFonts.count(), fallbackFonts.begin()); |
602 } | 600 } |
603 | 601 |
604 void SkFontConfigParser::GetCustomFontFamilies(SkTDArray<FontFamily*>& fontFamil
ies, | 602 void SkFontConfigParser::GetCustomFontFamilies(SkTDArray<FontFamily*>& fontFamil
ies, |
605 const SkString& basePath, | 603 const SkString& basePath, |
606 const char* fontsXml, | 604 const char* fontsXml, |
607 const char* fallbackFontsXml) | 605 const char* fallbackFontsXml, |
| 606 const char* langFallbackFontsDir) |
608 { | 607 { |
609 if (fontsXml) { | 608 if (fontsXml) { |
610 parse_config_file(fontsXml, fontFamilies, basePath, false); | 609 parse_config_file(fontsXml, fontFamilies, basePath, false); |
611 } | 610 } |
612 if (fallbackFontsXml) { | 611 if (fallbackFontsXml) { |
613 parse_config_file(fallbackFontsXml, fontFamilies, basePath, true); | 612 parse_config_file(fallbackFontsXml, fontFamilies, basePath, true); |
614 } | 613 } |
| 614 if (langFallbackFontsDir) { |
| 615 append_fallback_font_families_for_locale(fontFamilies, |
| 616 langFallbackFontsDir, |
| 617 basePath); |
| 618 } |
615 } | 619 } |
616 | 620 |
617 SkLanguage SkLanguage::getParent() const { | 621 SkLanguage SkLanguage::getParent() const { |
618 SkASSERT(!fTag.isEmpty()); | 622 SkASSERT(!fTag.isEmpty()); |
619 const char* tag = fTag.c_str(); | 623 const char* tag = fTag.c_str(); |
620 | 624 |
621 // strip off the rightmost "-.*" | 625 // strip off the rightmost "-.*" |
622 const char* parentTagEnd = strrchr(tag, '-'); | 626 const char* parentTagEnd = strrchr(tag, '-'); |
623 if (parentTagEnd == NULL) { | 627 if (parentTagEnd == NULL) { |
624 return SkLanguage(); | 628 return SkLanguage(); |
625 } | 629 } |
626 size_t parentTagLen = parentTagEnd - tag; | 630 size_t parentTagLen = parentTagEnd - tag; |
627 return SkLanguage(tag, parentTagLen); | 631 return SkLanguage(tag, parentTagLen); |
628 } | 632 } |
OLD | NEW |