| 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 "Resources.h" | 8 #include "Resources.h" |
| 9 #include "SkCommandLineFlags.h" | 9 #include "SkCommandLineFlags.h" |
| 10 #include "SkFontConfigParser_android.h" | 10 #include "SkFontConfigParser_android.h" |
| 11 #include "Test.h" | 11 #include "Test.h" |
| 12 | 12 |
| 13 DECLARE_bool(verboseFontMgr); | 13 DECLARE_bool(verboseFontMgr); |
| 14 | 14 |
| 15 int CountFallbacks(SkTDArray<FontFamily*> fontFamilies) { | 15 int CountFallbacks(SkTDArray<FontFamily*> fontFamilies) { |
| 16 int countOfFallbackFonts = 0; | 16 int countOfFallbackFonts = 0; |
| 17 for (int i = 0; i < fontFamilies.count(); i++) { | 17 for (int i = 0; i < fontFamilies.count(); i++) { |
| 18 if (fontFamilies[i]->fIsFallbackFont) { | 18 if (fontFamilies[i]->fIsFallbackFont) { |
| 19 countOfFallbackFonts++; | 19 countOfFallbackFonts++; |
| 20 } | 20 } |
| 21 } | 21 } |
| 22 return countOfFallbackFonts; | 22 return countOfFallbackFonts; |
| 23 } | 23 } |
| 24 | 24 |
| 25 //https://tools.ietf.org/html/rfc5234#appendix-B.1 |
| 26 static bool isALPHA(int c) { |
| 27 return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z'); |
| 28 } |
| 29 |
| 30 //https://tools.ietf.org/html/rfc5234#appendix-B.1 |
| 31 static bool isDIGIT(int c) { |
| 32 return ('0' <= c && c <= '9'); |
| 33 } |
| 34 |
| 25 void ValidateLoadedFonts(SkTDArray<FontFamily*> fontFamilies, const char* firstE
xpectedFile, | 35 void ValidateLoadedFonts(SkTDArray<FontFamily*> fontFamilies, const char* firstE
xpectedFile, |
| 26 skiatest::Reporter* reporter) { | 36 skiatest::Reporter* reporter) { |
| 27 REPORTER_ASSERT(reporter, fontFamilies[0]->fNames.count() == 5); | 37 REPORTER_ASSERT(reporter, fontFamilies[0]->fNames.count() == 5); |
| 28 REPORTER_ASSERT(reporter, !strcmp(fontFamilies[0]->fNames[0].c_str(), "sans-
serif")); | 38 REPORTER_ASSERT(reporter, !strcmp(fontFamilies[0]->fNames[0].c_str(), "sans-
serif")); |
| 29 REPORTER_ASSERT(reporter, | 39 REPORTER_ASSERT(reporter, |
| 30 !strcmp(fontFamilies[0]->fFonts[0].fFileName.c_str(), firstE
xpectedFile)); | 40 !strcmp(fontFamilies[0]->fFonts[0].fFileName.c_str(), firstE
xpectedFile)); |
| 31 REPORTER_ASSERT(reporter, !fontFamilies[0]->fIsFallbackFont); | 41 REPORTER_ASSERT(reporter, !fontFamilies[0]->fIsFallbackFont); |
| 42 |
| 43 // Check that the languages are all sane. |
| 44 for (int i = 0; i < fontFamilies.count(); ++i) { |
| 45 const SkString& lang = fontFamilies[i]->fLanguage.getTag(); |
| 46 for (size_t j = 0; j < lang.size(); ++j) { |
| 47 int c = lang[j]; |
| 48 REPORTER_ASSERT(reporter, isALPHA(c) || isDIGIT(c) || '-' == c); |
| 49 } |
| 50 } |
| 32 } | 51 } |
| 33 | 52 |
| 34 void DumpLoadedFonts(SkTDArray<FontFamily*> fontFamilies) { | 53 void DumpLoadedFonts(SkTDArray<FontFamily*> fontFamilies) { |
| 35 if (!FLAGS_verboseFontMgr) { | 54 if (!FLAGS_verboseFontMgr) { |
| 36 return; | 55 return; |
| 37 } | 56 } |
| 38 | 57 |
| 39 for (int i = 0; i < fontFamilies.count(); ++i) { | 58 for (int i = 0; i < fontFamilies.count(); ++i) { |
| 40 SkDebugf("Family %d:\n", i); | 59 SkDebugf("Family %d:\n", i); |
| 41 switch(fontFamilies[i]->fVariant) { | 60 switch(fontFamilies[i]->fVariant) { |
| 42 case kElegant_FontVariant: SkDebugf(" elegant\n"); break; | 61 case kElegant_FontVariant: SkDebugf(" elegant\n"); break; |
| 43 case kCompact_FontVariant: SkDebugf(" compact\n"); break; | 62 case kCompact_FontVariant: SkDebugf(" compact\n"); break; |
| 44 default: break; | 63 default: break; |
| 45 } | 64 } |
| 46 SkDebugf(" basePath %s\n", fontFamilies[i]->fBasePath.c_str()); | 65 SkDebugf(" basePath %s\n", fontFamilies[i]->fBasePath.c_str()); |
| 47 if (!fontFamilies[i]->fLanguage.getTag().isEmpty()) { | 66 if (!fontFamilies[i]->fLanguage.getTag().isEmpty()) { |
| 48 SkDebugf(" language %s\n", fontFamilies[i]->fLanguage.getTag().c_st
r()); | 67 SkDebugf(" language %s\n", fontFamilies[i]->fLanguage.getTag().c_st
r()); |
| 49 } | 68 } |
| 50 for (int j = 0; j < fontFamilies[i]->fNames.count(); ++j) { | 69 for (int j = 0; j < fontFamilies[i]->fNames.count(); ++j) { |
| 51 SkDebugf(" name %s\n", fontFamilies[i]->fNames[j].c_str()); | 70 SkDebugf(" name %s\n", fontFamilies[i]->fNames[j].c_str()); |
| 52 } | 71 } |
| 53 for (int j = 0; j < fontFamilies[i]->fFonts.count(); ++j) { | 72 for (int j = 0; j < fontFamilies[i]->fFonts.count(); ++j) { |
| 54 const FontFileInfo& ffi = fontFamilies[i]->fFonts[j]; | 73 const FontFileInfo& ffi = fontFamilies[i]->fFonts[j]; |
| 55 SkDebugf(" file (%d) %s#%d\n", ffi.fWeight, ffi.fFileName.c_str(),
ffi.fIndex); | 74 SkDebugf(" file (%d) %s#%d\n", ffi.fWeight, ffi.fFileName.c_str(),
ffi.fIndex); |
| 56 } | 75 } |
| 57 } | 76 } |
| 77 SkDebugf("\n\n"); |
| 58 } | 78 } |
| 59 | 79 |
| 60 DEF_TEST(FontConfigParserAndroid, reporter) { | 80 DEF_TEST(FontConfigParserAndroid, reporter) { |
| 61 | 81 |
| 62 bool resourcesMissing = false; | 82 bool resourcesMissing = false; |
| 63 | 83 |
| 64 SkTDArray<FontFamily*> preV17FontFamilies; | 84 SkTDArray<FontFamily*> preV17FontFamilies; |
| 65 SkFontConfigParser::GetCustomFontFamilies(preV17FontFamilies, | 85 SkFontConfigParser::GetCustomFontFamilies(preV17FontFamilies, |
| 66 SkString("/custom/font/path/"), | 86 SkString("/custom/font/path/"), |
| 67 GetResourcePath("android_fonts/pre_v17/system_fonts.xml").c_str(), | 87 GetResourcePath("android_fonts/pre_v17/system_fonts.xml").c_str(), |
| 68 GetResourcePath("android_fonts/pre_v17/fallback_fonts.xml").c_str()); | 88 GetResourcePath("android_fonts/pre_v17/fallback_fonts.xml").c_str()); |
| 69 | 89 |
| 70 if (preV17FontFamilies.count() > 0) { | 90 if (preV17FontFamilies.count() > 0) { |
| 71 REPORTER_ASSERT(reporter, preV17FontFamilies.count() == 14); | 91 REPORTER_ASSERT(reporter, preV17FontFamilies.count() == 14); |
| 72 REPORTER_ASSERT(reporter, CountFallbacks(preV17FontFamilies) == 10); | 92 REPORTER_ASSERT(reporter, CountFallbacks(preV17FontFamilies) == 10); |
| 73 | 93 |
| 74 DumpLoadedFonts(preV17FontFamilies); | 94 DumpLoadedFonts(preV17FontFamilies); |
| 75 ValidateLoadedFonts(preV17FontFamilies, "Roboto-Regular.ttf", reporter); | 95 ValidateLoadedFonts(preV17FontFamilies, "Roboto-Regular.ttf", reporter); |
| 76 } else { | 96 } else { |
| 77 resourcesMissing = true; | 97 resourcesMissing = true; |
| 78 } | 98 } |
| 79 | 99 |
| 80 | 100 |
| 81 SkTDArray<FontFamily*> v17FontFamilies; | 101 SkTDArray<FontFamily*> v17FontFamilies; |
| 82 SkFontConfigParser::GetCustomFontFamilies(v17FontFamilies, | 102 SkFontConfigParser::GetCustomFontFamilies(v17FontFamilies, |
| 83 SkString("/custom/font/path/"), | 103 SkString("/custom/font/path/"), |
| 84 GetResourcePath("android_fonts/v17/system_fonts.xml").c_str(), | 104 GetResourcePath("android_fonts/v17/system_fonts.xml").c_str(), |
| 85 GetResourcePath("android_fonts/v17/fallback_fonts.xml").c_str()); | 105 GetResourcePath("android_fonts/v17/fallback_fonts.xml").c_str(), |
| 106 GetResourcePath("android_fonts/v17").c_str()); |
| 86 | 107 |
| 87 if (v17FontFamilies.count() > 0) { | 108 if (v17FontFamilies.count() > 0) { |
| 88 REPORTER_ASSERT(reporter, v17FontFamilies.count() == 41); | 109 REPORTER_ASSERT(reporter, v17FontFamilies.count() == 56); |
| 89 REPORTER_ASSERT(reporter, CountFallbacks(v17FontFamilies) == 31); | 110 REPORTER_ASSERT(reporter, CountFallbacks(v17FontFamilies) == 46); |
| 90 | 111 |
| 91 DumpLoadedFonts(v17FontFamilies); | 112 DumpLoadedFonts(v17FontFamilies); |
| 92 ValidateLoadedFonts(v17FontFamilies, "Roboto-Regular.ttf", reporter); | 113 ValidateLoadedFonts(v17FontFamilies, "Roboto-Regular.ttf", reporter); |
| 93 } else { | 114 } else { |
| 94 resourcesMissing = true; | 115 resourcesMissing = true; |
| 95 } | 116 } |
| 96 | 117 |
| 97 | 118 |
| 98 SkTDArray<FontFamily*> v22FontFamilies; | 119 SkTDArray<FontFamily*> v22FontFamilies; |
| 99 SkFontConfigParser::GetCustomFontFamilies(v22FontFamilies, | 120 SkFontConfigParser::GetCustomFontFamilies(v22FontFamilies, |
| 100 SkString("/custom/font/path/"), | 121 SkString("/custom/font/path/"), |
| 101 GetResourcePath("android_fonts/v22/fonts.xml").c_str(), | 122 GetResourcePath("android_fonts/v22/fonts.xml").c_str(), |
| 102 NULL); | 123 NULL); |
| 103 | 124 |
| 104 if (v22FontFamilies.count() > 0) { | 125 if (v22FontFamilies.count() > 0) { |
| 105 REPORTER_ASSERT(reporter, v22FontFamilies.count() == 53); | 126 REPORTER_ASSERT(reporter, v22FontFamilies.count() == 53); |
| 106 REPORTER_ASSERT(reporter, CountFallbacks(v22FontFamilies) == 42); | 127 REPORTER_ASSERT(reporter, CountFallbacks(v22FontFamilies) == 42); |
| 107 | 128 |
| 108 DumpLoadedFonts(v22FontFamilies); | 129 DumpLoadedFonts(v22FontFamilies); |
| 109 ValidateLoadedFonts(v22FontFamilies, "Roboto-Thin.ttf", reporter); | 130 ValidateLoadedFonts(v22FontFamilies, "Roboto-Thin.ttf", reporter); |
| 110 } else { | 131 } else { |
| 111 resourcesMissing = true; | 132 resourcesMissing = true; |
| 112 } | 133 } |
| 113 | 134 |
| 114 if (resourcesMissing) { | 135 if (resourcesMissing) { |
| 115 SkDebugf("---- Resource files missing for FontConfigParser test\n"); | 136 SkDebugf("---- Resource files missing for FontConfigParser test\n"); |
| 116 } | 137 } |
| 117 } | 138 } |
| 118 | 139 |
| OLD | NEW |