• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "hb-utf.hh"
34 
35 
36 namespace OT {
37 
38 template <typename in_utf_t, typename out_utf_t>
39 inline unsigned int
hb_ot_name_convert_utf(hb_bytes_t bytes,unsigned int * text_size,typename out_utf_t::codepoint_t * text)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 {
languageOT::NameRecord97   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 
scoreOT::NameRecord118   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 
copyOT::NameRecord145   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 
isUnicodeOT::NameRecord210   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 
cmpOT::NameRecord219   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 
sanitizeOT::NameRecord242   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
_hb_ot_name_entry_cmp_key(const void * pa,const void * pb,bool exact)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
_hb_ot_name_entry_cmp(const void * pa,const void * pb)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 
get_sizeOT::name313   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 &))>
serializeOT::name318   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       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 
subsetOT::name385   bool subset (hb_subset_context_t *c) const
386   {
387     TRACE_SUBSET (this);
388 
389     name *name_prime = c->serializer->start_embed<name> ();
390     if (unlikely (!name_prime)) return_trace (false);
391 
392 #ifdef HB_EXPERIMENTAL_API
393     const hb_hashmap_t<hb_ot_name_record_ids_t, hb_bytes_t> *name_table_overrides =
394         c->plan->name_table_overrides;
395 #endif
396 
397     auto it =
398     + nameRecordZ.as_array (count)
399     | hb_filter (c->plan->name_ids, &NameRecord::nameID)
400     | hb_filter (c->plan->name_languages, &NameRecord::languageID)
401     | hb_filter ([&] (const NameRecord& namerecord) {
402       return
403           (c->plan->flags & HB_SUBSET_FLAGS_NAME_LEGACY)
404           || namerecord.isUnicode ();
405     })
406 #ifdef HB_EXPERIMENTAL_API
407     | hb_filter ([&] (const NameRecord& namerecord) {
408       if (name_table_overrides->is_empty ())
409         return true;
410       hb_ot_name_record_ids_t rec_ids (namerecord.platformID,
411                                        namerecord.encodingID,
412                                        namerecord.languageID,
413                                        namerecord.nameID);
414 
415       hb_bytes_t *p;
416       if (name_table_overrides->has (rec_ids, &p) &&
417           (*p).length == 0)
418         return false;
419       return true;
420     })
421 #endif
422     ;
423 
424 #ifdef HB_EXPERIMENTAL_API
425     hb_vector_t<hb_ot_name_record_ids_t> insert_name_records;
426     if (!name_table_overrides->is_empty ())
427     {
428       if (unlikely (!insert_name_records.alloc (name_table_overrides->get_population ())))
429         return_trace (false);
430       for (const auto& record_ids : name_table_overrides->keys ())
431       {
432         if (name_table_overrides->get (record_ids).length == 0)
433           continue;
434         if (has_name_record_with_ids (record_ids))
435           continue;
436         insert_name_records.push (record_ids);
437       }
438     }
439 #endif
440 
441     return (name_prime->serialize (c->serializer, it,
442                                    std::addressof (this + stringOffset)
443 #ifdef HB_EXPERIMENTAL_API
444                                    , insert_name_records
445                                    , name_table_overrides
446 #endif
447                                    ));
448   }
449 
sanitize_recordsOT::name450   bool sanitize_records (hb_sanitize_context_t *c) const
451   {
452     TRACE_SANITIZE (this);
453     const void *string_pool = (this+stringOffset).arrayZ;
454     return_trace (nameRecordZ.sanitize (c, count, string_pool));
455   }
456 
sanitizeOT::name457   bool sanitize (hb_sanitize_context_t *c) const
458   {
459     TRACE_SANITIZE (this);
460     return_trace (c->check_struct (this) &&
461 		  likely (format == 0 || format == 1) &&
462 		  c->check_array (nameRecordZ.arrayZ, count) &&
463 		  c->check_range (this, stringOffset) &&
464 		  sanitize_records (c));
465   }
466 
467   struct accelerator_t
468   {
accelerator_tOT::name::accelerator_t469     accelerator_t (hb_face_t *face)
470     {
471       this->table = hb_sanitize_context_t ().reference_table<name> (face);
472       assert (this->table.get_length () >= this->table->stringOffset);
473       this->pool = (const char *) (const void *) (this->table+this->table->stringOffset);
474       this->pool_len = this->table.get_length () - this->table->stringOffset;
475       const hb_array_t<const NameRecord> all_names (this->table->nameRecordZ.arrayZ,
476 						    this->table->count);
477 
478       this->names.alloc (all_names.length);
479 
480       for (unsigned int i = 0; i < all_names.length; i++)
481       {
482 	hb_ot_name_entry_t *entry = this->names.push ();
483 
484 	entry->name_id = all_names[i].nameID;
485 	entry->language = all_names[i].language (face);
486 	entry->entry_score =  all_names[i].score ();
487 	entry->entry_index = i;
488       }
489 
490       this->names.qsort (_hb_ot_name_entry_cmp);
491       /* Walk and pick best only for each name_id,language pair,
492        * while dropping unsupported encodings. */
493       unsigned int j = 0;
494       for (unsigned int i = 0; i < this->names.length; i++)
495       {
496 	if (this->names[i].entry_score == UNSUPPORTED ||
497 	    this->names[i].language == HB_LANGUAGE_INVALID)
498 	  continue;
499 	if (i &&
500 	    this->names[i - 1].name_id  == this->names[i].name_id &&
501 	    this->names[i - 1].language == this->names[i].language)
502 	  continue;
503 	this->names[j++] = this->names[i];
504       }
505       this->names.resize (j);
506     }
~accelerator_tOT::name::accelerator_t507     ~accelerator_t ()
508     {
509       this->table.destroy ();
510     }
511 
get_indexOT::name::accelerator_t512     int get_index (hb_ot_name_id_t  name_id,
513 		   hb_language_t    language,
514 		   unsigned int    *width=nullptr) const
515     {
516       const hb_ot_name_entry_t key = {name_id, {0}, language};
517       const hb_ot_name_entry_t *entry = hb_bsearch (key, (const hb_ot_name_entry_t *) this->names,
518 						    this->names.length,
519 						    sizeof (hb_ot_name_entry_t),
520 						    _hb_ot_name_entry_cmp_key,
521 						    true);
522 
523       if (!entry)
524       {
525 	entry = hb_bsearch (key, (const hb_ot_name_entry_t *) this->names,
526 			    this->names.length,
527 			    sizeof (hb_ot_name_entry_t),
528 			    _hb_ot_name_entry_cmp_key,
529 			    false);
530       }
531 
532       if (!entry)
533 	return -1;
534 
535       if (width)
536 	*width = entry->entry_score < 10 ? 2 : 1;
537 
538       return entry->entry_index;
539     }
540 
get_nameOT::name::accelerator_t541     hb_bytes_t get_name (unsigned int idx) const
542     {
543       const hb_array_t<const NameRecord> all_names (table->nameRecordZ.arrayZ, table->count);
544       const NameRecord &record = all_names[idx];
545       const hb_bytes_t string_pool (pool, pool_len);
546       return string_pool.sub_array (record.offset, record.length);
547     }
548 
549     private:
550     const char *pool;
551     unsigned int pool_len;
552     public:
553     hb_blob_ptr_t<name> table;
554     hb_vector_t<hb_ot_name_entry_t> names;
555   };
556 
557   private:
558   // sometimes NameRecords are not sorted in the font file, so use linear search
559   // here
has_name_record_with_idsOT::name560   bool has_name_record_with_ids (const hb_ot_name_record_ids_t& record_ids) const
561   {
562     for (const auto& record : nameRecordZ.as_array (count))
563     {
564       if (record.platformID == record_ids.platform_id &&
565           record.encodingID == record_ids.encoding_id &&
566           record.languageID == record_ids.language_id &&
567           record.nameID == record_ids.name_id)
568         return true;
569     }
570     return false;
571   }
572 
573   public:
574   /* We only implement format 0 for now. */
575   HBUINT16	format;		/* Format selector (=0/1). */
576   HBUINT16	count;		/* Number of name records. */
577   NNOffset16To<UnsizedArrayOf<HBUINT8>>
578 		stringOffset;	/* Offset to start of string storage (from start of table). */
579   UnsizedArrayOf<NameRecord>
580 		nameRecordZ;	/* The name records where count is the number of records. */
581   public:
582   DEFINE_SIZE_ARRAY (6, nameRecordZ);
583 };
584 
585 #undef entry_index
586 #undef entry_score
587 
588 struct name_accelerator_t : name::accelerator_t {
name_accelerator_tOT::name_accelerator_t589   name_accelerator_t (hb_face_t *face) : name::accelerator_t (face) {}
590 };
591 
592 } /* namespace OT */
593 
594 
595 #endif /* HB_OT_NAME_TABLE_HH */
596