blob: e58d35c625bb4bf52573c7f7c511b51f613bc250 [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"
tomhudsonf79673b2014-08-05 06:36:11 -070010#include "SkFontConfigParser_android.h"
11#include "Test.h"
12
bungemaneb2be7f2015-02-10 07:51:12 -080013DECLARE_bool(verboseFontMgr);
14
tomhudson2ed49a42014-08-13 07:53:48 -070015int CountFallbacks(SkTDArray<FontFamily*> fontFamilies) {
16 int countOfFallbackFonts = 0;
17 for (int i = 0; i < fontFamilies.count(); i++) {
18 if (fontFamilies[i]->fIsFallbackFont) {
19 countOfFallbackFonts++;
20 }
21 }
22 return countOfFallbackFonts;
23}
24
bungemanc3c69432015-02-11 07:18:51 -080025//https://tools.ietf.org/html/rfc5234#appendix-B.1
26static bool isALPHA(int c) {
27 return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z');
28}
29
30//https://tools.ietf.org/html/rfc5234#appendix-B.1
31static bool isDIGIT(int c) {
32 return ('0' <= c && c <= '9');
33}
34
bungeman4b86bac2014-11-04 10:54:31 -080035void ValidateLoadedFonts(SkTDArray<FontFamily*> fontFamilies, const char* firstExpectedFile,
tomhudsonf79673b2014-08-05 06:36:11 -070036 skiatest::Reporter* reporter) {
37 REPORTER_ASSERT(reporter, fontFamilies[0]->fNames.count() == 5);
38 REPORTER_ASSERT(reporter, !strcmp(fontFamilies[0]->fNames[0].c_str(), "sans-serif"));
39 REPORTER_ASSERT(reporter,
bungeman4b86bac2014-11-04 10:54:31 -080040 !strcmp(fontFamilies[0]->fFonts[0].fFileName.c_str(), firstExpectedFile));
tomhudsonf79673b2014-08-05 06:36:11 -070041 REPORTER_ASSERT(reporter, !fontFamilies[0]->fIsFallbackFont);
bungemanc3c69432015-02-11 07:18:51 -080042
43 // Check that the languages are all sane.
44 for (int i = 0; i < fontFamilies.count(); ++i) {
45 const SkString& lang = fontFamilies[i]->fLanguage.getTag();
46 for (size_t j = 0; j < lang.size(); ++j) {
47 int c = lang[j];
48 REPORTER_ASSERT(reporter, isALPHA(c) || isDIGIT(c) || '-' == c);
49 }
50 }
bungeman9a0808f2015-02-13 08:55:16 -080051
52 // All file names in the test configuration files start with a capital letter.
53 // This is not a general requirement, but it is true of all the test configuration data.
54 // Verifying ensures the filenames have been read sanely and have not been 'sliced'.
55 for (int i = 0; i < fontFamilies.count(); ++i) {
56 FontFamily& family = *fontFamilies[i];
57 for (int j = 0; j < family.fFonts.count(); ++j) {
58 FontFileInfo& file = family.fFonts[j];
59 REPORTER_ASSERT(reporter, !file.fFileName.isEmpty() &&
60 file.fFileName[0] >= 'A' &&
61 file.fFileName[0] <= 'Z');
62 }
63 }
tomhudsonf79673b2014-08-05 06:36:11 -070064}
65
bungeman3d47e212015-02-13 18:30:11 -080066void DumpLoadedFonts(SkTDArray<FontFamily*> fontFamilies, const char* label) {
bungemaneb2be7f2015-02-10 07:51:12 -080067 if (!FLAGS_verboseFontMgr) {
68 return;
69 }
70
bungeman3d47e212015-02-13 18:30:11 -080071 SkDebugf("\n--- Dumping %s\n", label);
tomhudsonf79673b2014-08-05 06:36:11 -070072 for (int i = 0; i < fontFamilies.count(); ++i) {
73 SkDebugf("Family %d:\n", i);
tomhudson07544752014-08-05 13:35:00 -070074 switch(fontFamilies[i]->fVariant) {
bungemana44e9b92015-01-30 19:58:19 -080075 case kElegant_FontVariant: SkDebugf(" elegant\n"); break;
76 case kCompact_FontVariant: SkDebugf(" compact\n"); break;
tomhudson07544752014-08-05 13:35:00 -070077 default: break;
78 }
bungemaneb2be7f2015-02-10 07:51:12 -080079 SkDebugf(" basePath %s\n", fontFamilies[i]->fBasePath.c_str());
tomhudson07544752014-08-05 13:35:00 -070080 if (!fontFamilies[i]->fLanguage.getTag().isEmpty()) {
bungemana44e9b92015-01-30 19:58:19 -080081 SkDebugf(" language %s\n", fontFamilies[i]->fLanguage.getTag().c_str());
tomhudson07544752014-08-05 13:35:00 -070082 }
tomhudsonf79673b2014-08-05 06:36:11 -070083 for (int j = 0; j < fontFamilies[i]->fNames.count(); ++j) {
84 SkDebugf(" name %s\n", fontFamilies[i]->fNames[j].c_str());
85 }
tomhudsond3ddea22014-08-11 11:28:00 -070086 for (int j = 0; j < fontFamilies[i]->fFonts.count(); ++j) {
87 const FontFileInfo& ffi = fontFamilies[i]->fFonts[j];
bungemana44e9b92015-01-30 19:58:19 -080088 SkDebugf(" file (%d) %s#%d\n", ffi.fWeight, ffi.fFileName.c_str(), ffi.fIndex);
tomhudson07544752014-08-05 13:35:00 -070089 }
tomhudsonf79673b2014-08-05 06:36:11 -070090 }
bungemanc3c69432015-02-11 07:18:51 -080091 SkDebugf("\n\n");
tomhudsonf79673b2014-08-05 06:36:11 -070092}
93
94DEF_TEST(FontConfigParserAndroid, reporter) {
95
tomhudson8aed3c12014-08-07 10:20:51 -070096 bool resourcesMissing = false;
97
tomhudsonf79673b2014-08-05 06:36:11 -070098 SkTDArray<FontFamily*> preV17FontFamilies;
bungeman7fa87cd2015-02-06 07:59:19 -080099 SkFontConfigParser::GetCustomFontFamilies(preV17FontFamilies,
100 SkString("/custom/font/path/"),
tomhudsonf79673b2014-08-05 06:36:11 -0700101 GetResourcePath("android_fonts/pre_v17/system_fonts.xml").c_str(),
102 GetResourcePath("android_fonts/pre_v17/fallback_fonts.xml").c_str());
103
tomhudson8aed3c12014-08-07 10:20:51 -0700104 if (preV17FontFamilies.count() > 0) {
105 REPORTER_ASSERT(reporter, preV17FontFamilies.count() == 14);
tomhudson2ed49a42014-08-13 07:53:48 -0700106 REPORTER_ASSERT(reporter, CountFallbacks(preV17FontFamilies) == 10);
tomhudsonf79673b2014-08-05 06:36:11 -0700107
bungeman3d47e212015-02-13 18:30:11 -0800108 DumpLoadedFonts(preV17FontFamilies, "pre version 17");
bungeman4b86bac2014-11-04 10:54:31 -0800109 ValidateLoadedFonts(preV17FontFamilies, "Roboto-Regular.ttf", reporter);
tomhudson8aed3c12014-08-07 10:20:51 -0700110 } else {
111 resourcesMissing = true;
112 }
tomhudsonf79673b2014-08-05 06:36:11 -0700113
tomhudson07544752014-08-05 13:35:00 -0700114
tomhudsonf79673b2014-08-05 06:36:11 -0700115 SkTDArray<FontFamily*> v17FontFamilies;
bungeman7fa87cd2015-02-06 07:59:19 -0800116 SkFontConfigParser::GetCustomFontFamilies(v17FontFamilies,
117 SkString("/custom/font/path/"),
tomhudsonf79673b2014-08-05 06:36:11 -0700118 GetResourcePath("android_fonts/v17/system_fonts.xml").c_str(),
bungemanc3c69432015-02-11 07:18:51 -0800119 GetResourcePath("android_fonts/v17/fallback_fonts.xml").c_str(),
120 GetResourcePath("android_fonts/v17").c_str());
tomhudsonf79673b2014-08-05 06:36:11 -0700121
tomhudson8aed3c12014-08-07 10:20:51 -0700122 if (v17FontFamilies.count() > 0) {
bungemanc3c69432015-02-11 07:18:51 -0800123 REPORTER_ASSERT(reporter, v17FontFamilies.count() == 56);
124 REPORTER_ASSERT(reporter, CountFallbacks(v17FontFamilies) == 46);
tomhudsonf79673b2014-08-05 06:36:11 -0700125
bungeman3d47e212015-02-13 18:30:11 -0800126 DumpLoadedFonts(v17FontFamilies, "version 17");
bungeman4b86bac2014-11-04 10:54:31 -0800127 ValidateLoadedFonts(v17FontFamilies, "Roboto-Regular.ttf", reporter);
tomhudson8aed3c12014-08-07 10:20:51 -0700128 } else {
129 resourcesMissing = true;
130 }
tomhudsonf79673b2014-08-05 06:36:11 -0700131
tomhudson07544752014-08-05 13:35:00 -0700132
tomhudsonf79673b2014-08-05 06:36:11 -0700133 SkTDArray<FontFamily*> v22FontFamilies;
bungeman7fa87cd2015-02-06 07:59:19 -0800134 SkFontConfigParser::GetCustomFontFamilies(v22FontFamilies,
135 SkString("/custom/font/path/"),
tomhudsonf79673b2014-08-05 06:36:11 -0700136 GetResourcePath("android_fonts/v22/fonts.xml").c_str(),
137 NULL);
138
tomhudson8aed3c12014-08-07 10:20:51 -0700139 if (v22FontFamilies.count() > 0) {
140 REPORTER_ASSERT(reporter, v22FontFamilies.count() == 53);
tomhudson2ed49a42014-08-13 07:53:48 -0700141 REPORTER_ASSERT(reporter, CountFallbacks(v22FontFamilies) == 42);
tomhudsonf79673b2014-08-05 06:36:11 -0700142
bungeman3d47e212015-02-13 18:30:11 -0800143 DumpLoadedFonts(v22FontFamilies, "version 22");
bungeman4b86bac2014-11-04 10:54:31 -0800144 ValidateLoadedFonts(v22FontFamilies, "Roboto-Thin.ttf", reporter);
tomhudson8aed3c12014-08-07 10:20:51 -0700145 } else {
146 resourcesMissing = true;
147 }
148
149 if (resourcesMissing) {
150 SkDebugf("---- Resource files missing for FontConfigParser test\n");
151 }
tomhudsonf79673b2014-08-05 06:36:11 -0700152}
153