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