Merge branch 'upstream' into tizen
[platform/upstream/harfbuzz.git] / src / OT / name / name.hh
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 OT_NAME_NAME_HH
28 #define OT_NAME_NAME_HH
29
30 #include "../../hb-open-type.hh"
31 #include "../../hb-ot-name-language.hh"
32 #include "../../hb-aat-layout.hh"
33 #include "../../hb-utf.hh"
34
35
36 namespace OT {
37
38 template <typename in_utf_t, typename out_utf_t>
39 inline unsigned int
40 hb_ot_name_convert_utf (hb_bytes_t                       bytes,
41                         unsigned int                    *text_size /* IN/OUT */,
42                         typename out_utf_t::codepoint_t *text /* OUT */)
43 {
44   unsigned int src_len = bytes.length / sizeof (typename in_utf_t::codepoint_t);
45   const typename in_utf_t::codepoint_t *src = (const typename in_utf_t::codepoint_t *) bytes.arrayZ;
46   const typename in_utf_t::codepoint_t *src_end = src + src_len;
47
48   typename out_utf_t::codepoint_t *dst = text;
49
50   hb_codepoint_t unicode;
51   const hb_codepoint_t replacement = HB_BUFFER_REPLACEMENT_CODEPOINT_DEFAULT;
52
53   if (text_size && *text_size)
54   {
55     (*text_size)--; /* Save room for NUL-termination. */
56     const typename out_utf_t::codepoint_t *dst_end = text + *text_size;
57
58     while (src < src_end && dst < dst_end)
59     {
60       const typename in_utf_t::codepoint_t *src_next = in_utf_t::next (src, src_end, &unicode, replacement);
61       typename out_utf_t::codepoint_t *dst_next = out_utf_t::encode (dst, dst_end, unicode);
62       if (dst_next == dst)
63         break; /* Out-of-room. */
64
65       dst = dst_next;
66       src = src_next;
67     }
68
69     *text_size = dst - text;
70     *dst = 0; /* NUL-terminate. */
71   }
72
73   /* Accumulate length of rest. */
74   unsigned int dst_len = dst - text;
75   while (src < src_end)
76   {
77     src = in_utf_t::next (src, src_end, &unicode, replacement);
78     dst_len += out_utf_t::encode_len (unicode);
79   }
80   return dst_len;
81 }
82
83 #define entry_score var.u16[0]
84 #define entry_index var.u16[1]
85
86
87 /*
88  * name -- Naming
89  * https://docs.microsoft.com/en-us/typography/opentype/spec/name
90  */
91 #define HB_OT_TAG_name HB_TAG('n','a','m','e')
92
93 #define UNSUPPORTED     42
94
95 struct NameRecord
96 {
97   hb_language_t language (hb_face_t *face) const
98   {
99 #ifndef HB_NO_OT_NAME_LANGUAGE
100     unsigned int p = platformID;
101     unsigned int l = languageID;
102
103     if (p == 3)
104       return _hb_ot_name_language_for_ms_code (l);
105
106     if (p == 1)
107       return _hb_ot_name_language_for_mac_code (l);
108
109 #ifndef HB_NO_OT_NAME_LANGUAGE_AAT
110     if (p == 0)
111       return face->table.ltag->get_language (l);
112 #endif
113
114 #endif
115     return HB_LANGUAGE_INVALID;
116   }
117
118   uint16_t score () const
119   {
120     /* Same order as in cmap::find_best_subtable(). */
121     unsigned int p = platformID;
122     unsigned int e = encodingID;
123
124     /* 32-bit. */
125     if (p == 3 && e == 10) return 0;
126     if (p == 0 && e ==  6) return 1;
127     if (p == 0 && e ==  4) return 2;
128
129     /* 16-bit. */
130     if (p == 3 && e ==  1) return 3;
131     if (p == 0 && e ==  3) return 4;
132     if (p == 0 && e ==  2) return 5;
133     if (p == 0 && e ==  1) return 6;
134     if (p == 0 && e ==  0) return 7;
135
136     /* Symbol. */
137     if (p == 3 && e ==  0) return 8;
138
139     /* We treat all Mac Latin names as ASCII only. */
140     if (p == 1 && e ==  0) return 10; /* 10 is magic number :| */
141
142     return UNSUPPORTED;
143   }
144
145   NameRecord* copy (hb_serialize_context_t *c, const void *base
146 #ifdef HB_EXPERIMENTAL_API
147                     , const hb_hashmap_t<hb_ot_name_record_ids_t, hb_bytes_t> *name_table_overrides
148 #endif
149                     ) const
150   {
151     TRACE_SERIALIZE (this);
152     HB_UNUSED auto snap = c->snapshot ();
153     auto *out = c->embed (this);
154     if (unlikely (!out)) return_trace (nullptr);
155 #ifdef HB_EXPERIMENTAL_API
156     hb_ot_name_record_ids_t record_ids (platformID, encodingID, languageID, nameID);
157     hb_bytes_t* name_bytes;
158
159     if (name_table_overrides->has (record_ids, &name_bytes)) {
160       hb_bytes_t encoded_bytes = *name_bytes;
161       char *name_str_utf16_be = nullptr;
162
163       if (platformID != 1)
164       {
165         unsigned text_size = hb_ot_name_convert_utf<hb_utf8_t, hb_utf16_be_t> (*name_bytes, nullptr, nullptr);
166   
167         text_size++; // needs to consider NULL terminator for use in hb_ot_name_convert_utf()
168         unsigned byte_len = text_size * hb_utf16_be_t::codepoint_t::static_size;
169         name_str_utf16_be = (char *) hb_calloc (byte_len, 1);
170         if (!name_str_utf16_be)
171         {
172           c->revert (snap);
173           return_trace (nullptr);
174         }
175         hb_ot_name_convert_utf<hb_utf8_t, hb_utf16_be_t> (*name_bytes, &text_size,
176                                                           (hb_utf16_be_t::codepoint_t *) name_str_utf16_be);
177   
178         unsigned encoded_byte_len = text_size * hb_utf16_be_t::codepoint_t::static_size;
179         if (!encoded_byte_len || !c->check_assign (out->length, encoded_byte_len, HB_SERIALIZE_ERROR_INT_OVERFLOW)) {
180           c->revert (snap);
181           hb_free (name_str_utf16_be);
182           return_trace (nullptr);
183         }
184   
185         encoded_bytes = hb_bytes_t (name_str_utf16_be, encoded_byte_len);
186       }
187       else
188       {
189         // mac platform, copy the UTF-8 string(all ascii characters) as is
190         if (!c->check_assign (out->length, encoded_bytes.length, HB_SERIALIZE_ERROR_INT_OVERFLOW)) {
191           c->revert (snap);
192           return_trace (nullptr);
193         }
194       }
195
196       out->offset = 0;
197       c->push ();
198       encoded_bytes.copy (c);
199       c->add_link (out->offset, c->pop_pack (), hb_serialize_context_t::Tail, 0);
200       hb_free (name_str_utf16_be);
201     }
202     else
203 #endif
204     {
205       out->offset.serialize_copy (c, offset, base, 0, hb_serialize_context_t::Tail, length);
206     }
207     return_trace (out);
208   }
209
210   bool isUnicode () const
211   {
212     unsigned int p = platformID;
213     unsigned int e = encodingID;
214
215     return (p == 0 ||
216             (p == 3 && (e == 0 || e == 1 || e == 10)));
217   }
218
219   static int cmp (const void *pa, const void *pb)
220   {
221     const NameRecord *a = (const NameRecord *)pa;
222     const NameRecord *b = (const NameRecord *)pb;
223
224     if (a->platformID != b->platformID)
225       return a->platformID - b->platformID;
226
227     if (a->encodingID != b->encodingID)
228       return a->encodingID - b->encodingID;
229
230     if (a->languageID != b->languageID)
231       return a->languageID - b->languageID;
232
233     if (a->nameID != b->nameID)
234       return a->nameID - b->nameID;
235
236     if (a->length != b->length)
237       return a->length - b->length;
238
239     return 0;
240   }
241
242   bool sanitize (hb_sanitize_context_t *c, const void *base) const
243   {
244     TRACE_SANITIZE (this);
245     return_trace (c->check_struct (this) && offset.sanitize (c, base, length));
246   }
247
248   HBUINT16      platformID;     /* Platform ID. */
249   HBUINT16      encodingID;     /* Platform-specific encoding ID. */
250   HBUINT16      languageID;     /* Language ID. */
251   HBUINT16      nameID;         /* Name ID. */
252   HBUINT16      length;         /* String length (in bytes). */
253   NNOffset16To<UnsizedArrayOf<HBUINT8>>
254                 offset;         /* String offset from start of storage area (in bytes). */
255   public:
256   DEFINE_SIZE_STATIC (12);
257 };
258
259 static int
260 _hb_ot_name_entry_cmp_key (const void *pa, const void *pb, bool exact)
261 {
262   const hb_ot_name_entry_t *a = (const hb_ot_name_entry_t *) pa;
263   const hb_ot_name_entry_t *b = (const hb_ot_name_entry_t *) pb;
264
265   /* Compare by name_id, then language. */
266
267   if (a->name_id != b->name_id)
268     return a->name_id - b->name_id;
269
270   if (a->language == b->language) return 0;
271   if (!a->language) return -1;
272   if (!b->language) return +1;
273
274   const char *astr = hb_language_to_string (a->language);
275   const char *bstr = hb_language_to_string (b->language);
276
277   signed c = strcmp (astr, bstr);
278
279   // 'a' is the user request, and 'b' is string in the font.
280   // If eg. user asks for "en-us" and font has "en", approve.
281   if (!exact && c &&
282       hb_language_matches (b->language, a->language))
283     return 0;
284
285   return c;
286 }
287
288 static int
289 _hb_ot_name_entry_cmp (const void *pa, const void *pb)
290 {
291   /* Compare by name_id, then language, then score, then index. */
292
293   int v = _hb_ot_name_entry_cmp_key (pa, pb, true);
294   if (v)
295     return v;
296
297   const hb_ot_name_entry_t *a = (const hb_ot_name_entry_t *) pa;
298   const hb_ot_name_entry_t *b = (const hb_ot_name_entry_t *) pb;
299
300   if (a->entry_score != b->entry_score)
301     return a->entry_score - b->entry_score;
302
303   if (a->entry_index != b->entry_index)
304     return a->entry_index - b->entry_index;
305
306   return 0;
307 }
308
309 struct name
310 {
311   static constexpr hb_tag_t tableTag = HB_OT_TAG_name;
312
313   unsigned int get_size () const
314   { return min_size + count * nameRecordZ.item_size; }
315
316   template <typename Iterator,
317             hb_requires (hb_is_source_of (Iterator, const NameRecord &))>
318   bool serialize (hb_serialize_context_t *c,
319                   Iterator it,
320                   const void *src_string_pool
321 #ifdef HB_EXPERIMENTAL_API
322                   , const hb_vector_t<hb_ot_name_record_ids_t>& insert_name_records
323                   , const hb_hashmap_t<hb_ot_name_record_ids_t, hb_bytes_t> *name_table_overrides
324 #endif
325                   )
326   {
327     TRACE_SERIALIZE (this);
328
329     if (unlikely (!c->extend_min ((*this))))  return_trace (false);
330
331     unsigned total_count = it.len ()
332 #ifdef HB_EXPERIMENTAL_API
333         + insert_name_records.length
334 #endif
335         ;
336     this->format = 0;
337     if (!c->check_assign (this->count, total_count, HB_SERIALIZE_ERROR_INT_OVERFLOW))
338       return false;
339
340     NameRecord *name_records = (NameRecord *) hb_calloc (total_count, NameRecord::static_size);
341     if (unlikely (!name_records)) return_trace (false);
342
343     hb_array_t<NameRecord> records (name_records, total_count);
344
345     for (const NameRecord& record : it)
346     {
347       hb_memcpy (name_records, &record, NameRecord::static_size);
348       name_records++;
349     }
350
351 #ifdef HB_EXPERIMENTAL_API
352     for (unsigned i = 0; i < insert_name_records.length; i++)
353     {
354       const hb_ot_name_record_ids_t& ids = insert_name_records[i];
355       NameRecord record;
356       record.platformID = ids.platform_id;
357       record.encodingID = ids.encoding_id;
358       record.languageID = ids.language_id;
359       record.nameID = ids.name_id;
360       record.length = 0; // handled in NameRecord copy()
361       record.offset = 0;
362       hb_memcpy (name_records, &record, NameRecord::static_size);
363       name_records++;
364     }
365 #endif
366
367     records.qsort ();
368
369     c->copy_all (records,
370                  src_string_pool
371 #ifdef HB_EXPERIMENTAL_API
372                  , name_table_overrides
373 #endif
374                  );
375     hb_free (records.arrayZ);
376
377
378     if (unlikely (c->ran_out_of_room ())) return_trace (false);
379
380     this->stringOffset = c->length ();
381
382     return_trace (true);
383   }
384
385   bool subset (hb_subset_context_t *c) const
386   {
387     auto *name_prime = c->serializer->start_embed<name> ();
388
389 #ifdef HB_EXPERIMENTAL_API
390     const hb_hashmap_t<hb_ot_name_record_ids_t, hb_bytes_t> *name_table_overrides =
391         &c->plan->name_table_overrides;
392 #endif
393     
394     auto it =
395     + nameRecordZ.as_array (count)
396     | hb_filter (c->plan->name_ids, &NameRecord::nameID)
397     | hb_filter (c->plan->name_languages, &NameRecord::languageID)
398     | hb_filter ([&] (const NameRecord& namerecord) {
399       return
400           (c->plan->flags & HB_SUBSET_FLAGS_NAME_LEGACY)
401           || namerecord.isUnicode ();
402     })
403 #ifdef HB_EXPERIMENTAL_API
404     | hb_filter ([&] (const NameRecord& namerecord) {
405       if (name_table_overrides->is_empty ())
406         return true;
407       hb_ot_name_record_ids_t rec_ids (namerecord.platformID,
408                                        namerecord.encodingID,
409                                        namerecord.languageID,
410                                        namerecord.nameID);
411
412       hb_bytes_t *p;
413       if (name_table_overrides->has (rec_ids, &p) &&
414           (*p).length == 0)
415         return false;
416       return true;
417     })
418 #endif
419     ;
420
421 #ifdef HB_EXPERIMENTAL_API
422     hb_hashmap_t<hb_ot_name_record_ids_t, unsigned> retained_name_record_ids;
423     for (const NameRecord& rec : it)
424     {
425       hb_ot_name_record_ids_t rec_ids (rec.platformID,
426                                        rec.encodingID,
427                                        rec.languageID,
428                                        rec.nameID);
429       retained_name_record_ids.set (rec_ids, 1);
430     }
431
432     hb_vector_t<hb_ot_name_record_ids_t> insert_name_records;
433     if (!name_table_overrides->is_empty ())
434     {
435       if (unlikely (!insert_name_records.alloc (name_table_overrides->get_population (), true)))
436         return false;
437       for (const auto& record_ids : name_table_overrides->keys ())
438       {
439         if (name_table_overrides->get (record_ids).length == 0)
440           continue;
441         if (retained_name_record_ids.has (record_ids))
442           continue;
443         insert_name_records.push (record_ids);
444       }
445     }
446 #endif
447
448     return name_prime->serialize (c->serializer, it,
449                                   std::addressof (this + stringOffset)
450 #ifdef HB_EXPERIMENTAL_API
451                                   , insert_name_records
452                                   , name_table_overrides
453 #endif
454                                   );
455   }
456
457   bool sanitize_records (hb_sanitize_context_t *c) const
458   {
459     TRACE_SANITIZE (this);
460     const void *string_pool = (this+stringOffset).arrayZ;
461     return_trace (nameRecordZ.sanitize (c, count, string_pool));
462   }
463
464   bool sanitize (hb_sanitize_context_t *c) const
465   {
466     TRACE_SANITIZE (this);
467     return_trace (c->check_struct (this) &&
468                   likely (format == 0 || format == 1) &&
469                   c->check_array (nameRecordZ.arrayZ, count) &&
470                   c->check_range (this, stringOffset) &&
471                   sanitize_records (c));
472   }
473
474   struct accelerator_t
475   {
476     accelerator_t (hb_face_t *face)
477     {
478       this->table = hb_sanitize_context_t ().reference_table<name> (face);
479       assert (this->table.get_length () >= this->table->stringOffset);
480       this->pool = (const char *) (const void *) (this->table+this->table->stringOffset);
481       this->pool_len = this->table.get_length () - this->table->stringOffset;
482       const hb_array_t<const NameRecord> all_names (this->table->nameRecordZ.arrayZ,
483                                                     this->table->count);
484
485       this->names.alloc (all_names.length, true);
486
487       for (unsigned int i = 0; i < all_names.length; i++)
488       {
489         hb_ot_name_entry_t *entry = this->names.push ();
490
491         entry->name_id = all_names[i].nameID;
492         entry->language = all_names[i].language (face);
493         entry->entry_score =  all_names[i].score ();
494         entry->entry_index = i;
495       }
496
497       this->names.qsort (_hb_ot_name_entry_cmp);
498       /* Walk and pick best only for each name_id,language pair,
499        * while dropping unsupported encodings. */
500       unsigned int j = 0;
501       for (unsigned int i = 0; i < this->names.length; i++)
502       {
503         if (this->names[i].entry_score == UNSUPPORTED ||
504             this->names[i].language == HB_LANGUAGE_INVALID)
505           continue;
506         if (i &&
507             this->names[i - 1].name_id  == this->names[i].name_id &&
508             this->names[i - 1].language == this->names[i].language)
509           continue;
510         this->names[j++] = this->names[i];
511       }
512       this->names.resize (j);
513     }
514     ~accelerator_t ()
515     {
516       this->table.destroy ();
517     }
518
519     int get_index (hb_ot_name_id_t  name_id,
520                    hb_language_t    language,
521                    unsigned int    *width=nullptr) const
522     {
523       const hb_ot_name_entry_t key = {name_id, {0}, language};
524       const hb_ot_name_entry_t *entry = hb_bsearch (key, (const hb_ot_name_entry_t *) this->names,
525                                                     this->names.length,
526                                                     sizeof (hb_ot_name_entry_t),
527                                                     _hb_ot_name_entry_cmp_key,
528                                                     true);
529
530       if (!entry)
531       {
532         entry = hb_bsearch (key, (const hb_ot_name_entry_t *) this->names,
533                             this->names.length,
534                             sizeof (hb_ot_name_entry_t),
535                             _hb_ot_name_entry_cmp_key,
536                             false);
537       }
538
539       if (!entry)
540         return -1;
541
542       if (width)
543         *width = entry->entry_score < 10 ? 2 : 1;
544
545       return entry->entry_index;
546     }
547
548     hb_bytes_t get_name (unsigned int idx) const
549     {
550       const hb_array_t<const NameRecord> all_names (table->nameRecordZ.arrayZ, table->count);
551       const NameRecord &record = all_names[idx];
552       const hb_bytes_t string_pool (pool, pool_len);
553       return string_pool.sub_array (record.offset, record.length);
554     }
555
556     private:
557     const char *pool;
558     unsigned int pool_len;
559     public:
560     hb_blob_ptr_t<name> table;
561     hb_vector_t<hb_ot_name_entry_t> names;
562   };
563
564   public:
565   /* We only implement format 0 for now. */
566   HBUINT16      format;         /* Format selector (=0/1). */
567   HBUINT16      count;          /* Number of name records. */
568   NNOffset16To<UnsizedArrayOf<HBUINT8>>
569                 stringOffset;   /* Offset to start of string storage (from start of table). */
570   UnsizedArrayOf<NameRecord>
571                 nameRecordZ;    /* The name records where count is the number of records. */
572   public:
573   DEFINE_SIZE_ARRAY (6, nameRecordZ);
574 };
575
576 #undef entry_index
577 #undef entry_score
578
579 struct name_accelerator_t : name::accelerator_t {
580   name_accelerator_t (hb_face_t *face) : name::accelerator_t (face) {}
581 };
582
583 } /* namespace OT */
584
585
586 #endif /* OT_NAME_NAME_HH */