blob: 432d69ec752a071cac29b9cbcd0a2113bcd34b89 [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
bungeman4b86bac2014-11-04 10:54:31 -080043void ValidateLoadedFonts(SkTDArray<FontFamily*> fontFamilies, const char* firstExpectedFile,
tomhudsonf79673b2014-08-05 06:36:11 -070044 skiatest::Reporter* reporter) {
45 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
bungeman3d47e212015-02-13 18:30:11 -080076void DumpLoadedFonts(SkTDArray<FontFamily*> fontFamilies, const char* label) {
bungemaneb2be7f2015-02-10 07:51:12 -080077 if (!FLAGS_verboseFontMgr) {
78 return;
79 }
80
bungeman3d47e212015-02-13 18:30:11 -080081 SkDebugf("\n--- Dumping %s\n", label);
tomhudsonf79673b2014-08-05 06:36:11 -070082 for (int i = 0; i < fontFamilies.count(); ++i) {
83 SkDebugf("Family %d:\n", i);
tomhudson07544752014-08-05 13:35:00 -070084 switch(fontFamilies[i]->fVariant) {
bungemana44e9b92015-01-30 19:58:19 -080085 case kElegant_FontVariant: SkDebugf(" elegant\n"); break;
86 case kCompact_FontVariant: SkDebugf(" compact\n"); break;
tomhudson07544752014-08-05 13:35:00 -070087 default: break;
88 }
bungemaneb2be7f2015-02-10 07:51:12 -080089 SkDebugf(" basePath %s\n", fontFamilies[i]->fBasePath.c_str());
Ben Wagneraee878d2017-08-10 13:49:41 -040090 if (!fontFamilies[i]->fLanguages.empty()) {
91 SkDebugf(" language");
92 for (const auto& lang : fontFamilies[i]->fLanguages) {
93 SkDebugf(" %s", lang.getTag().c_str());
94 }
95 SkDebugf("\n");
tomhudson07544752014-08-05 13:35:00 -070096 }
tomhudsonf79673b2014-08-05 06:36:11 -070097 for (int j = 0; j < fontFamilies[i]->fNames.count(); ++j) {
98 SkDebugf(" name %s\n", fontFamilies[i]->fNames[j].c_str());
99 }
tomhudsond3ddea22014-08-11 11:28:00 -0700100 for (int j = 0; j < fontFamilies[i]->fFonts.count(); ++j) {
101 const FontFileInfo& ffi = fontFamilies[i]->fFonts[j];
bungeman47a1e962016-02-25 11:20:01 -0800102 SkDebugf(" file (%d) %s#%d", ffi.fWeight, ffi.fFileName.c_str(), ffi.fIndex);
Ben Wagnerfc497342017-02-24 11:15:26 -0500103 for (const auto& coordinate : ffi.fVariationDesignPosition) {
bungeman47a1e962016-02-25 11:20:01 -0800104 SkDebugf(" @'%c%c%c%c'=%f",
Ben Wagnerfc497342017-02-24 11:15:26 -0500105 (coordinate.axis >> 24) & 0xFF,
106 (coordinate.axis >> 16) & 0xFF,
107 (coordinate.axis >> 8) & 0xFF,
108 (coordinate.axis ) & 0xFF,
109 coordinate.value);
bungeman47a1e962016-02-25 11:20:01 -0800110 }
111 SkDebugf("\n");
tomhudson07544752014-08-05 13:35:00 -0700112 }
tomhudsonf79673b2014-08-05 06:36:11 -0700113 }
bungemanc3c69432015-02-11 07:18:51 -0800114 SkDebugf("\n\n");
tomhudsonf79673b2014-08-05 06:36:11 -0700115}
116
bungeman41868fe2015-05-20 09:21:04 -0700117template <int N, typename T> static double test_parse_fixed_r(skiatest::Reporter* reporter,
118 double low, double high, double inc)
119{
120 double SK_FixedMax_double = nextafter(1 << (sizeof(T) * CHAR_BIT - N - 1), 0.0);
121 double SK_FixedEpsilon_double = (1.0 / (1 << N));
122 double maxError = 0;
123 char buffer[64];
124 for (double f = low; f < high; f += inc) {
125 SkString s;
126 // 'sprintf' formatting as expected depends on the current locale being "C".
127 // We currently expect tests and tools to run in the "C" locale.
128 sprintf(buffer, "%.20f", f);
129 T fix;
130 bool b = parse_fixed<N>(buffer, &fix);
131 if (b) {
132 double f2 = fix * SK_FixedEpsilon_double;
133 double error = fabs(f - f2);
134 REPORTER_ASSERT(reporter, error <= SK_FixedEpsilon_double);
135 maxError = SkTMax(maxError, error);
136 } else {
137 REPORTER_ASSERT(reporter, f < -SK_FixedMax_double || SK_FixedMax_double < f);
138 }
139 }
140
141 //SkDebugf("maxError: %.20f\n", maxError);
142 return maxError;
143}
144
145static void test_parse_fixed(skiatest::Reporter* reporter) {
146 test_parse_fixed_r<27, int32_t>(reporter, -8.1, -7.9, 0.000001);
147 test_parse_fixed_r<27, int32_t>(reporter, -0.1, 0.1, 0.000001);
148 test_parse_fixed_r<27, int32_t>(reporter, 7.9, 8.1, 0.000001);
149 test_parse_fixed_r<16, int32_t>(reporter, -0.125, 0.125, 1.0 / (1 << 19));
150 test_parse_fixed_r<16, int32_t>(reporter, -32768.125, -32766.875, 1.0 / (1 << 17));
151 test_parse_fixed_r<16, int32_t>(reporter, 32766.875, 32768.125, 1.0 / (1 << 17));
152 test_parse_fixed_r<16, int32_t>(reporter, -1.1, 1.1, 0.0001);
153
154 SkFixed fix;
155 REPORTER_ASSERT(reporter, !parse_fixed<27>("-17.1", &fix));
156 REPORTER_ASSERT(reporter, !parse_fixed<16>("32768", &fix));
157 REPORTER_ASSERT(reporter, !parse_fixed<16>("", &fix));
158 REPORTER_ASSERT(reporter, !parse_fixed<16>(".", &fix));
159 REPORTER_ASSERT(reporter, !parse_fixed<16>("123.", &fix));
160 REPORTER_ASSERT(reporter, !parse_fixed<16>("a", &fix));
161 REPORTER_ASSERT(reporter, !parse_fixed<16>(".123a", &fix));
162}
163
bungemanc5308542015-06-23 13:25:46 -0700164DEF_TEST(FontMgrAndroidParser, reporter) {
bungeman41868fe2015-05-20 09:21:04 -0700165 test_parse_fixed(reporter);
tomhudsonf79673b2014-08-05 06:36:11 -0700166
tomhudson8aed3c12014-08-07 10:20:51 -0700167 bool resourcesMissing = false;
168
tomhudsonf79673b2014-08-05 06:36:11 -0700169 SkTDArray<FontFamily*> preV17FontFamilies;
bungemanc5308542015-06-23 13:25:46 -0700170 SkFontMgr_Android_Parser::GetCustomFontFamilies(preV17FontFamilies,
bungeman7fa87cd2015-02-06 07:59:19 -0800171 SkString("/custom/font/path/"),
tomhudsonf79673b2014-08-05 06:36:11 -0700172 GetResourcePath("android_fonts/pre_v17/system_fonts.xml").c_str(),
173 GetResourcePath("android_fonts/pre_v17/fallback_fonts.xml").c_str());
174
tomhudson8aed3c12014-08-07 10:20:51 -0700175 if (preV17FontFamilies.count() > 0) {
176 REPORTER_ASSERT(reporter, preV17FontFamilies.count() == 14);
tomhudson2ed49a42014-08-13 07:53:48 -0700177 REPORTER_ASSERT(reporter, CountFallbacks(preV17FontFamilies) == 10);
tomhudsonf79673b2014-08-05 06:36:11 -0700178
bungeman3d47e212015-02-13 18:30:11 -0800179 DumpLoadedFonts(preV17FontFamilies, "pre version 17");
bungeman4b86bac2014-11-04 10:54:31 -0800180 ValidateLoadedFonts(preV17FontFamilies, "Roboto-Regular.ttf", reporter);
tomhudson8aed3c12014-08-07 10:20:51 -0700181 } else {
182 resourcesMissing = true;
183 }
bungeman91e51cb2015-07-15 14:29:25 -0400184 preV17FontFamilies.deleteAll();
tomhudsonf79673b2014-08-05 06:36:11 -0700185
tomhudson07544752014-08-05 13:35:00 -0700186
tomhudsonf79673b2014-08-05 06:36:11 -0700187 SkTDArray<FontFamily*> v17FontFamilies;
bungemanc5308542015-06-23 13:25:46 -0700188 SkFontMgr_Android_Parser::GetCustomFontFamilies(v17FontFamilies,
bungeman7fa87cd2015-02-06 07:59:19 -0800189 SkString("/custom/font/path/"),
tomhudsonf79673b2014-08-05 06:36:11 -0700190 GetResourcePath("android_fonts/v17/system_fonts.xml").c_str(),
bungemanc3c69432015-02-11 07:18:51 -0800191 GetResourcePath("android_fonts/v17/fallback_fonts.xml").c_str(),
192 GetResourcePath("android_fonts/v17").c_str());
tomhudsonf79673b2014-08-05 06:36:11 -0700193
tomhudson8aed3c12014-08-07 10:20:51 -0700194 if (v17FontFamilies.count() > 0) {
bungemanc3c69432015-02-11 07:18:51 -0800195 REPORTER_ASSERT(reporter, v17FontFamilies.count() == 56);
196 REPORTER_ASSERT(reporter, CountFallbacks(v17FontFamilies) == 46);
tomhudsonf79673b2014-08-05 06:36:11 -0700197
bungeman3d47e212015-02-13 18:30:11 -0800198 DumpLoadedFonts(v17FontFamilies, "version 17");
bungeman4b86bac2014-11-04 10:54:31 -0800199 ValidateLoadedFonts(v17FontFamilies, "Roboto-Regular.ttf", reporter);
tomhudson8aed3c12014-08-07 10:20:51 -0700200 } else {
201 resourcesMissing = true;
202 }
bungeman91e51cb2015-07-15 14:29:25 -0400203 v17FontFamilies.deleteAll();
tomhudsonf79673b2014-08-05 06:36:11 -0700204
tomhudson07544752014-08-05 13:35:00 -0700205
tomhudsonf79673b2014-08-05 06:36:11 -0700206 SkTDArray<FontFamily*> v22FontFamilies;
bungemanc5308542015-06-23 13:25:46 -0700207 SkFontMgr_Android_Parser::GetCustomFontFamilies(v22FontFamilies,
bungeman7fa87cd2015-02-06 07:59:19 -0800208 SkString("/custom/font/path/"),
tomhudsonf79673b2014-08-05 06:36:11 -0700209 GetResourcePath("android_fonts/v22/fonts.xml").c_str(),
halcanary96fcdcc2015-08-27 07:41:13 -0700210 nullptr);
tomhudsonf79673b2014-08-05 06:36:11 -0700211
tomhudson8aed3c12014-08-07 10:20:51 -0700212 if (v22FontFamilies.count() > 0) {
bungeman41868fe2015-05-20 09:21:04 -0700213 REPORTER_ASSERT(reporter, v22FontFamilies.count() == 54);
tomhudson2ed49a42014-08-13 07:53:48 -0700214 REPORTER_ASSERT(reporter, CountFallbacks(v22FontFamilies) == 42);
tomhudsonf79673b2014-08-05 06:36:11 -0700215
bungeman3d47e212015-02-13 18:30:11 -0800216 DumpLoadedFonts(v22FontFamilies, "version 22");
bungeman4b86bac2014-11-04 10:54:31 -0800217 ValidateLoadedFonts(v22FontFamilies, "Roboto-Thin.ttf", reporter);
tomhudson8aed3c12014-08-07 10:20:51 -0700218 } else {
219 resourcesMissing = true;
220 }
bungeman91e51cb2015-07-15 14:29:25 -0400221 v22FontFamilies.deleteAll();
tomhudson8aed3c12014-08-07 10:20:51 -0700222
223 if (resourcesMissing) {
224 SkDebugf("---- Resource files missing for FontConfigParser test\n");
225 }
tomhudsonf79673b2014-08-05 06:36:11 -0700226}
Ben Wagnerf1729a72017-11-13 11:14:36 -0500227
228DEF_TEST(FontMgrAndroidLegacyMakeTypeface, reporter) {
Hal Canarya4935102017-12-08 13:35:47 -0500229 constexpr char fontsXmlFilename[] = "fonts/fonts.xml";
Ben Wagnerf1729a72017-11-13 11:14:36 -0500230 SkString basePath = GetResourcePath("fonts/");
Hal Canarya4935102017-12-08 13:35:47 -0500231 SkString fontsXml = GetResourcePath(fontsXmlFilename);
232
233 if (!sk_exists(fontsXml.c_str())) {
234 ERRORF(reporter, "file missing: %s\n", fontsXmlFilename);
235 return;
236 }
Ben Wagnerf1729a72017-11-13 11:14:36 -0500237
238 SkFontMgr_Android_CustomFonts custom;
239 custom.fSystemFontUse = SkFontMgr_Android_CustomFonts::kOnlyCustom;
240 custom.fBasePath = basePath.c_str();
241 custom.fFontsXml = fontsXml.c_str();
242 custom.fFallbackFontsXml = nullptr;
243 custom.fIsolated = false;
244
245 sk_sp<SkFontMgr> fm(SkFontMgr_New_Android(&custom));
246 sk_sp<SkTypeface> t(fm->legacyMakeTypeface("non-existent-font", SkFontStyle()));
247 REPORTER_ASSERT(reporter, nullptr == t);
248}
Bruce Wang37b61092018-06-20 16:43:02 -0400249
250static bool bitmap_compare(const SkBitmap& ref, const SkBitmap& test) {
251 for (int y = 0; y < test.height(); ++y) {
252 for (int x = 0; x < test.width(); ++x) {
253 SkColor testColor = test.getColor(x, y);
254 SkColor refColor = ref.getColor(x, y);
255 if (refColor != testColor) {
256 return false;
257 }
258 }
259 }
260 return true;
261}
262
263DEF_TEST(FontMgrAndroidSystemVariableTypeface, reporter) {
264 constexpr char fontsXmlFilename[] = "fonts/fonts.xml";
265 SkString basePath = GetResourcePath("fonts/");
266 SkString fontsXml = GetResourcePath(fontsXmlFilename);
267
268 if (!sk_exists(fontsXml.c_str())) {
269 ERRORF(reporter, "file missing: %s\n", fontsXmlFilename);
270 return;
271 }
272
273 SkFontMgr_Android_CustomFonts custom;
274 custom.fSystemFontUse = SkFontMgr_Android_CustomFonts::kOnlyCustom;
275 custom.fBasePath = basePath.c_str();
276 custom.fFontsXml = fontsXml.c_str();
277 custom.fFallbackFontsXml = nullptr;
278 custom.fIsolated = false;
279
280 sk_sp<SkFontMgr> fontMgr(SkFontMgr_New_Android(&custom));
281 // "sans-serif" in "fonts/fonts.xml" is "fonts/Distortable.ttf"
282 sk_sp<SkTypeface> typeface(fontMgr->legacyMakeTypeface("sans-serif", SkFontStyle()));
283
284 SkBitmap bitmapStream;
285 bitmapStream.allocN32Pixels(64, 64);
286 SkCanvas canvasStream(bitmapStream);
287 canvasStream.drawColor(SK_ColorWHITE);
288
289 SkBitmap bitmapClone;
290 bitmapClone.allocN32Pixels(64, 64);
291 SkCanvas canvasClone(bitmapClone);
292 canvasStream.drawColor(SK_ColorWHITE);
293
294 SkPaint paintStream;
295 paintStream.setColor(SK_ColorGRAY);
296 paintStream.setTextSize(SkIntToScalar(20));
297 paintStream.setAntiAlias(true);
298 paintStream.setLCDRenderText(true);
299
300 SkPaint paintClone;
301 paintClone.setColor(SK_ColorGRAY);
302 paintClone.setTextSize(SkIntToScalar(20));
303 paintClone.setAntiAlias(true);
304 paintClone.setLCDRenderText(true);
305
306 std::unique_ptr<SkStreamAsset> distortableStream(
307 GetResourceAsStream("fonts/Distortable.ttf"));
308 if (!distortableStream) {
309 return;
310 }
311
312 const char* text = "abc";
313 const size_t textLen = strlen(text);
314 SkPoint point = SkPoint::Make(20.0f, 20.0f);
315 SkFourByteTag tag = SkSetFourByteTag('w', 'g', 'h', 't');
316
317 for (int i = 0; i < 10; ++i) {
318 SkScalar styleValue =
319 SkDoubleToScalar(0.5 + i * ((2.0 - 0.5) / 10));
320 SkFontArguments::VariationPosition::Coordinate
321 coordinates[] = {{tag, styleValue}};
322 SkFontArguments::VariationPosition
323 position = {coordinates, SK_ARRAY_COUNT(coordinates)};
324
325 paintStream.setTypeface(sk_sp<SkTypeface>(
326 fontMgr->makeFromStream(distortableStream->duplicate(),
327 SkFontArguments().setVariationDesignPosition(position))));
328
329 paintClone.setTypeface(sk_sp<SkTypeface>(
330 typeface->makeClone(SkFontArguments().setVariationDesignPosition(position))));
331
332 canvasStream.drawColor(SK_ColorWHITE);
333 canvasStream.drawText(text, textLen, point.fX, point.fY, paintStream);
334
335 canvasClone.drawColor(SK_ColorWHITE);
336 canvasClone.drawText(text, textLen, point.fX, point.fY, paintClone);
337
338 bool success = bitmap_compare(bitmapStream, bitmapClone);
339 REPORTER_ASSERT(reporter, success);
340 }
341}
342
343