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 {
languageOT::NameRecord52 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
scoreOT::NameRecord73 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
copyOT::NameRecord100 NameRecord* copy (hb_serialize_context_t *c,
101 const void *src_base,
102 const void *dst_base) const
103 {
104 TRACE_SERIALIZE (this);
105 auto *out = c->embed (this);
106 if (unlikely (!out)) return_trace (nullptr);
107 out->offset.serialize_copy (c, offset, src_base, dst_base, length);
108 return_trace (out);
109 }
110
sanitizeOT::NameRecord111 bool sanitize (hb_sanitize_context_t *c, const void *base) const
112 {
113 TRACE_SANITIZE (this);
114 return_trace (c->check_struct (this) && offset.sanitize (c, base, length));
115 }
116
117 HBUINT16 platformID; /* Platform ID. */
118 HBUINT16 encodingID; /* Platform-specific encoding ID. */
119 HBUINT16 languageID; /* Language ID. */
120 HBUINT16 nameID; /* Name ID. */
121 HBUINT16 length; /* String length (in bytes). */
122 NNOffsetTo<UnsizedArrayOf<HBUINT8>>
123 offset; /* String offset from start of storage area (in bytes). */
124 public:
125 DEFINE_SIZE_STATIC (12);
126 };
127
128 static int
_hb_ot_name_entry_cmp_key(const void * pa,const void * pb)129 _hb_ot_name_entry_cmp_key (const void *pa, const void *pb)
130 {
131 const hb_ot_name_entry_t *a = (const hb_ot_name_entry_t *) pa;
132 const hb_ot_name_entry_t *b = (const hb_ot_name_entry_t *) pb;
133
134 /* Compare by name_id, then language. */
135
136 if (a->name_id != b->name_id)
137 return a->name_id < b->name_id ? -1 : +1;
138
139 if (a->language == b->language) return 0;
140 if (!a->language) return -1;
141 if (!b->language) return +1;
142 return strcmp (hb_language_to_string (a->language),
143 hb_language_to_string (b->language));
144 }
145
146 static int
_hb_ot_name_entry_cmp(const void * pa,const void * pb)147 _hb_ot_name_entry_cmp (const void *pa, const void *pb)
148 {
149 /* Compare by name_id, then language, then score, then index. */
150
151 int v = _hb_ot_name_entry_cmp_key (pa, pb);
152 if (v)
153 return v;
154
155 const hb_ot_name_entry_t *a = (const hb_ot_name_entry_t *) pa;
156 const hb_ot_name_entry_t *b = (const hb_ot_name_entry_t *) pb;
157
158 if (a->entry_score != b->entry_score)
159 return a->entry_score < b->entry_score ? -1 : +1;
160
161 if (a->entry_index != b->entry_index)
162 return a->entry_index < b->entry_index ? -1 : +1;
163
164 return 0;
165 }
166
167 struct name
168 {
169 static constexpr hb_tag_t tableTag = HB_OT_TAG_name;
170
get_sizeOT::name171 unsigned int get_size () const
172 { return min_size + count * nameRecordZ.item_size; }
173
174 template <typename Iterator,
175 hb_requires (hb_is_source_of (Iterator, const NameRecord &))>
serializeOT::name176 bool serialize (hb_serialize_context_t *c,
177 Iterator it,
178 const void *src_string_pool)
179 {
180 TRACE_SERIALIZE (this);
181
182 if (unlikely (!c->extend_min ((*this)))) return_trace (false);
183
184 this->format = 0;
185 this->count = it.len ();
186
187 auto snap = c->snapshot ();
188 this->nameRecordZ.serialize (c, this->count);
189 if (unlikely (!c->check_assign (this->stringOffset, c->length ()))) return_trace (false);
190 c->revert (snap);
191
192 const void *dst_string_pool = &(this + this->stringOffset);
193
194 for (const auto &_ : it) c->copy (_, src_string_pool, dst_string_pool);
195
196 if (unlikely (c->ran_out_of_room)) return_trace (false);
197
198 assert (this->stringOffset == c->length ());
199
200 return_trace (true);
201 }
202
subsetOT::name203 bool subset (hb_subset_context_t *c) const
204 {
205 TRACE_SUBSET (this);
206
207 name *name_prime = c->serializer->start_embed<name> ();
208 if (unlikely (!name_prime)) return_trace (false);
209
210 auto it =
211 + nameRecordZ.as_array (count)
212 | hb_filter (c->plan->name_ids, &NameRecord::nameID)
213 ;
214
215 name_prime->serialize (c->serializer, it, hb_addressof (this + stringOffset));
216 return_trace (name_prime->count);
217 }
218
sanitize_recordsOT::name219 bool sanitize_records (hb_sanitize_context_t *c) const
220 {
221 TRACE_SANITIZE (this);
222 const void *string_pool = (this+stringOffset).arrayZ;
223 return_trace (nameRecordZ.sanitize (c, count, string_pool));
224 }
225
sanitizeOT::name226 bool sanitize (hb_sanitize_context_t *c) const
227 {
228 TRACE_SANITIZE (this);
229 return_trace (c->check_struct (this) &&
230 likely (format == 0 || format == 1) &&
231 c->check_array (nameRecordZ.arrayZ, count) &&
232 c->check_range (this, stringOffset) &&
233 sanitize_records (c));
234 }
235
236 struct accelerator_t
237 {
initOT::name::accelerator_t238 void init (hb_face_t *face)
239 {
240 this->table = hb_sanitize_context_t().reference_table<name> (face);
241 assert (this->table.get_length () >= this->table->stringOffset);
242 this->pool = (const char *) (const void *) (this->table+this->table->stringOffset);
243 this->pool_len = this->table.get_length () - this->table->stringOffset;
244 const hb_array_t<const NameRecord> all_names (this->table->nameRecordZ.arrayZ,
245 this->table->count);
246
247 this->names.init ();
248 this->names.alloc (all_names.length);
249
250 for (unsigned int i = 0; i < all_names.length; i++)
251 {
252 hb_ot_name_entry_t *entry = this->names.push ();
253
254 entry->name_id = all_names[i].nameID;
255 entry->language = all_names[i].language (face);
256 entry->entry_score = all_names[i].score ();
257 entry->entry_index = i;
258 }
259
260 this->names.qsort (_hb_ot_name_entry_cmp);
261 /* Walk and pick best only for each name_id,language pair,
262 * while dropping unsupported encodings. */
263 unsigned int j = 0;
264 for (unsigned int i = 0; i < this->names.length; i++)
265 {
266 if (this->names[i].entry_score == UNSUPPORTED ||
267 this->names[i].language == HB_LANGUAGE_INVALID)
268 continue;
269 if (i &&
270 this->names[i - 1].name_id == this->names[i].name_id &&
271 this->names[i - 1].language == this->names[i].language)
272 continue;
273 this->names[j++] = this->names[i];
274 }
275 this->names.resize (j);
276 }
277
finiOT::name::accelerator_t278 void fini ()
279 {
280 this->names.fini ();
281 this->table.destroy ();
282 }
283
get_indexOT::name::accelerator_t284 int get_index (hb_ot_name_id_t name_id,
285 hb_language_t language,
286 unsigned int *width=nullptr) const
287 {
288 const hb_ot_name_entry_t key = {name_id, {0}, language};
289 const hb_ot_name_entry_t *entry = (const hb_ot_name_entry_t *)
290 hb_bsearch (&key,
291 (const hb_ot_name_entry_t *) this->names,
292 this->names.length,
293 sizeof (key),
294 _hb_ot_name_entry_cmp_key);
295 if (!entry)
296 return -1;
297
298 if (width)
299 *width = entry->entry_score < 10 ? 2 : 1;
300
301 return entry->entry_index;
302 }
303
get_nameOT::name::accelerator_t304 hb_bytes_t get_name (unsigned int idx) const
305 {
306 const hb_array_t<const NameRecord> all_names (table->nameRecordZ.arrayZ, table->count);
307 const NameRecord &record = all_names[idx];
308 const hb_bytes_t string_pool (pool, pool_len);
309 return string_pool.sub_array (record.offset, record.length);
310 }
311
312 private:
313 const char *pool;
314 unsigned int pool_len;
315 public:
316 hb_blob_ptr_t<name> table;
317 hb_vector_t<hb_ot_name_entry_t> names;
318 };
319
320 /* We only implement format 0 for now. */
321 HBUINT16 format; /* Format selector (=0/1). */
322 HBUINT16 count; /* Number of name records. */
323 NNOffsetTo<UnsizedArrayOf<HBUINT8>>
324 stringOffset; /* Offset to start of string storage (from start of table). */
325 UnsizedArrayOf<NameRecord>
326 nameRecordZ; /* The name records where count is the number of records. */
327 public:
328 DEFINE_SIZE_ARRAY (6, nameRecordZ);
329 };
330
331 #undef entry_index
332 #undef entry_score
333
334 struct name_accelerator_t : name::accelerator_t {};
335
336 } /* namespace OT */
337
338
339 #endif /* HB_OT_NAME_TABLE_HH */
340