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 | |
bungeman@google.com | a980269 | 2013-08-07 02:45:25 +0000 | [diff] [blame] | 8 | #include "SkCommandLineFlags.h" |
| 9 | #include "SkFontMgr.h" |
bungeman@google.com | 07a69f8 | 2013-04-02 14:12:38 +0000 | [diff] [blame] | 10 | #include "SkOTTable_name.h" |
| 11 | #include "SkTypeface.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 12 | #include "Test.h" |
bungeman@google.com | 07a69f8 | 2013-04-02 14:12:38 +0000 | [diff] [blame] | 13 | |
| 14 | #include <stddef.h> |
| 15 | |
Ben Wagner | ad031f5 | 2018-08-20 13:45:57 -0400 | [diff] [blame] | 16 | namespace { |
| 17 | |
bungeman@google.com | 07a69f8 | 2013-04-02 14:12:38 +0000 | [diff] [blame] | 18 | template <size_t R, size_t D> struct Format0NameTable { |
| 19 | SkOTTableName header; |
| 20 | SkOTTableName::Record nameRecord[R]; |
| 21 | char data[D]; |
| 22 | }; |
| 23 | |
| 24 | template <size_t R, size_t L, size_t D> struct Format1NameTable { |
| 25 | SkOTTableName header; |
| 26 | SkOTTableName::Record nameRecord[R]; |
| 27 | struct { |
| 28 | SkOTTableName::Format1Ext header; |
| 29 | SkOTTableName::Format1Ext::LangTagRecord langTagRecord[L]; |
| 30 | } format1ext; |
| 31 | char data[D]; |
| 32 | }; |
| 33 | |
| 34 | typedef Format0NameTable<1, 9> SimpleFormat0NameTable; |
Ben Wagner | ad031f5 | 2018-08-20 13:45:57 -0400 | [diff] [blame] | 35 | constexpr SimpleFormat0NameTable simpleFormat0NameTable = { |
bungeman@google.com | 07a69f8 | 2013-04-02 14:12:38 +0000 | [diff] [blame] | 36 | /*header*/ { |
| 37 | /*format*/ SkOTTableName::format_0, |
| 38 | /*count*/ SkTEndianSwap16<1>::value, |
| 39 | /*stringOffset*/ SkTEndianSwap16<offsetof(SimpleFormat0NameTable, data)>::value, |
| 40 | }, |
| 41 | /*nameRecord[]*/ { |
| 42 | /*Record*/ { |
| 43 | /*platformID*/ { SkOTTableName::Record::PlatformID::Windows }, |
| 44 | /*encodingID*/ { SkOTTableName::Record::EncodingID::Windows::UnicodeBMPUCS2 }, |
| 45 | /*languageID*/ { SkOTTableName::Record::LanguageID::Windows::English_UnitedStates }, |
| 46 | /*nameID*/ { SkOTTableName::Record::NameID::Predefined::FontFamilyName }, |
| 47 | /*length*/ SkTEndianSwap16<8>::value, |
| 48 | /*offset*/ SkTEndianSwap16<0>::value, |
| 49 | } |
| 50 | }, |
| 51 | /*data*/ "\x0" "T" "\x0" "e" "\x0" "s" "\x0" "t", |
| 52 | }; |
| 53 | |
| 54 | typedef Format1NameTable<1, 1, 19> SimpleFormat1NameTable; |
Ben Wagner | ad031f5 | 2018-08-20 13:45:57 -0400 | [diff] [blame] | 55 | constexpr SimpleFormat1NameTable simpleFormat1NameTable = { |
bungeman@google.com | 07a69f8 | 2013-04-02 14:12:38 +0000 | [diff] [blame] | 56 | /*header*/ { |
| 57 | /*format*/ SkOTTableName::format_1, |
| 58 | /*count*/ SkTEndianSwap16<1>::value, |
| 59 | /*stringOffset*/ SkTEndianSwap16<offsetof(SimpleFormat1NameTable, data)>::value, |
| 60 | }, |
| 61 | /*nameRecord[]*/ { |
| 62 | /*Record*/ { |
| 63 | /*platformID*/ { SkOTTableName::Record::PlatformID::Windows }, |
| 64 | /*encodingID*/ { SkOTTableName::Record::EncodingID::Windows::UnicodeBMPUCS2 }, |
| 65 | /*languageID*/ { SkTEndianSwap16<0x8000 + 0>::value }, |
| 66 | /*nameID*/ { SkOTTableName::Record::NameID::Predefined::FontFamilyName }, |
| 67 | /*length*/ SkTEndianSwap16<8>::value, |
| 68 | /*offset*/ SkTEndianSwap16<0>::value, |
| 69 | } |
| 70 | }, |
| 71 | /*format1ext*/ { |
| 72 | /*header*/ { |
| 73 | /*langTagCount*/ SkTEndianSwap16<1>::value, |
| 74 | }, |
| 75 | /*langTagRecord[]*/ { |
| 76 | /*LangTagRecord*/ { |
| 77 | /*length*/ SkTEndianSwap16<10>::value, |
| 78 | /*offset*/ SkTEndianSwap16<8>::value, |
| 79 | }, |
| 80 | }, |
| 81 | }, |
| 82 | /*data*/ "\x0" "T" "\x0" "e" "\x0" "s" "\x0" "t" |
| 83 | "\x0" "e" "\x0" "n" "\x0" "-" "\x0" "U" "\x0" "S", |
| 84 | }; |
| 85 | |
| 86 | struct FontNamesTest { |
Ben Wagner | ad031f5 | 2018-08-20 13:45:57 -0400 | [diff] [blame] | 87 | const uint8_t* data; |
| 88 | size_t size; |
bungeman@google.com | 07a69f8 | 2013-04-02 14:12:38 +0000 | [diff] [blame] | 89 | SkOTTableName::Record::NameID nameID; |
| 90 | size_t nameCount; |
| 91 | struct { |
| 92 | const char* name; |
| 93 | const char* language; |
| 94 | } names[10]; |
| 95 | |
Ben Wagner | ad031f5 | 2018-08-20 13:45:57 -0400 | [diff] [blame] | 96 | } tests[] = { |
bungeman@google.com | 07a69f8 | 2013-04-02 14:12:38 +0000 | [diff] [blame] | 97 | { |
Ben Wagner | ad031f5 | 2018-08-20 13:45:57 -0400 | [diff] [blame] | 98 | reinterpret_cast<const uint8_t*>(&simpleFormat0NameTable), |
| 99 | sizeof(simpleFormat0NameTable), |
bungeman@google.com | 07a69f8 | 2013-04-02 14:12:38 +0000 | [diff] [blame] | 100 | { SkOTTableName::Record::NameID::Predefined::FontFamilyName }, |
| 101 | 1, |
| 102 | { |
| 103 | { "Test", "en-US" }, |
| 104 | }, |
| 105 | }, |
| 106 | { |
Ben Wagner | ad031f5 | 2018-08-20 13:45:57 -0400 | [diff] [blame] | 107 | reinterpret_cast<const uint8_t*>(&simpleFormat1NameTable), |
| 108 | sizeof(simpleFormat1NameTable), |
bungeman@google.com | 07a69f8 | 2013-04-02 14:12:38 +0000 | [diff] [blame] | 109 | { SkOTTableName::Record::NameID::Predefined::FontFamilyName }, |
| 110 | 1, |
| 111 | { |
| 112 | { "Test", "en-US" }, |
| 113 | }, |
| 114 | }, |
| 115 | }; |
| 116 | |
bungeman@google.com | a980269 | 2013-08-07 02:45:25 +0000 | [diff] [blame] | 117 | static void test_synthetic(skiatest::Reporter* reporter, bool verbose) { |
Ben Wagner | ad031f5 | 2018-08-20 13:45:57 -0400 | [diff] [blame] | 118 | for (const auto& test : tests) { |
| 119 | SkOTTableName::Iterator iter(test.data, test.size, test.nameID.predefined.value); |
bungeman@google.com | 07a69f8 | 2013-04-02 14:12:38 +0000 | [diff] [blame] | 120 | SkOTTableName::Iterator::Record record; |
| 121 | size_t nameIndex = 0; |
Ben Wagner | ad031f5 | 2018-08-20 13:45:57 -0400 | [diff] [blame] | 122 | while (nameIndex < test.nameCount && iter.next(record)) { |
Brian Salomon | 1c80e99 | 2018-01-29 09:50:47 -0500 | [diff] [blame] | 123 | REPORTER_ASSERT(reporter, |
Ben Wagner | ad031f5 | 2018-08-20 13:45:57 -0400 | [diff] [blame] | 124 | strcmp(test.names[nameIndex].name, record.name.c_str()) == 0, |
Brian Salomon | 1c80e99 | 2018-01-29 09:50:47 -0500 | [diff] [blame] | 125 | "Name did not match."); |
bungeman@google.com | 07a69f8 | 2013-04-02 14:12:38 +0000 | [diff] [blame] | 126 | |
Brian Salomon | 1c80e99 | 2018-01-29 09:50:47 -0500 | [diff] [blame] | 127 | REPORTER_ASSERT(reporter, |
Ben Wagner | ad031f5 | 2018-08-20 13:45:57 -0400 | [diff] [blame] | 128 | strcmp(test.names[nameIndex].language, record.language.c_str()) == 0, |
Brian Salomon | 1c80e99 | 2018-01-29 09:50:47 -0500 | [diff] [blame] | 129 | "Language did not match."); |
bungeman@google.com | 07a69f8 | 2013-04-02 14:12:38 +0000 | [diff] [blame] | 130 | |
Ben Wagner | ad031f5 | 2018-08-20 13:45:57 -0400 | [diff] [blame] | 131 | if (verbose) { |
| 132 | SkDebugf("%s <%s>\n", record.name.c_str(), record.language.c_str()); |
| 133 | } |
bungeman@google.com | 07a69f8 | 2013-04-02 14:12:38 +0000 | [diff] [blame] | 134 | |
| 135 | ++nameIndex; |
| 136 | } |
| 137 | |
Ben Wagner | ad031f5 | 2018-08-20 13:45:57 -0400 | [diff] [blame] | 138 | REPORTER_ASSERT(reporter, nameIndex == test.nameCount, "Fewer names than expected."); |
bungeman@google.com | 07a69f8 | 2013-04-02 14:12:38 +0000 | [diff] [blame] | 139 | |
Brian Salomon | 1c80e99 | 2018-01-29 09:50:47 -0500 | [diff] [blame] | 140 | REPORTER_ASSERT(reporter, !iter.next(record), "More names than expected."); |
bungeman@google.com | 07a69f8 | 2013-04-02 14:12:38 +0000 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | |
bungeman@google.com | a980269 | 2013-08-07 02:45:25 +0000 | [diff] [blame] | 144 | #define MAX_FAMILIES 1000 |
| 145 | static void test_systemfonts(skiatest::Reporter* reporter, bool verbose) { |
| 146 | static const SkFontTableTag nameTag = SkSetFourByteTag('n','a','m','e'); |
| 147 | |
Hal Canary | 342b7ac | 2016-11-04 11:49:42 -0400 | [diff] [blame] | 148 | sk_sp<SkFontMgr> fm(SkFontMgr::RefDefault()); |
bungeman@google.com | a980269 | 2013-08-07 02:45:25 +0000 | [diff] [blame] | 149 | int count = SkMin32(fm->countFamilies(), MAX_FAMILIES); |
| 150 | for (int i = 0; i < count; ++i) { |
Hal Canary | 342b7ac | 2016-11-04 11:49:42 -0400 | [diff] [blame] | 151 | sk_sp<SkFontStyleSet> set(fm->createStyleSet(i)); |
bungeman@google.com | a980269 | 2013-08-07 02:45:25 +0000 | [diff] [blame] | 152 | for (int j = 0; j < set->count(); ++j) { |
| 153 | SkString sname; |
| 154 | SkFontStyle fs; |
| 155 | set->getStyle(j, &fs, &sname); |
| 156 | |
Hal Canary | 342b7ac | 2016-11-04 11:49:42 -0400 | [diff] [blame] | 157 | sk_sp<SkTypeface> typeface(set->createTypeface(j)); |
bungeman@google.com | a980269 | 2013-08-07 02:45:25 +0000 | [diff] [blame] | 158 | |
| 159 | SkString familyName; |
| 160 | typeface->getFamilyName(&familyName); |
| 161 | if (verbose) { |
bungeman@google.com | fab44db | 2013-10-11 18:50:45 +0000 | [diff] [blame] | 162 | SkDebugf("[%s]\n", familyName.c_str()); |
bungeman@google.com | a980269 | 2013-08-07 02:45:25 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Hal Canary | 342b7ac | 2016-11-04 11:49:42 -0400 | [diff] [blame] | 165 | sk_sp<SkTypeface::LocalizedStrings> familyNamesIter( |
bungeman@google.com | 839702b | 2013-08-07 17:09:22 +0000 | [diff] [blame] | 166 | typeface->createFamilyNameIterator()); |
bungeman@google.com | a980269 | 2013-08-07 02:45:25 +0000 | [diff] [blame] | 167 | SkTypeface::LocalizedString familyNameLocalized; |
| 168 | while (familyNamesIter->next(&familyNameLocalized)) { |
| 169 | if (verbose) { |
bungeman@google.com | fab44db | 2013-10-11 18:50:45 +0000 | [diff] [blame] | 170 | SkDebugf("(%s) <%s>\n", familyNameLocalized.fString.c_str(), |
bungeman | a6785cc | 2014-08-25 12:00:49 -0700 | [diff] [blame] | 171 | familyNameLocalized.fLanguage.c_str()); |
bungeman@google.com | a980269 | 2013-08-07 02:45:25 +0000 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | |
| 175 | size_t nameTableSize = typeface->getTableSize(nameTag); |
| 176 | if (0 == nameTableSize) { |
| 177 | continue; |
| 178 | } |
| 179 | SkAutoTMalloc<uint8_t> nameTableData(nameTableSize); |
| 180 | size_t copied = typeface->getTableData(nameTag, 0, nameTableSize, nameTableData.get()); |
| 181 | if (copied != nameTableSize) { |
| 182 | continue; |
| 183 | } |
| 184 | |
| 185 | SkOTTableName::Iterator::Record record; |
Ben Wagner | ad031f5 | 2018-08-20 13:45:57 -0400 | [diff] [blame] | 186 | SkOTTableName::Iterator familyNameIter(nameTableData.get(), nameTableSize, |
bungeman@google.com | a980269 | 2013-08-07 02:45:25 +0000 | [diff] [blame] | 187 | SkOTTableName::Record::NameID::Predefined::FontFamilyName); |
| 188 | while (familyNameIter.next(record)) { |
Brian Salomon | 1c80e99 | 2018-01-29 09:50:47 -0500 | [diff] [blame] | 189 | REPORTER_ASSERT( |
| 190 | reporter, |
| 191 | SkOTTableName::Record::NameID::Predefined::FontFamilyName == record.type, |
| 192 | "Requested family name, got something else."); |
bungeman@google.com | a980269 | 2013-08-07 02:45:25 +0000 | [diff] [blame] | 193 | if (verbose) { |
bungeman@google.com | fab44db | 2013-10-11 18:50:45 +0000 | [diff] [blame] | 194 | SkDebugf("{%s} <%s>\n", record.name.c_str(), record.language.c_str()); |
bungeman@google.com | a980269 | 2013-08-07 02:45:25 +0000 | [diff] [blame] | 195 | } |
| 196 | } |
| 197 | |
Ben Wagner | ad031f5 | 2018-08-20 13:45:57 -0400 | [diff] [blame] | 198 | SkOTTableName::Iterator styleNameIter(nameTableData.get(), nameTableSize, |
bungeman@google.com | a980269 | 2013-08-07 02:45:25 +0000 | [diff] [blame] | 199 | SkOTTableName::Record::NameID::Predefined::FontSubfamilyName); |
| 200 | while (styleNameIter.next(record)) { |
Brian Salomon | 1c80e99 | 2018-01-29 09:50:47 -0500 | [diff] [blame] | 201 | REPORTER_ASSERT( |
| 202 | reporter, |
| 203 | SkOTTableName::Record::NameID::Predefined::FontSubfamilyName == record.type, |
| 204 | "Requested subfamily name, got something else."); |
bungeman@google.com | a980269 | 2013-08-07 02:45:25 +0000 | [diff] [blame] | 205 | if (verbose) { |
bungeman@google.com | fab44db | 2013-10-11 18:50:45 +0000 | [diff] [blame] | 206 | SkDebugf("{{%s}} <%s>\n", record.name.c_str(), record.language.c_str()); |
bungeman@google.com | a980269 | 2013-08-07 02:45:25 +0000 | [diff] [blame] | 207 | } |
| 208 | } |
| 209 | |
| 210 | if (verbose) { |
bungeman@google.com | fab44db | 2013-10-11 18:50:45 +0000 | [diff] [blame] | 211 | SkDebugf("\n"); |
bungeman@google.com | a980269 | 2013-08-07 02:45:25 +0000 | [diff] [blame] | 212 | } |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | |
Ben Wagner | ad031f5 | 2018-08-20 13:45:57 -0400 | [diff] [blame] | 217 | } // namespace |
| 218 | |
bungeman@google.com | a980269 | 2013-08-07 02:45:25 +0000 | [diff] [blame] | 219 | DEFINE_bool(verboseFontNames, false, "verbose FontNames test."); |
| 220 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 221 | DEF_TEST(FontNames, reporter) { |
bungeman@google.com | a980269 | 2013-08-07 02:45:25 +0000 | [diff] [blame] | 222 | test_synthetic(reporter, FLAGS_verboseFontNames); |
| 223 | test_systemfonts(reporter, FLAGS_verboseFontNames); |
| 224 | } |