blob: 9d904e106ae564c60838c3f840b9078530b5faba [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"
Bruce Wang37b61092018-06-20 16:43:02 -04009#include "SkCanvas.h"
bungemaneb2be7f2015-02-10 07:51:12 -080010#include "SkCommandLineFlags.h"
benjaminwagner6c71e0a2016-04-07 08:49:31 -070011#include "SkFixed.h"
Ben Wagnerf1729a72017-11-13 11:14:36 -050012#include "SkFontMgr_android.h"
bungemanc5308542015-06-23 13:25:46 -070013#include "SkFontMgr_android_parser.h"
Hal Canarya4935102017-12-08 13:35:47 -050014#include "SkOSFile.h"
Ben Wagnerf1729a72017-11-13 11:14:36 -050015#include "SkTypeface.h"
tomhudsonf79673b2014-08-05 06:36:11 -070016#include "Test.h"
17
bungeman41868fe2015-05-20 09:21:04 -070018#include <cmath>
19#include <cstdio>
20
bungemaneb2be7f2015-02-10 07:51:12 -080021DECLARE_bool(verboseFontMgr);
22
tomhudson2ed49a42014-08-13 07:53:48 -070023int 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
bungemanc3c69432015-02-11 07:18:51 -080033//https://tools.ietf.org/html/rfc5234#appendix-B.1
34static 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
39static bool isDIGIT(int c) {
40 return ('0' <= c && c <= '9');
41}
42
Ben Wagner9f0d8c22018-11-15 17:14:41 -050043static void ValidateLoadedFonts(SkTDArray<FontFamily*> fontFamilies, const char* firstExpectedFile,
44 skiatest::Reporter* reporter) {
tomhudsonf79673b2014-08-05 06:36:11 -070045 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,
bungeman4b86bac2014-11-04 10:54:31 -080048 !strcmp(fontFamilies[0]->fFonts[0].fFileName.c_str(), firstExpectedFile));
tomhudsonf79673b2014-08-05 06:36:11 -070049 REPORTER_ASSERT(reporter, !fontFamilies[0]->fIsFallbackFont);
bungemanc3c69432015-02-11 07:18:51 -080050
51 // Check that the languages are all sane.
Ben Wagneraee878d2017-08-10 13:49:41 -040052 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 }
bungemanc3c69432015-02-11 07:18:51 -080059 }
60 }
bungeman9a0808f2015-02-13 08:55:16 -080061
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 }
tomhudsonf79673b2014-08-05 06:36:11 -070074}
75
Ben Wagner9f0d8c22018-11-15 17:14:41 -050076static void DumpFiles(const FontFamily& fontFamily) {
77 for (int j = 0; j < fontFamily.fFonts.count(); ++j) {
78 const FontFileInfo& ffi = fontFamily.fFonts[j];
79 SkDebugf(" file (%d) %s#%d", ffi.fWeight, ffi.fFileName.c_str(), ffi.fIndex);
80 for (const auto& coordinate : ffi.fVariationDesignPosition) {
81 SkDebugf(" @'%c%c%c%c'=%f",
82 (coordinate.axis >> 24) & 0xFF,
83 (coordinate.axis >> 16) & 0xFF,
84 (coordinate.axis >> 8) & 0xFF,
85 (coordinate.axis ) & 0xFF,
86 coordinate.value);
87 }
88 SkDebugf("\n");
89 }
90}
91
92static void DumpLoadedFonts(SkTDArray<FontFamily*> fontFamilies, const char* label) {
bungemaneb2be7f2015-02-10 07:51:12 -080093 if (!FLAGS_verboseFontMgr) {
94 return;
95 }
96
bungeman3d47e212015-02-13 18:30:11 -080097 SkDebugf("\n--- Dumping %s\n", label);
tomhudsonf79673b2014-08-05 06:36:11 -070098 for (int i = 0; i < fontFamilies.count(); ++i) {
99 SkDebugf("Family %d:\n", i);
tomhudson07544752014-08-05 13:35:00 -0700100 switch(fontFamilies[i]->fVariant) {
bungemana44e9b92015-01-30 19:58:19 -0800101 case kElegant_FontVariant: SkDebugf(" elegant\n"); break;
102 case kCompact_FontVariant: SkDebugf(" compact\n"); break;
tomhudson07544752014-08-05 13:35:00 -0700103 default: break;
104 }
bungemaneb2be7f2015-02-10 07:51:12 -0800105 SkDebugf(" basePath %s\n", fontFamilies[i]->fBasePath.c_str());
Ben Wagneraee878d2017-08-10 13:49:41 -0400106 if (!fontFamilies[i]->fLanguages.empty()) {
107 SkDebugf(" language");
108 for (const auto& lang : fontFamilies[i]->fLanguages) {
109 SkDebugf(" %s", lang.getTag().c_str());
110 }
111 SkDebugf("\n");
tomhudson07544752014-08-05 13:35:00 -0700112 }
tomhudsonf79673b2014-08-05 06:36:11 -0700113 for (int j = 0; j < fontFamilies[i]->fNames.count(); ++j) {
114 SkDebugf(" name %s\n", fontFamilies[i]->fNames[j].c_str());
115 }
Ben Wagner9f0d8c22018-11-15 17:14:41 -0500116 DumpFiles(*fontFamilies[i]);
117 fontFamilies[i]->fallbackFamilies.foreach(
118 [](SkString, std::unique_ptr<FontFamily>* fallbackFamily) {
119 SkDebugf(" Fallback for: %s\n", (*fallbackFamily)->fFallbackFor.c_str());
120 DumpFiles(*(*fallbackFamily).get());
bungeman47a1e962016-02-25 11:20:01 -0800121 }
Ben Wagner9f0d8c22018-11-15 17:14:41 -0500122 );
tomhudsonf79673b2014-08-05 06:36:11 -0700123 }
bungemanc3c69432015-02-11 07:18:51 -0800124 SkDebugf("\n\n");
tomhudsonf79673b2014-08-05 06:36:11 -0700125}
126
bungeman41868fe2015-05-20 09:21:04 -0700127template <int N, typename T> static double test_parse_fixed_r(skiatest::Reporter* reporter,
128 double low, double high, double inc)
129{
130 double SK_FixedMax_double = nextafter(1 << (sizeof(T) * CHAR_BIT - N - 1), 0.0);
131 double SK_FixedEpsilon_double = (1.0 / (1 << N));
132 double maxError = 0;
133 char buffer[64];
134 for (double f = low; f < high; f += inc) {
135 SkString s;
136 // 'sprintf' formatting as expected depends on the current locale being "C".
137 // We currently expect tests and tools to run in the "C" locale.
138 sprintf(buffer, "%.20f", f);
139 T fix;
140 bool b = parse_fixed<N>(buffer, &fix);
141 if (b) {
142 double f2 = fix * SK_FixedEpsilon_double;
143 double error = fabs(f - f2);
144 REPORTER_ASSERT(reporter, error <= SK_FixedEpsilon_double);
145 maxError = SkTMax(maxError, error);
146 } else {
147 REPORTER_ASSERT(reporter, f < -SK_FixedMax_double || SK_FixedMax_double < f);
148 }
149 }
150
151 //SkDebugf("maxError: %.20f\n", maxError);
152 return maxError;
153}
154
155static void test_parse_fixed(skiatest::Reporter* reporter) {
156 test_parse_fixed_r<27, int32_t>(reporter, -8.1, -7.9, 0.000001);
157 test_parse_fixed_r<27, int32_t>(reporter, -0.1, 0.1, 0.000001);
158 test_parse_fixed_r<27, int32_t>(reporter, 7.9, 8.1, 0.000001);
159 test_parse_fixed_r<16, int32_t>(reporter, -0.125, 0.125, 1.0 / (1 << 19));
160 test_parse_fixed_r<16, int32_t>(reporter, -32768.125, -32766.875, 1.0 / (1 << 17));
161 test_parse_fixed_r<16, int32_t>(reporter, 32766.875, 32768.125, 1.0 / (1 << 17));
162 test_parse_fixed_r<16, int32_t>(reporter, -1.1, 1.1, 0.0001);
163
164 SkFixed fix;
165 REPORTER_ASSERT(reporter, !parse_fixed<27>("-17.1", &fix));
166 REPORTER_ASSERT(reporter, !parse_fixed<16>("32768", &fix));
167 REPORTER_ASSERT(reporter, !parse_fixed<16>("", &fix));
168 REPORTER_ASSERT(reporter, !parse_fixed<16>(".", &fix));
169 REPORTER_ASSERT(reporter, !parse_fixed<16>("123.", &fix));
170 REPORTER_ASSERT(reporter, !parse_fixed<16>("a", &fix));
171 REPORTER_ASSERT(reporter, !parse_fixed<16>(".123a", &fix));
172}
173
bungemanc5308542015-06-23 13:25:46 -0700174DEF_TEST(FontMgrAndroidParser, reporter) {
bungeman41868fe2015-05-20 09:21:04 -0700175 test_parse_fixed(reporter);
tomhudsonf79673b2014-08-05 06:36:11 -0700176
tomhudson8aed3c12014-08-07 10:20:51 -0700177 bool resourcesMissing = false;
178
tomhudsonf79673b2014-08-05 06:36:11 -0700179 SkTDArray<FontFamily*> preV17FontFamilies;
bungemanc5308542015-06-23 13:25:46 -0700180 SkFontMgr_Android_Parser::GetCustomFontFamilies(preV17FontFamilies,
bungeman7fa87cd2015-02-06 07:59:19 -0800181 SkString("/custom/font/path/"),
tomhudsonf79673b2014-08-05 06:36:11 -0700182 GetResourcePath("android_fonts/pre_v17/system_fonts.xml").c_str(),
183 GetResourcePath("android_fonts/pre_v17/fallback_fonts.xml").c_str());
184
tomhudson8aed3c12014-08-07 10:20:51 -0700185 if (preV17FontFamilies.count() > 0) {
186 REPORTER_ASSERT(reporter, preV17FontFamilies.count() == 14);
tomhudson2ed49a42014-08-13 07:53:48 -0700187 REPORTER_ASSERT(reporter, CountFallbacks(preV17FontFamilies) == 10);
tomhudsonf79673b2014-08-05 06:36:11 -0700188
bungeman3d47e212015-02-13 18:30:11 -0800189 DumpLoadedFonts(preV17FontFamilies, "pre version 17");
bungeman4b86bac2014-11-04 10:54:31 -0800190 ValidateLoadedFonts(preV17FontFamilies, "Roboto-Regular.ttf", reporter);
tomhudson8aed3c12014-08-07 10:20:51 -0700191 } else {
192 resourcesMissing = true;
193 }
bungeman91e51cb2015-07-15 14:29:25 -0400194 preV17FontFamilies.deleteAll();
tomhudsonf79673b2014-08-05 06:36:11 -0700195
tomhudson07544752014-08-05 13:35:00 -0700196
tomhudsonf79673b2014-08-05 06:36:11 -0700197 SkTDArray<FontFamily*> v17FontFamilies;
bungemanc5308542015-06-23 13:25:46 -0700198 SkFontMgr_Android_Parser::GetCustomFontFamilies(v17FontFamilies,
bungeman7fa87cd2015-02-06 07:59:19 -0800199 SkString("/custom/font/path/"),
tomhudsonf79673b2014-08-05 06:36:11 -0700200 GetResourcePath("android_fonts/v17/system_fonts.xml").c_str(),
bungemanc3c69432015-02-11 07:18:51 -0800201 GetResourcePath("android_fonts/v17/fallback_fonts.xml").c_str(),
202 GetResourcePath("android_fonts/v17").c_str());
tomhudsonf79673b2014-08-05 06:36:11 -0700203
tomhudson8aed3c12014-08-07 10:20:51 -0700204 if (v17FontFamilies.count() > 0) {
bungemanc3c69432015-02-11 07:18:51 -0800205 REPORTER_ASSERT(reporter, v17FontFamilies.count() == 56);
206 REPORTER_ASSERT(reporter, CountFallbacks(v17FontFamilies) == 46);
tomhudsonf79673b2014-08-05 06:36:11 -0700207
bungeman3d47e212015-02-13 18:30:11 -0800208 DumpLoadedFonts(v17FontFamilies, "version 17");
bungeman4b86bac2014-11-04 10:54:31 -0800209 ValidateLoadedFonts(v17FontFamilies, "Roboto-Regular.ttf", reporter);
tomhudson8aed3c12014-08-07 10:20:51 -0700210 } else {
211 resourcesMissing = true;
212 }
bungeman91e51cb2015-07-15 14:29:25 -0400213 v17FontFamilies.deleteAll();
tomhudsonf79673b2014-08-05 06:36:11 -0700214
tomhudson07544752014-08-05 13:35:00 -0700215
tomhudsonf79673b2014-08-05 06:36:11 -0700216 SkTDArray<FontFamily*> v22FontFamilies;
bungemanc5308542015-06-23 13:25:46 -0700217 SkFontMgr_Android_Parser::GetCustomFontFamilies(v22FontFamilies,
bungeman7fa87cd2015-02-06 07:59:19 -0800218 SkString("/custom/font/path/"),
tomhudsonf79673b2014-08-05 06:36:11 -0700219 GetResourcePath("android_fonts/v22/fonts.xml").c_str(),
halcanary96fcdcc2015-08-27 07:41:13 -0700220 nullptr);
tomhudsonf79673b2014-08-05 06:36:11 -0700221
tomhudson8aed3c12014-08-07 10:20:51 -0700222 if (v22FontFamilies.count() > 0) {
bungeman41868fe2015-05-20 09:21:04 -0700223 REPORTER_ASSERT(reporter, v22FontFamilies.count() == 54);
tomhudson2ed49a42014-08-13 07:53:48 -0700224 REPORTER_ASSERT(reporter, CountFallbacks(v22FontFamilies) == 42);
tomhudsonf79673b2014-08-05 06:36:11 -0700225
bungeman3d47e212015-02-13 18:30:11 -0800226 DumpLoadedFonts(v22FontFamilies, "version 22");
bungeman4b86bac2014-11-04 10:54:31 -0800227 ValidateLoadedFonts(v22FontFamilies, "Roboto-Thin.ttf", reporter);
tomhudson8aed3c12014-08-07 10:20:51 -0700228 } else {
229 resourcesMissing = true;
230 }
bungeman91e51cb2015-07-15 14:29:25 -0400231 v22FontFamilies.deleteAll();
tomhudson8aed3c12014-08-07 10:20:51 -0700232
233 if (resourcesMissing) {
234 SkDebugf("---- Resource files missing for FontConfigParser test\n");
235 }
tomhudsonf79673b2014-08-05 06:36:11 -0700236}
Ben Wagnerf1729a72017-11-13 11:14:36 -0500237
238DEF_TEST(FontMgrAndroidLegacyMakeTypeface, reporter) {
Hal Canarya4935102017-12-08 13:35:47 -0500239 constexpr char fontsXmlFilename[] = "fonts/fonts.xml";
Ben Wagnerf1729a72017-11-13 11:14:36 -0500240 SkString basePath = GetResourcePath("fonts/");
Hal Canarya4935102017-12-08 13:35:47 -0500241 SkString fontsXml = GetResourcePath(fontsXmlFilename);
242
243 if (!sk_exists(fontsXml.c_str())) {
244 ERRORF(reporter, "file missing: %s\n", fontsXmlFilename);
245 return;
246 }
Ben Wagnerf1729a72017-11-13 11:14:36 -0500247
248 SkFontMgr_Android_CustomFonts custom;
249 custom.fSystemFontUse = SkFontMgr_Android_CustomFonts::kOnlyCustom;
250 custom.fBasePath = basePath.c_str();
251 custom.fFontsXml = fontsXml.c_str();
252 custom.fFallbackFontsXml = nullptr;
253 custom.fIsolated = false;
254
255 sk_sp<SkFontMgr> fm(SkFontMgr_New_Android(&custom));
256 sk_sp<SkTypeface> t(fm->legacyMakeTypeface("non-existent-font", SkFontStyle()));
257 REPORTER_ASSERT(reporter, nullptr == t);
258}
Bruce Wang37b61092018-06-20 16:43:02 -0400259
260static bool bitmap_compare(const SkBitmap& ref, const SkBitmap& test) {
261 for (int y = 0; y < test.height(); ++y) {
262 for (int x = 0; x < test.width(); ++x) {
263 SkColor testColor = test.getColor(x, y);
264 SkColor refColor = ref.getColor(x, y);
265 if (refColor != testColor) {
266 return false;
267 }
268 }
269 }
270 return true;
271}
272
273DEF_TEST(FontMgrAndroidSystemVariableTypeface, reporter) {
274 constexpr char fontsXmlFilename[] = "fonts/fonts.xml";
275 SkString basePath = GetResourcePath("fonts/");
276 SkString fontsXml = GetResourcePath(fontsXmlFilename);
277
278 if (!sk_exists(fontsXml.c_str())) {
279 ERRORF(reporter, "file missing: %s\n", fontsXmlFilename);
280 return;
281 }
282
283 SkFontMgr_Android_CustomFonts custom;
284 custom.fSystemFontUse = SkFontMgr_Android_CustomFonts::kOnlyCustom;
285 custom.fBasePath = basePath.c_str();
286 custom.fFontsXml = fontsXml.c_str();
287 custom.fFallbackFontsXml = nullptr;
288 custom.fIsolated = false;
289
290 sk_sp<SkFontMgr> fontMgr(SkFontMgr_New_Android(&custom));
291 // "sans-serif" in "fonts/fonts.xml" is "fonts/Distortable.ttf"
292 sk_sp<SkTypeface> typeface(fontMgr->legacyMakeTypeface("sans-serif", SkFontStyle()));
293
294 SkBitmap bitmapStream;
295 bitmapStream.allocN32Pixels(64, 64);
296 SkCanvas canvasStream(bitmapStream);
297 canvasStream.drawColor(SK_ColorWHITE);
298
299 SkBitmap bitmapClone;
300 bitmapClone.allocN32Pixels(64, 64);
301 SkCanvas canvasClone(bitmapClone);
302 canvasStream.drawColor(SK_ColorWHITE);
303
304 SkPaint paintStream;
305 paintStream.setColor(SK_ColorGRAY);
306 paintStream.setTextSize(SkIntToScalar(20));
307 paintStream.setAntiAlias(true);
308 paintStream.setLCDRenderText(true);
309
310 SkPaint paintClone;
311 paintClone.setColor(SK_ColorGRAY);
312 paintClone.setTextSize(SkIntToScalar(20));
313 paintClone.setAntiAlias(true);
314 paintClone.setLCDRenderText(true);
315
316 std::unique_ptr<SkStreamAsset> distortableStream(
317 GetResourceAsStream("fonts/Distortable.ttf"));
318 if (!distortableStream) {
319 return;
320 }
321
322 const char* text = "abc";
323 const size_t textLen = strlen(text);
324 SkPoint point = SkPoint::Make(20.0f, 20.0f);
325 SkFourByteTag tag = SkSetFourByteTag('w', 'g', 'h', 't');
326
327 for (int i = 0; i < 10; ++i) {
328 SkScalar styleValue =
329 SkDoubleToScalar(0.5 + i * ((2.0 - 0.5) / 10));
330 SkFontArguments::VariationPosition::Coordinate
331 coordinates[] = {{tag, styleValue}};
332 SkFontArguments::VariationPosition
333 position = {coordinates, SK_ARRAY_COUNT(coordinates)};
334
335 paintStream.setTypeface(sk_sp<SkTypeface>(
336 fontMgr->makeFromStream(distortableStream->duplicate(),
337 SkFontArguments().setVariationDesignPosition(position))));
338
339 paintClone.setTypeface(sk_sp<SkTypeface>(
340 typeface->makeClone(SkFontArguments().setVariationDesignPosition(position))));
341
342 canvasStream.drawColor(SK_ColorWHITE);
343 canvasStream.drawText(text, textLen, point.fX, point.fY, paintStream);
344
345 canvasClone.drawColor(SK_ColorWHITE);
346 canvasClone.drawText(text, textLen, point.fX, point.fY, paintClone);
347
348 bool success = bitmap_compare(bitmapStream, bitmapClone);
349 REPORTER_ASSERT(reporter, success);
350 }
351}
352
Ben Wagner9f0d8c22018-11-15 17:14:41 -0500353DEF_TEST(FontMgrAndroidSystemFallbackFor, reporter) {
354 constexpr char fontsXmlFilename[] = "fonts/fonts.xml";
355 SkString basePath = GetResourcePath("fonts/");
356 SkString fontsXml = GetResourcePath(fontsXmlFilename);
Bruce Wang37b61092018-06-20 16:43:02 -0400357
Ben Wagner9f0d8c22018-11-15 17:14:41 -0500358 if (!sk_exists(fontsXml.c_str())) {
359 ERRORF(reporter, "file missing: %s\n", fontsXmlFilename);
360 return;
361 }
362
363 SkFontMgr_Android_CustomFonts custom;
364 custom.fSystemFontUse = SkFontMgr_Android_CustomFonts::kOnlyCustom;
365 custom.fBasePath = basePath.c_str();
366 custom.fFontsXml = fontsXml.c_str();
367 custom.fFallbackFontsXml = nullptr;
368 custom.fIsolated = false;
369
370 sk_sp<SkFontMgr> fontMgr(SkFontMgr_New_Android(&custom));
371 // "sans-serif" in "fonts/fonts.xml" is "fonts/Distortable.ttf", which doesn't have a '!'
372 // but "TestTTC" has a bold font which does have '!' and is marked as fallback for "sans-serif"
373 // and should take precedence over the same font marked as normal weight next to it.
374 sk_sp<SkTypeface> typeface(fontMgr->matchFamilyStyleCharacter(
375 "sans-serif", SkFontStyle(), nullptr, 0, '!'));
376
377 REPORTER_ASSERT(reporter, typeface->fontStyle() == SkFontStyle::Bold());
378}