blob: df358b7df6076fe049815e45687f791cf9003771 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "tests/Test.h"
Hal Canary3560ea72019-01-08 13:01:58 -05009
Mike Reedac9f0c92020-12-23 10:11:33 -050010#include "include/core/SkBitmap.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkCanvas.h"
12#include "include/core/SkFont.h"
13#include "include/core/SkTypeface.h"
14#include "include/ports/SkFontMgr_android.h"
15#include "include/private/SkFixed.h"
16#include "src/core/SkOSFile.h"
17#include "src/ports/SkFontMgr_android_parser.h"
18#include "tools/Resources.h"
19#include "tools/flags/CommandLineFlags.h"
tomhudsonf79673b2014-08-05 06:36:11 -070020
bungeman41868fe2015-05-20 09:21:04 -070021#include <cmath>
22#include <cstdio>
23
bungemaneb2be7f2015-02-10 07:51:12 -080024DECLARE_bool(verboseFontMgr);
25
tomhudson2ed49a42014-08-13 07:53:48 -070026int CountFallbacks(SkTDArray<FontFamily*> fontFamilies) {
27 int countOfFallbackFonts = 0;
28 for (int i = 0; i < fontFamilies.count(); i++) {
29 if (fontFamilies[i]->fIsFallbackFont) {
30 countOfFallbackFonts++;
31 }
32 }
33 return countOfFallbackFonts;
34}
35
bungemanc3c69432015-02-11 07:18:51 -080036//https://tools.ietf.org/html/rfc5234#appendix-B.1
37static bool isALPHA(int c) {
38 return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z');
39}
40
41//https://tools.ietf.org/html/rfc5234#appendix-B.1
42static bool isDIGIT(int c) {
43 return ('0' <= c && c <= '9');
44}
45
Ben Wagner9f0d8c22018-11-15 17:14:41 -050046static void ValidateLoadedFonts(SkTDArray<FontFamily*> fontFamilies, const char* firstExpectedFile,
47 skiatest::Reporter* reporter) {
tomhudsonf79673b2014-08-05 06:36:11 -070048 REPORTER_ASSERT(reporter, fontFamilies[0]->fNames.count() == 5);
49 REPORTER_ASSERT(reporter, !strcmp(fontFamilies[0]->fNames[0].c_str(), "sans-serif"));
50 REPORTER_ASSERT(reporter,
bungeman4b86bac2014-11-04 10:54:31 -080051 !strcmp(fontFamilies[0]->fFonts[0].fFileName.c_str(), firstExpectedFile));
tomhudsonf79673b2014-08-05 06:36:11 -070052 REPORTER_ASSERT(reporter, !fontFamilies[0]->fIsFallbackFont);
bungemanc3c69432015-02-11 07:18:51 -080053
54 // Check that the languages are all sane.
Ben Wagneraee878d2017-08-10 13:49:41 -040055 for (const auto& fontFamily : fontFamilies) {
56 for (const auto& lang : fontFamily->fLanguages) {
57 const SkString& langString = lang.getTag();
58 for (size_t i = 0; i < langString.size(); ++i) {
59 int c = langString[i];
60 REPORTER_ASSERT(reporter, isALPHA(c) || isDIGIT(c) || '-' == c);
61 }
bungemanc3c69432015-02-11 07:18:51 -080062 }
63 }
bungeman9a0808f2015-02-13 08:55:16 -080064
65 // All file names in the test configuration files start with a capital letter.
66 // This is not a general requirement, but it is true of all the test configuration data.
67 // Verifying ensures the filenames have been read sanely and have not been 'sliced'.
68 for (int i = 0; i < fontFamilies.count(); ++i) {
69 FontFamily& family = *fontFamilies[i];
70 for (int j = 0; j < family.fFonts.count(); ++j) {
71 FontFileInfo& file = family.fFonts[j];
72 REPORTER_ASSERT(reporter, !file.fFileName.isEmpty() &&
73 file.fFileName[0] >= 'A' &&
74 file.fFileName[0] <= 'Z');
75 }
76 }
tomhudsonf79673b2014-08-05 06:36:11 -070077}
78
Ben Wagner9f0d8c22018-11-15 17:14:41 -050079static void DumpFiles(const FontFamily& fontFamily) {
80 for (int j = 0; j < fontFamily.fFonts.count(); ++j) {
81 const FontFileInfo& ffi = fontFamily.fFonts[j];
82 SkDebugf(" file (%d) %s#%d", ffi.fWeight, ffi.fFileName.c_str(), ffi.fIndex);
83 for (const auto& coordinate : ffi.fVariationDesignPosition) {
84 SkDebugf(" @'%c%c%c%c'=%f",
85 (coordinate.axis >> 24) & 0xFF,
86 (coordinate.axis >> 16) & 0xFF,
87 (coordinate.axis >> 8) & 0xFF,
88 (coordinate.axis ) & 0xFF,
89 coordinate.value);
90 }
91 SkDebugf("\n");
92 }
93}
94
95static void DumpLoadedFonts(SkTDArray<FontFamily*> fontFamilies, const char* label) {
bungemaneb2be7f2015-02-10 07:51:12 -080096 if (!FLAGS_verboseFontMgr) {
97 return;
98 }
99
bungeman3d47e212015-02-13 18:30:11 -0800100 SkDebugf("\n--- Dumping %s\n", label);
tomhudsonf79673b2014-08-05 06:36:11 -0700101 for (int i = 0; i < fontFamilies.count(); ++i) {
102 SkDebugf("Family %d:\n", i);
tomhudson07544752014-08-05 13:35:00 -0700103 switch(fontFamilies[i]->fVariant) {
bungemana44e9b92015-01-30 19:58:19 -0800104 case kElegant_FontVariant: SkDebugf(" elegant\n"); break;
105 case kCompact_FontVariant: SkDebugf(" compact\n"); break;
tomhudson07544752014-08-05 13:35:00 -0700106 default: break;
107 }
bungemaneb2be7f2015-02-10 07:51:12 -0800108 SkDebugf(" basePath %s\n", fontFamilies[i]->fBasePath.c_str());
Ben Wagneraee878d2017-08-10 13:49:41 -0400109 if (!fontFamilies[i]->fLanguages.empty()) {
110 SkDebugf(" language");
111 for (const auto& lang : fontFamilies[i]->fLanguages) {
112 SkDebugf(" %s", lang.getTag().c_str());
113 }
114 SkDebugf("\n");
tomhudson07544752014-08-05 13:35:00 -0700115 }
tomhudsonf79673b2014-08-05 06:36:11 -0700116 for (int j = 0; j < fontFamilies[i]->fNames.count(); ++j) {
117 SkDebugf(" name %s\n", fontFamilies[i]->fNames[j].c_str());
118 }
Ben Wagner9f0d8c22018-11-15 17:14:41 -0500119 DumpFiles(*fontFamilies[i]);
John Stiles65b48272020-12-22 17:18:34 -0500120 for (const auto& [unused, fallbackFamily] : fontFamilies[i]->fallbackFamilies) {
121 SkDebugf(" Fallback for: %s\n", fallbackFamily->fFallbackFor.c_str());
122 DumpFiles(*fallbackFamily);
123 }
tomhudsonf79673b2014-08-05 06:36:11 -0700124 }
bungemanc3c69432015-02-11 07:18:51 -0800125 SkDebugf("\n\n");
tomhudsonf79673b2014-08-05 06:36:11 -0700126}
127
bungeman41868fe2015-05-20 09:21:04 -0700128template <int N, typename T> static double test_parse_fixed_r(skiatest::Reporter* reporter,
129 double low, double high, double inc)
130{
131 double SK_FixedMax_double = nextafter(1 << (sizeof(T) * CHAR_BIT - N - 1), 0.0);
132 double SK_FixedEpsilon_double = (1.0 / (1 << N));
133 double maxError = 0;
134 char buffer[64];
135 for (double f = low; f < high; f += inc) {
136 SkString s;
137 // 'sprintf' formatting as expected depends on the current locale being "C".
138 // We currently expect tests and tools to run in the "C" locale.
139 sprintf(buffer, "%.20f", f);
140 T fix;
141 bool b = parse_fixed<N>(buffer, &fix);
142 if (b) {
143 double f2 = fix * SK_FixedEpsilon_double;
144 double error = fabs(f - f2);
145 REPORTER_ASSERT(reporter, error <= SK_FixedEpsilon_double);
Brian Osman788b9162020-02-07 10:36:46 -0500146 maxError = std::max(maxError, error);
bungeman41868fe2015-05-20 09:21:04 -0700147 } else {
148 REPORTER_ASSERT(reporter, f < -SK_FixedMax_double || SK_FixedMax_double < f);
149 }
150 }
151
152 //SkDebugf("maxError: %.20f\n", maxError);
153 return maxError;
154}
155
156static void test_parse_fixed(skiatest::Reporter* reporter) {
157 test_parse_fixed_r<27, int32_t>(reporter, -8.1, -7.9, 0.000001);
158 test_parse_fixed_r<27, int32_t>(reporter, -0.1, 0.1, 0.000001);
159 test_parse_fixed_r<27, int32_t>(reporter, 7.9, 8.1, 0.000001);
160 test_parse_fixed_r<16, int32_t>(reporter, -0.125, 0.125, 1.0 / (1 << 19));
161 test_parse_fixed_r<16, int32_t>(reporter, -32768.125, -32766.875, 1.0 / (1 << 17));
162 test_parse_fixed_r<16, int32_t>(reporter, 32766.875, 32768.125, 1.0 / (1 << 17));
163 test_parse_fixed_r<16, int32_t>(reporter, -1.1, 1.1, 0.0001);
164
165 SkFixed fix;
166 REPORTER_ASSERT(reporter, !parse_fixed<27>("-17.1", &fix));
167 REPORTER_ASSERT(reporter, !parse_fixed<16>("32768", &fix));
168 REPORTER_ASSERT(reporter, !parse_fixed<16>("", &fix));
169 REPORTER_ASSERT(reporter, !parse_fixed<16>(".", &fix));
170 REPORTER_ASSERT(reporter, !parse_fixed<16>("123.", &fix));
171 REPORTER_ASSERT(reporter, !parse_fixed<16>("a", &fix));
172 REPORTER_ASSERT(reporter, !parse_fixed<16>(".123a", &fix));
173}
174
bungemanc5308542015-06-23 13:25:46 -0700175DEF_TEST(FontMgrAndroidParser, reporter) {
bungeman41868fe2015-05-20 09:21:04 -0700176 test_parse_fixed(reporter);
tomhudsonf79673b2014-08-05 06:36:11 -0700177
tomhudson8aed3c12014-08-07 10:20:51 -0700178 bool resourcesMissing = false;
179
tomhudsonf79673b2014-08-05 06:36:11 -0700180 SkTDArray<FontFamily*> preV17FontFamilies;
bungemanc5308542015-06-23 13:25:46 -0700181 SkFontMgr_Android_Parser::GetCustomFontFamilies(preV17FontFamilies,
bungeman7fa87cd2015-02-06 07:59:19 -0800182 SkString("/custom/font/path/"),
tomhudsonf79673b2014-08-05 06:36:11 -0700183 GetResourcePath("android_fonts/pre_v17/system_fonts.xml").c_str(),
184 GetResourcePath("android_fonts/pre_v17/fallback_fonts.xml").c_str());
185
tomhudson8aed3c12014-08-07 10:20:51 -0700186 if (preV17FontFamilies.count() > 0) {
187 REPORTER_ASSERT(reporter, preV17FontFamilies.count() == 14);
tomhudson2ed49a42014-08-13 07:53:48 -0700188 REPORTER_ASSERT(reporter, CountFallbacks(preV17FontFamilies) == 10);
tomhudsonf79673b2014-08-05 06:36:11 -0700189
bungeman3d47e212015-02-13 18:30:11 -0800190 DumpLoadedFonts(preV17FontFamilies, "pre version 17");
bungeman4b86bac2014-11-04 10:54:31 -0800191 ValidateLoadedFonts(preV17FontFamilies, "Roboto-Regular.ttf", reporter);
tomhudson8aed3c12014-08-07 10:20:51 -0700192 } else {
193 resourcesMissing = true;
194 }
bungeman91e51cb2015-07-15 14:29:25 -0400195 preV17FontFamilies.deleteAll();
tomhudsonf79673b2014-08-05 06:36:11 -0700196
tomhudson07544752014-08-05 13:35:00 -0700197
tomhudsonf79673b2014-08-05 06:36:11 -0700198 SkTDArray<FontFamily*> v17FontFamilies;
bungemanc5308542015-06-23 13:25:46 -0700199 SkFontMgr_Android_Parser::GetCustomFontFamilies(v17FontFamilies,
bungeman7fa87cd2015-02-06 07:59:19 -0800200 SkString("/custom/font/path/"),
tomhudsonf79673b2014-08-05 06:36:11 -0700201 GetResourcePath("android_fonts/v17/system_fonts.xml").c_str(),
bungemanc3c69432015-02-11 07:18:51 -0800202 GetResourcePath("android_fonts/v17/fallback_fonts.xml").c_str(),
203 GetResourcePath("android_fonts/v17").c_str());
tomhudsonf79673b2014-08-05 06:36:11 -0700204
tomhudson8aed3c12014-08-07 10:20:51 -0700205 if (v17FontFamilies.count() > 0) {
bungemanc3c69432015-02-11 07:18:51 -0800206 REPORTER_ASSERT(reporter, v17FontFamilies.count() == 56);
207 REPORTER_ASSERT(reporter, CountFallbacks(v17FontFamilies) == 46);
tomhudsonf79673b2014-08-05 06:36:11 -0700208
bungeman3d47e212015-02-13 18:30:11 -0800209 DumpLoadedFonts(v17FontFamilies, "version 17");
bungeman4b86bac2014-11-04 10:54:31 -0800210 ValidateLoadedFonts(v17FontFamilies, "Roboto-Regular.ttf", reporter);
tomhudson8aed3c12014-08-07 10:20:51 -0700211 } else {
212 resourcesMissing = true;
213 }
bungeman91e51cb2015-07-15 14:29:25 -0400214 v17FontFamilies.deleteAll();
tomhudsonf79673b2014-08-05 06:36:11 -0700215
tomhudson07544752014-08-05 13:35:00 -0700216
tomhudsonf79673b2014-08-05 06:36:11 -0700217 SkTDArray<FontFamily*> v22FontFamilies;
bungemanc5308542015-06-23 13:25:46 -0700218 SkFontMgr_Android_Parser::GetCustomFontFamilies(v22FontFamilies,
bungeman7fa87cd2015-02-06 07:59:19 -0800219 SkString("/custom/font/path/"),
tomhudsonf79673b2014-08-05 06:36:11 -0700220 GetResourcePath("android_fonts/v22/fonts.xml").c_str(),
halcanary96fcdcc2015-08-27 07:41:13 -0700221 nullptr);
tomhudsonf79673b2014-08-05 06:36:11 -0700222
tomhudson8aed3c12014-08-07 10:20:51 -0700223 if (v22FontFamilies.count() > 0) {
bungeman41868fe2015-05-20 09:21:04 -0700224 REPORTER_ASSERT(reporter, v22FontFamilies.count() == 54);
tomhudson2ed49a42014-08-13 07:53:48 -0700225 REPORTER_ASSERT(reporter, CountFallbacks(v22FontFamilies) == 42);
tomhudsonf79673b2014-08-05 06:36:11 -0700226
bungeman3d47e212015-02-13 18:30:11 -0800227 DumpLoadedFonts(v22FontFamilies, "version 22");
bungeman4b86bac2014-11-04 10:54:31 -0800228 ValidateLoadedFonts(v22FontFamilies, "Roboto-Thin.ttf", reporter);
tomhudson8aed3c12014-08-07 10:20:51 -0700229 } else {
230 resourcesMissing = true;
231 }
bungeman91e51cb2015-07-15 14:29:25 -0400232 v22FontFamilies.deleteAll();
tomhudson8aed3c12014-08-07 10:20:51 -0700233
234 if (resourcesMissing) {
235 SkDebugf("---- Resource files missing for FontConfigParser test\n");
236 }
tomhudsonf79673b2014-08-05 06:36:11 -0700237}
Ben Wagnerf1729a72017-11-13 11:14:36 -0500238
239DEF_TEST(FontMgrAndroidLegacyMakeTypeface, reporter) {
Hal Canarya4935102017-12-08 13:35:47 -0500240 constexpr char fontsXmlFilename[] = "fonts/fonts.xml";
Ben Wagnerf1729a72017-11-13 11:14:36 -0500241 SkString basePath = GetResourcePath("fonts/");
Hal Canarya4935102017-12-08 13:35:47 -0500242 SkString fontsXml = GetResourcePath(fontsXmlFilename);
243
244 if (!sk_exists(fontsXml.c_str())) {
245 ERRORF(reporter, "file missing: %s\n", fontsXmlFilename);
246 return;
247 }
Ben Wagnerf1729a72017-11-13 11:14:36 -0500248
249 SkFontMgr_Android_CustomFonts custom;
250 custom.fSystemFontUse = SkFontMgr_Android_CustomFonts::kOnlyCustom;
251 custom.fBasePath = basePath.c_str();
252 custom.fFontsXml = fontsXml.c_str();
253 custom.fFallbackFontsXml = nullptr;
254 custom.fIsolated = false;
255
256 sk_sp<SkFontMgr> fm(SkFontMgr_New_Android(&custom));
257 sk_sp<SkTypeface> t(fm->legacyMakeTypeface("non-existent-font", SkFontStyle()));
258 REPORTER_ASSERT(reporter, nullptr == t);
259}
Bruce Wang37b61092018-06-20 16:43:02 -0400260
261static bool bitmap_compare(const SkBitmap& ref, const SkBitmap& test) {
262 for (int y = 0; y < test.height(); ++y) {
263 for (int x = 0; x < test.width(); ++x) {
264 SkColor testColor = test.getColor(x, y);
265 SkColor refColor = ref.getColor(x, y);
266 if (refColor != testColor) {
267 return false;
268 }
269 }
270 }
271 return true;
272}
273
274DEF_TEST(FontMgrAndroidSystemVariableTypeface, reporter) {
275 constexpr char fontsXmlFilename[] = "fonts/fonts.xml";
276 SkString basePath = GetResourcePath("fonts/");
277 SkString fontsXml = GetResourcePath(fontsXmlFilename);
278
279 if (!sk_exists(fontsXml.c_str())) {
280 ERRORF(reporter, "file missing: %s\n", fontsXmlFilename);
281 return;
282 }
283
284 SkFontMgr_Android_CustomFonts custom;
285 custom.fSystemFontUse = SkFontMgr_Android_CustomFonts::kOnlyCustom;
286 custom.fBasePath = basePath.c_str();
287 custom.fFontsXml = fontsXml.c_str();
288 custom.fFallbackFontsXml = nullptr;
289 custom.fIsolated = false;
290
291 sk_sp<SkFontMgr> fontMgr(SkFontMgr_New_Android(&custom));
292 // "sans-serif" in "fonts/fonts.xml" is "fonts/Distortable.ttf"
293 sk_sp<SkTypeface> typeface(fontMgr->legacyMakeTypeface("sans-serif", SkFontStyle()));
294
295 SkBitmap bitmapStream;
296 bitmapStream.allocN32Pixels(64, 64);
297 SkCanvas canvasStream(bitmapStream);
298 canvasStream.drawColor(SK_ColorWHITE);
299
300 SkBitmap bitmapClone;
301 bitmapClone.allocN32Pixels(64, 64);
302 SkCanvas canvasClone(bitmapClone);
303 canvasStream.drawColor(SK_ColorWHITE);
304
Hal Canary3560ea72019-01-08 13:01:58 -0500305 SkPaint paint;
306 paint.setColor(SK_ColorGRAY);
307 paint.setAntiAlias(true);
308 constexpr float kTextSize = 20;
Bruce Wang37b61092018-06-20 16:43:02 -0400309
310 std::unique_ptr<SkStreamAsset> distortableStream(
311 GetResourceAsStream("fonts/Distortable.ttf"));
312 if (!distortableStream) {
313 return;
314 }
315
Bruce Wang37b61092018-06-20 16:43:02 -0400316 SkPoint point = SkPoint::Make(20.0f, 20.0f);
317 SkFourByteTag tag = SkSetFourByteTag('w', 'g', 'h', 't');
318
319 for (int i = 0; i < 10; ++i) {
320 SkScalar styleValue =
321 SkDoubleToScalar(0.5 + i * ((2.0 - 0.5) / 10));
322 SkFontArguments::VariationPosition::Coordinate
323 coordinates[] = {{tag, styleValue}};
324 SkFontArguments::VariationPosition
325 position = {coordinates, SK_ARRAY_COUNT(coordinates)};
326
Hal Canary3560ea72019-01-08 13:01:58 -0500327 SkFont fontStream(
Bruce Wang37b61092018-06-20 16:43:02 -0400328 fontMgr->makeFromStream(distortableStream->duplicate(),
Hal Canary3560ea72019-01-08 13:01:58 -0500329 SkFontArguments().setVariationDesignPosition(position)),
330 kTextSize);
331 fontStream.setEdging(SkFont::Edging::kSubpixelAntiAlias);
Bruce Wang37b61092018-06-20 16:43:02 -0400332
Hal Canary3560ea72019-01-08 13:01:58 -0500333
334 SkFont fontClone(
335 typeface->makeClone(SkFontArguments().setVariationDesignPosition(position)), kTextSize);
336 fontClone.setEdging(SkFont::Edging::kSubpixelAntiAlias);
337
338 constexpr char text[] = "abc";
Bruce Wang37b61092018-06-20 16:43:02 -0400339
340 canvasStream.drawColor(SK_ColorWHITE);
Hal Canary3560ea72019-01-08 13:01:58 -0500341 canvasStream.drawString(text, point.fX, point.fY, fontStream, paint);
Bruce Wang37b61092018-06-20 16:43:02 -0400342
343 canvasClone.drawColor(SK_ColorWHITE);
Hal Canary3560ea72019-01-08 13:01:58 -0500344 canvasClone.drawString(text, point.fX, point.fY, fontClone, paint);
Bruce Wang37b61092018-06-20 16:43:02 -0400345
346 bool success = bitmap_compare(bitmapStream, bitmapClone);
347 REPORTER_ASSERT(reporter, success);
348 }
349}
350
Ben Wagner9f0d8c22018-11-15 17:14:41 -0500351DEF_TEST(FontMgrAndroidSystemFallbackFor, reporter) {
352 constexpr char fontsXmlFilename[] = "fonts/fonts.xml";
353 SkString basePath = GetResourcePath("fonts/");
354 SkString fontsXml = GetResourcePath(fontsXmlFilename);
Bruce Wang37b61092018-06-20 16:43:02 -0400355
Ben Wagner9f0d8c22018-11-15 17:14:41 -0500356 if (!sk_exists(fontsXml.c_str())) {
357 ERRORF(reporter, "file missing: %s\n", fontsXmlFilename);
358 return;
359 }
360
361 SkFontMgr_Android_CustomFonts custom;
362 custom.fSystemFontUse = SkFontMgr_Android_CustomFonts::kOnlyCustom;
363 custom.fBasePath = basePath.c_str();
364 custom.fFontsXml = fontsXml.c_str();
365 custom.fFallbackFontsXml = nullptr;
366 custom.fIsolated = false;
367
368 sk_sp<SkFontMgr> fontMgr(SkFontMgr_New_Android(&custom));
369 // "sans-serif" in "fonts/fonts.xml" is "fonts/Distortable.ttf", which doesn't have a '!'
370 // but "TestTTC" has a bold font which does have '!' and is marked as fallback for "sans-serif"
371 // and should take precedence over the same font marked as normal weight next to it.
372 sk_sp<SkTypeface> typeface(fontMgr->matchFamilyStyleCharacter(
373 "sans-serif", SkFontStyle(), nullptr, 0, '!'));
374
375 REPORTER_ASSERT(reporter, typeface->fontStyle() == SkFontStyle::Bold());
376}