| 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 | |
| 27 | #ifndef HB_OT_NAME_TABLE_HH |
| 28 | #define HB_OT_NAME_TABLE_HH |
| 29 | |
| 30 | #include "hb-open-type.hh" |
| 31 | #include "hb-ot-name-language.hh" |
| 32 | #include "hb-aat-layout.hh" |
| 33 | |
| 34 | |
| 35 | namespace OT { |
| 36 | |
| 37 | |
| 38 | #define entry_score var.u16[0] |
| 39 | #define entry_index var.u16[1] |
| 40 | |
| 41 | |
| 42 | /* |
| 43 | * name -- Naming |
| 44 | * https://docs.microsoft.com/en-us/typography/opentype/spec/name |
| 45 | */ |
| 46 | #define HB_OT_TAG_name HB_TAG('n','a','m','e') |
| 47 | |
| 48 | #define UNSUPPORTED 42 |
| 49 | |
| 50 | struct NameRecord |
| 51 | { |
| 52 | hb_language_t language (hb_face_t *face) const |
| 53 | { |
| 54 | #ifndef HB_NO_OT_NAME_LANGUAGE |
| 55 | unsigned int p = platformID; |
| 56 | unsigned int l = languageID; |
| 57 | |
| 58 | if (p == 3) |
| 59 | return _hb_ot_name_language_for_ms_code (l); |
| 60 | |
| 61 | if (p == 1) |
| 62 | return _hb_ot_name_language_for_mac_code (l); |
| 63 | |
| 64 | #ifndef HB_NO_OT_NAME_LANGUAGE_AAT |
| 65 | if (p == 0) |
| 66 | return face->table.ltag->get_language (l); |
| 67 | #endif |
| 68 | |
| 69 | #endif |
| 70 | return HB_LANGUAGE_INVALID; |
| 71 | } |
| 72 | |
| 73 | uint16_t score () const |
| 74 | { |
| 75 | /* Same order as in cmap::find_best_subtable(). */ |
| 76 | unsigned int p = platformID; |
| 77 | unsigned int e = encodingID; |
| 78 | |
| 79 | /* 32-bit. */ |
| 80 | if (p == 3 && e == 10) return 0; |
| 81 | if (p == 0 && e == 6) return 1; |
| 82 | if (p == 0 && e == 4) return 2; |
| 83 | |
| 84 | /* 16-bit. */ |
| 85 | if (p == 3 && e == 1) return 3; |
| 86 | if (p == 0 && e == 3) return 4; |
| 87 | if (p == 0 && e == 2) return 5; |
| 88 | if (p == 0 && e == 1) return 6; |
| 89 | if (p == 0 && e == 0) return 7; |
| 90 | |
| 91 | /* Symbol. */ |
| 92 | if (p == 3 && e == 0) return 8; |
| 93 | |
| 94 | /* We treat all Mac Latin names as ASCII only. */ |
| 95 | if (p == 1 && e == 0) return 10; /* 10 is magic number :| */ |
| 96 | |
| 97 | return UNSUPPORTED; |
| 98 | } |
| 99 | |
| 100 | NameRecord* copy (hb_serialize_context_t *c, const void *base) const |
| 101 | { |
| 102 | TRACE_SERIALIZE (this); |
| 103 | auto *out = c->embed (this); |
| 104 | if (unlikely (!out)) return_trace (nullptr); |
| 105 | out->offset.serialize_copy (c, offset, base, 0, hb_serialize_context_t::Tail, length); |
| 106 | return_trace (out); |
| 107 | } |
| 108 | |
| 109 | bool isUnicode () const |
| 110 | { |
| 111 | unsigned int p = platformID; |
| 112 | unsigned int e = encodingID; |
| 113 | |
| 114 | return (p == 0 || |
| 115 | (p == 3 && (e == 0 || e == 1 || e == 10))); |
| 116 | } |
| 117 | |
| 118 | static int cmp (const void *pa, const void *pb) |
| 119 | { |
| 120 | const NameRecord *a = (const NameRecord *)pa; |
| 121 | const NameRecord *b = (const NameRecord *)pb; |
| 122 | |
| 123 | if (a->platformID != b->platformID) |
| 124 | return a->platformID - b->platformID; |
| 125 | |
| 126 | if (a->encodingID != b->encodingID) |
| 127 | return a->encodingID - b->encodingID; |
| 128 | |
| 129 | if (a->languageID != b->languageID) |
| 130 | return a->languageID - b->languageID; |
| 131 | |
| 132 | if (a->nameID != b->nameID) |
| 133 | return a->nameID - b->nameID; |
| 134 | |
| 135 | if (a->length != b->length) |
| 136 | return a->length - b->length; |
| 137 | |
| 138 | return 0; |
| 139 | } |
| 140 | |
| 141 | bool sanitize (hb_sanitize_context_t *c, const void *base) const |
| 142 | { |
| 143 | TRACE_SANITIZE (this); |
| 144 | return_trace (c->check_struct (this) && offset.sanitize (c, base, length)); |
| 145 | } |
| 146 | |
| 147 | HBUINT16 platformID; /* Platform ID. */ |
| 148 | HBUINT16 encodingID; /* Platform-specific encoding ID. */ |
| 149 | HBUINT16 languageID; /* Language ID. */ |
| 150 | HBUINT16 nameID; /* Name ID. */ |
| 151 | HBUINT16 length; /* String length (in bytes). */ |
| 152 | NNOffsetTo<UnsizedArrayOf<HBUINT8>> |
| 153 | offset; /* String offset from start of storage area (in bytes). */ |
| 154 | public: |
| 155 | DEFINE_SIZE_STATIC (12); |
| 156 | }; |
| 157 | |
| 158 | static int |
| 159 | _hb_ot_name_entry_cmp_key (const void *pa, const void *pb) |
| 160 | { |
| 161 | const hb_ot_name_entry_t *a = (const hb_ot_name_entry_t *) pa; |
| 162 | const hb_ot_name_entry_t *b = (const hb_ot_name_entry_t *) pb; |
| 163 | |
| 164 | /* Compare by name_id, then language. */ |
| 165 | |
| 166 | if (a->name_id != b->name_id) |
| 167 | return a->name_id - b->name_id; |
| 168 | |
| 169 | if (a->language == b->language) return 0; |
| 170 | if (!a->language) return -1; |
| 171 | if (!b->language) return +1; |
| 172 | return strcmp (hb_language_to_string (a->language), |
| 173 | hb_language_to_string (b->language)); |
| 174 | } |
| 175 | |
| 176 | static int |
| 177 | _hb_ot_name_entry_cmp (const void *pa, const void *pb) |
| 178 | { |
| 179 | /* Compare by name_id, then language, then score, then index. */ |
| 180 | |
| 181 | int v = _hb_ot_name_entry_cmp_key (pa, pb); |
| 182 | if (v) |
| 183 | return v; |
| 184 | |
| 185 | const hb_ot_name_entry_t *a = (const hb_ot_name_entry_t *) pa; |
| 186 | const hb_ot_name_entry_t *b = (const hb_ot_name_entry_t *) pb; |
| 187 | |
| 188 | if (a->entry_score != b->entry_score) |
| 189 | return a->entry_score - b->entry_score; |
| 190 | |
| 191 | if (a->entry_index != b->entry_index) |
| 192 | return a->entry_index - b->entry_index; |
| 193 | |
| 194 | return 0; |
| 195 | } |
| 196 | |
| 197 | struct name |
| 198 | { |
| 199 | static constexpr hb_tag_t tableTag = HB_OT_TAG_name; |
| 200 | |
| 201 | unsigned int get_size () const |
| 202 | { return min_size + count * nameRecordZ.item_size; } |
| 203 | |
| 204 | template <typename Iterator, |
| 205 | hb_requires (hb_is_source_of (Iterator, const NameRecord &))> |
| 206 | bool serialize (hb_serialize_context_t *c, |
| 207 | Iterator it, |
| 208 | const void *src_string_pool) |
| 209 | { |
| 210 | TRACE_SERIALIZE (this); |
| 211 | |
| 212 | if (unlikely (!c->extend_min ((*this)))) return_trace (false); |
| 213 | |
| 214 | this->format = 0; |
| 215 | this->count = it.len (); |
| 216 | |
| 217 | NameRecord *name_records = (NameRecord *) calloc (it.len (), NameRecord::static_size); |
| 218 | hb_array_t<NameRecord> records (name_records, it.len ()); |
| 219 | |
| 220 | for (const NameRecord& record : it) |
| 221 | { |
| 222 | memcpy (name_records, &record, NameRecord::static_size); |
| 223 | name_records++; |
| 224 | } |
| 225 | |
| 226 | records.qsort (); |
| 227 | |
| 228 | c->copy_all (records, src_string_pool); |
| 229 | free (records.arrayZ); |
| 230 | |
| 231 | if (unlikely (c->ran_out_of_room)) return_trace (false); |
| 232 | |
| 233 | this->stringOffset = c->length (); |
| 234 | |
| 235 | return_trace (true); |
| 236 | } |
| 237 | |
| 238 | bool subset (hb_subset_context_t *c) const |
| 239 | { |
| 240 | TRACE_SUBSET (this); |
| 241 | |
| 242 | name *name_prime = c->serializer->start_embed<name> (); |
| 243 | if (unlikely (!name_prime)) return_trace (false); |
| 244 | |
| 245 | auto it = |
| 246 | + nameRecordZ.as_array (count) |
| 247 | | hb_filter (c->plan->name_ids, &NameRecord::nameID) |
| 248 | | hb_filter (c->plan->name_languages, &NameRecord::languageID) |
| 249 | | hb_filter ([&] (const NameRecord& namerecord) { return c->plan->name_legacy || namerecord.isUnicode (); }) |
| 250 | ; |
| 251 | |
| 252 | name_prime->serialize (c->serializer, it, hb_addressof (this + stringOffset)); |
| 253 | return_trace (name_prime->count); |
| 254 | } |
| 255 | |
| 256 | bool sanitize_records (hb_sanitize_context_t *c) const |
| 257 | { |
| 258 | TRACE_SANITIZE (this); |
| 259 | const void *string_pool = (this+stringOffset).arrayZ; |
| 260 | return_trace (nameRecordZ.sanitize (c, count, string_pool)); |
| 261 | } |
| 262 | |
| 263 | bool sanitize (hb_sanitize_context_t *c) const |
| 264 | { |
| 265 | TRACE_SANITIZE (this); |
| 266 | return_trace (c->check_struct (this) && |
| 267 | likely (format == 0 || format == 1) && |
| 268 | c->check_array (nameRecordZ.arrayZ, count) && |
| 269 | c->check_range (this, stringOffset) && |
| 270 | sanitize_records (c)); |
| 271 | } |
| 272 | |
| 273 | struct accelerator_t |
| 274 | { |
| 275 | void init (hb_face_t *face) |
| 276 | { |
| 277 | this->table = hb_sanitize_context_t ().reference_table<name> (face); |
| 278 | assert (this->table.get_length () >= this->table->stringOffset); |
| 279 | this->pool = (const char *) (const void *) (this->table+this->table->stringOffset); |
| 280 | this->pool_len = this->table.get_length () - this->table->stringOffset; |
| 281 | const hb_array_t<const NameRecord> all_names (this->table->nameRecordZ.arrayZ, |
| 282 | this->table->count); |
| 283 | |
| 284 | this->names.init (); |
| 285 | this->names.alloc (all_names.length); |
| 286 | |
| 287 | for (unsigned int i = 0; i < all_names.length; i++) |
| 288 | { |
| 289 | hb_ot_name_entry_t *entry = this->names.push (); |
| 290 | |
| 291 | entry->name_id = all_names[i].nameID; |
| 292 | entry->language = all_names[i].language (face); |
| 293 | entry->entry_score = all_names[i].score (); |
| 294 | entry->entry_index = i; |
| 295 | } |
| 296 | |
| 297 | this->names.qsort (_hb_ot_name_entry_cmp); |
| 298 | /* Walk and pick best only for each name_id,language pair, |
| 299 | * while dropping unsupported encodings. */ |
| 300 | unsigned int j = 0; |
| 301 | for (unsigned int i = 0; i < this->names.length; i++) |
| 302 | { |
| 303 | if (this->names[i].entry_score == UNSUPPORTED || |
| 304 | this->names[i].language == HB_LANGUAGE_INVALID) |
| 305 | continue; |
| 306 | if (i && |
| 307 | this->names[i - 1].name_id == this->names[i].name_id && |
| 308 | this->names[i - 1].language == this->names[i].language) |
| 309 | continue; |
| 310 | this->names[j++] = this->names[i]; |
| 311 | } |
| 312 | this->names.resize (j); |
| 313 | } |
| 314 | |
| 315 | void fini () |
| 316 | { |
| 317 | this->names.fini (); |
| 318 | this->table.destroy (); |
| 319 | } |
| 320 | |
| 321 | int get_index (hb_ot_name_id_t name_id, |
| 322 | hb_language_t language, |
| 323 | unsigned int *width=nullptr) const |
| 324 | { |
| 325 | const hb_ot_name_entry_t key = {name_id, {0}, language}; |
| 326 | const hb_ot_name_entry_t *entry = hb_bsearch (key, (const hb_ot_name_entry_t *) this->names, |
| 327 | this->names.length, |
| 328 | sizeof (hb_ot_name_entry_t), |
| 329 | _hb_ot_name_entry_cmp_key); |
| 330 | if (!entry) |
| 331 | return -1; |
| 332 | |
| 333 | if (width) |
| 334 | *width = entry->entry_score < 10 ? 2 : 1; |
| 335 | |
| 336 | return entry->entry_index; |
| 337 | } |
| 338 | |
| 339 | hb_bytes_t get_name (unsigned int idx) const |
| 340 | { |
| 341 | const hb_array_t<const NameRecord> all_names (table->nameRecordZ.arrayZ, table->count); |
| 342 | const NameRecord &record = all_names[idx]; |
| 343 | const hb_bytes_t string_pool (pool, pool_len); |
| 344 | return string_pool.sub_array (record.offset, record.length); |
| 345 | } |
| 346 | |
| 347 | private: |
| 348 | const char *pool; |
| 349 | unsigned int pool_len; |
| 350 | public: |
| 351 | hb_blob_ptr_t<name> table; |
| 352 | hb_vector_t<hb_ot_name_entry_t> names; |
| 353 | }; |
| 354 | |
| 355 | /* We only implement format 0 for now. */ |
| 356 | HBUINT16 format; /* Format selector (=0/1). */ |
| 357 | HBUINT16 count; /* Number of name records. */ |
| 358 | NNOffsetTo<UnsizedArrayOf<HBUINT8>> |
| 359 | stringOffset; /* Offset to start of string storage (from start of table). */ |
| 360 | UnsizedArrayOf<NameRecord> |
| 361 | nameRecordZ; /* The name records where count is the number of records. */ |
| 362 | public: |
| 363 | DEFINE_SIZE_ARRAY (6, nameRecordZ); |
| 364 | }; |
| 365 | |
| 366 | #undef entry_index |
| 367 | #undef entry_score |
| 368 | |
| 369 | struct name_accelerator_t : name::accelerator_t {}; |
| 370 | |
| 371 | } /* namespace OT */ |
| 372 | |
| 373 | |
| 374 | #endif /* HB_OT_NAME_TABLE_HH */ |
| 375 | |