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" |
bungeman | c530854 | 2015-06-23 13:25:46 -0700 | [diff] [blame] | 10 | #include "SkFontMgr_android_parser.h" |
tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 11 | #include "Test.h" |
| 12 | |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 13 | #include <cmath> |
| 14 | #include <cstdio> |
| 15 | |
bungeman | eb2be7f | 2015-02-10 07:51:12 -0800 | [diff] [blame] | 16 | DECLARE_bool(verboseFontMgr); |
| 17 | |
tomhudson | 2ed49a4 | 2014-08-13 07:53:48 -0700 | [diff] [blame] | 18 | int CountFallbacks(SkTDArray<FontFamily*> fontFamilies) { |
| 19 | int countOfFallbackFonts = 0; |
| 20 | for (int i = 0; i < fontFamilies.count(); i++) { |
| 21 | if (fontFamilies[i]->fIsFallbackFont) { |
| 22 | countOfFallbackFonts++; |
| 23 | } |
| 24 | } |
| 25 | return countOfFallbackFonts; |
| 26 | } |
| 27 | |
bungeman | c3c6943 | 2015-02-11 07:18:51 -0800 | [diff] [blame] | 28 | //https://tools.ietf.org/html/rfc5234#appendix-B.1 |
| 29 | static bool isALPHA(int c) { |
| 30 | return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z'); |
| 31 | } |
| 32 | |
| 33 | //https://tools.ietf.org/html/rfc5234#appendix-B.1 |
| 34 | static bool isDIGIT(int c) { |
| 35 | return ('0' <= c && c <= '9'); |
| 36 | } |
| 37 | |
bungeman | 4b86bac | 2014-11-04 10:54:31 -0800 | [diff] [blame] | 38 | void ValidateLoadedFonts(SkTDArray<FontFamily*> fontFamilies, const char* firstExpectedFile, |
tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 39 | skiatest::Reporter* reporter) { |
| 40 | REPORTER_ASSERT(reporter, fontFamilies[0]->fNames.count() == 5); |
| 41 | REPORTER_ASSERT(reporter, !strcmp(fontFamilies[0]->fNames[0].c_str(), "sans-serif")); |
| 42 | REPORTER_ASSERT(reporter, |
bungeman | 4b86bac | 2014-11-04 10:54:31 -0800 | [diff] [blame] | 43 | !strcmp(fontFamilies[0]->fFonts[0].fFileName.c_str(), firstExpectedFile)); |
tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 44 | REPORTER_ASSERT(reporter, !fontFamilies[0]->fIsFallbackFont); |
bungeman | c3c6943 | 2015-02-11 07:18:51 -0800 | [diff] [blame] | 45 | |
| 46 | // Check that the languages are all sane. |
| 47 | for (int i = 0; i < fontFamilies.count(); ++i) { |
| 48 | const SkString& lang = fontFamilies[i]->fLanguage.getTag(); |
| 49 | for (size_t j = 0; j < lang.size(); ++j) { |
| 50 | int c = lang[j]; |
| 51 | REPORTER_ASSERT(reporter, isALPHA(c) || isDIGIT(c) || '-' == c); |
| 52 | } |
| 53 | } |
bungeman | 9a0808f | 2015-02-13 08:55:16 -0800 | [diff] [blame] | 54 | |
| 55 | // All file names in the test configuration files start with a capital letter. |
| 56 | // This is not a general requirement, but it is true of all the test configuration data. |
| 57 | // Verifying ensures the filenames have been read sanely and have not been 'sliced'. |
| 58 | for (int i = 0; i < fontFamilies.count(); ++i) { |
| 59 | FontFamily& family = *fontFamilies[i]; |
| 60 | for (int j = 0; j < family.fFonts.count(); ++j) { |
| 61 | FontFileInfo& file = family.fFonts[j]; |
| 62 | REPORTER_ASSERT(reporter, !file.fFileName.isEmpty() && |
| 63 | file.fFileName[0] >= 'A' && |
| 64 | file.fFileName[0] <= 'Z'); |
| 65 | } |
| 66 | } |
tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 67 | } |
| 68 | |
bungeman | 3d47e21 | 2015-02-13 18:30:11 -0800 | [diff] [blame] | 69 | void DumpLoadedFonts(SkTDArray<FontFamily*> fontFamilies, const char* label) { |
bungeman | eb2be7f | 2015-02-10 07:51:12 -0800 | [diff] [blame] | 70 | if (!FLAGS_verboseFontMgr) { |
| 71 | return; |
| 72 | } |
| 73 | |
bungeman | 3d47e21 | 2015-02-13 18:30:11 -0800 | [diff] [blame] | 74 | SkDebugf("\n--- Dumping %s\n", label); |
tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 75 | for (int i = 0; i < fontFamilies.count(); ++i) { |
| 76 | SkDebugf("Family %d:\n", i); |
tomhudson | 0754475 | 2014-08-05 13:35:00 -0700 | [diff] [blame] | 77 | switch(fontFamilies[i]->fVariant) { |
bungeman | a44e9b9 | 2015-01-30 19:58:19 -0800 | [diff] [blame] | 78 | case kElegant_FontVariant: SkDebugf(" elegant\n"); break; |
| 79 | case kCompact_FontVariant: SkDebugf(" compact\n"); break; |
tomhudson | 0754475 | 2014-08-05 13:35:00 -0700 | [diff] [blame] | 80 | default: break; |
| 81 | } |
bungeman | eb2be7f | 2015-02-10 07:51:12 -0800 | [diff] [blame] | 82 | SkDebugf(" basePath %s\n", fontFamilies[i]->fBasePath.c_str()); |
tomhudson | 0754475 | 2014-08-05 13:35:00 -0700 | [diff] [blame] | 83 | if (!fontFamilies[i]->fLanguage.getTag().isEmpty()) { |
bungeman | a44e9b9 | 2015-01-30 19:58:19 -0800 | [diff] [blame] | 84 | SkDebugf(" language %s\n", fontFamilies[i]->fLanguage.getTag().c_str()); |
tomhudson | 0754475 | 2014-08-05 13:35:00 -0700 | [diff] [blame] | 85 | } |
tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 86 | for (int j = 0; j < fontFamilies[i]->fNames.count(); ++j) { |
| 87 | SkDebugf(" name %s\n", fontFamilies[i]->fNames[j].c_str()); |
| 88 | } |
tomhudson | d3ddea2 | 2014-08-11 11:28:00 -0700 | [diff] [blame] | 89 | for (int j = 0; j < fontFamilies[i]->fFonts.count(); ++j) { |
| 90 | const FontFileInfo& ffi = fontFamilies[i]->fFonts[j]; |
bungeman | a44e9b9 | 2015-01-30 19:58:19 -0800 | [diff] [blame] | 91 | 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] | 92 | } |
tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 93 | } |
bungeman | c3c6943 | 2015-02-11 07:18:51 -0800 | [diff] [blame] | 94 | SkDebugf("\n\n"); |
tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 95 | } |
| 96 | |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 97 | template <int N, typename T> static double test_parse_fixed_r(skiatest::Reporter* reporter, |
| 98 | double low, double high, double inc) |
| 99 | { |
| 100 | double SK_FixedMax_double = nextafter(1 << (sizeof(T) * CHAR_BIT - N - 1), 0.0); |
| 101 | double SK_FixedEpsilon_double = (1.0 / (1 << N)); |
| 102 | double maxError = 0; |
| 103 | char buffer[64]; |
| 104 | for (double f = low; f < high; f += inc) { |
| 105 | SkString s; |
| 106 | // 'sprintf' formatting as expected depends on the current locale being "C". |
| 107 | // We currently expect tests and tools to run in the "C" locale. |
| 108 | sprintf(buffer, "%.20f", f); |
| 109 | T fix; |
| 110 | bool b = parse_fixed<N>(buffer, &fix); |
| 111 | if (b) { |
| 112 | double f2 = fix * SK_FixedEpsilon_double; |
| 113 | double error = fabs(f - f2); |
| 114 | REPORTER_ASSERT(reporter, error <= SK_FixedEpsilon_double); |
| 115 | maxError = SkTMax(maxError, error); |
| 116 | } else { |
| 117 | REPORTER_ASSERT(reporter, f < -SK_FixedMax_double || SK_FixedMax_double < f); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | //SkDebugf("maxError: %.20f\n", maxError); |
| 122 | return maxError; |
| 123 | } |
| 124 | |
| 125 | static void test_parse_fixed(skiatest::Reporter* reporter) { |
| 126 | test_parse_fixed_r<27, int32_t>(reporter, -8.1, -7.9, 0.000001); |
| 127 | test_parse_fixed_r<27, int32_t>(reporter, -0.1, 0.1, 0.000001); |
| 128 | test_parse_fixed_r<27, int32_t>(reporter, 7.9, 8.1, 0.000001); |
| 129 | test_parse_fixed_r<16, int32_t>(reporter, -0.125, 0.125, 1.0 / (1 << 19)); |
| 130 | test_parse_fixed_r<16, int32_t>(reporter, -32768.125, -32766.875, 1.0 / (1 << 17)); |
| 131 | test_parse_fixed_r<16, int32_t>(reporter, 32766.875, 32768.125, 1.0 / (1 << 17)); |
| 132 | test_parse_fixed_r<16, int32_t>(reporter, -1.1, 1.1, 0.0001); |
| 133 | |
| 134 | SkFixed fix; |
| 135 | REPORTER_ASSERT(reporter, !parse_fixed<27>("-17.1", &fix)); |
| 136 | REPORTER_ASSERT(reporter, !parse_fixed<16>("32768", &fix)); |
| 137 | REPORTER_ASSERT(reporter, !parse_fixed<16>("", &fix)); |
| 138 | REPORTER_ASSERT(reporter, !parse_fixed<16>(".", &fix)); |
| 139 | REPORTER_ASSERT(reporter, !parse_fixed<16>("123.", &fix)); |
| 140 | REPORTER_ASSERT(reporter, !parse_fixed<16>("a", &fix)); |
| 141 | REPORTER_ASSERT(reporter, !parse_fixed<16>(".123a", &fix)); |
| 142 | } |
| 143 | |
bungeman | c530854 | 2015-06-23 13:25:46 -0700 | [diff] [blame] | 144 | DEF_TEST(FontMgrAndroidParser, reporter) { |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 145 | test_parse_fixed(reporter); |
tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 146 | |
tomhudson | 8aed3c1 | 2014-08-07 10:20:51 -0700 | [diff] [blame] | 147 | bool resourcesMissing = false; |
| 148 | |
tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 149 | SkTDArray<FontFamily*> preV17FontFamilies; |
bungeman | c530854 | 2015-06-23 13:25:46 -0700 | [diff] [blame] | 150 | SkFontMgr_Android_Parser::GetCustomFontFamilies(preV17FontFamilies, |
bungeman | 7fa87cd | 2015-02-06 07:59:19 -0800 | [diff] [blame] | 151 | SkString("/custom/font/path/"), |
tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 152 | GetResourcePath("android_fonts/pre_v17/system_fonts.xml").c_str(), |
| 153 | GetResourcePath("android_fonts/pre_v17/fallback_fonts.xml").c_str()); |
| 154 | |
tomhudson | 8aed3c1 | 2014-08-07 10:20:51 -0700 | [diff] [blame] | 155 | if (preV17FontFamilies.count() > 0) { |
| 156 | REPORTER_ASSERT(reporter, preV17FontFamilies.count() == 14); |
tomhudson | 2ed49a4 | 2014-08-13 07:53:48 -0700 | [diff] [blame] | 157 | REPORTER_ASSERT(reporter, CountFallbacks(preV17FontFamilies) == 10); |
tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 158 | |
bungeman | 3d47e21 | 2015-02-13 18:30:11 -0800 | [diff] [blame] | 159 | DumpLoadedFonts(preV17FontFamilies, "pre version 17"); |
bungeman | 4b86bac | 2014-11-04 10:54:31 -0800 | [diff] [blame] | 160 | ValidateLoadedFonts(preV17FontFamilies, "Roboto-Regular.ttf", reporter); |
tomhudson | 8aed3c1 | 2014-08-07 10:20:51 -0700 | [diff] [blame] | 161 | } else { |
| 162 | resourcesMissing = true; |
| 163 | } |
bungeman | 91e51cb | 2015-07-15 14:29:25 -0400 | [diff] [blame^] | 164 | preV17FontFamilies.deleteAll(); |
tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 165 | |
tomhudson | 0754475 | 2014-08-05 13:35:00 -0700 | [diff] [blame] | 166 | |
tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 167 | SkTDArray<FontFamily*> v17FontFamilies; |
bungeman | c530854 | 2015-06-23 13:25:46 -0700 | [diff] [blame] | 168 | SkFontMgr_Android_Parser::GetCustomFontFamilies(v17FontFamilies, |
bungeman | 7fa87cd | 2015-02-06 07:59:19 -0800 | [diff] [blame] | 169 | SkString("/custom/font/path/"), |
tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 170 | GetResourcePath("android_fonts/v17/system_fonts.xml").c_str(), |
bungeman | c3c6943 | 2015-02-11 07:18:51 -0800 | [diff] [blame] | 171 | GetResourcePath("android_fonts/v17/fallback_fonts.xml").c_str(), |
| 172 | GetResourcePath("android_fonts/v17").c_str()); |
tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 173 | |
tomhudson | 8aed3c1 | 2014-08-07 10:20:51 -0700 | [diff] [blame] | 174 | if (v17FontFamilies.count() > 0) { |
bungeman | c3c6943 | 2015-02-11 07:18:51 -0800 | [diff] [blame] | 175 | REPORTER_ASSERT(reporter, v17FontFamilies.count() == 56); |
| 176 | REPORTER_ASSERT(reporter, CountFallbacks(v17FontFamilies) == 46); |
tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 177 | |
bungeman | 3d47e21 | 2015-02-13 18:30:11 -0800 | [diff] [blame] | 178 | DumpLoadedFonts(v17FontFamilies, "version 17"); |
bungeman | 4b86bac | 2014-11-04 10:54:31 -0800 | [diff] [blame] | 179 | ValidateLoadedFonts(v17FontFamilies, "Roboto-Regular.ttf", reporter); |
tomhudson | 8aed3c1 | 2014-08-07 10:20:51 -0700 | [diff] [blame] | 180 | } else { |
| 181 | resourcesMissing = true; |
| 182 | } |
bungeman | 91e51cb | 2015-07-15 14:29:25 -0400 | [diff] [blame^] | 183 | v17FontFamilies.deleteAll(); |
tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 184 | |
tomhudson | 0754475 | 2014-08-05 13:35:00 -0700 | [diff] [blame] | 185 | |
tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 186 | SkTDArray<FontFamily*> v22FontFamilies; |
bungeman | c530854 | 2015-06-23 13:25:46 -0700 | [diff] [blame] | 187 | SkFontMgr_Android_Parser::GetCustomFontFamilies(v22FontFamilies, |
bungeman | 7fa87cd | 2015-02-06 07:59:19 -0800 | [diff] [blame] | 188 | SkString("/custom/font/path/"), |
tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 189 | GetResourcePath("android_fonts/v22/fonts.xml").c_str(), |
| 190 | NULL); |
| 191 | |
tomhudson | 8aed3c1 | 2014-08-07 10:20:51 -0700 | [diff] [blame] | 192 | if (v22FontFamilies.count() > 0) { |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 193 | REPORTER_ASSERT(reporter, v22FontFamilies.count() == 54); |
tomhudson | 2ed49a4 | 2014-08-13 07:53:48 -0700 | [diff] [blame] | 194 | REPORTER_ASSERT(reporter, CountFallbacks(v22FontFamilies) == 42); |
tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 195 | |
bungeman | 3d47e21 | 2015-02-13 18:30:11 -0800 | [diff] [blame] | 196 | DumpLoadedFonts(v22FontFamilies, "version 22"); |
bungeman | 4b86bac | 2014-11-04 10:54:31 -0800 | [diff] [blame] | 197 | ValidateLoadedFonts(v22FontFamilies, "Roboto-Thin.ttf", reporter); |
tomhudson | 8aed3c1 | 2014-08-07 10:20:51 -0700 | [diff] [blame] | 198 | } else { |
| 199 | resourcesMissing = true; |
| 200 | } |
bungeman | 91e51cb | 2015-07-15 14:29:25 -0400 | [diff] [blame^] | 201 | v22FontFamilies.deleteAll(); |
tomhudson | 8aed3c1 | 2014-08-07 10:20:51 -0700 | [diff] [blame] | 202 | |
| 203 | if (resourcesMissing) { |
| 204 | SkDebugf("---- Resource files missing for FontConfigParser test\n"); |
| 205 | } |
tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 206 | } |
| 207 | |