Home | History | Annotate | Download | only in tests
      1 /*
      2  * Copyright 2012 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 "Test.h"
      9 #include "SkTypeface.h"
     10 #include "SkFontHost.h"
     11 
     12 //#define DUMP_TABLES
     13 
     14 #define kFontTableTag_head          SkSetFourByteTag('h', 'e', 'a', 'd')
     15 #define kFontTableTag_hhea          SkSetFourByteTag('h', 'h', 'e', 'a')
     16 #define kFontTableTag_maxp          SkSetFourByteTag('m', 'a', 'x', 'p')
     17 
     18 static const struct TagSize {
     19     SkFontTableTag  fTag;
     20     size_t          fSize;
     21 } gKnownTableSizes[] = {
     22     {   kFontTableTag_head,         54 },
     23     {   kFontTableTag_hhea,         36 },
     24     {   kFontTableTag_maxp,         32 },
     25 };
     26 
     27 static void test_tables(skiatest::Reporter* reporter, SkTypeface* face) {
     28     SkFontID fontID = face->uniqueID();
     29 
     30     int count = SkFontHost::CountTables(fontID);
     31 
     32     SkAutoTMalloc<SkFontTableTag> storage(count);
     33     SkFontTableTag* tags = storage.get();
     34 
     35     int count2 = SkFontHost::GetTableTags(fontID, tags);
     36     REPORTER_ASSERT(reporter, count2 == count);
     37 
     38     for (int i = 0; i < count; ++i) {
     39         size_t size = SkFontHost::GetTableSize(fontID, tags[i]);
     40         REPORTER_ASSERT(reporter, size > 0);
     41 
     42 #ifdef DUMP_TABLES
     43         char name[5];
     44         name[0] = (tags[i] >> 24) & 0xFF;
     45         name[1] = (tags[i] >> 16) & 0xFF;
     46         name[2] = (tags[i] >>  8) & 0xFF;
     47         name[3] = (tags[i] >>  0) & 0xFF;
     48         name[4] = 0;
     49         SkDebugf("%s %d\n", name, size);
     50 #endif
     51 
     52         for (size_t j = 0; j < SK_ARRAY_COUNT(gKnownTableSizes); ++j) {
     53             if (gKnownTableSizes[j].fTag == tags[i]) {
     54                 REPORTER_ASSERT(reporter, gKnownTableSizes[j].fSize == size);
     55             }
     56         }
     57 
     58         // do we get the same size from GetTableData and GetTableSize
     59         {
     60             SkAutoMalloc data(size);
     61             size_t size2 = SkFontHost::GetTableData(fontID, tags[i], 0, size,
     62                                                     data.get());
     63             REPORTER_ASSERT(reporter, size2 == size);
     64         }
     65     }
     66 }
     67 
     68 static void test_tables(skiatest::Reporter* reporter) {
     69     static const char* const gNames[] = {
     70         NULL,   // default font
     71         "Arial", "Times", "Times New Roman", "Helvetica", "Courier",
     72         "Courier New",
     73     };
     74 
     75     for (size_t i = 0; i < SK_ARRAY_COUNT(gNames); ++i) {
     76         SkTypeface* face = SkTypeface::CreateFromName(gNames[i],
     77                                                       SkTypeface::kNormal);
     78         if (face) {
     79 #ifdef DUMP_TABLES
     80             SkDebugf("%s\n", gNames[i]);
     81 #endif
     82             test_tables(reporter, face);
     83             face->unref();
     84         }
     85     }
     86 }
     87 
     88 static void TestFontHost(skiatest::Reporter* reporter) {
     89     test_tables(reporter);
     90 }
     91 
     92 // need tests for SkStrSearch
     93 
     94 #include "TestClassDef.h"
     95 DEFINE_TESTCLASS("FontHost", FontHostTestClass, TestFontHost)
     96