Home | History | Annotate | Download | only in tests
      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 "SkFixed.h"
     11 #include "SkFontMgr_android.h"
     12 #include "SkFontMgr_android_parser.h"
     13 #include "SkOSFile.h"
     14 #include "SkTypeface.h"
     15 #include "Test.h"
     16 
     17 #include <cmath>
     18 #include <cstdio>
     19 
     20 DECLARE_bool(verboseFontMgr);
     21 
     22 int CountFallbacks(SkTDArray<FontFamily*> fontFamilies) {
     23     int countOfFallbackFonts = 0;
     24     for (int i = 0; i < fontFamilies.count(); i++) {
     25         if (fontFamilies[i]->fIsFallbackFont) {
     26             countOfFallbackFonts++;
     27         }
     28     }
     29     return countOfFallbackFonts;
     30 }
     31 
     32 //https://tools.ietf.org/html/rfc5234#appendix-B.1
     33 static bool isALPHA(int c) {
     34     return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z');
     35 }
     36 
     37 //https://tools.ietf.org/html/rfc5234#appendix-B.1
     38 static bool isDIGIT(int c) {
     39     return ('0' <= c && c <= '9');
     40 }
     41 
     42 void ValidateLoadedFonts(SkTDArray<FontFamily*> fontFamilies, const char* firstExpectedFile,
     43                          skiatest::Reporter* reporter) {
     44     REPORTER_ASSERT(reporter, fontFamilies[0]->fNames.count() == 5);
     45     REPORTER_ASSERT(reporter, !strcmp(fontFamilies[0]->fNames[0].c_str(), "sans-serif"));
     46     REPORTER_ASSERT(reporter,
     47                     !strcmp(fontFamilies[0]->fFonts[0].fFileName.c_str(), firstExpectedFile));
     48     REPORTER_ASSERT(reporter, !fontFamilies[0]->fIsFallbackFont);
     49 
     50     // Check that the languages are all sane.
     51     for (const auto& fontFamily : fontFamilies) {
     52         for (const auto& lang : fontFamily->fLanguages) {
     53             const SkString& langString = lang.getTag();
     54             for (size_t i = 0; i < langString.size(); ++i) {
     55                 int c = langString[i];
     56                 REPORTER_ASSERT(reporter, isALPHA(c) || isDIGIT(c) || '-' == c);
     57             }
     58         }
     59     }
     60 
     61     // All file names in the test configuration files start with a capital letter.
     62     // This is not a general requirement, but it is true of all the test configuration data.
     63     // Verifying ensures the filenames have been read sanely and have not been 'sliced'.
     64     for (int i = 0; i < fontFamilies.count(); ++i) {
     65         FontFamily& family = *fontFamilies[i];
     66         for (int j = 0; j < family.fFonts.count(); ++j) {
     67             FontFileInfo& file = family.fFonts[j];
     68             REPORTER_ASSERT(reporter, !file.fFileName.isEmpty() &&
     69                                       file.fFileName[0] >= 'A' &&
     70                                       file.fFileName[0] <= 'Z');
     71         }
     72     }
     73 }
     74 
     75 void DumpLoadedFonts(SkTDArray<FontFamily*> fontFamilies, const char* label) {
     76     if (!FLAGS_verboseFontMgr) {
     77         return;
     78     }
     79 
     80     SkDebugf("\n--- Dumping %s\n", label);
     81     for (int i = 0; i < fontFamilies.count(); ++i) {
     82         SkDebugf("Family %d:\n", i);
     83         switch(fontFamilies[i]->fVariant) {
     84             case kElegant_FontVariant: SkDebugf("  elegant\n"); break;
     85             case kCompact_FontVariant: SkDebugf("  compact\n"); break;
     86             default: break;
     87         }
     88         SkDebugf("  basePath %s\n", fontFamilies[i]->fBasePath.c_str());
     89         if (!fontFamilies[i]->fLanguages.empty()) {
     90             SkDebugf("  language");
     91             for (const auto& lang : fontFamilies[i]->fLanguages) {
     92                 SkDebugf(" %s", lang.getTag().c_str());
     93             }
     94             SkDebugf("\n");
     95         }
     96         for (int j = 0; j < fontFamilies[i]->fNames.count(); ++j) {
     97             SkDebugf("  name %s\n", fontFamilies[i]->fNames[j].c_str());
     98         }
     99         for (int j = 0; j < fontFamilies[i]->fFonts.count(); ++j) {
    100             const FontFileInfo& ffi = fontFamilies[i]->fFonts[j];
    101             SkDebugf("  file (%d) %s#%d", ffi.fWeight, ffi.fFileName.c_str(), ffi.fIndex);
    102             for (const auto& coordinate : ffi.fVariationDesignPosition) {
    103                 SkDebugf(" @'%c%c%c%c'=%f",
    104                          (coordinate.axis >> 24) & 0xFF,
    105                          (coordinate.axis >> 16) & 0xFF,
    106                          (coordinate.axis >>  8) & 0xFF,
    107                          (coordinate.axis      ) & 0xFF,
    108                          coordinate.value);
    109             }
    110             SkDebugf("\n");
    111         }
    112     }
    113     SkDebugf("\n\n");
    114 }
    115 
    116 template <int N, typename T> static double test_parse_fixed_r(skiatest::Reporter* reporter,
    117                                                               double low, double high, double inc)
    118 {
    119     double SK_FixedMax_double = nextafter(1 << (sizeof(T) * CHAR_BIT - N - 1), 0.0);
    120     double SK_FixedEpsilon_double = (1.0 / (1 << N));
    121     double maxError = 0;
    122     char buffer[64];
    123     for (double f = low; f < high; f += inc) {
    124         SkString s;
    125         // 'sprintf' formatting as expected depends on the current locale being "C".
    126         // We currently expect tests and tools to run in the "C" locale.
    127         sprintf(buffer, "%.20f", f);
    128         T fix;
    129         bool b = parse_fixed<N>(buffer, &fix);
    130         if (b) {
    131             double f2 = fix * SK_FixedEpsilon_double;
    132             double error = fabs(f - f2);
    133             REPORTER_ASSERT(reporter,  error <= SK_FixedEpsilon_double);
    134             maxError = SkTMax(maxError, error);
    135         } else {
    136             REPORTER_ASSERT(reporter, f < -SK_FixedMax_double || SK_FixedMax_double < f);
    137         }
    138     }
    139 
    140     //SkDebugf("maxError: %.20f\n", maxError);
    141     return maxError;
    142 }
    143 
    144 static void test_parse_fixed(skiatest::Reporter* reporter) {
    145     test_parse_fixed_r<27, int32_t>(reporter, -8.1, -7.9, 0.000001);
    146     test_parse_fixed_r<27, int32_t>(reporter, -0.1, 0.1, 0.000001);
    147     test_parse_fixed_r<27, int32_t>(reporter, 7.9, 8.1, 0.000001);
    148     test_parse_fixed_r<16, int32_t>(reporter, -0.125, 0.125, 1.0 / (1 << 19));
    149     test_parse_fixed_r<16, int32_t>(reporter, -32768.125, -32766.875, 1.0 / (1 << 17));
    150     test_parse_fixed_r<16, int32_t>(reporter, 32766.875, 32768.125, 1.0 / (1 << 17));
    151     test_parse_fixed_r<16, int32_t>(reporter, -1.1, 1.1, 0.0001);
    152 
    153     SkFixed fix;
    154     REPORTER_ASSERT(reporter, !parse_fixed<27>("-17.1", &fix));
    155     REPORTER_ASSERT(reporter, !parse_fixed<16>("32768", &fix));
    156     REPORTER_ASSERT(reporter, !parse_fixed<16>("", &fix));
    157     REPORTER_ASSERT(reporter, !parse_fixed<16>(".", &fix));
    158     REPORTER_ASSERT(reporter, !parse_fixed<16>("123.", &fix));
    159     REPORTER_ASSERT(reporter, !parse_fixed<16>("a", &fix));
    160     REPORTER_ASSERT(reporter, !parse_fixed<16>(".123a", &fix));
    161 }
    162 
    163 DEF_TEST(FontMgrAndroidParser, reporter) {
    164     test_parse_fixed(reporter);
    165 
    166     bool resourcesMissing = false;
    167 
    168     SkTDArray<FontFamily*> preV17FontFamilies;
    169     SkFontMgr_Android_Parser::GetCustomFontFamilies(preV17FontFamilies,
    170         SkString("/custom/font/path/"),
    171         GetResourcePath("android_fonts/pre_v17/system_fonts.xml").c_str(),
    172         GetResourcePath("android_fonts/pre_v17/fallback_fonts.xml").c_str());
    173 
    174     if (preV17FontFamilies.count() > 0) {
    175         REPORTER_ASSERT(reporter, preV17FontFamilies.count() == 14);
    176         REPORTER_ASSERT(reporter, CountFallbacks(preV17FontFamilies) == 10);
    177 
    178         DumpLoadedFonts(preV17FontFamilies, "pre version 17");
    179         ValidateLoadedFonts(preV17FontFamilies, "Roboto-Regular.ttf", reporter);
    180     } else {
    181         resourcesMissing = true;
    182     }
    183     preV17FontFamilies.deleteAll();
    184 
    185 
    186     SkTDArray<FontFamily*> v17FontFamilies;
    187     SkFontMgr_Android_Parser::GetCustomFontFamilies(v17FontFamilies,
    188         SkString("/custom/font/path/"),
    189         GetResourcePath("android_fonts/v17/system_fonts.xml").c_str(),
    190         GetResourcePath("android_fonts/v17/fallback_fonts.xml").c_str(),
    191         GetResourcePath("android_fonts/v17").c_str());
    192 
    193     if (v17FontFamilies.count() > 0) {
    194         REPORTER_ASSERT(reporter, v17FontFamilies.count() == 56);
    195         REPORTER_ASSERT(reporter, CountFallbacks(v17FontFamilies) == 46);
    196 
    197         DumpLoadedFonts(v17FontFamilies, "version 17");
    198         ValidateLoadedFonts(v17FontFamilies, "Roboto-Regular.ttf", reporter);
    199     } else {
    200         resourcesMissing = true;
    201     }
    202     v17FontFamilies.deleteAll();
    203 
    204 
    205     SkTDArray<FontFamily*> v22FontFamilies;
    206     SkFontMgr_Android_Parser::GetCustomFontFamilies(v22FontFamilies,
    207         SkString("/custom/font/path/"),
    208         GetResourcePath("android_fonts/v22/fonts.xml").c_str(),
    209         nullptr);
    210 
    211     if (v22FontFamilies.count() > 0) {
    212         REPORTER_ASSERT(reporter, v22FontFamilies.count() == 54);
    213         REPORTER_ASSERT(reporter, CountFallbacks(v22FontFamilies) == 42);
    214 
    215         DumpLoadedFonts(v22FontFamilies, "version 22");
    216         ValidateLoadedFonts(v22FontFamilies, "Roboto-Thin.ttf", reporter);
    217     } else {
    218         resourcesMissing = true;
    219     }
    220     v22FontFamilies.deleteAll();
    221 
    222     if (resourcesMissing) {
    223         SkDebugf("---- Resource files missing for FontConfigParser test\n");
    224     }
    225 }
    226 
    227 DEF_TEST(FontMgrAndroidLegacyMakeTypeface, reporter) {
    228     constexpr char fontsXmlFilename[] = "fonts/fonts.xml";
    229     SkString basePath = GetResourcePath("fonts/");
    230     SkString fontsXml = GetResourcePath(fontsXmlFilename);
    231 
    232     if (!sk_exists(fontsXml.c_str())) {
    233         ERRORF(reporter, "file missing: %s\n", fontsXmlFilename);
    234         return;
    235     }
    236 
    237     SkFontMgr_Android_CustomFonts custom;
    238     custom.fSystemFontUse = SkFontMgr_Android_CustomFonts::kOnlyCustom;
    239     custom.fBasePath = basePath.c_str();
    240     custom.fFontsXml = fontsXml.c_str();
    241     custom.fFallbackFontsXml = nullptr;
    242     custom.fIsolated = false;
    243 
    244     sk_sp<SkFontMgr> fm(SkFontMgr_New_Android(&custom));
    245     sk_sp<SkTypeface> t(fm->legacyMakeTypeface("non-existent-font", SkFontStyle()));
    246     REPORTER_ASSERT(reporter, nullptr == t);
    247 }
    248