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 "SkOTTable_name.h" 9 10 #include "SkEndian.h" 11 #include "SkString.h" 12 #include "SkTSearch.h" 13 #include "SkTemplates.h" 14 #include "SkUtils.h" 15 16 static SkUnichar SkUTF16BE_NextUnichar(const uint16_t** srcPtr) { 17 SkASSERT(srcPtr && *srcPtr); 18 19 const uint16_t* src = *srcPtr; 20 SkUnichar c = SkEndian_SwapBE16(*src++); 21 22 SkASSERT(!SkUTF16_IsLowSurrogate(c)); 23 if (SkUTF16_IsHighSurrogate(c)) { 24 unsigned c2 = SkEndian_SwapBE16(*src++); 25 SkASSERT(SkUTF16_IsLowSurrogate(c2)); 26 27 c = (c << 10) + c2 + (0x10000 - (0xD800 << 10) - 0xDC00); 28 } 29 *srcPtr = src; 30 return c; 31 } 32 33 static void SkStringFromUTF16BE(const uint16_t* utf16be, size_t length, SkString& utf8) { 34 SkASSERT(utf16be != NULL); 35 36 utf8.reset(); 37 size_t numberOf16BitValues = length / 2; 38 const uint16_t* end = utf16be + numberOf16BitValues; 39 while (utf16be < end) { 40 utf8.appendUnichar(SkUTF16BE_NextUnichar(&utf16be)); 41 } 42 } 43 44 /** UnicodeFromMacRoman[macRomanPoint - 0x80] -> unicodeCodePoint. 45 * Derived from http://www.unicode.org/Public/MAPPINGS/VENDORS/APPLE/ROMAN.TXT . 46 * In MacRoman the first 128 code points match ASCII code points. 47 * This maps the second 128 MacRoman code points to unicode code points. 48 */ 49 static uint16_t UnicodeFromMacRoman[0x80] = { 50 0x00C4, 0x00C5, 0x00C7, 0x00C9, 0x00D1, 0x00D6, 0x00DC, 0x00E1, 51 0x00E0, 0x00E2, 0x00E4, 0x00E3, 0x00E5, 0x00E7, 0x00E9, 0x00E8, 52 0x00EA, 0x00EB, 0x00ED, 0x00EC, 0x00EE, 0x00EF, 0x00F1, 0x00F3, 53 0x00F2, 0x00F4, 0x00F6, 0x00F5, 0x00FA, 0x00F9, 0x00FB, 0x00FC, 54 0x2020, 0x00B0, 0x00A2, 0x00A3, 0x00A7, 0x2022, 0x00B6, 0x00DF, 55 0x00AE, 0x00A9, 0x2122, 0x00B4, 0x00A8, 0x2260, 0x00C6, 0x00D8, 56 0x221E, 0x00B1, 0x2264, 0x2265, 0x00A5, 0x00B5, 0x2202, 0x2211, 57 0x220F, 0x03C0, 0x222B, 0x00AA, 0x00BA, 0x03A9, 0x00E6, 0x00F8, 58 0x00BF, 0x00A1, 0x00AC, 0x221A, 0x0192, 0x2248, 0x2206, 0x00AB, 59 0x00BB, 0x2026, 0x00A0, 0x00C0, 0x00C3, 0x00D5, 0x0152, 0x0153, 60 0x2013, 0x2014, 0x201C, 0x201D, 0x2018, 0x2019, 0x00F7, 0x25CA, 61 0x00FF, 0x0178, 0x2044, 0x20AC, 0x2039, 0x203A, 0xFB01, 0xFB02, 62 0x2021, 0x00B7, 0x201A, 0x201E, 0x2030, 0x00C2, 0x00CA, 0x00C1, 63 0x00CB, 0x00C8, 0x00CD, 0x00CE, 0x00CF, 0x00CC, 0x00D3, 0x00D4, 64 0xF8FF, 0x00D2, 0x00DA, 0x00DB, 0x00D9, 0x0131, 0x02C6, 0x02DC, 65 0x00AF, 0x02D8, 0x02D9, 0x02DA, 0x00B8, 0x02DD, 0x02DB, 0x02C7, 66 }; 67 68 static void SkStringFromMacRoman(const uint8_t* macRoman, size_t length, SkString& utf8) { 69 utf8.reset(); 70 for (size_t i = 0; i < length; ++i) { 71 utf8.appendUnichar(macRoman[i] < 0x80 ? macRoman[i] : UnicodeFromMacRoman[macRoman[i]]); 72 } 73 } 74 75 static struct BCP47FromLanguageId { 76 uint16_t languageID; 77 const char* bcp47; 78 } 79 /** The Mac and Windows values do not conflict, so this is currently one single table. */ 80 BCP47FromLanguageID[] = { 81 /** A mapping from Mac Language Designators to BCP 47 codes. 82 * The following list was constructed more or less manually. 83 * Apple now uses BCP 47 (post OSX10.4), so there will be no new entries. 84 */ 85 {0, "en"}, //English 86 {1, "fr"}, //French 87 {2, "de"}, //German 88 {3, "it"}, //Italian 89 {4, "nl"}, //Dutch 90 {5, "sv"}, //Swedish 91 {6, "es"}, //Spanish 92 {7, "da"}, //Danish 93 {8, "pt"}, //Portuguese 94 {9, "nb"}, //Norwegian 95 {10, "he"}, //Hebrew 96 {11, "ja"}, //Japanese 97 {12, "ar"}, //Arabic 98 {13, "fi"}, //Finnish 99 {14, "el"}, //Greek 100 {15, "is"}, //Icelandic 101 {16, "mt"}, //Maltese 102 {17, "tr"}, //Turkish 103 {18, "hr"}, //Croatian 104 {19, "zh-Hant"}, //Chinese (Traditional) 105 {20, "ur"}, //Urdu 106 {21, "hi"}, //Hindi 107 {22, "th"}, //Thai 108 {23, "ko"}, //Korean 109 {24, "lt"}, //Lithuanian 110 {25, "pl"}, //Polish 111 {26, "hu"}, //Hungarian 112 {27, "et"}, //Estonian 113 {28, "lv"}, //Latvian 114 {29, "se"}, //Sami 115 {30, "fo"}, //Faroese 116 {31, "fa"}, //Farsi (Persian) 117 {32, "ru"}, //Russian 118 {33, "zh-Hans"}, //Chinese (Simplified) 119 {34, "nl"}, //Dutch 120 {35, "ga"}, //Irish(Gaelic) 121 {36, "sq"}, //Albanian 122 {37, "ro"}, //Romanian 123 {38, "cs"}, //Czech 124 {39, "sk"}, //Slovak 125 {40, "sl"}, //Slovenian 126 {41, "yi"}, //Yiddish 127 {42, "sr"}, //Serbian 128 {43, "mk"}, //Macedonian 129 {44, "bg"}, //Bulgarian 130 {45, "uk"}, //Ukrainian 131 {46, "be"}, //Byelorussian 132 {47, "uz"}, //Uzbek 133 {48, "kk"}, //Kazakh 134 {49, "az-Cyrl"}, //Azerbaijani (Cyrillic) 135 {50, "az-Arab"}, //Azerbaijani (Arabic) 136 {51, "hy"}, //Armenian 137 {52, "ka"}, //Georgian 138 {53, "mo"}, //Moldavian 139 {54, "ky"}, //Kirghiz 140 {55, "tg"}, //Tajiki 141 {56, "tk"}, //Turkmen 142 {57, "mn-Mong"}, //Mongolian (Traditional) 143 {58, "mn-Cyrl"}, //Mongolian (Cyrillic) 144 {59, "ps"}, //Pashto 145 {60, "ku"}, //Kurdish 146 {61, "ks"}, //Kashmiri 147 {62, "sd"}, //Sindhi 148 {63, "bo"}, //Tibetan 149 {64, "ne"}, //Nepali 150 {65, "sa"}, //Sanskrit 151 {66, "mr"}, //Marathi 152 {67, "bn"}, //Bengali 153 {68, "as"}, //Assamese 154 {69, "gu"}, //Gujarati 155 {70, "pa"}, //Punjabi 156 {71, "or"}, //Oriya 157 {72, "ml"}, //Malayalam 158 {73, "kn"}, //Kannada 159 {74, "ta"}, //Tamil 160 {75, "te"}, //Telugu 161 {76, "si"}, //Sinhalese 162 {77, "my"}, //Burmese 163 {78, "km"}, //Khmer 164 {79, "lo"}, //Lao 165 {80, "vi"}, //Vietnamese 166 {81, "id"}, //Indonesian 167 {82, "tl"}, //Tagalog 168 {83, "ms-Latn"}, //Malay (Roman) 169 {84, "ms-Arab"}, //Malay (Arabic) 170 {85, "am"}, //Amharic 171 {86, "ti"}, //Tigrinya 172 {87, "om"}, //Oromo 173 {88, "so"}, //Somali 174 {89, "sw"}, //Swahili 175 {90, "rw"}, //Kinyarwanda/Ruanda 176 {91, "rn"}, //Rundi 177 {92, "ny"}, //Nyanja/Chewa 178 {93, "mg"}, //Malagasy 179 {94, "eo"}, //Esperanto 180 {128, "cy"}, //Welsh 181 {129, "eu"}, //Basque 182 {130, "ca"}, //Catalan 183 {131, "la"}, //Latin 184 {132, "qu"}, //Quechua 185 {133, "gn"}, //Guarani 186 {134, "ay"}, //Aymara 187 {135, "tt"}, //Tatar 188 {136, "ug"}, //Uighur 189 {137, "dz"}, //Dzongkha 190 {138, "jv-Latn"}, //Javanese (Roman) 191 {139, "su-Latn"}, //Sundanese (Roman) 192 {140, "gl"}, //Galician 193 {141, "af"}, //Afrikaans 194 {142, "br"}, //Breton 195 {143, "iu"}, //Inuktitut 196 {144, "gd"}, //Scottish (Gaelic) 197 {145, "gv"}, //Manx (Gaelic) 198 {146, "ga"}, //Irish (Gaelic with Lenition) 199 {147, "to"}, //Tongan 200 {148, "el"}, //Greek (Polytonic) Note: ISO 15924 does not have an equivalent script name. 201 {149, "kl"}, //Greenlandic 202 {150, "az-Latn"}, //Azerbaijani (Roman) 203 {151, "nn"}, //Nynorsk 204 205 /** A mapping from Windows LCID to BCP 47 codes. 206 * This list is the sorted, curated output of tools/win_lcid.cpp. 207 * Note that these are sorted by value for quick binary lookup, and not logically by lsb. 208 * The 'bare' language ids (e.g. 0x0001 for Arabic) are ommitted 209 * as they do not appear as valid language ids in the OpenType specification. 210 */ 211 { 0x0401, "ar-SA" }, //Arabic 212 { 0x0402, "bg-BG" }, //Bulgarian 213 { 0x0403, "ca-ES" }, //Catalan 214 { 0x0404, "zh-TW" }, //Chinese (Traditional) 215 { 0x0405, "cs-CZ" }, //Czech 216 { 0x0406, "da-DK" }, //Danish 217 { 0x0407, "de-DE" }, //German 218 { 0x0408, "el-GR" }, //Greek 219 { 0x0409, "en-US" }, //English 220 { 0x040a, "es-ES_tradnl" }, //Spanish 221 { 0x040b, "fi-FI" }, //Finnish 222 { 0x040c, "fr-FR" }, //French 223 { 0x040d, "he-IL" }, //Hebrew 224 { 0x040d, "he" }, //Hebrew 225 { 0x040e, "hu-HU" }, //Hungarian 226 { 0x040e, "hu" }, //Hungarian 227 { 0x040f, "is-IS" }, //Icelandic 228 { 0x0410, "it-IT" }, //Italian 229 { 0x0411, "ja-JP" }, //Japanese 230 { 0x0412, "ko-KR" }, //Korean 231 { 0x0413, "nl-NL" }, //Dutch 232 { 0x0414, "nb-NO" }, //Norwegian (Bokml) 233 { 0x0415, "pl-PL" }, //Polish 234 { 0x0416, "pt-BR" }, //Portuguese 235 { 0x0417, "rm-CH" }, //Romansh 236 { 0x0418, "ro-RO" }, //Romanian 237 { 0x0419, "ru-RU" }, //Russian 238 { 0x041a, "hr-HR" }, //Croatian 239 { 0x041b, "sk-SK" }, //Slovak 240 { 0x041c, "sq-AL" }, //Albanian 241 { 0x041d, "sv-SE" }, //Swedish 242 { 0x041e, "th-TH" }, //Thai 243 { 0x041f, "tr-TR" }, //Turkish 244 { 0x0420, "ur-PK" }, //Urdu 245 { 0x0421, "id-ID" }, //Indonesian 246 { 0x0422, "uk-UA" }, //Ukrainian 247 { 0x0423, "be-BY" }, //Belarusian 248 { 0x0424, "sl-SI" }, //Slovenian 249 { 0x0425, "et-EE" }, //Estonian 250 { 0x0426, "lv-LV" }, //Latvian 251 { 0x0427, "lt-LT" }, //Lithuanian 252 { 0x0428, "tg-Cyrl-TJ" }, //Tajik (Cyrillic) 253 { 0x0429, "fa-IR" }, //Persian 254 { 0x042a, "vi-VN" }, //Vietnamese 255 { 0x042b, "hy-AM" }, //Armenian 256 { 0x042c, "az-Latn-AZ" }, //Azeri (Latin) 257 { 0x042d, "eu-ES" }, //Basque 258 { 0x042e, "hsb-DE" }, //Upper Sorbian 259 { 0x042f, "mk-MK" }, //Macedonian (FYROM) 260 { 0x0432, "tn-ZA" }, //Setswana 261 { 0x0434, "xh-ZA" }, //isiXhosa 262 { 0x0435, "zu-ZA" }, //isiZulu 263 { 0x0436, "af-ZA" }, //Afrikaans 264 { 0x0437, "ka-GE" }, //Georgian 265 { 0x0438, "fo-FO" }, //Faroese 266 { 0x0439, "hi-IN" }, //Hindi 267 { 0x043a, "mt-MT" }, //Maltese 268 { 0x043b, "se-NO" }, //Sami (Northern) 269 { 0x043e, "ms-MY" }, //Malay 270 { 0x043f, "kk-KZ" }, //Kazakh 271 { 0x0440, "ky-KG" }, //Kyrgyz 272 { 0x0441, "sw-KE" }, //Kiswahili 273 { 0x0442, "tk-TM" }, //Turkmen 274 { 0x0443, "uz-Latn-UZ" }, //Uzbek (Latin) 275 { 0x0443, "uz" }, //Uzbek 276 { 0x0444, "tt-RU" }, //Tatar 277 { 0x0445, "bn-IN" }, //Bengali 278 { 0x0446, "pa-IN" }, //Punjabi 279 { 0x0447, "gu-IN" }, //Gujarati 280 { 0x0448, "or-IN" }, //Oriya 281 { 0x0449, "ta-IN" }, //Tamil 282 { 0x044a, "te-IN" }, //Telugu 283 { 0x044b, "kn-IN" }, //Kannada 284 { 0x044c, "ml-IN" }, //Malayalam 285 { 0x044d, "as-IN" }, //Assamese 286 { 0x044e, "mr-IN" }, //Marathi 287 { 0x044f, "sa-IN" }, //Sanskrit 288 { 0x0450, "mn-Cyrl" }, //Mongolian (Cyrillic) 289 { 0x0451, "bo-CN" }, //Tibetan 290 { 0x0452, "cy-GB" }, //Welsh 291 { 0x0453, "km-KH" }, //Khmer 292 { 0x0454, "lo-LA" }, //Lao 293 { 0x0456, "gl-ES" }, //Galician 294 { 0x0457, "kok-IN" }, //Konkani 295 { 0x045a, "syr-SY" }, //Syriac 296 { 0x045b, "si-LK" }, //Sinhala 297 { 0x045d, "iu-Cans-CA" }, //Inuktitut (Syllabics) 298 { 0x045e, "am-ET" }, //Amharic 299 { 0x0461, "ne-NP" }, //Nepali 300 { 0x0462, "fy-NL" }, //Frisian 301 { 0x0463, "ps-AF" }, //Pashto 302 { 0x0464, "fil-PH" }, //Filipino 303 { 0x0465, "dv-MV" }, //Divehi 304 { 0x0468, "ha-Latn-NG" }, //Hausa (Latin) 305 { 0x046a, "yo-NG" }, //Yoruba 306 { 0x046b, "quz-BO" }, //Quechua 307 { 0x046c, "nso-ZA" }, //Sesotho sa Leboa 308 { 0x046d, "ba-RU" }, //Bashkir 309 { 0x046e, "lb-LU" }, //Luxembourgish 310 { 0x046f, "kl-GL" }, //Greenlandic 311 { 0x0470, "ig-NG" }, //Igbo 312 { 0x0478, "ii-CN" }, //Yi 313 { 0x047a, "arn-CL" }, //Mapudungun 314 { 0x047c, "moh-CA" }, //Mohawk 315 { 0x047e, "br-FR" }, //Breton 316 { 0x0480, "ug-CN" }, //Uyghur 317 { 0x0481, "mi-NZ" }, //Maori 318 { 0x0482, "oc-FR" }, //Occitan 319 { 0x0483, "co-FR" }, //Corsican 320 { 0x0484, "gsw-FR" }, //Alsatian 321 { 0x0485, "sah-RU" }, //Yakut 322 { 0x0486, "qut-GT" }, //K'iche 323 { 0x0487, "rw-RW" }, //Kinyarwanda 324 { 0x0488, "wo-SN" }, //Wolof 325 { 0x048c, "prs-AF" }, //Dari 326 { 0x0491, "gd-GB" }, //Scottish Gaelic 327 { 0x0801, "ar-IQ" }, //Arabic 328 { 0x0804, "zh-Hans" }, //Chinese (Simplified) 329 { 0x0807, "de-CH" }, //German 330 { 0x0809, "en-GB" }, //English 331 { 0x080a, "es-MX" }, //Spanish 332 { 0x080c, "fr-BE" }, //French 333 { 0x0810, "it-CH" }, //Italian 334 { 0x0813, "nl-BE" }, //Dutch 335 { 0x0814, "nn-NO" }, //Norwegian (Nynorsk) 336 { 0x0816, "pt-PT" }, //Portuguese 337 { 0x081a, "sr-Latn-CS" }, //Serbian (Latin) 338 { 0x081d, "sv-FI" }, //Swedish 339 { 0x082c, "az-Cyrl-AZ" }, //Azeri (Cyrillic) 340 { 0x082e, "dsb-DE" }, //Lower Sorbian 341 { 0x082e, "dsb" }, //Lower Sorbian 342 { 0x083b, "se-SE" }, //Sami (Northern) 343 { 0x083c, "ga-IE" }, //Irish 344 { 0x083e, "ms-BN" }, //Malay 345 { 0x0843, "uz-Cyrl-UZ" }, //Uzbek (Cyrillic) 346 { 0x0845, "bn-BD" }, //Bengali 347 { 0x0850, "mn-Mong-CN" }, //Mongolian (Traditional Mongolian) 348 { 0x085d, "iu-Latn-CA" }, //Inuktitut (Latin) 349 { 0x085f, "tzm-Latn-DZ" }, //Tamazight (Latin) 350 { 0x086b, "quz-EC" }, //Quechua 351 { 0x0c01, "ar-EG" }, //Arabic 352 { 0x0c04, "zh-Hant" }, //Chinese (Traditional) 353 { 0x0c07, "de-AT" }, //German 354 { 0x0c09, "en-AU" }, //English 355 { 0x0c0a, "es-ES" }, //Spanish 356 { 0x0c0c, "fr-CA" }, //French 357 { 0x0c1a, "sr-Cyrl-CS" }, //Serbian (Cyrillic) 358 { 0x0c3b, "se-FI" }, //Sami (Northern) 359 { 0x0c6b, "quz-PE" }, //Quechua 360 { 0x1001, "ar-LY" }, //Arabic 361 { 0x1004, "zh-SG" }, //Chinese (Simplified) 362 { 0x1007, "de-LU" }, //German 363 { 0x1009, "en-CA" }, //English 364 { 0x100a, "es-GT" }, //Spanish 365 { 0x100c, "fr-CH" }, //French 366 { 0x101a, "hr-BA" }, //Croatian (Latin) 367 { 0x103b, "smj-NO" }, //Sami (Lule) 368 { 0x1401, "ar-DZ" }, //Arabic 369 { 0x1404, "zh-MO" }, //Chinese (Traditional) 370 { 0x1407, "de-LI" }, //German 371 { 0x1409, "en-NZ" }, //English 372 { 0x140a, "es-CR" }, //Spanish 373 { 0x140c, "fr-LU" }, //French 374 { 0x141a, "bs-Latn-BA" }, //Bosnian (Latin) 375 { 0x141a, "bs" }, //Bosnian 376 { 0x143b, "smj-SE" }, //Sami (Lule) 377 { 0x143b, "smj" }, //Sami (Lule) 378 { 0x1801, "ar-MA" }, //Arabic 379 { 0x1809, "en-IE" }, //English 380 { 0x180a, "es-PA" }, //Spanish 381 { 0x180c, "fr-MC" }, //French 382 { 0x181a, "sr-Latn-BA" }, //Serbian (Latin) 383 { 0x183b, "sma-NO" }, //Sami (Southern) 384 { 0x1c01, "ar-TN" }, //Arabic 385 { 0x1c09, "en-ZA" }, //English 386 { 0x1c0a, "es-DO" }, //Spanish 387 { 0x1c1a, "sr-Cyrl-BA" }, //Serbian (Cyrillic) 388 { 0x1c3b, "sma-SE" }, //Sami (Southern) 389 { 0x1c3b, "sma" }, //Sami (Southern) 390 { 0x2001, "ar-OM" }, //Arabic 391 { 0x2009, "en-JM" }, //English 392 { 0x200a, "es-VE" }, //Spanish 393 { 0x201a, "bs-Cyrl-BA" }, //Bosnian (Cyrillic) 394 { 0x201a, "bs-Cyrl" }, //Bosnian (Cyrillic) 395 { 0x203b, "sms-FI" }, //Sami (Skolt) 396 { 0x203b, "sms" }, //Sami (Skolt) 397 { 0x2401, "ar-YE" }, //Arabic 398 { 0x2409, "en-029" }, //English 399 { 0x240a, "es-CO" }, //Spanish 400 { 0x241a, "sr-Latn-RS" }, //Serbian (Latin) 401 { 0x243b, "smn-FI" }, //Sami (Inari) 402 { 0x2801, "ar-SY" }, //Arabic 403 { 0x2809, "en-BZ" }, //English 404 { 0x280a, "es-PE" }, //Spanish 405 { 0x281a, "sr-Cyrl-RS" }, //Serbian (Cyrillic) 406 { 0x2c01, "ar-JO" }, //Arabic 407 { 0x2c09, "en-TT" }, //English 408 { 0x2c0a, "es-AR" }, //Spanish 409 { 0x2c1a, "sr-Latn-ME" }, //Serbian (Latin) 410 { 0x3001, "ar-LB" }, //Arabic 411 { 0x3009, "en-ZW" }, //English 412 { 0x300a, "es-EC" }, //Spanish 413 { 0x301a, "sr-Cyrl-ME" }, //Serbian (Cyrillic) 414 { 0x3401, "ar-KW" }, //Arabic 415 { 0x3409, "en-PH" }, //English 416 { 0x340a, "es-CL" }, //Spanish 417 { 0x3801, "ar-AE" }, //Arabic 418 { 0x380a, "es-UY" }, //Spanish 419 { 0x3c01, "ar-BH" }, //Arabic 420 { 0x3c0a, "es-PY" }, //Spanish 421 { 0x4001, "ar-QA" }, //Arabic 422 { 0x4009, "en-IN" }, //English 423 { 0x400a, "es-BO" }, //Spanish 424 { 0x4409, "en-MY" }, //English 425 { 0x440a, "es-SV" }, //Spanish 426 { 0x4809, "en-SG" }, //English 427 { 0x480a, "es-HN" }, //Spanish 428 { 0x4c0a, "es-NI" }, //Spanish 429 { 0x500a, "es-PR" }, //Spanish 430 { 0x540a, "es-US" }, //Spanish 431 }; 432 433 namespace { 434 bool BCP47FromLanguageIdLess(const BCP47FromLanguageId& a, const BCP47FromLanguageId& b) { 435 return a.languageID < b.languageID; 436 } 437 } 438 439 bool SkOTTableName::Iterator::next(SkOTTableName::Iterator::Record& record) { 440 const size_t nameRecordsCount = SkEndian_SwapBE16(fName.count); 441 const SkOTTableName::Record* nameRecords = SkTAfter<const SkOTTableName::Record>(&fName); 442 const SkOTTableName::Record* nameRecord; 443 444 // Find the next record which matches the requested type. 445 do { 446 if (fIndex >= nameRecordsCount) { 447 return false; 448 } 449 450 nameRecord = &nameRecords[fIndex]; 451 ++fIndex; 452 } while (fType != -1 && nameRecord->nameID.fontSpecific != fType); 453 454 record.type = nameRecord->nameID.fontSpecific; 455 456 const uint16_t stringTableOffset = SkEndian_SwapBE16(fName.stringOffset); 457 const char* stringTable = SkTAddOffset<const char>(&fName, stringTableOffset); 458 459 // Decode the name into UTF-8. 460 const uint16_t nameOffset = SkEndian_SwapBE16(nameRecord->offset); 461 const uint16_t nameLength = SkEndian_SwapBE16(nameRecord->length); 462 const char* nameString = SkTAddOffset<const char>(stringTable, nameOffset); 463 switch (nameRecord->platformID.value) { 464 case SkOTTableName::Record::PlatformID::Windows: 465 if (SkOTTableName::Record::EncodingID::Windows::UnicodeBMPUCS2 466 != nameRecord->encodingID.windows.value 467 && SkOTTableName::Record::EncodingID::Windows::UnicodeUCS4 468 != nameRecord->encodingID.windows.value 469 && SkOTTableName::Record::EncodingID::Windows::Symbol 470 != nameRecord->encodingID.windows.value) 471 { 472 record.name.reset(); 473 break; 474 } 475 case SkOTTableName::Record::PlatformID::Unicode: 476 case SkOTTableName::Record::PlatformID::ISO: 477 SkStringFromUTF16BE((const uint16_t*)nameString, nameLength, record.name); 478 break; 479 480 case SkOTTableName::Record::PlatformID::Macintosh: 481 // TODO: need better decoding, especially on Mac. 482 if (SkOTTableName::Record::EncodingID::Macintosh::Roman 483 != nameRecord->encodingID.macintosh.value) 484 { 485 record.name.reset(); 486 break; 487 } 488 SkStringFromMacRoman((const uint8_t*)nameString, nameLength, record.name); 489 break; 490 491 case SkOTTableName::Record::PlatformID::Custom: 492 // These should never appear in a 'name' table. 493 default: 494 SkASSERT(false); 495 record.name.reset(); 496 break; 497 } 498 499 // Determine the language. 500 const uint16_t languageID = SkEndian_SwapBE16(nameRecord->languageID.languageTagID); 501 502 // Handle format 1 languages. 503 if (SkOTTableName::format_1 == fName.format && languageID >= 0x8000) { 504 const uint16_t languageTagRecordIndex = languageID - 0x8000; 505 506 const SkOTTableName::Format1Ext* format1ext = 507 SkTAfter<const SkOTTableName::Format1Ext>(nameRecords, nameRecordsCount); 508 509 if (languageTagRecordIndex < SkEndian_SwapBE16(format1ext->langTagCount)) { 510 const SkOTTableName::Format1Ext::LangTagRecord* languageTagRecord = 511 SkTAfter<const SkOTTableName::Format1Ext::LangTagRecord>(format1ext); 512 513 uint16_t offset = SkEndian_SwapBE16(languageTagRecord[languageTagRecordIndex].offset); 514 uint16_t length = SkEndian_SwapBE16(languageTagRecord[languageTagRecordIndex].length); 515 const uint16_t* string = SkTAddOffset<const uint16_t>(stringTable, offset); 516 SkStringFromUTF16BE(string, length, record.language); 517 return true; 518 } 519 } 520 521 // Handle format 0 languages, translating them into BCP 47. 522 const BCP47FromLanguageId target = { languageID, "" }; 523 int languageIndex = SkTSearch<BCP47FromLanguageId, BCP47FromLanguageIdLess>( 524 BCP47FromLanguageID, SK_ARRAY_COUNT(BCP47FromLanguageID), target, sizeof(target)); 525 if (languageIndex >= 0) { 526 record.language = BCP47FromLanguageID[languageIndex].bcp47; 527 return true; 528 } 529 530 // Unknown language, return the BCP 47 code 'und' for 'undetermined'. 531 SkASSERT(false); 532 record.language = "und"; 533 return true; 534 } 535