blob: cd10d04dc6ad72c2e5248430da73d47638eefe46 [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"
Ben Wagnerf1729a72017-11-13 11:14:36 -050013#include "SkTypeface.h"
tomhudsonf79673b2014-08-05 06:36:11 -070014#include "Test.h"
15
bungeman41868fe2015-05-20 09:21:04 -070016#include <cmath>
17#include <cstdio>
18
bungemaneb2be7f2015-02-10 07:51:12 -080019DECLARE_bool(verboseFontMgr);
20
tomhudson2ed49a42014-08-13 07:53:48 -070021int CountFallbacks(SkTDArray<FontFamily*> fontFamilies) {
22 int countOfFallbackFonts = 0;
23 for (int i = 0; i < fontFamilies.count(); i++) {
24 if (fontFamilies[i]->fIsFallbackFont) {
25 countOfFallbackFonts++;
26 }
27 }
28 return countOfFallbackFonts;
29}
30
bungemanc3c69432015-02-11 07:18:51 -080031//https://tools.ietf.org/html/rfc5234#appendix-B.1
32static bool isALPHA(int c) {
33 return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z');
34}
35
36//https://tools.ietf.org/html/rfc5234#appendix-B.1
37static bool isDIGIT(int c) {
38 return ('0' <= c && c <= '9');
39}
40
bungeman4b86bac2014-11-04 10:54:31 -080041void ValidateLoadedFonts(SkTDArray<FontFamily*> fontFamilies, const char* firstExpectedFile,
tomhudsonf79673b2014-08-05 06:36:11 -070042 skiatest::Reporter* reporter) {
43 REPORTER_ASSERT(reporter, fontFamilies[0]->fNames.count() == 5);
44 REPORTER_ASSERT(reporter, !strcmp(fontFamilies[0]->fNames[0].c_str(), "sans-serif"));
45 REPORTER_ASSERT(reporter,
bungeman4b86bac2014-11-04 10:54:31 -080046 !strcmp(fontFamilies[0]->fFonts[0].fFileName.c_str(), firstExpectedFile));
tomhudsonf79673b2014-08-05 06:36:11 -070047 REPORTER_ASSERT(reporter, !fontFamilies[0]->fIsFallbackFont);
bungemanc3c69432015-02-11 07:18:51 -080048
49 // Check that the languages are all sane.
Ben Wagneraee878d2017-08-10 13:49:41 -040050 for (const auto& fontFamily : fontFamilies) {
51 for (const auto& lang : fontFamily->fLanguages) {
52 const SkString& langString = lang.getTag();
53 for (size_t i = 0; i < langString.size(); ++i) {
54 int c = langString[i];
55 REPORTER_ASSERT(reporter, isALPHA(c) || isDIGIT(c) || '-' == c);
56 }
bungemanc3c69432015-02-11 07:18:51 -080057 }
58 }
bungeman9a0808f2015-02-13 08:55:16 -080059
60 // All file names in the test configuration files start with a capital letter.
61 // This is not a general requirement, but it is true of all the test configuration data.
62 // Verifying ensures the filenames have been read sanely and have not been 'sliced'.
63 for (int i = 0; i < fontFamilies.count(); ++i) {
64 FontFamily& family = *fontFamilies[i];
65 for (int j = 0; j < family.fFonts.count(); ++j) {
66 FontFileInfo& file = family.fFonts[j];
67 REPORTER_ASSERT(reporter, !file.fFileName.isEmpty() &&
68 file.fFileName[0] >= 'A' &&
69 file.fFileName[0] <= 'Z');
70 }
71 }
tomhudsonf79673b2014-08-05 06:36:11 -070072}
73
bungeman3d47e212015-02-13 18:30:11 -080074void DumpLoadedFonts(SkTDArray<FontFamily*> fontFamilies, const char* label) {
bungemaneb2be7f2015-02-10 07:51:12 -080075 if (!FLAGS_verboseFontMgr) {
76 return;
77 }
78
bungeman3d47e212015-02-13 18:30:11 -080079 SkDebugf("\n--- Dumping %s\n", label);
tomhudsonf79673b2014-08-05 06:36:11 -070080 for (int i = 0; i < fontFamilies.count(); ++i) {
81 SkDebugf("Family %d:\n", i);
tomhudson07544752014-08-05 13:35:00 -070082 switch(fontFamilies[i]->fVariant) {
bungemana44e9b92015-01-30 19:58:19 -080083 case kElegant_FontVariant: SkDebugf(" elegant\n"); break;
84 case kCompact_FontVariant: SkDebugf(" compact\n"); break;
tomhudson07544752014-08-05 13:35:00 -070085 default: break;
86 }
bungemaneb2be7f2015-02-10 07:51:12 -080087 SkDebugf(" basePath %s\n", fontFamilies[i]->fBasePath.c_str());
Ben Wagneraee878d2017-08-10 13:49:41 -040088 if (!fontFamilies[i]->fLanguages.empty()) {
89 SkDebugf(" language");
90 for (const auto& lang : fontFamilies[i]->fLanguages) {
91 SkDebugf(" %s", lang.getTag().c_str());
92 }
93 SkDebugf("\n");
tomhudson07544752014-08-05 13:35:00 -070094 }
tomhudsonf79673b2014-08-05 06:36:11 -070095 for (int j = 0; j < fontFamilies[i]->fNames.count(); ++j) {
96 SkDebugf(" name %s\n", fontFamilies[i]->fNames[j].c_str());
97 }
tomhudsond3ddea22014-08-11 11:28:00 -070098 for (int j = 0; j < fontFamilies[i]->fFonts.count(); ++j) {
99 const FontFileInfo& ffi = fontFamilies[i]->fFonts[j];
bungeman47a1e962016-02-25 11:20:01 -0800100 SkDebugf(" file (%d) %s#%d", ffi.fWeight, ffi.fFileName.c_str(), ffi.fIndex);
Ben Wagnerfc497342017-02-24 11:15:26 -0500101 for (const auto& coordinate : ffi.fVariationDesignPosition) {
bungeman47a1e962016-02-25 11:20:01 -0800102 SkDebugf(" @'%c%c%c%c'=%f",
Ben Wagnerfc497342017-02-24 11:15:26 -0500103 (coordinate.axis >> 24) & 0xFF,
104 (coordinate.axis >> 16) & 0xFF,
105 (coordinate.axis >> 8) & 0xFF,
106 (coordinate.axis ) & 0xFF,
107 coordinate.value);
bungeman47a1e962016-02-25 11:20:01 -0800108 }
109 SkDebugf("\n");
tomhudson07544752014-08-05 13:35:00 -0700110 }
tomhudsonf79673b2014-08-05 06:36:11 -0700111 }
bungemanc3c69432015-02-11 07:18:51 -0800112 SkDebugf("\n\n");
tomhudsonf79673b2014-08-05 06:36:11 -0700113}
114
bungeman41868fe2015-05-20 09:21:04 -0700115template <int N, typename T> static double test_parse_fixed_r(skiatest::Reporter* reporter,
116 double low, double high, double inc)
117{
118 double SK_FixedMax_double = nextafter(1 << (sizeof(T) * CHAR_BIT - N - 1), 0.0);
119 double SK_FixedEpsilon_double = (1.0 / (1 << N));
120 double maxError = 0;
121 char buffer[64];
122 for (double f = low; f < high; f += inc) {
123 SkString s;
124 // 'sprintf' formatting as expected depends on the current locale being "C".
125 // We currently expect tests and tools to run in the "C" locale.
126 sprintf(buffer, "%.20f", f);
127 T fix;
128 bool b = parse_fixed<N>(buffer, &fix);
129 if (b) {
130 double f2 = fix * SK_FixedEpsilon_double;
131 double error = fabs(f - f2);
132 REPORTER_ASSERT(reporter, error <= SK_FixedEpsilon_double);
133 maxError = SkTMax(maxError, error);
134 } else {
135 REPORTER_ASSERT(reporter, f < -SK_FixedMax_double || SK_FixedMax_double < f);
136 }
137 }
138
139 //SkDebugf("maxError: %.20f\n", maxError);
140 return maxError;
141}
142
143static void test_parse_fixed(skiatest::Reporter* reporter) {
144 test_parse_fixed_r<27, int32_t>(reporter, -8.1, -7.9, 0.000001);
145 test_parse_fixed_r<27, int32_t>(reporter, -0.1, 0.1, 0.000001);
146 test_parse_fixed_r<27, int32_t>(reporter, 7.9, 8.1, 0.000001);
147 test_parse_fixed_r<16, int32_t>(reporter, -0.125, 0.125, 1.0 / (1 << 19));
148 test_parse_fixed_r<16, int32_t>(reporter, -32768.125, -32766.875, 1.0 / (1 << 17));
149 test_parse_fixed_r<16, int32_t>(reporter, 32766.875, 32768.125, 1.0 / (1 << 17));
150 test_parse_fixed_r<16, int32_t>(reporter, -1.1, 1.1, 0.0001);
151
152 SkFixed fix;
153 REPORTER_ASSERT(reporter, !parse_fixed<27>("-17.1", &fix));
154 REPORTER_ASSERT(reporter, !parse_fixed<16>("32768", &fix));
155 REPORTER_ASSERT(reporter, !parse_fixed<16>("", &fix));
156 REPORTER_ASSERT(reporter, !parse_fixed<16>(".", &fix));
157 REPORTER_ASSERT(reporter, !parse_fixed<16>("123.", &fix));
158 REPORTER_ASSERT(reporter, !parse_fixed<16>("a", &fix));
159 REPORTER_ASSERT(reporter, !parse_fixed<16>(".123a", &fix));
160}
161
bungemanc5308542015-06-23 13:25:46 -0700162DEF_TEST(FontMgrAndroidParser, reporter) {
bungeman41868fe2015-05-20 09:21:04 -0700163 test_parse_fixed(reporter);
tomhudsonf79673b2014-08-05 06:36:11 -0700164
tomhudson8aed3c12014-08-07 10:20:51 -0700165 bool resourcesMissing = false;
166
tomhudsonf79673b2014-08-05 06:36:11 -0700167 SkTDArray<FontFamily*> preV17FontFamilies;
bungemanc5308542015-06-23 13:25:46 -0700168 SkFontMgr_Android_Parser::GetCustomFontFamilies(preV17FontFamilies,
bungeman7fa87cd2015-02-06 07:59:19 -0800169 SkString("/custom/font/path/"),
tomhudsonf79673b2014-08-05 06:36:11 -0700170 GetResourcePath("android_fonts/pre_v17/system_fonts.xml").c_str(),
171 GetResourcePath("android_fonts/pre_v17/fallback_fonts.xml").c_str());
172
tomhudson8aed3c12014-08-07 10:20:51 -0700173 if (preV17FontFamilies.count() > 0) {
174 REPORTER_ASSERT(reporter, preV17FontFamilies.count() == 14);
tomhudson2ed49a42014-08-13 07:53:48 -0700175 REPORTER_ASSERT(reporter, CountFallbacks(preV17FontFamilies) == 10);
tomhudsonf79673b2014-08-05 06:36:11 -0700176
bungeman3d47e212015-02-13 18:30:11 -0800177 DumpLoadedFonts(preV17FontFamilies, "pre version 17");
bungeman4b86bac2014-11-04 10:54:31 -0800178 ValidateLoadedFonts(preV17FontFamilies, "Roboto-Regular.ttf", reporter);
tomhudson8aed3c12014-08-07 10:20:51 -0700179 } else {
180 resourcesMissing = true;
181 }
bungeman91e51cb2015-07-15 14:29:25 -0400182 preV17FontFamilies.deleteAll();
tomhudsonf79673b2014-08-05 06:36:11 -0700183
tomhudson07544752014-08-05 13:35:00 -0700184
tomhudsonf79673b2014-08-05 06:36:11 -0700185 SkTDArray<FontFamily*> v17FontFamilies;
bungemanc5308542015-06-23 13:25:46 -0700186 SkFontMgr_Android_Parser::GetCustomFontFamilies(v17FontFamilies,
bungeman7fa87cd2015-02-06 07:59:19 -0800187 SkString("/custom/font/path/"),
tomhudsonf79673b2014-08-05 06:36:11 -0700188 GetResourcePath("android_fonts/v17/system_fonts.xml").c_str(),
bungemanc3c69432015-02-11 07:18:51 -0800189 GetResourcePath("android_fonts/v17/fallback_fonts.xml").c_str(),
190 GetResourcePath("android_fonts/v17").c_str());
tomhudsonf79673b2014-08-05 06:36:11 -0700191
tomhudson8aed3c12014-08-07 10:20:51 -0700192 if (v17FontFamilies.count() > 0) {
bungemanc3c69432015-02-11 07:18:51 -0800193 REPORTER_ASSERT(reporter, v17FontFamilies.count() == 56);
194 REPORTER_ASSERT(reporter, CountFallbacks(v17FontFamilies) == 46);
tomhudsonf79673b2014-08-05 06:36:11 -0700195
bungeman3d47e212015-02-13 18:30:11 -0800196 DumpLoadedFonts(v17FontFamilies, "version 17");
bungeman4b86bac2014-11-04 10:54:31 -0800197 ValidateLoadedFonts(v17FontFamilies, "Roboto-Regular.ttf", reporter);
tomhudson8aed3c12014-08-07 10:20:51 -0700198 } else {
199 resourcesMissing = true;
200 }
bungeman91e51cb2015-07-15 14:29:25 -0400201 v17FontFamilies.deleteAll();
tomhudsonf79673b2014-08-05 06:36:11 -0700202
tomhudson07544752014-08-05 13:35:00 -0700203
tomhudsonf79673b2014-08-05 06:36:11 -0700204 SkTDArray<FontFamily*> v22FontFamilies;
bungemanc5308542015-06-23 13:25:46 -0700205 SkFontMgr_Android_Parser::GetCustomFontFamilies(v22FontFamilies,
bungeman7fa87cd2015-02-06 07:59:19 -0800206 SkString("/custom/font/path/"),
tomhudsonf79673b2014-08-05 06:36:11 -0700207 GetResourcePath("android_fonts/v22/fonts.xml").c_str(),
halcanary96fcdcc2015-08-27 07:41:13 -0700208 nullptr);
tomhudsonf79673b2014-08-05 06:36:11 -0700209
tomhudson8aed3c12014-08-07 10:20:51 -0700210 if (v22FontFamilies.count() > 0) {
bungeman41868fe2015-05-20 09:21:04 -0700211 REPORTER_ASSERT(reporter, v22FontFamilies.count() == 54);
tomhudson2ed49a42014-08-13 07:53:48 -0700212 REPORTER_ASSERT(reporter, CountFallbacks(v22FontFamilies) == 42);
tomhudsonf79673b2014-08-05 06:36:11 -0700213
bungeman3d47e212015-02-13 18:30:11 -0800214 DumpLoadedFonts(v22FontFamilies, "version 22");
bungeman4b86bac2014-11-04 10:54:31 -0800215 ValidateLoadedFonts(v22FontFamilies, "Roboto-Thin.ttf", reporter);
tomhudson8aed3c12014-08-07 10:20:51 -0700216 } else {
217 resourcesMissing = true;
218 }
bungeman91e51cb2015-07-15 14:29:25 -0400219 v22FontFamilies.deleteAll();
tomhudson8aed3c12014-08-07 10:20:51 -0700220
221 if (resourcesMissing) {
222 SkDebugf("---- Resource files missing for FontConfigParser test\n");
223 }
tomhudsonf79673b2014-08-05 06:36:11 -0700224}
Ben Wagnerf1729a72017-11-13 11:14:36 -0500225
226DEF_TEST(FontMgrAndroidLegacyMakeTypeface, reporter) {
227 SkString basePath = GetResourcePath("fonts/");
228 SkString fontsXml = GetResourcePath("fonts/fonts.xml");
229
230 SkFontMgr_Android_CustomFonts custom;
231 custom.fSystemFontUse = SkFontMgr_Android_CustomFonts::kOnlyCustom;
232 custom.fBasePath = basePath.c_str();
233 custom.fFontsXml = fontsXml.c_str();
234 custom.fFallbackFontsXml = nullptr;
235 custom.fIsolated = false;
236
237 sk_sp<SkFontMgr> fm(SkFontMgr_New_Android(&custom));
238 sk_sp<SkTypeface> t(fm->legacyMakeTypeface("non-existent-font", SkFontStyle()));
239 REPORTER_ASSERT(reporter, nullptr == t);
240}