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