1 /*
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"
9 #include "SkCommandLineFlags.h"
10 #include "SkFontConfigParser_android.h"
11 #include "Test.h"
12 
13 DECLARE_bool(verboseFontMgr);
14 
CountFallbacks(SkTDArray<FontFamily * > fontFamilies)15 int 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 
25 //https://tools.ietf.org/html/rfc5234#appendix-B.1
isALPHA(int c)26 static 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
isDIGIT(int c)31 static bool isDIGIT(int c) {
32     return ('0' <= c && c <= '9');
33 }
34 
ValidateLoadedFonts(SkTDArray<FontFamily * > fontFamilies,const char * firstExpectedFile,skiatest::Reporter * reporter)35 void ValidateLoadedFonts(SkTDArray<FontFamily*> fontFamilies, const char* firstExpectedFile,
36                          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,
40                     !strcmp(fontFamilies[0]->fFonts[0].fFileName.c_str(), firstExpectedFile));
41     REPORTER_ASSERT(reporter, !fontFamilies[0]->fIsFallbackFont);
42 
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     }
51 
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     }
64 }
65 
DumpLoadedFonts(SkTDArray<FontFamily * > fontFamilies,const char * label)66 void DumpLoadedFonts(SkTDArray<FontFamily*> fontFamilies, const char* label) {
67     if (!FLAGS_verboseFontMgr) {
68         return;
69     }
70 
71     SkDebugf("\n--- Dumping %s\n", label);
72     for (int i = 0; i < fontFamilies.count(); ++i) {
73         SkDebugf("Family %d:\n", i);
74         switch(fontFamilies[i]->fVariant) {
75             case kElegant_FontVariant: SkDebugf("  elegant\n"); break;
76             case kCompact_FontVariant: SkDebugf("  compact\n"); break;
77             default: break;
78         }
79         SkDebugf("  basePath %s\n", fontFamilies[i]->fBasePath.c_str());
80         if (!fontFamilies[i]->fLanguage.getTag().isEmpty()) {
81             SkDebugf("  language %s\n", fontFamilies[i]->fLanguage.getTag().c_str());
82         }
83         for (int j = 0; j < fontFamilies[i]->fNames.count(); ++j) {
84             SkDebugf("  name %s\n", fontFamilies[i]->fNames[j].c_str());
85         }
86         for (int j = 0; j < fontFamilies[i]->fFonts.count(); ++j) {
87             const FontFileInfo& ffi = fontFamilies[i]->fFonts[j];
88             SkDebugf("  file (%d) %s#%d\n", ffi.fWeight, ffi.fFileName.c_str(), ffi.fIndex);
89         }
90     }
91     SkDebugf("\n\n");
92 }
93 
DEF_TEST(FontConfigParserAndroid,reporter)94 DEF_TEST(FontConfigParserAndroid, reporter) {
95 
96     bool resourcesMissing = false;
97 
98     SkTDArray<FontFamily*> preV17FontFamilies;
99     SkFontConfigParser::GetCustomFontFamilies(preV17FontFamilies,
100         SkString("/custom/font/path/"),
101         GetResourcePath("android_fonts/pre_v17/system_fonts.xml").c_str(),
102         GetResourcePath("android_fonts/pre_v17/fallback_fonts.xml").c_str());
103 
104     if (preV17FontFamilies.count() > 0) {
105         REPORTER_ASSERT(reporter, preV17FontFamilies.count() == 14);
106         REPORTER_ASSERT(reporter, CountFallbacks(preV17FontFamilies) == 10);
107 
108         DumpLoadedFonts(preV17FontFamilies, "pre version 17");
109         ValidateLoadedFonts(preV17FontFamilies, "Roboto-Regular.ttf", reporter);
110     } else {
111         resourcesMissing = true;
112     }
113 
114 
115     SkTDArray<FontFamily*> v17FontFamilies;
116     SkFontConfigParser::GetCustomFontFamilies(v17FontFamilies,
117         SkString("/custom/font/path/"),
118         GetResourcePath("android_fonts/v17/system_fonts.xml").c_str(),
119         GetResourcePath("android_fonts/v17/fallback_fonts.xml").c_str(),
120         GetResourcePath("android_fonts/v17").c_str());
121 
122     if (v17FontFamilies.count() > 0) {
123         REPORTER_ASSERT(reporter, v17FontFamilies.count() == 56);
124         REPORTER_ASSERT(reporter, CountFallbacks(v17FontFamilies) == 46);
125 
126         DumpLoadedFonts(v17FontFamilies, "version 17");
127         ValidateLoadedFonts(v17FontFamilies, "Roboto-Regular.ttf", reporter);
128     } else {
129         resourcesMissing = true;
130     }
131 
132 
133     SkTDArray<FontFamily*> v22FontFamilies;
134     SkFontConfigParser::GetCustomFontFamilies(v22FontFamilies,
135         SkString("/custom/font/path/"),
136         GetResourcePath("android_fonts/v22/fonts.xml").c_str(),
137         NULL);
138 
139     if (v22FontFamilies.count() > 0) {
140         REPORTER_ASSERT(reporter, v22FontFamilies.count() == 53);
141         REPORTER_ASSERT(reporter, CountFallbacks(v22FontFamilies) == 42);
142 
143         DumpLoadedFonts(v22FontFamilies, "version 22");
144         ValidateLoadedFonts(v22FontFamilies, "Roboto-Thin.ttf", reporter);
145     } else {
146         resourcesMissing = true;
147     }
148 
149     if (resourcesMissing) {
150         SkDebugf("---- Resource files missing for FontConfigParser test\n");
151     }
152 }
153 
154