bungeman@google.com | 07a69f8 | 2013-04-02 14:12:38 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 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 | |
| 10 | #include "SkOTTable_name.h" |
| 11 | #include "SkTypeface.h" |
| 12 | |
| 13 | #include <stddef.h> |
| 14 | |
| 15 | template <size_t R, size_t D> struct Format0NameTable { |
| 16 | SkOTTableName header; |
| 17 | SkOTTableName::Record nameRecord[R]; |
| 18 | char data[D]; |
| 19 | }; |
| 20 | |
| 21 | template <size_t R, size_t L, size_t D> struct Format1NameTable { |
| 22 | SkOTTableName header; |
| 23 | SkOTTableName::Record nameRecord[R]; |
| 24 | struct { |
| 25 | SkOTTableName::Format1Ext header; |
| 26 | SkOTTableName::Format1Ext::LangTagRecord langTagRecord[L]; |
| 27 | } format1ext; |
| 28 | char data[D]; |
| 29 | }; |
| 30 | |
| 31 | typedef Format0NameTable<1, 9> SimpleFormat0NameTable; |
| 32 | SimpleFormat0NameTable simpleFormat0NameTable = { |
| 33 | /*header*/ { |
| 34 | /*format*/ SkOTTableName::format_0, |
| 35 | /*count*/ SkTEndianSwap16<1>::value, |
| 36 | /*stringOffset*/ SkTEndianSwap16<offsetof(SimpleFormat0NameTable, data)>::value, |
| 37 | }, |
| 38 | /*nameRecord[]*/ { |
| 39 | /*Record*/ { |
| 40 | /*platformID*/ { SkOTTableName::Record::PlatformID::Windows }, |
| 41 | /*encodingID*/ { SkOTTableName::Record::EncodingID::Windows::UnicodeBMPUCS2 }, |
| 42 | /*languageID*/ { SkOTTableName::Record::LanguageID::Windows::English_UnitedStates }, |
| 43 | /*nameID*/ { SkOTTableName::Record::NameID::Predefined::FontFamilyName }, |
| 44 | /*length*/ SkTEndianSwap16<8>::value, |
| 45 | /*offset*/ SkTEndianSwap16<0>::value, |
| 46 | } |
| 47 | }, |
| 48 | /*data*/ "\x0" "T" "\x0" "e" "\x0" "s" "\x0" "t", |
| 49 | }; |
| 50 | |
| 51 | typedef Format1NameTable<1, 1, 19> SimpleFormat1NameTable; |
| 52 | SimpleFormat1NameTable simpleFormat1NameTable = { |
| 53 | /*header*/ { |
| 54 | /*format*/ SkOTTableName::format_1, |
| 55 | /*count*/ SkTEndianSwap16<1>::value, |
| 56 | /*stringOffset*/ SkTEndianSwap16<offsetof(SimpleFormat1NameTable, data)>::value, |
| 57 | }, |
| 58 | /*nameRecord[]*/ { |
| 59 | /*Record*/ { |
| 60 | /*platformID*/ { SkOTTableName::Record::PlatformID::Windows }, |
| 61 | /*encodingID*/ { SkOTTableName::Record::EncodingID::Windows::UnicodeBMPUCS2 }, |
| 62 | /*languageID*/ { SkTEndianSwap16<0x8000 + 0>::value }, |
| 63 | /*nameID*/ { SkOTTableName::Record::NameID::Predefined::FontFamilyName }, |
| 64 | /*length*/ SkTEndianSwap16<8>::value, |
| 65 | /*offset*/ SkTEndianSwap16<0>::value, |
| 66 | } |
| 67 | }, |
| 68 | /*format1ext*/ { |
| 69 | /*header*/ { |
| 70 | /*langTagCount*/ SkTEndianSwap16<1>::value, |
| 71 | }, |
| 72 | /*langTagRecord[]*/ { |
| 73 | /*LangTagRecord*/ { |
| 74 | /*length*/ SkTEndianSwap16<10>::value, |
| 75 | /*offset*/ SkTEndianSwap16<8>::value, |
| 76 | }, |
| 77 | }, |
| 78 | }, |
| 79 | /*data*/ "\x0" "T" "\x0" "e" "\x0" "s" "\x0" "t" |
| 80 | "\x0" "e" "\x0" "n" "\x0" "-" "\x0" "U" "\x0" "S", |
| 81 | }; |
| 82 | |
| 83 | struct FontNamesTest { |
| 84 | SkOTTableName* data; |
| 85 | SkOTTableName::Record::NameID nameID; |
| 86 | size_t nameCount; |
| 87 | struct { |
| 88 | const char* name; |
| 89 | const char* language; |
| 90 | } names[10]; |
| 91 | |
| 92 | } test[] = { |
| 93 | { |
| 94 | (SkOTTableName*)&simpleFormat0NameTable, |
| 95 | { SkOTTableName::Record::NameID::Predefined::FontFamilyName }, |
| 96 | 1, |
| 97 | { |
| 98 | { "Test", "en-US" }, |
| 99 | }, |
| 100 | }, |
| 101 | { |
| 102 | (SkOTTableName*)&simpleFormat1NameTable, |
| 103 | { SkOTTableName::Record::NameID::Predefined::FontFamilyName }, |
| 104 | 1, |
| 105 | { |
| 106 | { "Test", "en-US" }, |
| 107 | }, |
| 108 | }, |
| 109 | }; |
| 110 | |
| 111 | static void TestFontNames(skiatest::Reporter* reporter) { |
| 112 | static const char* interestingFont[] = { |
| 113 | "Arial", |
| 114 | "Times New Roman", |
| 115 | "MS PGothic", // Has Japanese name. |
| 116 | "Wingdings", // Uses 'Symbol' name encoding. |
| 117 | }; |
| 118 | static const SkFontTableTag nameTag = SkSetFourByteTag('n','a','m','e'); |
| 119 | |
| 120 | for (size_t i = 0; i < SK_ARRAY_COUNT(interestingFont); ++i) { |
| 121 | SkAutoTUnref<SkTypeface> typeface(SkTypeface::CreateFromName(interestingFont[i], |
| 122 | SkTypeface::kNormal)); |
| 123 | if (NULL == typeface.get()) { |
| 124 | continue; |
| 125 | } |
| 126 | size_t nameTableSize = typeface->getTableSize(nameTag); |
| 127 | if (0 == nameTableSize) { |
| 128 | continue; |
| 129 | } |
| 130 | uint8_t* nameTableData = new uint8_t[nameTableSize]; |
| 131 | SkAutoTDeleteArray<uint8_t> ada(nameTableData); |
| 132 | size_t copied = typeface->getTableData(nameTag, 0, nameTableSize, nameTableData); |
| 133 | if (copied != nameTableSize) { |
| 134 | continue; |
| 135 | } |
| 136 | |
| 137 | SkOTTableName::Iterator iter(*((SkOTTableName*)nameTableData), |
| 138 | SkOTTableName::Record::NameID::Predefined::FontFamilyName); |
| 139 | SkOTTableName::Iterator::Record record; |
| 140 | while (iter.next(record)) { |
| 141 | //printf("%s <%s>\n", record.name.c_str(), record.language.c_str()); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | for (size_t i = 0; i < SK_ARRAY_COUNT(test); ++i) { |
| 146 | SkOTTableName::Iterator iter(*test[i].data, test[i].nameID.predefined.value); |
| 147 | SkOTTableName::Iterator::Record record; |
| 148 | size_t nameIndex = 0; |
| 149 | while (nameIndex < test[i].nameCount && iter.next(record)) { |
| 150 | REPORTER_ASSERT_MESSAGE(reporter, |
| 151 | strcmp(test[i].names[nameIndex].name, record.name.c_str()) == 0, |
| 152 | "Name did not match." |
| 153 | ); |
| 154 | |
| 155 | REPORTER_ASSERT_MESSAGE(reporter, |
| 156 | strcmp(test[i].names[nameIndex].language, record.language.c_str()) == 0, |
| 157 | "Language did not match." |
| 158 | ); |
| 159 | |
| 160 | //printf("%s <%s>\n", record.name.c_str(), record.language.c_str()); |
| 161 | |
| 162 | ++nameIndex; |
| 163 | } |
| 164 | |
| 165 | REPORTER_ASSERT_MESSAGE(reporter, nameIndex == test[i].nameCount, |
| 166 | "Fewer names than expected."); |
| 167 | |
| 168 | REPORTER_ASSERT_MESSAGE(reporter, !iter.next(record), |
| 169 | "More names than expected."); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | |
| 174 | #include "TestClassDef.h" |
| 175 | DEFINE_TESTCLASS("FontNames", FontNamesTestClass, TestFontNames) |