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