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