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