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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "tests/Test.h" |
Hal Canary | 3560ea7 | 2019-01-08 13:01:58 -0500 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/core/SkCanvas.h" |
| 11 | #include "include/core/SkFont.h" |
| 12 | #include "include/core/SkTypeface.h" |
| 13 | #include "include/ports/SkFontMgr_android.h" |
| 14 | #include "include/private/SkFixed.h" |
| 15 | #include "src/core/SkOSFile.h" |
| 16 | #include "src/ports/SkFontMgr_android_parser.h" |
| 17 | #include "tools/Resources.h" |
| 18 | #include "tools/flags/CommandLineFlags.h" |
tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 19 | |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 20 | #include <cmath> |
| 21 | #include <cstdio> |
| 22 | |
bungeman | eb2be7f | 2015-02-10 07:51:12 -0800 | [diff] [blame] | 23 | DECLARE_bool(verboseFontMgr); |
| 24 | |
tomhudson | 2ed49a4 | 2014-08-13 07:53:48 -0700 | [diff] [blame] | 25 | int CountFallbacks(SkTDArray<FontFamily*> fontFamilies) { |
| 26 | int countOfFallbackFonts = 0; |
| 27 | for (int i = 0; i < fontFamilies.count(); i++) { |
| 28 | if (fontFamilies[i]->fIsFallbackFont) { |
| 29 | countOfFallbackFonts++; |
| 30 | } |
| 31 | } |
| 32 | return countOfFallbackFonts; |
| 33 | } |
| 34 | |
bungeman | c3c6943 | 2015-02-11 07:18:51 -0800 | [diff] [blame] | 35 | //https://tools.ietf.org/html/rfc5234#appendix-B.1 |
| 36 | static bool isALPHA(int c) { |
| 37 | return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z'); |
| 38 | } |
| 39 | |
| 40 | //https://tools.ietf.org/html/rfc5234#appendix-B.1 |
| 41 | static bool isDIGIT(int c) { |
| 42 | return ('0' <= c && c <= '9'); |
| 43 | } |
| 44 | |
Ben Wagner | 9f0d8c2 | 2018-11-15 17:14:41 -0500 | [diff] [blame] | 45 | static void ValidateLoadedFonts(SkTDArray<FontFamily*> fontFamilies, const char* firstExpectedFile, |
| 46 | skiatest::Reporter* reporter) { |
tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 47 | REPORTER_ASSERT(reporter, fontFamilies[0]->fNames.count() == 5); |
| 48 | REPORTER_ASSERT(reporter, !strcmp(fontFamilies[0]->fNames[0].c_str(), "sans-serif")); |
| 49 | REPORTER_ASSERT(reporter, |
bungeman | 4b86bac | 2014-11-04 10:54:31 -0800 | [diff] [blame] | 50 | !strcmp(fontFamilies[0]->fFonts[0].fFileName.c_str(), firstExpectedFile)); |
tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 51 | REPORTER_ASSERT(reporter, !fontFamilies[0]->fIsFallbackFont); |
bungeman | c3c6943 | 2015-02-11 07:18:51 -0800 | [diff] [blame] | 52 | |
| 53 | // Check that the languages are all sane. |
Ben Wagner | aee878d | 2017-08-10 13:49:41 -0400 | [diff] [blame] | 54 | for (const auto& fontFamily : fontFamilies) { |
| 55 | for (const auto& lang : fontFamily->fLanguages) { |
| 56 | const SkString& langString = lang.getTag(); |
| 57 | for (size_t i = 0; i < langString.size(); ++i) { |
| 58 | int c = langString[i]; |
| 59 | REPORTER_ASSERT(reporter, isALPHA(c) || isDIGIT(c) || '-' == c); |
| 60 | } |
bungeman | c3c6943 | 2015-02-11 07:18:51 -0800 | [diff] [blame] | 61 | } |
| 62 | } |
bungeman | 9a0808f | 2015-02-13 08:55:16 -0800 | [diff] [blame] | 63 | |
| 64 | // All file names in the test configuration files start with a capital letter. |
| 65 | // This is not a general requirement, but it is true of all the test configuration data. |
| 66 | // Verifying ensures the filenames have been read sanely and have not been 'sliced'. |
| 67 | for (int i = 0; i < fontFamilies.count(); ++i) { |
| 68 | FontFamily& family = *fontFamilies[i]; |
| 69 | for (int j = 0; j < family.fFonts.count(); ++j) { |
| 70 | FontFileInfo& file = family.fFonts[j]; |
| 71 | REPORTER_ASSERT(reporter, !file.fFileName.isEmpty() && |
| 72 | file.fFileName[0] >= 'A' && |
| 73 | file.fFileName[0] <= 'Z'); |
| 74 | } |
| 75 | } |
tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Ben Wagner | 9f0d8c2 | 2018-11-15 17:14:41 -0500 | [diff] [blame] | 78 | static void DumpFiles(const FontFamily& fontFamily) { |
| 79 | for (int j = 0; j < fontFamily.fFonts.count(); ++j) { |
| 80 | const FontFileInfo& ffi = fontFamily.fFonts[j]; |
| 81 | SkDebugf(" file (%d) %s#%d", ffi.fWeight, ffi.fFileName.c_str(), ffi.fIndex); |
| 82 | for (const auto& coordinate : ffi.fVariationDesignPosition) { |
| 83 | SkDebugf(" @'%c%c%c%c'=%f", |
| 84 | (coordinate.axis >> 24) & 0xFF, |
| 85 | (coordinate.axis >> 16) & 0xFF, |
| 86 | (coordinate.axis >> 8) & 0xFF, |
| 87 | (coordinate.axis ) & 0xFF, |
| 88 | coordinate.value); |
| 89 | } |
| 90 | SkDebugf("\n"); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | static void DumpLoadedFonts(SkTDArray<FontFamily*> fontFamilies, const char* label) { |
bungeman | eb2be7f | 2015-02-10 07:51:12 -0800 | [diff] [blame] | 95 | if (!FLAGS_verboseFontMgr) { |
| 96 | return; |
| 97 | } |
| 98 | |
bungeman | 3d47e21 | 2015-02-13 18:30:11 -0800 | [diff] [blame] | 99 | SkDebugf("\n--- Dumping %s\n", label); |
tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 100 | for (int i = 0; i < fontFamilies.count(); ++i) { |
| 101 | SkDebugf("Family %d:\n", i); |
tomhudson | 0754475 | 2014-08-05 13:35:00 -0700 | [diff] [blame] | 102 | switch(fontFamilies[i]->fVariant) { |
bungeman | a44e9b9 | 2015-01-30 19:58:19 -0800 | [diff] [blame] | 103 | case kElegant_FontVariant: SkDebugf(" elegant\n"); break; |
| 104 | case kCompact_FontVariant: SkDebugf(" compact\n"); break; |
tomhudson | 0754475 | 2014-08-05 13:35:00 -0700 | [diff] [blame] | 105 | default: break; |
| 106 | } |
bungeman | eb2be7f | 2015-02-10 07:51:12 -0800 | [diff] [blame] | 107 | SkDebugf(" basePath %s\n", fontFamilies[i]->fBasePath.c_str()); |
Ben Wagner | aee878d | 2017-08-10 13:49:41 -0400 | [diff] [blame] | 108 | if (!fontFamilies[i]->fLanguages.empty()) { |
| 109 | SkDebugf(" language"); |
| 110 | for (const auto& lang : fontFamilies[i]->fLanguages) { |
| 111 | SkDebugf(" %s", lang.getTag().c_str()); |
| 112 | } |
| 113 | SkDebugf("\n"); |
tomhudson | 0754475 | 2014-08-05 13:35:00 -0700 | [diff] [blame] | 114 | } |
tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 115 | for (int j = 0; j < fontFamilies[i]->fNames.count(); ++j) { |
| 116 | SkDebugf(" name %s\n", fontFamilies[i]->fNames[j].c_str()); |
| 117 | } |
Ben Wagner | 9f0d8c2 | 2018-11-15 17:14:41 -0500 | [diff] [blame] | 118 | DumpFiles(*fontFamilies[i]); |
| 119 | fontFamilies[i]->fallbackFamilies.foreach( |
| 120 | [](SkString, std::unique_ptr<FontFamily>* fallbackFamily) { |
| 121 | SkDebugf(" Fallback for: %s\n", (*fallbackFamily)->fFallbackFor.c_str()); |
| 122 | DumpFiles(*(*fallbackFamily).get()); |
bungeman | 47a1e96 | 2016-02-25 11:20:01 -0800 | [diff] [blame] | 123 | } |
Ben Wagner | 9f0d8c2 | 2018-11-15 17:14:41 -0500 | [diff] [blame] | 124 | ); |
tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 125 | } |
bungeman | c3c6943 | 2015-02-11 07:18:51 -0800 | [diff] [blame] | 126 | SkDebugf("\n\n"); |
tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 127 | } |
| 128 | |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 129 | template <int N, typename T> static double test_parse_fixed_r(skiatest::Reporter* reporter, |
| 130 | double low, double high, double inc) |
| 131 | { |
| 132 | double SK_FixedMax_double = nextafter(1 << (sizeof(T) * CHAR_BIT - N - 1), 0.0); |
| 133 | double SK_FixedEpsilon_double = (1.0 / (1 << N)); |
| 134 | double maxError = 0; |
| 135 | char buffer[64]; |
| 136 | for (double f = low; f < high; f += inc) { |
| 137 | SkString s; |
| 138 | // 'sprintf' formatting as expected depends on the current locale being "C". |
| 139 | // We currently expect tests and tools to run in the "C" locale. |
| 140 | sprintf(buffer, "%.20f", f); |
| 141 | T fix; |
| 142 | bool b = parse_fixed<N>(buffer, &fix); |
| 143 | if (b) { |
| 144 | double f2 = fix * SK_FixedEpsilon_double; |
| 145 | double error = fabs(f - f2); |
| 146 | REPORTER_ASSERT(reporter, error <= SK_FixedEpsilon_double); |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 147 | maxError = std::max(maxError, error); |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 148 | } else { |
| 149 | REPORTER_ASSERT(reporter, f < -SK_FixedMax_double || SK_FixedMax_double < f); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | //SkDebugf("maxError: %.20f\n", maxError); |
| 154 | return maxError; |
| 155 | } |
| 156 | |
| 157 | static void test_parse_fixed(skiatest::Reporter* reporter) { |
| 158 | test_parse_fixed_r<27, int32_t>(reporter, -8.1, -7.9, 0.000001); |
| 159 | test_parse_fixed_r<27, int32_t>(reporter, -0.1, 0.1, 0.000001); |
| 160 | test_parse_fixed_r<27, int32_t>(reporter, 7.9, 8.1, 0.000001); |
| 161 | test_parse_fixed_r<16, int32_t>(reporter, -0.125, 0.125, 1.0 / (1 << 19)); |
| 162 | test_parse_fixed_r<16, int32_t>(reporter, -32768.125, -32766.875, 1.0 / (1 << 17)); |
| 163 | test_parse_fixed_r<16, int32_t>(reporter, 32766.875, 32768.125, 1.0 / (1 << 17)); |
| 164 | test_parse_fixed_r<16, int32_t>(reporter, -1.1, 1.1, 0.0001); |
| 165 | |
| 166 | SkFixed fix; |
| 167 | REPORTER_ASSERT(reporter, !parse_fixed<27>("-17.1", &fix)); |
| 168 | REPORTER_ASSERT(reporter, !parse_fixed<16>("32768", &fix)); |
| 169 | REPORTER_ASSERT(reporter, !parse_fixed<16>("", &fix)); |
| 170 | REPORTER_ASSERT(reporter, !parse_fixed<16>(".", &fix)); |
| 171 | REPORTER_ASSERT(reporter, !parse_fixed<16>("123.", &fix)); |
| 172 | REPORTER_ASSERT(reporter, !parse_fixed<16>("a", &fix)); |
| 173 | REPORTER_ASSERT(reporter, !parse_fixed<16>(".123a", &fix)); |
| 174 | } |
| 175 | |
bungeman | c530854 | 2015-06-23 13:25:46 -0700 | [diff] [blame] | 176 | DEF_TEST(FontMgrAndroidParser, reporter) { |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 177 | test_parse_fixed(reporter); |
tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 178 | |
tomhudson | 8aed3c1 | 2014-08-07 10:20:51 -0700 | [diff] [blame] | 179 | bool resourcesMissing = false; |
| 180 | |
tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 181 | SkTDArray<FontFamily*> preV17FontFamilies; |
bungeman | c530854 | 2015-06-23 13:25:46 -0700 | [diff] [blame] | 182 | SkFontMgr_Android_Parser::GetCustomFontFamilies(preV17FontFamilies, |
bungeman | 7fa87cd | 2015-02-06 07:59:19 -0800 | [diff] [blame] | 183 | SkString("/custom/font/path/"), |
tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 184 | GetResourcePath("android_fonts/pre_v17/system_fonts.xml").c_str(), |
| 185 | GetResourcePath("android_fonts/pre_v17/fallback_fonts.xml").c_str()); |
| 186 | |
tomhudson | 8aed3c1 | 2014-08-07 10:20:51 -0700 | [diff] [blame] | 187 | if (preV17FontFamilies.count() > 0) { |
| 188 | REPORTER_ASSERT(reporter, preV17FontFamilies.count() == 14); |
tomhudson | 2ed49a4 | 2014-08-13 07:53:48 -0700 | [diff] [blame] | 189 | REPORTER_ASSERT(reporter, CountFallbacks(preV17FontFamilies) == 10); |
tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 190 | |
bungeman | 3d47e21 | 2015-02-13 18:30:11 -0800 | [diff] [blame] | 191 | DumpLoadedFonts(preV17FontFamilies, "pre version 17"); |
bungeman | 4b86bac | 2014-11-04 10:54:31 -0800 | [diff] [blame] | 192 | ValidateLoadedFonts(preV17FontFamilies, "Roboto-Regular.ttf", reporter); |
tomhudson | 8aed3c1 | 2014-08-07 10:20:51 -0700 | [diff] [blame] | 193 | } else { |
| 194 | resourcesMissing = true; |
| 195 | } |
bungeman | 91e51cb | 2015-07-15 14:29:25 -0400 | [diff] [blame] | 196 | preV17FontFamilies.deleteAll(); |
tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 197 | |
tomhudson | 0754475 | 2014-08-05 13:35:00 -0700 | [diff] [blame] | 198 | |
tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 199 | SkTDArray<FontFamily*> v17FontFamilies; |
bungeman | c530854 | 2015-06-23 13:25:46 -0700 | [diff] [blame] | 200 | SkFontMgr_Android_Parser::GetCustomFontFamilies(v17FontFamilies, |
bungeman | 7fa87cd | 2015-02-06 07:59:19 -0800 | [diff] [blame] | 201 | SkString("/custom/font/path/"), |
tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 202 | GetResourcePath("android_fonts/v17/system_fonts.xml").c_str(), |
bungeman | c3c6943 | 2015-02-11 07:18:51 -0800 | [diff] [blame] | 203 | GetResourcePath("android_fonts/v17/fallback_fonts.xml").c_str(), |
| 204 | GetResourcePath("android_fonts/v17").c_str()); |
tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 205 | |
tomhudson | 8aed3c1 | 2014-08-07 10:20:51 -0700 | [diff] [blame] | 206 | if (v17FontFamilies.count() > 0) { |
bungeman | c3c6943 | 2015-02-11 07:18:51 -0800 | [diff] [blame] | 207 | REPORTER_ASSERT(reporter, v17FontFamilies.count() == 56); |
| 208 | REPORTER_ASSERT(reporter, CountFallbacks(v17FontFamilies) == 46); |
tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 209 | |
bungeman | 3d47e21 | 2015-02-13 18:30:11 -0800 | [diff] [blame] | 210 | DumpLoadedFonts(v17FontFamilies, "version 17"); |
bungeman | 4b86bac | 2014-11-04 10:54:31 -0800 | [diff] [blame] | 211 | ValidateLoadedFonts(v17FontFamilies, "Roboto-Regular.ttf", reporter); |
tomhudson | 8aed3c1 | 2014-08-07 10:20:51 -0700 | [diff] [blame] | 212 | } else { |
| 213 | resourcesMissing = true; |
| 214 | } |
bungeman | 91e51cb | 2015-07-15 14:29:25 -0400 | [diff] [blame] | 215 | v17FontFamilies.deleteAll(); |
tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 216 | |
tomhudson | 0754475 | 2014-08-05 13:35:00 -0700 | [diff] [blame] | 217 | |
tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 218 | SkTDArray<FontFamily*> v22FontFamilies; |
bungeman | c530854 | 2015-06-23 13:25:46 -0700 | [diff] [blame] | 219 | SkFontMgr_Android_Parser::GetCustomFontFamilies(v22FontFamilies, |
bungeman | 7fa87cd | 2015-02-06 07:59:19 -0800 | [diff] [blame] | 220 | SkString("/custom/font/path/"), |
tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 221 | GetResourcePath("android_fonts/v22/fonts.xml").c_str(), |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 222 | nullptr); |
tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 223 | |
tomhudson | 8aed3c1 | 2014-08-07 10:20:51 -0700 | [diff] [blame] | 224 | if (v22FontFamilies.count() > 0) { |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 225 | REPORTER_ASSERT(reporter, v22FontFamilies.count() == 54); |
tomhudson | 2ed49a4 | 2014-08-13 07:53:48 -0700 | [diff] [blame] | 226 | REPORTER_ASSERT(reporter, CountFallbacks(v22FontFamilies) == 42); |
tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 227 | |
bungeman | 3d47e21 | 2015-02-13 18:30:11 -0800 | [diff] [blame] | 228 | DumpLoadedFonts(v22FontFamilies, "version 22"); |
bungeman | 4b86bac | 2014-11-04 10:54:31 -0800 | [diff] [blame] | 229 | ValidateLoadedFonts(v22FontFamilies, "Roboto-Thin.ttf", reporter); |
tomhudson | 8aed3c1 | 2014-08-07 10:20:51 -0700 | [diff] [blame] | 230 | } else { |
| 231 | resourcesMissing = true; |
| 232 | } |
bungeman | 91e51cb | 2015-07-15 14:29:25 -0400 | [diff] [blame] | 233 | v22FontFamilies.deleteAll(); |
tomhudson | 8aed3c1 | 2014-08-07 10:20:51 -0700 | [diff] [blame] | 234 | |
| 235 | if (resourcesMissing) { |
| 236 | SkDebugf("---- Resource files missing for FontConfigParser test\n"); |
| 237 | } |
tomhudson | f79673b | 2014-08-05 06:36:11 -0700 | [diff] [blame] | 238 | } |
Ben Wagner | f1729a7 | 2017-11-13 11:14:36 -0500 | [diff] [blame] | 239 | |
| 240 | DEF_TEST(FontMgrAndroidLegacyMakeTypeface, reporter) { |
Hal Canary | a493510 | 2017-12-08 13:35:47 -0500 | [diff] [blame] | 241 | constexpr char fontsXmlFilename[] = "fonts/fonts.xml"; |
Ben Wagner | f1729a7 | 2017-11-13 11:14:36 -0500 | [diff] [blame] | 242 | SkString basePath = GetResourcePath("fonts/"); |
Hal Canary | a493510 | 2017-12-08 13:35:47 -0500 | [diff] [blame] | 243 | SkString fontsXml = GetResourcePath(fontsXmlFilename); |
| 244 | |
| 245 | if (!sk_exists(fontsXml.c_str())) { |
| 246 | ERRORF(reporter, "file missing: %s\n", fontsXmlFilename); |
| 247 | return; |
| 248 | } |
Ben Wagner | f1729a7 | 2017-11-13 11:14:36 -0500 | [diff] [blame] | 249 | |
| 250 | SkFontMgr_Android_CustomFonts custom; |
| 251 | custom.fSystemFontUse = SkFontMgr_Android_CustomFonts::kOnlyCustom; |
| 252 | custom.fBasePath = basePath.c_str(); |
| 253 | custom.fFontsXml = fontsXml.c_str(); |
| 254 | custom.fFallbackFontsXml = nullptr; |
| 255 | custom.fIsolated = false; |
| 256 | |
| 257 | sk_sp<SkFontMgr> fm(SkFontMgr_New_Android(&custom)); |
| 258 | sk_sp<SkTypeface> t(fm->legacyMakeTypeface("non-existent-font", SkFontStyle())); |
| 259 | REPORTER_ASSERT(reporter, nullptr == t); |
| 260 | } |
Bruce Wang | 37b6109 | 2018-06-20 16:43:02 -0400 | [diff] [blame] | 261 | |
| 262 | static bool bitmap_compare(const SkBitmap& ref, const SkBitmap& test) { |
| 263 | for (int y = 0; y < test.height(); ++y) { |
| 264 | for (int x = 0; x < test.width(); ++x) { |
| 265 | SkColor testColor = test.getColor(x, y); |
| 266 | SkColor refColor = ref.getColor(x, y); |
| 267 | if (refColor != testColor) { |
| 268 | return false; |
| 269 | } |
| 270 | } |
| 271 | } |
| 272 | return true; |
| 273 | } |
| 274 | |
| 275 | DEF_TEST(FontMgrAndroidSystemVariableTypeface, reporter) { |
| 276 | constexpr char fontsXmlFilename[] = "fonts/fonts.xml"; |
| 277 | SkString basePath = GetResourcePath("fonts/"); |
| 278 | SkString fontsXml = GetResourcePath(fontsXmlFilename); |
| 279 | |
| 280 | if (!sk_exists(fontsXml.c_str())) { |
| 281 | ERRORF(reporter, "file missing: %s\n", fontsXmlFilename); |
| 282 | return; |
| 283 | } |
| 284 | |
| 285 | SkFontMgr_Android_CustomFonts custom; |
| 286 | custom.fSystemFontUse = SkFontMgr_Android_CustomFonts::kOnlyCustom; |
| 287 | custom.fBasePath = basePath.c_str(); |
| 288 | custom.fFontsXml = fontsXml.c_str(); |
| 289 | custom.fFallbackFontsXml = nullptr; |
| 290 | custom.fIsolated = false; |
| 291 | |
| 292 | sk_sp<SkFontMgr> fontMgr(SkFontMgr_New_Android(&custom)); |
| 293 | // "sans-serif" in "fonts/fonts.xml" is "fonts/Distortable.ttf" |
| 294 | sk_sp<SkTypeface> typeface(fontMgr->legacyMakeTypeface("sans-serif", SkFontStyle())); |
| 295 | |
| 296 | SkBitmap bitmapStream; |
| 297 | bitmapStream.allocN32Pixels(64, 64); |
| 298 | SkCanvas canvasStream(bitmapStream); |
| 299 | canvasStream.drawColor(SK_ColorWHITE); |
| 300 | |
| 301 | SkBitmap bitmapClone; |
| 302 | bitmapClone.allocN32Pixels(64, 64); |
| 303 | SkCanvas canvasClone(bitmapClone); |
| 304 | canvasStream.drawColor(SK_ColorWHITE); |
| 305 | |
Hal Canary | 3560ea7 | 2019-01-08 13:01:58 -0500 | [diff] [blame] | 306 | SkPaint paint; |
| 307 | paint.setColor(SK_ColorGRAY); |
| 308 | paint.setAntiAlias(true); |
| 309 | constexpr float kTextSize = 20; |
Bruce Wang | 37b6109 | 2018-06-20 16:43:02 -0400 | [diff] [blame] | 310 | |
| 311 | std::unique_ptr<SkStreamAsset> distortableStream( |
| 312 | GetResourceAsStream("fonts/Distortable.ttf")); |
| 313 | if (!distortableStream) { |
| 314 | return; |
| 315 | } |
| 316 | |
Bruce Wang | 37b6109 | 2018-06-20 16:43:02 -0400 | [diff] [blame] | 317 | SkPoint point = SkPoint::Make(20.0f, 20.0f); |
| 318 | SkFourByteTag tag = SkSetFourByteTag('w', 'g', 'h', 't'); |
| 319 | |
| 320 | for (int i = 0; i < 10; ++i) { |
| 321 | SkScalar styleValue = |
| 322 | SkDoubleToScalar(0.5 + i * ((2.0 - 0.5) / 10)); |
| 323 | SkFontArguments::VariationPosition::Coordinate |
| 324 | coordinates[] = {{tag, styleValue}}; |
| 325 | SkFontArguments::VariationPosition |
| 326 | position = {coordinates, SK_ARRAY_COUNT(coordinates)}; |
| 327 | |
Hal Canary | 3560ea7 | 2019-01-08 13:01:58 -0500 | [diff] [blame] | 328 | SkFont fontStream( |
Bruce Wang | 37b6109 | 2018-06-20 16:43:02 -0400 | [diff] [blame] | 329 | fontMgr->makeFromStream(distortableStream->duplicate(), |
Hal Canary | 3560ea7 | 2019-01-08 13:01:58 -0500 | [diff] [blame] | 330 | SkFontArguments().setVariationDesignPosition(position)), |
| 331 | kTextSize); |
| 332 | fontStream.setEdging(SkFont::Edging::kSubpixelAntiAlias); |
Bruce Wang | 37b6109 | 2018-06-20 16:43:02 -0400 | [diff] [blame] | 333 | |
Hal Canary | 3560ea7 | 2019-01-08 13:01:58 -0500 | [diff] [blame] | 334 | |
| 335 | SkFont fontClone( |
| 336 | typeface->makeClone(SkFontArguments().setVariationDesignPosition(position)), kTextSize); |
| 337 | fontClone.setEdging(SkFont::Edging::kSubpixelAntiAlias); |
| 338 | |
| 339 | constexpr char text[] = "abc"; |
Bruce Wang | 37b6109 | 2018-06-20 16:43:02 -0400 | [diff] [blame] | 340 | |
| 341 | canvasStream.drawColor(SK_ColorWHITE); |
Hal Canary | 3560ea7 | 2019-01-08 13:01:58 -0500 | [diff] [blame] | 342 | canvasStream.drawString(text, point.fX, point.fY, fontStream, paint); |
Bruce Wang | 37b6109 | 2018-06-20 16:43:02 -0400 | [diff] [blame] | 343 | |
| 344 | canvasClone.drawColor(SK_ColorWHITE); |
Hal Canary | 3560ea7 | 2019-01-08 13:01:58 -0500 | [diff] [blame] | 345 | canvasClone.drawString(text, point.fX, point.fY, fontClone, paint); |
Bruce Wang | 37b6109 | 2018-06-20 16:43:02 -0400 | [diff] [blame] | 346 | |
| 347 | bool success = bitmap_compare(bitmapStream, bitmapClone); |
| 348 | REPORTER_ASSERT(reporter, success); |
| 349 | } |
| 350 | } |
| 351 | |
Ben Wagner | 9f0d8c2 | 2018-11-15 17:14:41 -0500 | [diff] [blame] | 352 | DEF_TEST(FontMgrAndroidSystemFallbackFor, reporter) { |
| 353 | constexpr char fontsXmlFilename[] = "fonts/fonts.xml"; |
| 354 | SkString basePath = GetResourcePath("fonts/"); |
| 355 | SkString fontsXml = GetResourcePath(fontsXmlFilename); |
Bruce Wang | 37b6109 | 2018-06-20 16:43:02 -0400 | [diff] [blame] | 356 | |
Ben Wagner | 9f0d8c2 | 2018-11-15 17:14:41 -0500 | [diff] [blame] | 357 | if (!sk_exists(fontsXml.c_str())) { |
| 358 | ERRORF(reporter, "file missing: %s\n", fontsXmlFilename); |
| 359 | return; |
| 360 | } |
| 361 | |
| 362 | SkFontMgr_Android_CustomFonts custom; |
| 363 | custom.fSystemFontUse = SkFontMgr_Android_CustomFonts::kOnlyCustom; |
| 364 | custom.fBasePath = basePath.c_str(); |
| 365 | custom.fFontsXml = fontsXml.c_str(); |
| 366 | custom.fFallbackFontsXml = nullptr; |
| 367 | custom.fIsolated = false; |
| 368 | |
| 369 | sk_sp<SkFontMgr> fontMgr(SkFontMgr_New_Android(&custom)); |
| 370 | // "sans-serif" in "fonts/fonts.xml" is "fonts/Distortable.ttf", which doesn't have a '!' |
| 371 | // but "TestTTC" has a bold font which does have '!' and is marked as fallback for "sans-serif" |
| 372 | // and should take precedence over the same font marked as normal weight next to it. |
| 373 | sk_sp<SkTypeface> typeface(fontMgr->matchFamilyStyleCharacter( |
| 374 | "sans-serif", SkFontStyle(), nullptr, 0, '!')); |
| 375 | |
| 376 | REPORTER_ASSERT(reporter, typeface->fontStyle() == SkFontStyle::Bold()); |
| 377 | } |