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