blob: 6af6637ca1467af36ed67445b26a8e4c61b6b6ab [file] [log] [blame]
tomhudsonf79673b2014-08-05 06:36:11 -07001/*
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"
bungemaneb2be7f2015-02-10 07:51:12 -08009#include "SkCommandLineFlags.h"
benjaminwagner6c71e0a2016-04-07 08:49:31 -070010#include "SkFixed.h"
Ben Wagnerf1729a72017-11-13 11:14:36 -050011#include "SkFontMgr_android.h"
bungemanc5308542015-06-23 13:25:46 -070012#include "SkFontMgr_android_parser.h"
Hal Canarya4935102017-12-08 13:35:47 -050013#include "SkOSFile.h"
Ben Wagnerf1729a72017-11-13 11:14:36 -050014#include "SkTypeface.h"
tomhudsonf79673b2014-08-05 06:36:11 -070015#include "Test.h"
16
bungeman41868fe2015-05-20 09:21:04 -070017#include <cmath>
18#include <cstdio>
19
bungemaneb2be7f2015-02-10 07:51:12 -080020DECLARE_bool(verboseFontMgr);
21
tomhudson2ed49a42014-08-13 07:53:48 -070022int CountFallbacks(SkTDArray<FontFamily*> fontFamilies) {
23 int countOfFallbackFonts = 0;
24 for (int i = 0; i < fontFamilies.count(); i++) {
25 if (fontFamilies[i]->fIsFallbackFont) {
26 countOfFallbackFonts++;
27 }
28 }
29 return countOfFallbackFonts;
30}
31
bungemanc3c69432015-02-11 07:18:51 -080032//https://tools.ietf.org/html/rfc5234#appendix-B.1
33static bool isALPHA(int c) {
34 return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z');
35}
36
37//https://tools.ietf.org/html/rfc5234#appendix-B.1
38static bool isDIGIT(int c) {
39 return ('0' <= c && c <= '9');
40}
41
bungeman4b86bac2014-11-04 10:54:31 -080042void ValidateLoadedFonts(SkTDArray<FontFamily*> fontFamilies, const char* firstExpectedFile,
tomhudsonf79673b2014-08-05 06:36:11 -070043 skiatest::Reporter* reporter) {
44 REPORTER_ASSERT(reporter, fontFamilies[0]->fNames.count() == 5);
45 REPORTER_ASSERT(reporter, !strcmp(fontFamilies[0]->fNames[0].c_str(), "sans-serif"));
46 REPORTER_ASSERT(reporter,
bungeman4b86bac2014-11-04 10:54:31 -080047 !strcmp(fontFamilies[0]->fFonts[0].fFileName.c_str(), firstExpectedFile));
tomhudsonf79673b2014-08-05 06:36:11 -070048 REPORTER_ASSERT(reporter, !fontFamilies[0]->fIsFallbackFont);
bungemanc3c69432015-02-11 07:18:51 -080049
50 // Check that the languages are all sane.
Ben Wagneraee878d2017-08-10 13:49:41 -040051 for (const auto& fontFamily : fontFamilies) {
52 for (const auto& lang : fontFamily->fLanguages) {
53 const SkString& langString = lang.getTag();
54 for (size_t i = 0; i < langString.size(); ++i) {
55 int c = langString[i];
56 REPORTER_ASSERT(reporter, isALPHA(c) || isDIGIT(c) || '-' == c);
57 }
bungemanc3c69432015-02-11 07:18:51 -080058 }
59 }
bungeman9a0808f2015-02-13 08:55:16 -080060
61 // All file names in the test configuration files start with a capital letter.
62 // This is not a general requirement, but it is true of all the test configuration data.
63 // Verifying ensures the filenames have been read sanely and have not been 'sliced'.
64 for (int i = 0; i < fontFamilies.count(); ++i) {
65 FontFamily& family = *fontFamilies[i];
66 for (int j = 0; j < family.fFonts.count(); ++j) {
67 FontFileInfo& file = family.fFonts[j];
68 REPORTER_ASSERT(reporter, !file.fFileName.isEmpty() &&
69 file.fFileName[0] >= 'A' &&
70 file.fFileName[0] <= 'Z');
71 }
72 }
tomhudsonf79673b2014-08-05 06:36:11 -070073}
74
bungeman3d47e212015-02-13 18:30:11 -080075void DumpLoadedFonts(SkTDArray<FontFamily*> fontFamilies, const char* label) {
bungemaneb2be7f2015-02-10 07:51:12 -080076 if (!FLAGS_verboseFontMgr) {
77 return;
78 }
79
bungeman3d47e212015-02-13 18:30:11 -080080 SkDebugf("\n--- Dumping %s\n", label);
tomhudsonf79673b2014-08-05 06:36:11 -070081 for (int i = 0; i < fontFamilies.count(); ++i) {
82 SkDebugf("Family %d:\n", i);
tomhudson07544752014-08-05 13:35:00 -070083 switch(fontFamilies[i]->fVariant) {
bungemana44e9b92015-01-30 19:58:19 -080084 case kElegant_FontVariant: SkDebugf(" elegant\n"); break;
85 case kCompact_FontVariant: SkDebugf(" compact\n"); break;
tomhudson07544752014-08-05 13:35:00 -070086 default: break;
87 }
bungemaneb2be7f2015-02-10 07:51:12 -080088 SkDebugf(" basePath %s\n", fontFamilies[i]->fBasePath.c_str());
Ben Wagneraee878d2017-08-10 13:49:41 -040089 if (!fontFamilies[i]->fLanguages.empty()) {
90 SkDebugf(" language");
91 for (const auto& lang : fontFamilies[i]->fLanguages) {
92 SkDebugf(" %s", lang.getTag().c_str());
93 }
94 SkDebugf("\n");
tomhudson07544752014-08-05 13:35:00 -070095 }
tomhudsonf79673b2014-08-05 06:36:11 -070096 for (int j = 0; j < fontFamilies[i]->fNames.count(); ++j) {
97 SkDebugf(" name %s\n", fontFamilies[i]->fNames[j].c_str());
98 }
tomhudsond3ddea22014-08-11 11:28:00 -070099 for (int j = 0; j < fontFamilies[i]->fFonts.count(); ++j) {
100 const FontFileInfo& ffi = fontFamilies[i]->fFonts[j];
bungeman47a1e962016-02-25 11:20:01 -0800101 SkDebugf(" file (%d) %s#%d", ffi.fWeight, ffi.fFileName.c_str(), ffi.fIndex);
Ben Wagnerfc497342017-02-24 11:15:26 -0500102 for (const auto& coordinate : ffi.fVariationDesignPosition) {
bungeman47a1e962016-02-25 11:20:01 -0800103 SkDebugf(" @'%c%c%c%c'=%f",
Ben Wagnerfc497342017-02-24 11:15:26 -0500104 (coordinate.axis >> 24) & 0xFF,
105 (coordinate.axis >> 16) & 0xFF,
106 (coordinate.axis >> 8) & 0xFF,
107 (coordinate.axis ) & 0xFF,
108 coordinate.value);
bungeman47a1e962016-02-25 11:20:01 -0800109 }
110 SkDebugf("\n");
tomhudson07544752014-08-05 13:35:00 -0700111 }
tomhudsonf79673b2014-08-05 06:36:11 -0700112 }
bungemanc3c69432015-02-11 07:18:51 -0800113 SkDebugf("\n\n");
tomhudsonf79673b2014-08-05 06:36:11 -0700114}
115
bungeman41868fe2015-05-20 09:21:04 -0700116template <int N, typename T> static double test_parse_fixed_r(skiatest::Reporter* reporter,
117 double low, double high, double inc)
118{
119 double SK_FixedMax_double = nextafter(1 << (sizeof(T) * CHAR_BIT - N - 1), 0.0);
120 double SK_FixedEpsilon_double = (1.0 / (1 << N));
121 double maxError = 0;
122 char buffer[64];
123 for (double f = low; f < high; f += inc) {
124 SkString s;
125 // 'sprintf' formatting as expected depends on the current locale being "C".
126 // We currently expect tests and tools to run in the "C" locale.
127 sprintf(buffer, "%.20f", f);
128 T fix;
129 bool b = parse_fixed<N>(buffer, &fix);
130 if (b) {
131 double f2 = fix * SK_FixedEpsilon_double;
132 double error = fabs(f - f2);
133 REPORTER_ASSERT(reporter, error <= SK_FixedEpsilon_double);
134 maxError = SkTMax(maxError, error);
135 } else {
136 REPORTER_ASSERT(reporter, f < -SK_FixedMax_double || SK_FixedMax_double < f);
137 }
138 }
139
140 //SkDebugf("maxError: %.20f\n", maxError);
141 return maxError;
142}
143
144static void test_parse_fixed(skiatest::Reporter* reporter) {
145 test_parse_fixed_r<27, int32_t>(reporter, -8.1, -7.9, 0.000001);
146 test_parse_fixed_r<27, int32_t>(reporter, -0.1, 0.1, 0.000001);
147 test_parse_fixed_r<27, int32_t>(reporter, 7.9, 8.1, 0.000001);
148 test_parse_fixed_r<16, int32_t>(reporter, -0.125, 0.125, 1.0 / (1 << 19));
149 test_parse_fixed_r<16, int32_t>(reporter, -32768.125, -32766.875, 1.0 / (1 << 17));
150 test_parse_fixed_r<16, int32_t>(reporter, 32766.875, 32768.125, 1.0 / (1 << 17));
151 test_parse_fixed_r<16, int32_t>(reporter, -1.1, 1.1, 0.0001);
152
153 SkFixed fix;
154 REPORTER_ASSERT(reporter, !parse_fixed<27>("-17.1", &fix));
155 REPORTER_ASSERT(reporter, !parse_fixed<16>("32768", &fix));
156 REPORTER_ASSERT(reporter, !parse_fixed<16>("", &fix));
157 REPORTER_ASSERT(reporter, !parse_fixed<16>(".", &fix));
158 REPORTER_ASSERT(reporter, !parse_fixed<16>("123.", &fix));
159 REPORTER_ASSERT(reporter, !parse_fixed<16>("a", &fix));
160 REPORTER_ASSERT(reporter, !parse_fixed<16>(".123a", &fix));
161}
162
bungemanc5308542015-06-23 13:25:46 -0700163DEF_TEST(FontMgrAndroidParser, reporter) {
bungeman41868fe2015-05-20 09:21:04 -0700164 test_parse_fixed(reporter);
tomhudsonf79673b2014-08-05 06:36:11 -0700165
tomhudson8aed3c12014-08-07 10:20:51 -0700166 bool resourcesMissing = false;
167
tomhudsonf79673b2014-08-05 06:36:11 -0700168 SkTDArray<FontFamily*> preV17FontFamilies;
bungemanc5308542015-06-23 13:25:46 -0700169 SkFontMgr_Android_Parser::GetCustomFontFamilies(preV17FontFamilies,
bungeman7fa87cd2015-02-06 07:59:19 -0800170 SkString("/custom/font/path/"),
tomhudsonf79673b2014-08-05 06:36:11 -0700171 GetResourcePath("android_fonts/pre_v17/system_fonts.xml").c_str(),
172 GetResourcePath("android_fonts/pre_v17/fallback_fonts.xml").c_str());
173
tomhudson8aed3c12014-08-07 10:20:51 -0700174 if (preV17FontFamilies.count() > 0) {
175 REPORTER_ASSERT(reporter, preV17FontFamilies.count() == 14);
tomhudson2ed49a42014-08-13 07:53:48 -0700176 REPORTER_ASSERT(reporter, CountFallbacks(preV17FontFamilies) == 10);
tomhudsonf79673b2014-08-05 06:36:11 -0700177
bungeman3d47e212015-02-13 18:30:11 -0800178 DumpLoadedFonts(preV17FontFamilies, "pre version 17");
bungeman4b86bac2014-11-04 10:54:31 -0800179 ValidateLoadedFonts(preV17FontFamilies, "Roboto-Regular.ttf", reporter);
tomhudson8aed3c12014-08-07 10:20:51 -0700180 } else {
181 resourcesMissing = true;
182 }
bungeman91e51cb2015-07-15 14:29:25 -0400183 preV17FontFamilies.deleteAll();
tomhudsonf79673b2014-08-05 06:36:11 -0700184
tomhudson07544752014-08-05 13:35:00 -0700185
tomhudsonf79673b2014-08-05 06:36:11 -0700186 SkTDArray<FontFamily*> v17FontFamilies;
bungemanc5308542015-06-23 13:25:46 -0700187 SkFontMgr_Android_Parser::GetCustomFontFamilies(v17FontFamilies,
bungeman7fa87cd2015-02-06 07:59:19 -0800188 SkString("/custom/font/path/"),
tomhudsonf79673b2014-08-05 06:36:11 -0700189 GetResourcePath("android_fonts/v17/system_fonts.xml").c_str(),
bungemanc3c69432015-02-11 07:18:51 -0800190 GetResourcePath("android_fonts/v17/fallback_fonts.xml").c_str(),
191 GetResourcePath("android_fonts/v17").c_str());
tomhudsonf79673b2014-08-05 06:36:11 -0700192
tomhudson8aed3c12014-08-07 10:20:51 -0700193 if (v17FontFamilies.count() > 0) {
bungemanc3c69432015-02-11 07:18:51 -0800194 REPORTER_ASSERT(reporter, v17FontFamilies.count() == 56);
195 REPORTER_ASSERT(reporter, CountFallbacks(v17FontFamilies) == 46);
tomhudsonf79673b2014-08-05 06:36:11 -0700196
bungeman3d47e212015-02-13 18:30:11 -0800197 DumpLoadedFonts(v17FontFamilies, "version 17");
bungeman4b86bac2014-11-04 10:54:31 -0800198 ValidateLoadedFonts(v17FontFamilies, "Roboto-Regular.ttf", reporter);
tomhudson8aed3c12014-08-07 10:20:51 -0700199 } else {
200 resourcesMissing = true;
201 }
bungeman91e51cb2015-07-15 14:29:25 -0400202 v17FontFamilies.deleteAll();
tomhudsonf79673b2014-08-05 06:36:11 -0700203
tomhudson07544752014-08-05 13:35:00 -0700204
tomhudsonf79673b2014-08-05 06:36:11 -0700205 SkTDArray<FontFamily*> v22FontFamilies;
bungemanc5308542015-06-23 13:25:46 -0700206 SkFontMgr_Android_Parser::GetCustomFontFamilies(v22FontFamilies,
bungeman7fa87cd2015-02-06 07:59:19 -0800207 SkString("/custom/font/path/"),
tomhudsonf79673b2014-08-05 06:36:11 -0700208 GetResourcePath("android_fonts/v22/fonts.xml").c_str(),
halcanary96fcdcc2015-08-27 07:41:13 -0700209 nullptr);
tomhudsonf79673b2014-08-05 06:36:11 -0700210
tomhudson8aed3c12014-08-07 10:20:51 -0700211 if (v22FontFamilies.count() > 0) {
bungeman41868fe2015-05-20 09:21:04 -0700212 REPORTER_ASSERT(reporter, v22FontFamilies.count() == 54);
tomhudson2ed49a42014-08-13 07:53:48 -0700213 REPORTER_ASSERT(reporter, CountFallbacks(v22FontFamilies) == 42);
tomhudsonf79673b2014-08-05 06:36:11 -0700214
bungeman3d47e212015-02-13 18:30:11 -0800215 DumpLoadedFonts(v22FontFamilies, "version 22");
bungeman4b86bac2014-11-04 10:54:31 -0800216 ValidateLoadedFonts(v22FontFamilies, "Roboto-Thin.ttf", reporter);
tomhudson8aed3c12014-08-07 10:20:51 -0700217 } else {
218 resourcesMissing = true;
219 }
bungeman91e51cb2015-07-15 14:29:25 -0400220 v22FontFamilies.deleteAll();
tomhudson8aed3c12014-08-07 10:20:51 -0700221
222 if (resourcesMissing) {
223 SkDebugf("---- Resource files missing for FontConfigParser test\n");
224 }
tomhudsonf79673b2014-08-05 06:36:11 -0700225}
Ben Wagnerf1729a72017-11-13 11:14:36 -0500226
227DEF_TEST(FontMgrAndroidLegacyMakeTypeface, reporter) {
Hal Canarya4935102017-12-08 13:35:47 -0500228 constexpr char fontsXmlFilename[] = "fonts/fonts.xml";
Ben Wagnerf1729a72017-11-13 11:14:36 -0500229 SkString basePath = GetResourcePath("fonts/");
Hal Canarya4935102017-12-08 13:35:47 -0500230 SkString fontsXml = GetResourcePath(fontsXmlFilename);
231
232 if (!sk_exists(fontsXml.c_str())) {
233 ERRORF(reporter, "file missing: %s\n", fontsXmlFilename);
234 return;
235 }
Ben Wagnerf1729a72017-11-13 11:14:36 -0500236
237 SkFontMgr_Android_CustomFonts custom;
238 custom.fSystemFontUse = SkFontMgr_Android_CustomFonts::kOnlyCustom;
239 custom.fBasePath = basePath.c_str();
240 custom.fFontsXml = fontsXml.c_str();
241 custom.fFallbackFontsXml = nullptr;
242 custom.fIsolated = false;
243
244 sk_sp<SkFontMgr> fm(SkFontMgr_New_Android(&custom));
245 sk_sp<SkTypeface> t(fm->legacyMakeTypeface("non-existent-font", SkFontStyle()));
246 REPORTER_ASSERT(reporter, nullptr == t);
247}