tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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 "Resources.h" |
bungeman | eb2be7f | 2015-02-10 07:51:12 -0800 | [diff] [blame] | 9 | #include "SkCommandLineFlags.h" |
tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 10 | #include "SkFontConfigParser_android.h" |
| 11 | #include "Test.h" |
| 12 | |
bungeman | eb2be7f | 2015-02-10 07:51:12 -0800 | [diff] [blame] | 13 | DECLARE_bool(verboseFontMgr); |
| 14 | |
tomhudson | 2ed49a4 | 2014-08-13 07:53:48 -0700 | [diff] [blame] | 15 | int CountFallbacks(SkTDArray<FontFamily*> fontFamilies) { |
| 16 | int countOfFallbackFonts = 0; |
| 17 | for (int i = 0; i < fontFamilies.count(); i++) { |
| 18 | if (fontFamilies[i]->fIsFallbackFont) { |
| 19 | countOfFallbackFonts++; |
| 20 | } |
| 21 | } |
| 22 | return countOfFallbackFonts; |
| 23 | } |
| 24 | |
bungeman | c3c6943 | 2015-02-11 07:18:51 -0800 | [diff] [blame] | 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 | |
bungeman | 4b86bac | 2014-11-04 10:54:31 -0800 | [diff] [blame] | 35 | void ValidateLoadedFonts(SkTDArray<FontFamily*> fontFamilies, const char* firstExpectedFile, |
tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 36 | skiatest::Reporter* reporter) { |
| 37 | REPORTER_ASSERT(reporter, fontFamilies[0]->fNames.count() == 5); |
| 38 | REPORTER_ASSERT(reporter, !strcmp(fontFamilies[0]->fNames[0].c_str(), "sans-serif")); |
| 39 | REPORTER_ASSERT(reporter, |
bungeman | 4b86bac | 2014-11-04 10:54:31 -0800 | [diff] [blame] | 40 | !strcmp(fontFamilies[0]->fFonts[0].fFileName.c_str(), firstExpectedFile)); |
tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 41 | REPORTER_ASSERT(reporter, !fontFamilies[0]->fIsFallbackFont); |
bungeman | c3c6943 | 2015-02-11 07:18:51 -0800 | [diff] [blame] | 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 | } |
bungeman | 9a0808f | 2015-02-13 08:55:16 -0800 | [diff] [blame] | 51 | |
| 52 | // All file names in the test configuration files start with a capital letter. |
| 53 | // This is not a general requirement, but it is true of all the test configuration data. |
| 54 | // Verifying ensures the filenames have been read sanely and have not been 'sliced'. |
| 55 | for (int i = 0; i < fontFamilies.count(); ++i) { |
| 56 | FontFamily& family = *fontFamilies[i]; |
| 57 | for (int j = 0; j < family.fFonts.count(); ++j) { |
| 58 | FontFileInfo& file = family.fFonts[j]; |
| 59 | REPORTER_ASSERT(reporter, !file.fFileName.isEmpty() && |
| 60 | file.fFileName[0] >= 'A' && |
| 61 | file.fFileName[0] <= 'Z'); |
| 62 | } |
| 63 | } |
tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 64 | } |
| 65 | |
bungeman | 3d47e21 | 2015-02-13 18:30:11 -0800 | [diff] [blame] | 66 | void DumpLoadedFonts(SkTDArray<FontFamily*> fontFamilies, const char* label) { |
bungeman | eb2be7f | 2015-02-10 07:51:12 -0800 | [diff] [blame] | 67 | if (!FLAGS_verboseFontMgr) { |
| 68 | return; |
| 69 | } |
| 70 | |
bungeman | 3d47e21 | 2015-02-13 18:30:11 -0800 | [diff] [blame] | 71 | SkDebugf("\n--- Dumping %s\n", label); |
tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 72 | for (int i = 0; i < fontFamilies.count(); ++i) { |
| 73 | SkDebugf("Family %d:\n", i); |
tomhudson | 0754475 | 2014-08-05 13:35:00 -0700 | [diff] [blame] | 74 | switch(fontFamilies[i]->fVariant) { |
bungeman | a44e9b9 | 2015-01-30 19:58:19 -0800 | [diff] [blame] | 75 | case kElegant_FontVariant: SkDebugf(" elegant\n"); break; |
| 76 | case kCompact_FontVariant: SkDebugf(" compact\n"); break; |
tomhudson | 0754475 | 2014-08-05 13:35:00 -0700 | [diff] [blame] | 77 | default: break; |
| 78 | } |
bungeman | eb2be7f | 2015-02-10 07:51:12 -0800 | [diff] [blame] | 79 | SkDebugf(" basePath %s\n", fontFamilies[i]->fBasePath.c_str()); |
tomhudson | 0754475 | 2014-08-05 13:35:00 -0700 | [diff] [blame] | 80 | if (!fontFamilies[i]->fLanguage.getTag().isEmpty()) { |
bungeman | a44e9b9 | 2015-01-30 19:58:19 -0800 | [diff] [blame] | 81 | SkDebugf(" language %s\n", fontFamilies[i]->fLanguage.getTag().c_str()); |
tomhudson | 0754475 | 2014-08-05 13:35:00 -0700 | [diff] [blame] | 82 | } |
tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 83 | for (int j = 0; j < fontFamilies[i]->fNames.count(); ++j) { |
| 84 | SkDebugf(" name %s\n", fontFamilies[i]->fNames[j].c_str()); |
| 85 | } |
tomhudson | d3ddea2 | 2014-08-11 11:28:00 -0700 | [diff] [blame] | 86 | for (int j = 0; j < fontFamilies[i]->fFonts.count(); ++j) { |
| 87 | const FontFileInfo& ffi = fontFamilies[i]->fFonts[j]; |
bungeman | a44e9b9 | 2015-01-30 19:58:19 -0800 | [diff] [blame] | 88 | SkDebugf(" file (%d) %s#%d\n", ffi.fWeight, ffi.fFileName.c_str(), ffi.fIndex); |
tomhudson | 0754475 | 2014-08-05 13:35:00 -0700 | [diff] [blame] | 89 | } |
tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 90 | } |
bungeman | c3c6943 | 2015-02-11 07:18:51 -0800 | [diff] [blame] | 91 | SkDebugf("\n\n"); |
tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | DEF_TEST(FontConfigParserAndroid, reporter) { |
| 95 | |
tomhudson | 8aed3c1 | 2014-08-07 10:20:51 -0700 | [diff] [blame] | 96 | bool resourcesMissing = false; |
| 97 | |
tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 98 | SkTDArray<FontFamily*> preV17FontFamilies; |
bungeman | 7fa87cd | 2015-02-06 07:59:19 -0800 | [diff] [blame] | 99 | SkFontConfigParser::GetCustomFontFamilies(preV17FontFamilies, |
| 100 | SkString("/custom/font/path/"), |
tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 101 | GetResourcePath("android_fonts/pre_v17/system_fonts.xml").c_str(), |
| 102 | GetResourcePath("android_fonts/pre_v17/fallback_fonts.xml").c_str()); |
| 103 | |
tomhudson | 8aed3c1 | 2014-08-07 10:20:51 -0700 | [diff] [blame] | 104 | if (preV17FontFamilies.count() > 0) { |
| 105 | REPORTER_ASSERT(reporter, preV17FontFamilies.count() == 14); |
tomhudson | 2ed49a4 | 2014-08-13 07:53:48 -0700 | [diff] [blame] | 106 | REPORTER_ASSERT(reporter, CountFallbacks(preV17FontFamilies) == 10); |
tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 107 | |
bungeman | 3d47e21 | 2015-02-13 18:30:11 -0800 | [diff] [blame] | 108 | DumpLoadedFonts(preV17FontFamilies, "pre version 17"); |
bungeman | 4b86bac | 2014-11-04 10:54:31 -0800 | [diff] [blame] | 109 | ValidateLoadedFonts(preV17FontFamilies, "Roboto-Regular.ttf", reporter); |
tomhudson | 8aed3c1 | 2014-08-07 10:20:51 -0700 | [diff] [blame] | 110 | } else { |
| 111 | resourcesMissing = true; |
| 112 | } |
tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 113 | |
tomhudson | 0754475 | 2014-08-05 13:35:00 -0700 | [diff] [blame] | 114 | |
tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 115 | SkTDArray<FontFamily*> v17FontFamilies; |
bungeman | 7fa87cd | 2015-02-06 07:59:19 -0800 | [diff] [blame] | 116 | SkFontConfigParser::GetCustomFontFamilies(v17FontFamilies, |
| 117 | SkString("/custom/font/path/"), |
tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 118 | GetResourcePath("android_fonts/v17/system_fonts.xml").c_str(), |
bungeman | c3c6943 | 2015-02-11 07:18:51 -0800 | [diff] [blame] | 119 | GetResourcePath("android_fonts/v17/fallback_fonts.xml").c_str(), |
| 120 | GetResourcePath("android_fonts/v17").c_str()); |
tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 121 | |
tomhudson | 8aed3c1 | 2014-08-07 10:20:51 -0700 | [diff] [blame] | 122 | if (v17FontFamilies.count() > 0) { |
bungeman | c3c6943 | 2015-02-11 07:18:51 -0800 | [diff] [blame] | 123 | REPORTER_ASSERT(reporter, v17FontFamilies.count() == 56); |
| 124 | REPORTER_ASSERT(reporter, CountFallbacks(v17FontFamilies) == 46); |
tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 125 | |
bungeman | 3d47e21 | 2015-02-13 18:30:11 -0800 | [diff] [blame] | 126 | DumpLoadedFonts(v17FontFamilies, "version 17"); |
bungeman | 4b86bac | 2014-11-04 10:54:31 -0800 | [diff] [blame] | 127 | ValidateLoadedFonts(v17FontFamilies, "Roboto-Regular.ttf", reporter); |
tomhudson | 8aed3c1 | 2014-08-07 10:20:51 -0700 | [diff] [blame] | 128 | } else { |
| 129 | resourcesMissing = true; |
| 130 | } |
tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 131 | |
tomhudson | 0754475 | 2014-08-05 13:35:00 -0700 | [diff] [blame] | 132 | |
tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 133 | SkTDArray<FontFamily*> v22FontFamilies; |
bungeman | 7fa87cd | 2015-02-06 07:59:19 -0800 | [diff] [blame] | 134 | SkFontConfigParser::GetCustomFontFamilies(v22FontFamilies, |
| 135 | SkString("/custom/font/path/"), |
tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 136 | GetResourcePath("android_fonts/v22/fonts.xml").c_str(), |
| 137 | NULL); |
| 138 | |
tomhudson | 8aed3c1 | 2014-08-07 10:20:51 -0700 | [diff] [blame] | 139 | if (v22FontFamilies.count() > 0) { |
| 140 | REPORTER_ASSERT(reporter, v22FontFamilies.count() == 53); |
tomhudson | 2ed49a4 | 2014-08-13 07:53:48 -0700 | [diff] [blame] | 141 | REPORTER_ASSERT(reporter, CountFallbacks(v22FontFamilies) == 42); |
tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 142 | |
bungeman | 3d47e21 | 2015-02-13 18:30:11 -0800 | [diff] [blame] | 143 | DumpLoadedFonts(v22FontFamilies, "version 22"); |
bungeman | 4b86bac | 2014-11-04 10:54:31 -0800 | [diff] [blame] | 144 | ValidateLoadedFonts(v22FontFamilies, "Roboto-Thin.ttf", reporter); |
tomhudson | 8aed3c1 | 2014-08-07 10:20:51 -0700 | [diff] [blame] | 145 | } else { |
| 146 | resourcesMissing = true; |
| 147 | } |
| 148 | |
| 149 | if (resourcesMissing) { |
| 150 | SkDebugf("---- Resource files missing for FontConfigParser test\n"); |
| 151 | } |
tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 152 | } |
| 153 | |