hb-ot-name-table.hh 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. /*
  2. * Copyright © 2011,2012 Google, Inc.
  3. *
  4. * This is part of HarfBuzz, a text shaping library.
  5. *
  6. * Permission is hereby granted, without written agreement and without
  7. * license or royalty fees, to use, copy, modify, and distribute this
  8. * software and its documentation for any purpose, provided that the
  9. * above copyright notice and the following two paragraphs appear in
  10. * all copies of this software.
  11. *
  12. * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
  13. * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
  14. * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
  15. * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
  16. * DAMAGE.
  17. *
  18. * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
  19. * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  20. * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
  21. * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
  22. * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  23. *
  24. * Google Author(s): Behdad Esfahbod
  25. */
  26. #ifndef HB_OT_NAME_TABLE_HH
  27. #define HB_OT_NAME_TABLE_HH
  28. #include "hb-open-type.hh"
  29. #include "hb-ot-name-language.hh"
  30. #include "hb-aat-layout.hh"
  31. #include "hb-utf.hh"
  32. namespace OT {
  33. template <typename in_utf_t, typename out_utf_t>
  34. inline unsigned int
  35. hb_ot_name_convert_utf (hb_bytes_t bytes,
  36. unsigned int *text_size /* IN/OUT */,
  37. typename out_utf_t::codepoint_t *text /* OUT */)
  38. {
  39. unsigned int src_len = bytes.length / sizeof (typename in_utf_t::codepoint_t);
  40. const typename in_utf_t::codepoint_t *src = (const typename in_utf_t::codepoint_t *) bytes.arrayZ;
  41. const typename in_utf_t::codepoint_t *src_end = src + src_len;
  42. typename out_utf_t::codepoint_t *dst = text;
  43. hb_codepoint_t unicode;
  44. const hb_codepoint_t replacement = HB_BUFFER_REPLACEMENT_CODEPOINT_DEFAULT;
  45. if (text_size && *text_size)
  46. {
  47. (*text_size)--; /* Save room for NUL-termination. */
  48. const typename out_utf_t::codepoint_t *dst_end = text + *text_size;
  49. while (src < src_end && dst < dst_end)
  50. {
  51. const typename in_utf_t::codepoint_t *src_next = in_utf_t::next (src, src_end, &unicode, replacement);
  52. typename out_utf_t::codepoint_t *dst_next = out_utf_t::encode (dst, dst_end, unicode);
  53. if (dst_next == dst)
  54. break; /* Out-of-room. */
  55. dst = dst_next;
  56. src = src_next;
  57. }
  58. *text_size = dst - text;
  59. *dst = 0; /* NUL-terminate. */
  60. }
  61. /* Accumulate length of rest. */
  62. unsigned int dst_len = dst - text;
  63. while (src < src_end)
  64. {
  65. src = in_utf_t::next (src, src_end, &unicode, replacement);
  66. dst_len += out_utf_t::encode_len (unicode);
  67. }
  68. return dst_len;
  69. }
  70. #define entry_score var.u16[0]
  71. #define entry_index var.u16[1]
  72. /*
  73. * name -- Naming
  74. * https://docs.microsoft.com/en-us/typography/opentype/spec/name
  75. */
  76. #define HB_OT_TAG_name HB_TAG('n','a','m','e')
  77. #define UNSUPPORTED 42
  78. struct NameRecord
  79. {
  80. hb_language_t language (hb_face_t *face) const
  81. {
  82. #ifndef HB_NO_OT_NAME_LANGUAGE
  83. unsigned int p = platformID;
  84. unsigned int l = languageID;
  85. if (p == 3)
  86. return _hb_ot_name_language_for_ms_code (l);
  87. if (p == 1)
  88. return _hb_ot_name_language_for_mac_code (l);
  89. #ifndef HB_NO_OT_NAME_LANGUAGE_AAT
  90. if (p == 0)
  91. return face->table.ltag->get_language (l);
  92. #endif
  93. #endif
  94. return HB_LANGUAGE_INVALID;
  95. }
  96. uint16_t score () const
  97. {
  98. /* Same order as in cmap::find_best_subtable(). */
  99. unsigned int p = platformID;
  100. unsigned int e = encodingID;
  101. /* 32-bit. */
  102. if (p == 3 && e == 10) return 0;
  103. if (p == 0 && e == 6) return 1;
  104. if (p == 0 && e == 4) return 2;
  105. /* 16-bit. */
  106. if (p == 3 && e == 1) return 3;
  107. if (p == 0 && e == 3) return 4;
  108. if (p == 0 && e == 2) return 5;
  109. if (p == 0 && e == 1) return 6;
  110. if (p == 0 && e == 0) return 7;
  111. /* Symbol. */
  112. if (p == 3 && e == 0) return 8;
  113. /* We treat all Mac Latin names as ASCII only. */
  114. if (p == 1 && e == 0) return 10; /* 10 is magic number :| */
  115. return UNSUPPORTED;
  116. }
  117. NameRecord* copy (hb_serialize_context_t *c, const void *base
  118. #ifdef HB_EXPERIMENTAL_API
  119. , const hb_hashmap_t<hb_ot_name_record_ids_t, hb_bytes_t> *name_table_overrides
  120. #endif
  121. ) const
  122. {
  123. TRACE_SERIALIZE (this);
  124. HB_UNUSED auto snap = c->snapshot ();
  125. auto *out = c->embed (this);
  126. if (unlikely (!out)) return_trace (nullptr);
  127. #ifdef HB_EXPERIMENTAL_API
  128. hb_ot_name_record_ids_t record_ids (platformID, encodingID, languageID, nameID);
  129. hb_bytes_t* name_bytes;
  130. if (name_table_overrides->has (record_ids, &name_bytes)) {
  131. hb_bytes_t encoded_bytes = *name_bytes;
  132. char *name_str_utf16_be = nullptr;
  133. if (platformID != 1)
  134. {
  135. unsigned text_size = hb_ot_name_convert_utf<hb_utf8_t, hb_utf16_be_t> (*name_bytes, nullptr, nullptr);
  136. text_size++; // needs to consider NULL terminator for use in hb_ot_name_convert_utf()
  137. unsigned byte_len = text_size * hb_utf16_be_t::codepoint_t::static_size;
  138. name_str_utf16_be = (char *) hb_calloc (byte_len, 1);
  139. if (!name_str_utf16_be)
  140. {
  141. c->revert (snap);
  142. return_trace (nullptr);
  143. }
  144. hb_ot_name_convert_utf<hb_utf8_t, hb_utf16_be_t> (*name_bytes, &text_size,
  145. (hb_utf16_be_t::codepoint_t *) name_str_utf16_be);
  146. unsigned encoded_byte_len = text_size * hb_utf16_be_t::codepoint_t::static_size;
  147. if (!encoded_byte_len || !c->check_assign (out->length, encoded_byte_len, HB_SERIALIZE_ERROR_INT_OVERFLOW)) {
  148. c->revert (snap);
  149. hb_free (name_str_utf16_be);
  150. return_trace (nullptr);
  151. }
  152. encoded_bytes = hb_bytes_t (name_str_utf16_be, encoded_byte_len);
  153. }
  154. else
  155. {
  156. // mac platform, copy the UTF-8 string(all ascii characters) as is
  157. if (!c->check_assign (out->length, encoded_bytes.length, HB_SERIALIZE_ERROR_INT_OVERFLOW)) {
  158. c->revert (snap);
  159. return_trace (nullptr);
  160. }
  161. }
  162. out->offset = 0;
  163. c->push ();
  164. encoded_bytes.copy (c);
  165. c->add_link (out->offset, c->pop_pack (), hb_serialize_context_t::Tail, 0);
  166. hb_free (name_str_utf16_be);
  167. }
  168. else
  169. #endif
  170. {
  171. out->offset.serialize_copy (c, offset, base, 0, hb_serialize_context_t::Tail, length);
  172. }
  173. return_trace (out);
  174. }
  175. bool isUnicode () const
  176. {
  177. unsigned int p = platformID;
  178. unsigned int e = encodingID;
  179. return (p == 0 ||
  180. (p == 3 && (e == 0 || e == 1 || e == 10)));
  181. }
  182. static int cmp (const void *pa, const void *pb)
  183. {
  184. const NameRecord *a = (const NameRecord *)pa;
  185. const NameRecord *b = (const NameRecord *)pb;
  186. if (a->platformID != b->platformID)
  187. return a->platformID - b->platformID;
  188. if (a->encodingID != b->encodingID)
  189. return a->encodingID - b->encodingID;
  190. if (a->languageID != b->languageID)
  191. return a->languageID - b->languageID;
  192. if (a->nameID != b->nameID)
  193. return a->nameID - b->nameID;
  194. if (a->length != b->length)
  195. return a->length - b->length;
  196. return 0;
  197. }
  198. bool sanitize (hb_sanitize_context_t *c, const void *base) const
  199. {
  200. TRACE_SANITIZE (this);
  201. return_trace (c->check_struct (this) && offset.sanitize (c, base, length));
  202. }
  203. HBUINT16 platformID; /* Platform ID. */
  204. HBUINT16 encodingID; /* Platform-specific encoding ID. */
  205. HBUINT16 languageID; /* Language ID. */
  206. HBUINT16 nameID; /* Name ID. */
  207. HBUINT16 length; /* String length (in bytes). */
  208. NNOffset16To<UnsizedArrayOf<HBUINT8>>
  209. offset; /* String offset from start of storage area (in bytes). */
  210. public:
  211. DEFINE_SIZE_STATIC (12);
  212. };
  213. static int
  214. _hb_ot_name_entry_cmp_key (const void *pa, const void *pb, bool exact)
  215. {
  216. const hb_ot_name_entry_t *a = (const hb_ot_name_entry_t *) pa;
  217. const hb_ot_name_entry_t *b = (const hb_ot_name_entry_t *) pb;
  218. /* Compare by name_id, then language. */
  219. if (a->name_id != b->name_id)
  220. return a->name_id - b->name_id;
  221. if (a->language == b->language) return 0;
  222. if (!a->language) return -1;
  223. if (!b->language) return +1;
  224. const char *astr = hb_language_to_string (a->language);
  225. const char *bstr = hb_language_to_string (b->language);
  226. signed c = strcmp (astr, bstr);
  227. // 'a' is the user request, and 'b' is string in the font.
  228. // If eg. user asks for "en-us" and font has "en", approve.
  229. if (!exact && c &&
  230. hb_language_matches (b->language, a->language))
  231. return 0;
  232. return c;
  233. }
  234. static int
  235. _hb_ot_name_entry_cmp (const void *pa, const void *pb)
  236. {
  237. /* Compare by name_id, then language, then score, then index. */
  238. int v = _hb_ot_name_entry_cmp_key (pa, pb, true);
  239. if (v)
  240. return v;
  241. const hb_ot_name_entry_t *a = (const hb_ot_name_entry_t *) pa;
  242. const hb_ot_name_entry_t *b = (const hb_ot_name_entry_t *) pb;
  243. if (a->entry_score != b->entry_score)
  244. return a->entry_score - b->entry_score;
  245. if (a->entry_index != b->entry_index)
  246. return a->entry_index - b->entry_index;
  247. return 0;
  248. }
  249. struct name
  250. {
  251. static constexpr hb_tag_t tableTag = HB_OT_TAG_name;
  252. unsigned int get_size () const
  253. { return min_size + count * nameRecordZ.item_size; }
  254. template <typename Iterator,
  255. hb_requires (hb_is_source_of (Iterator, const NameRecord &))>
  256. bool serialize (hb_serialize_context_t *c,
  257. Iterator it,
  258. const void *src_string_pool
  259. #ifdef HB_EXPERIMENTAL_API
  260. , const hb_vector_t<hb_ot_name_record_ids_t>& insert_name_records
  261. , const hb_hashmap_t<hb_ot_name_record_ids_t, hb_bytes_t> *name_table_overrides
  262. #endif
  263. )
  264. {
  265. TRACE_SERIALIZE (this);
  266. if (unlikely (!c->extend_min ((*this)))) return_trace (false);
  267. unsigned total_count = it.len ()
  268. #ifdef HB_EXPERIMENTAL_API
  269. + insert_name_records.length
  270. #endif
  271. ;
  272. this->format = 0;
  273. if (!c->check_assign (this->count, total_count, HB_SERIALIZE_ERROR_INT_OVERFLOW))
  274. return false;
  275. NameRecord *name_records = (NameRecord *) hb_calloc (total_count, NameRecord::static_size);
  276. if (unlikely (!name_records)) return_trace (false);
  277. hb_array_t<NameRecord> records (name_records, total_count);
  278. for (const NameRecord& record : it)
  279. {
  280. hb_memcpy (name_records, &record, NameRecord::static_size);
  281. name_records++;
  282. }
  283. #ifdef HB_EXPERIMENTAL_API
  284. for (unsigned i = 0; i < insert_name_records.length; i++)
  285. {
  286. const hb_ot_name_record_ids_t& ids = insert_name_records[i];
  287. NameRecord record;
  288. record.platformID = ids.platform_id;
  289. record.encodingID = ids.encoding_id;
  290. record.languageID = ids.language_id;
  291. record.nameID = ids.name_id;
  292. record.length = 0; // handled in NameRecord copy()
  293. record.offset = 0;
  294. memcpy (name_records, &record, NameRecord::static_size);
  295. name_records++;
  296. }
  297. #endif
  298. records.qsort ();
  299. c->copy_all (records,
  300. src_string_pool
  301. #ifdef HB_EXPERIMENTAL_API
  302. , name_table_overrides
  303. #endif
  304. );
  305. hb_free (records.arrayZ);
  306. if (unlikely (c->ran_out_of_room ())) return_trace (false);
  307. this->stringOffset = c->length ();
  308. return_trace (true);
  309. }
  310. bool subset (hb_subset_context_t *c) const
  311. {
  312. TRACE_SUBSET (this);
  313. name *name_prime = c->serializer->start_embed<name> ();
  314. if (unlikely (!name_prime)) return_trace (false);
  315. #ifdef HB_EXPERIMENTAL_API
  316. const hb_hashmap_t<hb_ot_name_record_ids_t, hb_bytes_t> *name_table_overrides =
  317. c->plan->name_table_overrides;
  318. #endif
  319. auto it =
  320. + nameRecordZ.as_array (count)
  321. | hb_filter (c->plan->name_ids, &NameRecord::nameID)
  322. | hb_filter (c->plan->name_languages, &NameRecord::languageID)
  323. | hb_filter ([&] (const NameRecord& namerecord) {
  324. return
  325. (c->plan->flags & HB_SUBSET_FLAGS_NAME_LEGACY)
  326. || namerecord.isUnicode ();
  327. })
  328. #ifdef HB_EXPERIMENTAL_API
  329. | hb_filter ([&] (const NameRecord& namerecord) {
  330. if (name_table_overrides->is_empty ())
  331. return true;
  332. hb_ot_name_record_ids_t rec_ids (namerecord.platformID,
  333. namerecord.encodingID,
  334. namerecord.languageID,
  335. namerecord.nameID);
  336. hb_bytes_t *p;
  337. if (name_table_overrides->has (rec_ids, &p) &&
  338. (*p).length == 0)
  339. return false;
  340. return true;
  341. })
  342. #endif
  343. ;
  344. #ifdef HB_EXPERIMENTAL_API
  345. hb_vector_t<hb_ot_name_record_ids_t> insert_name_records;
  346. if (!name_table_overrides->is_empty ())
  347. {
  348. if (unlikely (!insert_name_records.alloc (name_table_overrides->get_population ())))
  349. return_trace (false);
  350. for (const auto& record_ids : name_table_overrides->keys ())
  351. {
  352. if (name_table_overrides->get (record_ids).length == 0)
  353. continue;
  354. if (has_name_record_with_ids (record_ids))
  355. continue;
  356. insert_name_records.push (record_ids);
  357. }
  358. }
  359. #endif
  360. return (name_prime->serialize (c->serializer, it,
  361. std::addressof (this + stringOffset)
  362. #ifdef HB_EXPERIMENTAL_API
  363. , insert_name_records
  364. , name_table_overrides
  365. #endif
  366. ));
  367. }
  368. bool sanitize_records (hb_sanitize_context_t *c) const
  369. {
  370. TRACE_SANITIZE (this);
  371. const void *string_pool = (this+stringOffset).arrayZ;
  372. return_trace (nameRecordZ.sanitize (c, count, string_pool));
  373. }
  374. bool sanitize (hb_sanitize_context_t *c) const
  375. {
  376. TRACE_SANITIZE (this);
  377. return_trace (c->check_struct (this) &&
  378. likely (format == 0 || format == 1) &&
  379. c->check_array (nameRecordZ.arrayZ, count) &&
  380. c->check_range (this, stringOffset) &&
  381. sanitize_records (c));
  382. }
  383. struct accelerator_t
  384. {
  385. accelerator_t (hb_face_t *face)
  386. {
  387. this->table = hb_sanitize_context_t ().reference_table<name> (face);
  388. assert (this->table.get_length () >= this->table->stringOffset);
  389. this->pool = (const char *) (const void *) (this->table+this->table->stringOffset);
  390. this->pool_len = this->table.get_length () - this->table->stringOffset;
  391. const hb_array_t<const NameRecord> all_names (this->table->nameRecordZ.arrayZ,
  392. this->table->count);
  393. this->names.alloc (all_names.length);
  394. for (unsigned int i = 0; i < all_names.length; i++)
  395. {
  396. hb_ot_name_entry_t *entry = this->names.push ();
  397. entry->name_id = all_names[i].nameID;
  398. entry->language = all_names[i].language (face);
  399. entry->entry_score = all_names[i].score ();
  400. entry->entry_index = i;
  401. }
  402. this->names.qsort (_hb_ot_name_entry_cmp);
  403. /* Walk and pick best only for each name_id,language pair,
  404. * while dropping unsupported encodings. */
  405. unsigned int j = 0;
  406. for (unsigned int i = 0; i < this->names.length; i++)
  407. {
  408. if (this->names[i].entry_score == UNSUPPORTED ||
  409. this->names[i].language == HB_LANGUAGE_INVALID)
  410. continue;
  411. if (i &&
  412. this->names[i - 1].name_id == this->names[i].name_id &&
  413. this->names[i - 1].language == this->names[i].language)
  414. continue;
  415. this->names[j++] = this->names[i];
  416. }
  417. this->names.resize (j);
  418. }
  419. ~accelerator_t ()
  420. {
  421. this->table.destroy ();
  422. }
  423. int get_index (hb_ot_name_id_t name_id,
  424. hb_language_t language,
  425. unsigned int *width=nullptr) const
  426. {
  427. const hb_ot_name_entry_t key = {name_id, {0}, language};
  428. const hb_ot_name_entry_t *entry = hb_bsearch (key, (const hb_ot_name_entry_t *) this->names,
  429. this->names.length,
  430. sizeof (hb_ot_name_entry_t),
  431. _hb_ot_name_entry_cmp_key,
  432. true);
  433. if (!entry)
  434. {
  435. entry = hb_bsearch (key, (const hb_ot_name_entry_t *) this->names,
  436. this->names.length,
  437. sizeof (hb_ot_name_entry_t),
  438. _hb_ot_name_entry_cmp_key,
  439. false);
  440. }
  441. if (!entry)
  442. return -1;
  443. if (width)
  444. *width = entry->entry_score < 10 ? 2 : 1;
  445. return entry->entry_index;
  446. }
  447. hb_bytes_t get_name (unsigned int idx) const
  448. {
  449. const hb_array_t<const NameRecord> all_names (table->nameRecordZ.arrayZ, table->count);
  450. const NameRecord &record = all_names[idx];
  451. const hb_bytes_t string_pool (pool, pool_len);
  452. return string_pool.sub_array (record.offset, record.length);
  453. }
  454. private:
  455. const char *pool;
  456. unsigned int pool_len;
  457. public:
  458. hb_blob_ptr_t<name> table;
  459. hb_vector_t<hb_ot_name_entry_t> names;
  460. };
  461. private:
  462. // sometimes NameRecords are not sorted in the font file, so use linear search
  463. // here
  464. bool has_name_record_with_ids (const hb_ot_name_record_ids_t& record_ids) const
  465. {
  466. for (const auto& record : nameRecordZ.as_array (count))
  467. {
  468. if (record.platformID == record_ids.platform_id &&
  469. record.encodingID == record_ids.encoding_id &&
  470. record.languageID == record_ids.language_id &&
  471. record.nameID == record_ids.name_id)
  472. return true;
  473. }
  474. return false;
  475. }
  476. public:
  477. /* We only implement format 0 for now. */
  478. HBUINT16 format; /* Format selector (=0/1). */
  479. HBUINT16 count; /* Number of name records. */
  480. NNOffset16To<UnsizedArrayOf<HBUINT8>>
  481. stringOffset; /* Offset to start of string storage (from start of table). */
  482. UnsizedArrayOf<NameRecord>
  483. nameRecordZ; /* The name records where count is the number of records. */
  484. public:
  485. DEFINE_SIZE_ARRAY (6, nameRecordZ);
  486. };
  487. #undef entry_index
  488. #undef entry_score
  489. struct name_accelerator_t : name::accelerator_t {
  490. name_accelerator_t (hb_face_t *face) : name::accelerator_t (face) {}
  491. };
  492. } /* namespace OT */
  493. #endif /* HB_OT_NAME_TABLE_HH */