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
32 /* hb_script_t */
33
34 static hb_tag_t
hb_ot_old_tag_from_script(hb_script_t script)35 hb_ot_old_tag_from_script (hb_script_t script)
36 {
37 /* This seems to be accurate as of end of 2012. */
38
39 switch ((hb_tag_t) script)
40 {
41 case HB_SCRIPT_INVALID: return HB_OT_TAG_DEFAULT_SCRIPT;
42
43 /* KATAKANA and HIRAGANA both map to 'kana' */
44 case HB_SCRIPT_HIRAGANA: return HB_TAG('k','a','n','a');
45
46 /* Spaces at the end are preserved, unlike ISO 15924 */
47 case HB_SCRIPT_LAO: return HB_TAG('l','a','o',' ');
48 case HB_SCRIPT_YI: return HB_TAG('y','i',' ',' ');
49 /* Unicode-5.0 additions */
50 case HB_SCRIPT_NKO: return HB_TAG('n','k','o',' ');
51 /* Unicode-5.1 additions */
52 case HB_SCRIPT_VAI: return HB_TAG('v','a','i',' ');
53 }
54
55 /* Else, just change first char to lowercase and return */
56 return ((hb_tag_t) script) | 0x20000000u;
57 }
58
59 static hb_script_t
hb_ot_old_tag_to_script(hb_tag_t tag)60 hb_ot_old_tag_to_script (hb_tag_t tag)
61 {
62 if (unlikely (tag == HB_OT_TAG_DEFAULT_SCRIPT))
63 return HB_SCRIPT_INVALID;
64
65 /* This side of the conversion is fully algorithmic. */
66
67 /* Any spaces at the end of the tag are replaced by repeating the last
68 * letter. Eg 'nko ' -> 'Nkoo' */
69 if (unlikely ((tag & 0x0000FF00u) == 0x00002000u))
70 tag |= (tag >> 8) & 0x0000FF00u; /* Copy second letter to third */
71 if (unlikely ((tag & 0x000000FFu) == 0x00000020u))
72 tag |= (tag >> 8) & 0x000000FFu; /* Copy third letter to fourth */
73
74 /* Change first char to uppercase and return */
75 return (hb_script_t) (tag & ~0x20000000u);
76 }
77
78 static hb_tag_t
hb_ot_new_tag_from_script(hb_script_t script)79 hb_ot_new_tag_from_script (hb_script_t script)
80 {
81 switch ((hb_tag_t) script) {
82 case HB_SCRIPT_BENGALI: return HB_TAG('b','n','g','2');
83 case HB_SCRIPT_DEVANAGARI: return HB_TAG('d','e','v','2');
84 case HB_SCRIPT_GUJARATI: return HB_TAG('g','j','r','2');
85 case HB_SCRIPT_GURMUKHI: return HB_TAG('g','u','r','2');
86 case HB_SCRIPT_KANNADA: return HB_TAG('k','n','d','2');
87 case HB_SCRIPT_MALAYALAM: return HB_TAG('m','l','m','2');
88 case HB_SCRIPT_ORIYA: return HB_TAG('o','r','y','2');
89 case HB_SCRIPT_TAMIL: return HB_TAG('t','m','l','2');
90 case HB_SCRIPT_TELUGU: return HB_TAG('t','e','l','2');
91 case HB_SCRIPT_MYANMAR: return HB_TAG('m','y','m','2');
92 }
93
94 return HB_OT_TAG_DEFAULT_SCRIPT;
95 }
96
97 static hb_script_t
hb_ot_new_tag_to_script(hb_tag_t tag)98 hb_ot_new_tag_to_script (hb_tag_t tag)
99 {
100 switch (tag) {
101 case HB_TAG('b','n','g','2'): return HB_SCRIPT_BENGALI;
102 case HB_TAG('d','e','v','2'): return HB_SCRIPT_DEVANAGARI;
103 case HB_TAG('g','j','r','2'): return HB_SCRIPT_GUJARATI;
104 case HB_TAG('g','u','r','2'): return HB_SCRIPT_GURMUKHI;
105 case HB_TAG('k','n','d','2'): return HB_SCRIPT_KANNADA;
106 case HB_TAG('m','l','m','2'): return HB_SCRIPT_MALAYALAM;
107 case HB_TAG('o','r','y','2'): return HB_SCRIPT_ORIYA;
108 case HB_TAG('t','m','l','2'): return HB_SCRIPT_TAMIL;
109 case HB_TAG('t','e','l','2'): return HB_SCRIPT_TELUGU;
110 case HB_TAG('m','y','m','2'): return HB_SCRIPT_MYANMAR;
111 }
112
113 return HB_SCRIPT_UNKNOWN;
114 }
115
116 void
hb_ot_tags_from_script(hb_script_t script,hb_tag_t * script_tag_1,hb_tag_t * script_tag_2)117 hb_ot_tags_from_script (hb_script_t script,
118 hb_tag_t *script_tag_1,
119 hb_tag_t *script_tag_2)
120 {
121 unsigned int count = 2;
122 hb_tag_t tags[2];
123 hb_ot_tags_from_script_and_language (script, HB_LANGUAGE_INVALID, &count, tags, nullptr, nullptr);
124 *script_tag_1 = count > 0 ? tags[0] : HB_OT_TAG_DEFAULT_SCRIPT;
125 *script_tag_2 = count > 1 ? tags[1] : HB_OT_TAG_DEFAULT_SCRIPT;
126 }
127
128 /*
129 * Complete list at:
130 * https://docs.microsoft.com/en-us/typography/opentype/spec/scripttags
131 *
132 * Most of the script tags are the same as the ISO 15924 tag but lowercased.
133 * So we just do that, and handle the exceptional cases in a switch.
134 */
135
136 static void
hb_ot_all_tags_from_script(hb_script_t script,unsigned int * count,hb_tag_t * tags)137 hb_ot_all_tags_from_script (hb_script_t script,
138 unsigned int *count /* IN/OUT */,
139 hb_tag_t *tags /* OUT */)
140 {
141 unsigned int i = 0;
142
143 hb_tag_t new_tag = hb_ot_new_tag_from_script (script);
144 if (unlikely (new_tag != HB_OT_TAG_DEFAULT_SCRIPT))
145 {
146 tags[i++] = new_tag | '3';
147 if (*count > i)
148 tags[i++] = new_tag;
149 }
150
151 if (*count > i)
152 {
153 hb_tag_t old_tag = hb_ot_old_tag_from_script (script);
154 if (old_tag != HB_OT_TAG_DEFAULT_SCRIPT)
155 tags[i++] = old_tag;
156 }
157
158 *count = i;
159 }
160
161 hb_script_t
hb_ot_tag_to_script(hb_tag_t tag)162 hb_ot_tag_to_script (hb_tag_t tag)
163 {
164 unsigned char digit = tag & 0x000000FFu;
165 if (unlikely (digit == '2' || digit == '3'))
166 return hb_ot_new_tag_to_script (tag & 0xFFFFFF32);
167
168 return hb_ot_old_tag_to_script (tag);
169 }
170
171
172 /* hb_language_t */
173
174 static int
lang_compare_first_component(const void * pa,const void * pb)175 lang_compare_first_component (const void *pa,
176 const void *pb)
177 {
178 const char *a = (const char *) pa;
179 const char *b = (const char *) pb;
180 unsigned int da, db;
181 const char *p;
182
183 p = strchr (a, '-');
184 da = p ? (unsigned int) (p - a) : strlen (a);
185
186 p = strchr (b, '-');
187 db = p ? (unsigned int) (p - b) : strlen (b);
188
189 return strncmp (a, b, MAX (da, db));
190 }
191
192 static bool
subtag_matches(const char * lang_str,const char * limit,const char * subtag)193 subtag_matches (const char *lang_str,
194 const char *limit,
195 const char *subtag)
196 {
197 do {
198 const char *s = strstr (lang_str, subtag);
199 if (!s || s >= limit)
200 return false;
201 if (!ISALNUM (s[strlen (subtag)]))
202 return true;
203 lang_str = s + strlen (subtag);
204 } while (true);
205 }
206
207 static hb_bool_t
lang_matches(const char * lang_str,const char * spec)208 lang_matches (const char *lang_str, const char *spec)
209 {
210 unsigned int len = strlen (spec);
211
212 return strncmp (lang_str, spec, len) == 0 &&
213 (lang_str[len] == '\0' || lang_str[len] == '-');
214 }
215
216 typedef struct {
217 char language[4];
218 hb_tag_t tags[HB_OT_MAX_TAGS_PER_LANGUAGE];
219 } LangTag;
220
221 #include "hb-ot-tag-table.hh"
222
223 /* The corresponding languages IDs for the following IDs are unclear,
224 * overlap, or are architecturally weird. Needs more research. */
225
226 /*{"??", {HB_TAG('B','C','R',' ')}},*/ /* Bible Cree */
227 /*{"zh?", {HB_TAG('C','H','N',' ')}},*/ /* Chinese (seen in Microsoft fonts) */
228 /*{"ar-Syrc?", {HB_TAG('G','A','R',' ')}},*/ /* Garshuni */
229 /*{"??", {HB_TAG('N','G','R',' ')}},*/ /* Nagari */
230 /*{"??", {HB_TAG('Y','I','C',' ')}},*/ /* Yi Classic */
231 /*{"zh?", {HB_TAG('Z','H','P',' ')}},*/ /* Chinese Phonetic */
232
233 hb_tag_t
hb_ot_tag_from_language(hb_language_t language)234 hb_ot_tag_from_language (hb_language_t language)
235 {
236 unsigned int count = 1;
237 hb_tag_t tags[1];
238 hb_ot_tags_from_script_and_language (HB_SCRIPT_UNKNOWN, language, nullptr, nullptr, &count, tags);
239 return count > 0 ? tags[0] : HB_OT_TAG_DEFAULT_LANGUAGE;
240 }
241
242 static void
hb_ot_tags_from_language(const char * lang_str,const char * limit,unsigned int * count,hb_tag_t * tags)243 hb_ot_tags_from_language (const char *lang_str,
244 const char *limit,
245 unsigned int *count,
246 hb_tag_t *tags)
247 {
248 const char *s;
249
250 /* Check for matches of multiple subtags. */
251 if (hb_ot_tags_from_complex_language (lang_str, limit, count, tags))
252 return;
253
254 /* Find a language matching in the first component. */
255 s = strchr (lang_str, '-');
256 {
257 const LangTag *lang_tag;
258 if (s && limit - lang_str >= 6)
259 {
260 const char *extlang_end = strchr (s + 1, '-');
261 /* If there is an extended language tag, use it. */
262 if (3 == (extlang_end ? extlang_end - s - 1 : strlen (s + 1)) &&
263 ISALPHA (s[1]))
264 lang_str = s + 1;
265 }
266 lang_tag = (LangTag *) bsearch (lang_str, ot_languages,
267 ARRAY_LENGTH (ot_languages), sizeof (LangTag),
268 lang_compare_first_component);
269 if (lang_tag)
270 {
271 unsigned int i;
272 for (i = 0; i < *count && lang_tag->tags[i] != HB_TAG_NONE; i++)
273 tags[i] = lang_tag->tags[i];
274 *count = i;
275 return;
276 }
277 }
278
279 if (!s)
280 s = lang_str + strlen (lang_str);
281 if (s - lang_str == 3) {
282 /* Assume it's ISO-639-3 and upper-case and use it. */
283 tags[0] = hb_tag_from_string (lang_str, s - lang_str) & ~0x20202000u;
284 *count = 1;
285 return;
286 }
287
288 *count = 0;
289 }
290
291 static bool
parse_private_use_subtag(const char * private_use_subtag,unsigned int * count,hb_tag_t * tags,const char * prefix,unsigned char (* normalize)(unsigned char))292 parse_private_use_subtag (const char *private_use_subtag,
293 unsigned int *count,
294 hb_tag_t *tags,
295 const char *prefix,
296 unsigned char (*normalize) (unsigned char))
297 {
298 if (private_use_subtag && count && tags && *count)
299 {
300 const char *s = strstr (private_use_subtag, prefix);
301 if (s)
302 {
303 char tag[4];
304 int i;
305 s += strlen (prefix);
306 for (i = 0; i < 4 && ISALNUM (s[i]); i++)
307 tag[i] = normalize (s[i]);
308 if (i)
309 {
310 for (; i < 4; i++)
311 tag[i] = ' ';
312 tags[0] = HB_TAG (tag[0], tag[1], tag[2], tag[3]);
313 if ((tags[0] & 0xDFDFDFDF) == HB_OT_TAG_DEFAULT_SCRIPT)
314 tags[0] ^= ~0xDFDFDFDF;
315 *count = 1;
316 return false;
317 }
318 }
319 }
320 return true;
321 }
322
323 /**
324 * hb_ot_tags_from_script_and_language:
325 * @script: an #hb_script_t to convert.
326 * @language: an #hb_language_t to convert.
327 * @script_count: (allow-none): maximum number of script tags to retrieve (IN)
328 * and actual number of script tags retrieved (OUT)
329 * @script_tags: (out) (allow-none): array of size at least @script_count to store the
330 * script tag results
331 * @language_count: (allow-none): maximum number of language tags to retrieve
332 * (IN) and actual number of language tags retrieved (OUT)
333 * @language_tags: (out) (allow-none): array of size at least @language_count to store
334 * the language tag results
335 *
336 * Converts an #hb_script_t and an #hb_language_t to script and language tags.
337 *
338 * Since: 2.0.0
339 **/
340 void
hb_ot_tags_from_script_and_language(hb_script_t script,hb_language_t language,unsigned int * script_count,hb_tag_t * script_tags,unsigned int * language_count,hb_tag_t * language_tags)341 hb_ot_tags_from_script_and_language (hb_script_t script,
342 hb_language_t language,
343 unsigned int *script_count /* IN/OUT */,
344 hb_tag_t *script_tags /* OUT */,
345 unsigned int *language_count /* IN/OUT */,
346 hb_tag_t *language_tags /* OUT */)
347 {
348 bool needs_script = true;
349
350 if (language == HB_LANGUAGE_INVALID)
351 {
352 if (language_count && language_tags && *language_count)
353 *language_count = 0;
354 }
355 else
356 {
357 const char *lang_str, *s, *limit, *private_use_subtag;
358 bool needs_language;
359
360 lang_str = hb_language_to_string (language);
361 limit = nullptr;
362 private_use_subtag = nullptr;
363 if (lang_str[0] == 'x' && lang_str[1] == '-')
364 {
365 private_use_subtag = lang_str;
366 } else {
367 for (s = lang_str + 1; *s; s++)
368 {
369 if (s[-1] == '-' && s[1] == '-')
370 {
371 if (s[0] == 'x')
372 {
373 private_use_subtag = s;
374 if (!limit)
375 limit = s - 1;
376 break;
377 } else if (!limit)
378 {
379 limit = s - 1;
380 }
381 }
382 }
383 if (!limit)
384 limit = s;
385 }
386
387 needs_script = parse_private_use_subtag (private_use_subtag, script_count, script_tags, "-hbsc", TOLOWER);
388 needs_language = parse_private_use_subtag (private_use_subtag, language_count, language_tags, "-hbot", TOUPPER);
389
390 if (needs_language && language_count && language_tags && *language_count)
391 hb_ot_tags_from_language (lang_str, limit, language_count, language_tags);
392 }
393
394 if (needs_script && script_count && script_tags && *script_count)
395 hb_ot_all_tags_from_script (script, script_count, script_tags);
396 }
397
398 /**
399 * hb_ot_tag_to_language:
400 *
401 *
402 *
403 * Return value: (transfer none):
404 *
405 * Since: 0.9.2
406 **/
407 hb_language_t
hb_ot_tag_to_language(hb_tag_t tag)408 hb_ot_tag_to_language (hb_tag_t tag)
409 {
410 unsigned int i;
411
412 if (tag == HB_OT_TAG_DEFAULT_LANGUAGE)
413 return nullptr;
414
415 {
416 hb_language_t disambiguated_tag = hb_ot_ambiguous_tag_to_language (tag);
417 if (disambiguated_tag != HB_LANGUAGE_INVALID)
418 return disambiguated_tag;
419 }
420
421 for (i = 0; i < ARRAY_LENGTH (ot_languages); i++)
422 if (ot_languages[i].tags[0] == tag)
423 return hb_language_from_string (ot_languages[i].language, -1);
424
425 /* Else return a custom language in the form of "x-hbotABCD" */
426 {
427 unsigned char buf[11] = "x-hbot";
428 buf[6] = tag >> 24;
429 buf[7] = (tag >> 16) & 0xFF;
430 buf[8] = (tag >> 8) & 0xFF;
431 buf[9] = tag & 0xFF;
432 if (buf[9] == 0x20)
433 buf[9] = '\0';
434 buf[10] = '\0';
435 return hb_language_from_string ((char *) buf, -1);
436 }
437 }
438
439 /**
440 * hb_ot_tags_to_script_and_language:
441 * @script_tag: a script tag
442 * @language_tag: a language tag
443 * @script: (allow-none): the #hb_script_t corresponding to @script_tag (OUT).
444 * @language: (allow-none): the #hb_language_t corresponding to @script_tag and
445 * @language_tag (OUT).
446 *
447 * Converts a script tag and a language tag to an #hb_script_t and an
448 * #hb_language_t.
449 *
450 * Since: 2.0.0
451 **/
452 void
hb_ot_tags_to_script_and_language(hb_tag_t script_tag,hb_tag_t language_tag,hb_script_t * script,hb_language_t * language)453 hb_ot_tags_to_script_and_language (hb_tag_t script_tag,
454 hb_tag_t language_tag,
455 hb_script_t *script /* OUT */,
456 hb_language_t *language /* OUT */)
457 {
458 hb_script_t script_out = hb_ot_tag_to_script (script_tag);
459 if (script)
460 *script = script_out;
461 if (language)
462 {
463 unsigned int script_count = 1;
464 hb_tag_t primary_script_tag[1];
465 hb_ot_tags_from_script_and_language (script_out,
466 HB_LANGUAGE_INVALID,
467 &script_count,
468 primary_script_tag,
469 nullptr, nullptr);
470 *language = hb_ot_tag_to_language (language_tag);
471 if (script_count == 0 || primary_script_tag[0] != script_tag)
472 {
473 unsigned char *buf;
474 const char *lang_str = hb_language_to_string (*language);
475 size_t len = strlen (lang_str);
476 buf = (unsigned char *) malloc (len + 11);
477 if (unlikely (!buf))
478 {
479 *language = nullptr;
480 }
481 else
482 {
483 memcpy (buf, lang_str, len);
484 if (lang_str[0] != 'x' || lang_str[1] != '-') {
485 buf[len++] = '-';
486 buf[len++] = 'x';
487 }
488 buf[len++] = '-';
489 buf[len++] = 'h';
490 buf[len++] = 'b';
491 buf[len++] = 's';
492 buf[len++] = 'c';
493 buf[len++] = script_tag >> 24;
494 buf[len++] = (script_tag >> 16) & 0xFF;
495 buf[len++] = (script_tag >> 8) & 0xFF;
496 buf[len++] = script_tag & 0xFF;
497 *language = hb_language_from_string ((char *) buf, len);
498 free (buf);
499 }
500 }
501 }
502 }
503
504 #ifdef MAIN
505 static inline void
test_langs_sorted()506 test_langs_sorted ()
507 {
508 for (unsigned int i = 1; i < ARRAY_LENGTH (ot_languages); i++)
509 {
510 int c = lang_compare_first_component (ot_languages[i-1].language, ot_languages[i].language);
511 if (c >= 0)
512 {
513 fprintf (stderr, "ot_languages not sorted at index %d: %s %d %s\n",
514 i, ot_languages[i-1].language, c, ot_languages[i].language);
515 abort();
516 }
517 }
518 }
519
520 int
main()521 main ()
522 {
523 test_langs_sorted ();
524 return 0;
525 }
526
527 #endif
528