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