/src/skia/third_party/externals/harfbuzz/src/hb-ot-tag.cc
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright © 2009 Red Hat, Inc. |
3 | | * Copyright © 2011 Google, Inc. |
4 | | * |
5 | | * This is part of HarfBuzz, a text shaping library. |
6 | | * |
7 | | * Permission is hereby granted, without written agreement and without |
8 | | * license or royalty fees, to use, copy, modify, and distribute this |
9 | | * software and its documentation for any purpose, provided that the |
10 | | * above copyright notice and the following two paragraphs appear in |
11 | | * all copies of this software. |
12 | | * |
13 | | * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR |
14 | | * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES |
15 | | * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN |
16 | | * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH |
17 | | * DAMAGE. |
18 | | * |
19 | | * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, |
20 | | * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND |
21 | | * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS |
22 | | * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO |
23 | | * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. |
24 | | * |
25 | | * Red Hat Author(s): Behdad Esfahbod |
26 | | * Google Author(s): Behdad Esfahbod, Roozbeh Pournader |
27 | | */ |
28 | | |
29 | | #include "hb.hh" |
30 | | |
31 | | #ifndef HB_NO_OT_TAG |
32 | | |
33 | | |
34 | | /* hb_script_t */ |
35 | | |
36 | | static hb_tag_t |
37 | | hb_ot_old_tag_from_script (hb_script_t script) |
38 | 171 | { |
39 | | /* This seems to be accurate as of end of 2012. */ |
40 | | |
41 | 171 | switch ((hb_tag_t) script) |
42 | 171 | { |
43 | 0 | case HB_SCRIPT_INVALID: return HB_OT_TAG_DEFAULT_SCRIPT; |
44 | | |
45 | | /* KATAKANA and HIRAGANA both map to 'kana' */ |
46 | 2 | case HB_SCRIPT_HIRAGANA: return HB_TAG('k','a','n','a'); |
47 | | |
48 | | /* Spaces at the end are preserved, unlike ISO 15924 */ |
49 | 2 | case HB_SCRIPT_LAO: return HB_TAG('l','a','o',' '); |
50 | 2 | case HB_SCRIPT_YI: return HB_TAG('y','i',' ',' '); |
51 | | /* Unicode-5.0 additions */ |
52 | 2 | case HB_SCRIPT_NKO: return HB_TAG('n','k','o',' '); |
53 | | /* Unicode-5.1 additions */ |
54 | 2 | case HB_SCRIPT_VAI: return HB_TAG('v','a','i',' '); |
55 | 161 | } |
56 | | |
57 | | /* Else, just change first char to lowercase and return */ |
58 | 161 | return ((hb_tag_t) script) | 0x20000000u; |
59 | 161 | } |
60 | | |
61 | | static hb_script_t |
62 | | hb_ot_old_tag_to_script (hb_tag_t tag) |
63 | 0 | { |
64 | 0 | if (unlikely (tag == HB_OT_TAG_DEFAULT_SCRIPT)) |
65 | 0 | return HB_SCRIPT_INVALID; |
66 | | |
67 | | /* This side of the conversion is fully algorithmic. */ |
68 | | |
69 | | /* Any spaces at the end of the tag are replaced by repeating the last |
70 | | * letter. Eg 'nko ' -> 'Nkoo' */ |
71 | 0 | if (unlikely ((tag & 0x0000FF00u) == 0x00002000u)) |
72 | 0 | tag |= (tag >> 8) & 0x0000FF00u; /* Copy second letter to third */ |
73 | 0 | if (unlikely ((tag & 0x000000FFu) == 0x00000020u)) |
74 | 0 | tag |= (tag >> 8) & 0x000000FFu; /* Copy third letter to fourth */ |
75 | | |
76 | | /* Change first char to uppercase and return */ |
77 | 0 | return (hb_script_t) (tag & ~0x20000000u); |
78 | 0 | } |
79 | | |
80 | | static hb_tag_t |
81 | | hb_ot_new_tag_from_script (hb_script_t script) |
82 | 171 | { |
83 | 171 | switch ((hb_tag_t) script) { |
84 | 2 | case HB_SCRIPT_BENGALI: return HB_TAG('b','n','g','2'); |
85 | 2 | case HB_SCRIPT_DEVANAGARI: return HB_TAG('d','e','v','2'); |
86 | 2 | case HB_SCRIPT_GUJARATI: return HB_TAG('g','j','r','2'); |
87 | 2 | case HB_SCRIPT_GURMUKHI: return HB_TAG('g','u','r','2'); |
88 | 2 | case HB_SCRIPT_KANNADA: return HB_TAG('k','n','d','2'); |
89 | 2 | case HB_SCRIPT_MALAYALAM: return HB_TAG('m','l','m','2'); |
90 | 2 | case HB_SCRIPT_ORIYA: return HB_TAG('o','r','y','2'); |
91 | 2 | case HB_SCRIPT_TAMIL: return HB_TAG('t','m','l','2'); |
92 | 1 | case HB_SCRIPT_TELUGU: return HB_TAG('t','e','l','2'); |
93 | 2 | case HB_SCRIPT_MYANMAR: return HB_TAG('m','y','m','2'); |
94 | 152 | } |
95 | | |
96 | 152 | return HB_OT_TAG_DEFAULT_SCRIPT; |
97 | 152 | } |
98 | | |
99 | | static hb_script_t |
100 | | hb_ot_new_tag_to_script (hb_tag_t tag) |
101 | 0 | { |
102 | 0 | switch (tag) { |
103 | 0 | case HB_TAG('b','n','g','2'): return HB_SCRIPT_BENGALI; |
104 | 0 | case HB_TAG('d','e','v','2'): return HB_SCRIPT_DEVANAGARI; |
105 | 0 | case HB_TAG('g','j','r','2'): return HB_SCRIPT_GUJARATI; |
106 | 0 | case HB_TAG('g','u','r','2'): return HB_SCRIPT_GURMUKHI; |
107 | 0 | case HB_TAG('k','n','d','2'): return HB_SCRIPT_KANNADA; |
108 | 0 | case HB_TAG('m','l','m','2'): return HB_SCRIPT_MALAYALAM; |
109 | 0 | case HB_TAG('o','r','y','2'): return HB_SCRIPT_ORIYA; |
110 | 0 | case HB_TAG('t','m','l','2'): return HB_SCRIPT_TAMIL; |
111 | 0 | case HB_TAG('t','e','l','2'): return HB_SCRIPT_TELUGU; |
112 | 0 | case HB_TAG('m','y','m','2'): return HB_SCRIPT_MYANMAR; |
113 | 0 | } |
114 | | |
115 | 0 | return HB_SCRIPT_UNKNOWN; |
116 | 0 | } |
117 | | |
118 | | #ifndef HB_DISABLE_DEPRECATED |
119 | | void |
120 | | hb_ot_tags_from_script (hb_script_t script, |
121 | | hb_tag_t *script_tag_1, |
122 | | hb_tag_t *script_tag_2) |
123 | 0 | { |
124 | 0 | unsigned int count = 2; |
125 | 0 | hb_tag_t tags[2]; |
126 | 0 | hb_ot_tags_from_script_and_language (script, HB_LANGUAGE_INVALID, &count, tags, nullptr, nullptr); |
127 | 0 | *script_tag_1 = count > 0 ? tags[0] : HB_OT_TAG_DEFAULT_SCRIPT; |
128 | 0 | *script_tag_2 = count > 1 ? tags[1] : HB_OT_TAG_DEFAULT_SCRIPT; |
129 | 0 | } |
130 | | #endif |
131 | | |
132 | | /* |
133 | | * Complete list at: |
134 | | * https://docs.microsoft.com/en-us/typography/opentype/spec/scripttags |
135 | | * |
136 | | * Most of the script tags are the same as the ISO 15924 tag but lowercased. |
137 | | * So we just do that, and handle the exceptional cases in a switch. |
138 | | */ |
139 | | |
140 | | static void |
141 | | hb_ot_all_tags_from_script (hb_script_t script, |
142 | | unsigned int *count /* IN/OUT */, |
143 | | hb_tag_t *tags /* OUT */) |
144 | 171 | { |
145 | 171 | unsigned int i = 0; |
146 | | |
147 | 171 | hb_tag_t new_tag = hb_ot_new_tag_from_script (script); |
148 | 171 | if (unlikely (new_tag != HB_OT_TAG_DEFAULT_SCRIPT)) |
149 | 19 | { |
150 | | /* HB_SCRIPT_MYANMAR maps to 'mym2', but there is no 'mym3'. */ |
151 | 19 | if (new_tag != HB_TAG('m','y','m','2')) |
152 | 17 | tags[i++] = new_tag | '3'; |
153 | 19 | if (*count > i) |
154 | 19 | tags[i++] = new_tag; |
155 | 19 | } |
156 | | |
157 | 171 | if (*count > i) |
158 | 171 | { |
159 | 171 | hb_tag_t old_tag = hb_ot_old_tag_from_script (script); |
160 | 171 | if (old_tag != HB_OT_TAG_DEFAULT_SCRIPT) |
161 | 171 | tags[i++] = old_tag; |
162 | 171 | } |
163 | | |
164 | 171 | *count = i; |
165 | 171 | } |
166 | | |
167 | | /** |
168 | | * hb_ot_tag_to_script: |
169 | | * @tag: a script tag |
170 | | * |
171 | | * Converts a script tag to an #hb_script_t. |
172 | | * |
173 | | * Return value: The #hb_script_t corresponding to @tag. |
174 | | * |
175 | | **/ |
176 | | hb_script_t |
177 | | hb_ot_tag_to_script (hb_tag_t tag) |
178 | 0 | { |
179 | 0 | unsigned char digit = tag & 0x000000FFu; |
180 | 0 | if (unlikely (digit == '2' || digit == '3')) |
181 | 0 | return hb_ot_new_tag_to_script (tag & 0xFFFFFF32); |
182 | | |
183 | 0 | return hb_ot_old_tag_to_script (tag); |
184 | 0 | } |
185 | | |
186 | | |
187 | | /* hb_language_t */ |
188 | | |
189 | | static bool |
190 | | subtag_matches (const char *lang_str, |
191 | | const char *limit, |
192 | | const char *subtag) |
193 | 1.53k | { |
194 | 1.53k | do { |
195 | 1.53k | const char *s = strstr (lang_str, subtag); |
196 | 1.53k | if (!s || s >= limit) |
197 | 1.53k | return false; |
198 | 0 | if (!ISALNUM (s[strlen (subtag)])) |
199 | 0 | return true; |
200 | 0 | lang_str = s + strlen (subtag); |
201 | 0 | } while (true); |
202 | 1.53k | } |
203 | | |
204 | | static hb_bool_t |
205 | | lang_matches (const char *lang_str, const char *spec) |
206 | 5.47k | { |
207 | 5.47k | unsigned int len = strlen (spec); |
208 | | |
209 | 5.47k | return strncmp (lang_str, spec, len) == 0 && |
210 | 0 | (lang_str[len] == '\0' || lang_str[len] == '-'); |
211 | 5.47k | } |
212 | | |
213 | | struct LangTag |
214 | | { |
215 | | char language[4]; |
216 | | hb_tag_t tag; |
217 | | |
218 | | int cmp (const char *a) const |
219 | 1.88k | { |
220 | 1.88k | const char *b = this->language; |
221 | 1.88k | unsigned int da, db; |
222 | 1.88k | const char *p; |
223 | | |
224 | 1.88k | p = strchr (a, '-'); |
225 | 1.88k | da = p ? (unsigned int) (p - a) : strlen (a); |
226 | | |
227 | 1.88k | p = strchr (b, '-'); |
228 | 1.88k | db = p ? (unsigned int) (p - b) : strlen (b); |
229 | | |
230 | 1.88k | return strncmp (a, b, hb_max (da, db)); |
231 | 1.88k | } |
232 | | int cmp (const LangTag *that) const |
233 | 0 | { return cmp (that->language); } |
234 | | }; |
235 | | |
236 | | #include "hb-ot-tag-table.hh" |
237 | | |
238 | | /* The corresponding languages IDs for the following IDs are unclear, |
239 | | * overlap, or are architecturally weird. Needs more research. */ |
240 | | |
241 | | /*{"??", {HB_TAG('B','C','R',' ')}},*/ /* Bible Cree */ |
242 | | /*{"zh?", {HB_TAG('C','H','N',' ')}},*/ /* Chinese (seen in Microsoft fonts) */ |
243 | | /*{"ar-Syrc?", {HB_TAG('G','A','R',' ')}},*/ /* Garshuni */ |
244 | | /*{"??", {HB_TAG('N','G','R',' ')}},*/ /* Nagari */ |
245 | | /*{"??", {HB_TAG('Y','I','C',' ')}},*/ /* Yi Classic */ |
246 | | /*{"zh?", {HB_TAG('Z','H','P',' ')}},*/ /* Chinese Phonetic */ |
247 | | |
248 | | #ifndef HB_DISABLE_DEPRECATED |
249 | | hb_tag_t |
250 | | hb_ot_tag_from_language (hb_language_t language) |
251 | 0 | { |
252 | 0 | unsigned int count = 1; |
253 | 0 | hb_tag_t tags[1]; |
254 | 0 | hb_ot_tags_from_script_and_language (HB_SCRIPT_UNKNOWN, language, nullptr, nullptr, &count, tags); |
255 | 0 | return count > 0 ? tags[0] : HB_OT_TAG_DEFAULT_LANGUAGE; |
256 | 0 | } |
257 | | #endif |
258 | | |
259 | | static void |
260 | | hb_ot_tags_from_language (const char *lang_str, |
261 | | const char *limit, |
262 | | unsigned int *count, |
263 | | hb_tag_t *tags) |
264 | 171 | { |
265 | 171 | const char *s; |
266 | 171 | unsigned int tag_idx; |
267 | | |
268 | | /* Check for matches of multiple subtags. */ |
269 | 171 | if (hb_ot_tags_from_complex_language (lang_str, limit, count, tags)) |
270 | 0 | return; |
271 | | |
272 | | /* Find a language matching in the first component. */ |
273 | 171 | s = strchr (lang_str, '-'); |
274 | 171 | { |
275 | 171 | if (s && limit - lang_str >= 6) |
276 | 0 | { |
277 | 0 | const char *extlang_end = strchr (s + 1, '-'); |
278 | | /* If there is an extended language tag, use it. */ |
279 | 0 | if (3 == (extlang_end ? extlang_end - s - 1 : strlen (s + 1)) && |
280 | 0 | ISALPHA (s[1])) |
281 | 0 | lang_str = s + 1; |
282 | 0 | } |
283 | 171 | if (hb_sorted_array (ot_languages).bfind (lang_str, &tag_idx)) |
284 | 0 | { |
285 | 0 | unsigned int i; |
286 | 0 | while (tag_idx != 0 && |
287 | 0 | 0 == strcmp (ot_languages[tag_idx].language, ot_languages[tag_idx - 1].language)) |
288 | 0 | tag_idx--; |
289 | 0 | for (i = 0; |
290 | 0 | i < *count && |
291 | 0 | tag_idx + i < ARRAY_LENGTH (ot_languages) && |
292 | 0 | ot_languages[tag_idx + i].tag != HB_TAG_NONE && |
293 | 0 | 0 == strcmp (ot_languages[tag_idx + i].language, ot_languages[tag_idx].language); |
294 | 0 | i++) |
295 | 0 | tags[i] = ot_languages[tag_idx + i].tag; |
296 | 0 | *count = i; |
297 | 0 | return; |
298 | 0 | } |
299 | 171 | } |
300 | | |
301 | 171 | if (!s) |
302 | 171 | s = lang_str + strlen (lang_str); |
303 | 171 | if (s - lang_str == 3) { |
304 | | /* Assume it's ISO-639-3 and upper-case and use it. */ |
305 | 0 | tags[0] = hb_tag_from_string (lang_str, s - lang_str) & ~0x20202000u; |
306 | 0 | *count = 1; |
307 | 0 | return; |
308 | 0 | } |
309 | | |
310 | 171 | *count = 0; |
311 | 171 | } |
312 | | |
313 | | static bool |
314 | | parse_private_use_subtag (const char *private_use_subtag, |
315 | | unsigned int *count, |
316 | | hb_tag_t *tags, |
317 | | const char *prefix, |
318 | | unsigned char (*normalize) (unsigned char)) |
319 | 342 | { |
320 | | #ifdef HB_NO_LANGUAGE_PRIVATE_SUBTAG |
321 | | return false; |
322 | | #endif |
323 | | |
324 | 342 | if (!(private_use_subtag && count && tags && *count)) return false; |
325 | | |
326 | 0 | const char *s = strstr (private_use_subtag, prefix); |
327 | 0 | if (!s) return false; |
328 | | |
329 | 0 | char tag[4]; |
330 | 0 | int i; |
331 | 0 | s += strlen (prefix); |
332 | 0 | if (s[0] == '-') { |
333 | 0 | s += 1; |
334 | 0 | char c; |
335 | 0 | for (i = 0; i < 8 && ISHEX (s[i]); i++) |
336 | 0 | { |
337 | 0 | c = FROMHEX (s[i]); |
338 | 0 | if (i % 2 == 0) |
339 | 0 | tag[i / 2] = c << 4; |
340 | 0 | else |
341 | 0 | tag[i / 2] += c; |
342 | 0 | } |
343 | 0 | if (i != 8) return false; |
344 | 0 | } else { |
345 | 0 | for (i = 0; i < 4 && ISALNUM (s[i]); i++) |
346 | 0 | tag[i] = normalize (s[i]); |
347 | 0 | if (!i) return false; |
348 | | |
349 | 0 | for (; i < 4; i++) |
350 | 0 | tag[i] = ' '; |
351 | 0 | } |
352 | 0 | tags[0] = HB_TAG (tag[0], tag[1], tag[2], tag[3]); |
353 | 0 | if ((tags[0] & 0xDFDFDFDF) == HB_OT_TAG_DEFAULT_SCRIPT) |
354 | 0 | tags[0] ^= ~0xDFDFDFDF; |
355 | 0 | *count = 1; |
356 | 0 | return true; |
357 | 0 | } |
358 | | |
359 | | /** |
360 | | * hb_ot_tags_from_script_and_language: |
361 | | * @script: an #hb_script_t to convert. |
362 | | * @language: an #hb_language_t to convert. |
363 | | * @script_count: (inout) (optional): maximum number of script tags to retrieve (IN) |
364 | | * and actual number of script tags retrieved (OUT) |
365 | | * @script_tags: (out) (optional): array of size at least @script_count to store the |
366 | | * script tag results |
367 | | * @language_count: (inout) (optional): maximum number of language tags to retrieve |
368 | | * (IN) and actual number of language tags retrieved (OUT) |
369 | | * @language_tags: (out) (optional): array of size at least @language_count to store |
370 | | * the language tag results |
371 | | * |
372 | | * Converts an #hb_script_t and an #hb_language_t to script and language tags. |
373 | | * |
374 | | * Since: 2.0.0 |
375 | | **/ |
376 | | void |
377 | | hb_ot_tags_from_script_and_language (hb_script_t script, |
378 | | hb_language_t language, |
379 | | unsigned int *script_count /* IN/OUT */, |
380 | | hb_tag_t *script_tags /* OUT */, |
381 | | unsigned int *language_count /* IN/OUT */, |
382 | | hb_tag_t *language_tags /* OUT */) |
383 | 171 | { |
384 | 171 | bool needs_script = true; |
385 | | |
386 | 171 | if (language == HB_LANGUAGE_INVALID) |
387 | 0 | { |
388 | 0 | if (language_count && language_tags && *language_count) |
389 | 0 | *language_count = 0; |
390 | 0 | } |
391 | 171 | else |
392 | 171 | { |
393 | 171 | const char *lang_str, *s, *limit, *private_use_subtag; |
394 | 171 | bool needs_language; |
395 | | |
396 | 171 | lang_str = hb_language_to_string (language); |
397 | 171 | limit = nullptr; |
398 | 171 | private_use_subtag = nullptr; |
399 | 171 | if (lang_str[0] == 'x' && lang_str[1] == '-') |
400 | 0 | { |
401 | 0 | private_use_subtag = lang_str; |
402 | 171 | } else { |
403 | 171 | for (s = lang_str + 1; *s; s++) |
404 | 0 | { |
405 | 0 | if (s[-1] == '-' && s[1] == '-') |
406 | 0 | { |
407 | 0 | if (s[0] == 'x') |
408 | 0 | { |
409 | 0 | private_use_subtag = s; |
410 | 0 | if (!limit) |
411 | 0 | limit = s - 1; |
412 | 0 | break; |
413 | 0 | } else if (!limit) |
414 | 0 | { |
415 | 0 | limit = s - 1; |
416 | 0 | } |
417 | 0 | } |
418 | 0 | } |
419 | 171 | if (!limit) |
420 | 171 | limit = s; |
421 | 171 | } |
422 | | |
423 | 171 | needs_script = !parse_private_use_subtag (private_use_subtag, script_count, script_tags, "-hbsc", TOLOWER); |
424 | 171 | needs_language = !parse_private_use_subtag (private_use_subtag, language_count, language_tags, "-hbot", TOUPPER); |
425 | | |
426 | 171 | if (needs_language && language_count && language_tags && *language_count) |
427 | 171 | hb_ot_tags_from_language (lang_str, limit, language_count, language_tags); |
428 | 171 | } |
429 | | |
430 | 171 | if (needs_script && script_count && script_tags && *script_count) |
431 | 171 | hb_ot_all_tags_from_script (script, script_count, script_tags); |
432 | 171 | } |
433 | | |
434 | | /** |
435 | | * hb_ot_tag_to_language: |
436 | | * @tag: an language tag |
437 | | * |
438 | | * Converts a language tag to an #hb_language_t. |
439 | | * |
440 | | * Return value: (transfer none) (nullable): |
441 | | * The #hb_language_t corresponding to @tag. |
442 | | * |
443 | | * Since: 0.9.2 |
444 | | **/ |
445 | | hb_language_t |
446 | | hb_ot_tag_to_language (hb_tag_t tag) |
447 | 0 | { |
448 | 0 | unsigned int i; |
449 | |
|
450 | 0 | if (tag == HB_OT_TAG_DEFAULT_LANGUAGE) |
451 | 0 | return nullptr; |
452 | | |
453 | 0 | { |
454 | 0 | hb_language_t disambiguated_tag = hb_ot_ambiguous_tag_to_language (tag); |
455 | 0 | if (disambiguated_tag != HB_LANGUAGE_INVALID) |
456 | 0 | return disambiguated_tag; |
457 | 0 | } |
458 | | |
459 | 0 | for (i = 0; i < ARRAY_LENGTH (ot_languages); i++) |
460 | 0 | if (ot_languages[i].tag == tag) |
461 | 0 | return hb_language_from_string (ot_languages[i].language, -1); |
462 | | |
463 | | /* Return a custom language in the form of "x-hbot-AABBCCDD". |
464 | | * If it's three letters long, also guess it's ISO 639-3 and lower-case and |
465 | | * prepend it (if it's not a registered tag, the private use subtags will |
466 | | * ensure that calling hb_ot_tag_from_language on the result will still return |
467 | | * the same tag as the original tag). |
468 | | */ |
469 | 0 | { |
470 | 0 | char buf[20]; |
471 | 0 | char *str = buf; |
472 | 0 | if (ISALPHA (tag >> 24) |
473 | 0 | && ISALPHA ((tag >> 16) & 0xFF) |
474 | 0 | && ISALPHA ((tag >> 8) & 0xFF) |
475 | 0 | && (tag & 0xFF) == ' ') |
476 | 0 | { |
477 | 0 | buf[0] = TOLOWER (tag >> 24); |
478 | 0 | buf[1] = TOLOWER ((tag >> 16) & 0xFF); |
479 | 0 | buf[2] = TOLOWER ((tag >> 8) & 0xFF); |
480 | 0 | buf[3] = '-'; |
481 | 0 | str += 4; |
482 | 0 | } |
483 | 0 | snprintf (str, 16, "x-hbot-%08x", tag); |
484 | 0 | return hb_language_from_string (&*buf, -1); |
485 | 0 | } |
486 | 0 | } |
487 | | |
488 | | /** |
489 | | * hb_ot_tags_to_script_and_language: |
490 | | * @script_tag: a script tag |
491 | | * @language_tag: a language tag |
492 | | * @script: (out) (optional): the #hb_script_t corresponding to @script_tag. |
493 | | * @language: (out) (optional): the #hb_language_t corresponding to @script_tag and |
494 | | * @language_tag. |
495 | | * |
496 | | * Converts a script tag and a language tag to an #hb_script_t and an |
497 | | * #hb_language_t. |
498 | | * |
499 | | * Since: 2.0.0 |
500 | | **/ |
501 | | void |
502 | | hb_ot_tags_to_script_and_language (hb_tag_t script_tag, |
503 | | hb_tag_t language_tag, |
504 | | hb_script_t *script /* OUT */, |
505 | | hb_language_t *language /* OUT */) |
506 | 0 | { |
507 | 0 | hb_script_t script_out = hb_ot_tag_to_script (script_tag); |
508 | 0 | if (script) |
509 | 0 | *script = script_out; |
510 | 0 | if (language) |
511 | 0 | { |
512 | 0 | unsigned int script_count = 1; |
513 | 0 | hb_tag_t primary_script_tag[1]; |
514 | 0 | hb_ot_tags_from_script_and_language (script_out, |
515 | 0 | HB_LANGUAGE_INVALID, |
516 | 0 | &script_count, |
517 | 0 | primary_script_tag, |
518 | 0 | nullptr, nullptr); |
519 | 0 | *language = hb_ot_tag_to_language (language_tag); |
520 | 0 | if (script_count == 0 || primary_script_tag[0] != script_tag) |
521 | 0 | { |
522 | 0 | unsigned char *buf; |
523 | 0 | const char *lang_str = hb_language_to_string (*language); |
524 | 0 | size_t len = strlen (lang_str); |
525 | 0 | buf = (unsigned char *) hb_malloc (len + 16); |
526 | 0 | if (unlikely (!buf)) |
527 | 0 | { |
528 | 0 | *language = nullptr; |
529 | 0 | } |
530 | 0 | else |
531 | 0 | { |
532 | 0 | int shift; |
533 | 0 | memcpy (buf, lang_str, len); |
534 | 0 | if (lang_str[0] != 'x' || lang_str[1] != '-') { |
535 | 0 | buf[len++] = '-'; |
536 | 0 | buf[len++] = 'x'; |
537 | 0 | } |
538 | 0 | buf[len++] = '-'; |
539 | 0 | buf[len++] = 'h'; |
540 | 0 | buf[len++] = 'b'; |
541 | 0 | buf[len++] = 's'; |
542 | 0 | buf[len++] = 'c'; |
543 | 0 | buf[len++] = '-'; |
544 | 0 | for (shift = 28; shift >= 0; shift -= 4) |
545 | 0 | buf[len++] = TOHEX (script_tag >> shift); |
546 | 0 | *language = hb_language_from_string ((char *) buf, len); |
547 | 0 | hb_free (buf); |
548 | 0 | } |
549 | 0 | } |
550 | 0 | } |
551 | 0 | } |
552 | | |
553 | | #ifdef MAIN |
554 | | static inline void |
555 | | test_langs_sorted () |
556 | | { |
557 | | for (unsigned int i = 1; i < ARRAY_LENGTH (ot_languages); i++) |
558 | | { |
559 | | int c = ot_languages[i].cmp (&ot_languages[i - 1]); |
560 | | if (c > 0) |
561 | | { |
562 | | fprintf (stderr, "ot_languages not sorted at index %d: %s %d %s\n", |
563 | | i, ot_languages[i-1].language, c, ot_languages[i].language); |
564 | | abort(); |
565 | | } |
566 | | } |
567 | | } |
568 | | |
569 | | int |
570 | | main () |
571 | | { |
572 | | test_langs_sorted (); |
573 | | return 0; |
574 | | } |
575 | | |
576 | | #endif |
577 | | |
578 | | |
579 | | #endif |