Home | History | Annotate | Download | only in src
      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-private.hh"
     30 #include "hb-ot.h"
     31 
     32 #include <string.h>
     33 
     34 
     35 
     36 /* hb_script_t */
     37 
     38 static hb_tag_t
     39 hb_ot_old_tag_from_script (hb_script_t script)
     40 {
     41   /* This seems to be accurate as of end of 2012. */
     42 
     43   switch ((hb_tag_t) script) {
     44     case HB_SCRIPT_INVALID:		return HB_OT_TAG_DEFAULT_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     /* Unicode-5.2 additions */
     57     /* Unicode-6.0 additions */
     58   }
     59 
     60   /* Else, just change first char to lowercase and return */
     61   return ((hb_tag_t) script) | 0x20000000;
     62 }
     63 
     64 static hb_script_t
     65 hb_ot_old_tag_to_script (hb_tag_t tag)
     66 {
     67   if (unlikely (tag == HB_OT_TAG_DEFAULT_SCRIPT))
     68     return HB_SCRIPT_INVALID;
     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 & 0x0000FF00) == 0x00002000))
     75     tag |= (tag >> 8) & 0x0000FF00; /* Copy second letter to third */
     76   if (unlikely ((tag & 0x000000FF) == 0x00000020))
     77     tag |= (tag >> 8) & 0x000000FF; /* Copy third letter to fourth */
     78 
     79   /* Change first char to uppercase and return */
     80   return (hb_script_t) (tag & ~0x20000000);
     81 }
     82 
     83 static hb_tag_t
     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
    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 /*
    122  * Complete list at:
    123  * https://www.microsoft.com/typography/otspec/scripttags.htm
    124  * https://www.microsoft.com/typography/otspec160/scripttagsProposed.htm
    125  *
    126  * Most of the script tags are the same as the ISO 15924 tag but lowercased.
    127  * So we just do that, and handle the exceptional cases in a switch.
    128  */
    129 
    130 void
    131 hb_ot_tags_from_script (hb_script_t  script,
    132 			hb_tag_t    *script_tag_1,
    133 			hb_tag_t    *script_tag_2)
    134 {
    135   hb_tag_t new_tag;
    136 
    137   *script_tag_2 = HB_OT_TAG_DEFAULT_SCRIPT;
    138   *script_tag_1 = hb_ot_old_tag_from_script (script);
    139 
    140   new_tag = hb_ot_new_tag_from_script (script);
    141   if (unlikely (new_tag != HB_OT_TAG_DEFAULT_SCRIPT)) {
    142     *script_tag_2 = *script_tag_1;
    143     *script_tag_1 = new_tag;
    144   }
    145 }
    146 
    147 hb_script_t
    148 hb_ot_tag_to_script (hb_tag_t tag)
    149 {
    150   if (unlikely ((tag & 0x000000FF) == '2'))
    151     return hb_ot_new_tag_to_script (tag);
    152 
    153   return hb_ot_old_tag_to_script (tag);
    154 }
    155 
    156 
    157 /* hb_language_t */
    158 
    159 typedef struct {
    160   char language[6];
    161   hb_tag_t tag;
    162 } LangTag;
    163 
    164 /*
    165  * Complete list at:
    166  * http://www.microsoft.com/typography/otspec/languagetags.htm
    167  *
    168  * Generated by intersecting the OpenType language tag list from
    169  * Draft OpenType 1.5 spec, with with the ISO 639-3 codes from
    170  * 2008/08/04, matching on name, and finally adjusted manually.
    171  *
    172  * Updated on 2012/12/07 with more research into remaining codes.
    173  *
    174  * Some items still missing.  Those are commented out at the end.
    175  * Keep sorted for bsearch.
    176  */
    177 
    178 static const LangTag ot_languages[] = {
    179   {"aa",	HB_TAG('A','F','R',' ')},	/* Afar */
    180   {"ab",	HB_TAG('A','B','K',' ')},	/* Abkhazian */
    181   {"abq",	HB_TAG('A','B','A',' ')},	/* Abaza */
    182   {"ada",	HB_TAG('D','N','G',' ')},	/* Dangme */
    183   {"ady",	HB_TAG('A','D','Y',' ')},	/* Adyghe */
    184   {"af",	HB_TAG('A','F','K',' ')},	/* Afrikaans */
    185   {"aii",	HB_TAG('S','W','A',' ')},	/* Swadaya Aramaic */
    186   {"aiw",	HB_TAG('A','R','I',' ')},	/* Aari */
    187   {"alt",	HB_TAG('A','L','T',' ')},	/* [Southern] Altai */
    188   {"am",	HB_TAG('A','M','H',' ')},	/* Amharic */
    189   {"amf",	HB_TAG('H','B','N',' ')},	/* Hammer-Banna */
    190   {"ar",	HB_TAG('A','R','A',' ')},	/* Arabic */
    191   {"arn",	HB_TAG('M','A','P',' ')},	/* Mapudungun */
    192   {"as",	HB_TAG('A','S','M',' ')},	/* Assamese */
    193   {"ath",	HB_TAG('A','T','H',' ')},	/* Athapaskan [family] */
    194   {"atv",	HB_TAG('A','L','T',' ')},	/* [Northern] Altai */
    195   {"av",	HB_TAG('A','V','R',' ')},	/* Avaric */
    196   {"awa",	HB_TAG('A','W','A',' ')},	/* Awadhi */
    197   {"ay",	HB_TAG('A','Y','M',' ')},	/* Aymara */
    198   {"az",	HB_TAG('A','Z','E',' ')},	/* Azerbaijani */
    199   {"ba",	HB_TAG('B','S','H',' ')},	/* Bashkir */
    200   {"bai",	HB_TAG('B','M','L',' ')},	/* Bamileke [family] */
    201   {"bal",	HB_TAG('B','L','I',' ')},	/* Baluchi */
    202   {"bci",	HB_TAG('B','A','U',' ')},	/* Baule */
    203   {"bcq",	HB_TAG('B','C','H',' ')},	/* Bench */
    204   {"be",	HB_TAG('B','E','L',' ')},  	/* Belarussian */
    205   {"bem",	HB_TAG('B','E','M',' ')},	/* Bemba (Zambia) */
    206   {"ber",	HB_TAG('B','E','R',' ')},  	/* Berber [family] */
    207   {"bfq",	HB_TAG('B','A','D',' ')},	/* Badaga */
    208   {"bft",	HB_TAG('B','L','T',' ')},	/* Balti */
    209   {"bfy",	HB_TAG('B','A','G',' ')},	/* Baghelkhandi */
    210   {"bg",	HB_TAG('B','G','R',' ')},	/* Bulgarian */
    211   {"bhb",	HB_TAG('B','H','I',' ')},	/* Bhili */
    212   {"bho",	HB_TAG('B','H','O',' ')},	/* Bhojpuri */
    213   {"bik",	HB_TAG('B','I','K',' ')},	/* Bikol */
    214   {"bin",	HB_TAG('E','D','O',' ')},	/* Bini */
    215   {"bjt",	HB_TAG('B','L','N',' ')},	/* Balanta-Ganja */
    216   {"bla",	HB_TAG('B','K','F',' ')},	/* Blackfoot */
    217   {"ble",	HB_TAG('B','L','N',' ')},	/* Balanta-Kentohe */
    218   {"bm",	HB_TAG('B','M','B',' ')},	/* Bambara */
    219   {"bn",	HB_TAG('B','E','N',' ')},	/* Bengali */
    220   {"bo",	HB_TAG('T','I','B',' ')},	/* Tibetan */
    221   {"br",	HB_TAG('B','R','E',' ')},	/* Breton */
    222   {"bra",	HB_TAG('B','R','I',' ')},	/* Braj Bhasha */
    223   {"brh",	HB_TAG('B','R','H',' ')},	/* Brahui */
    224   {"bs",	HB_TAG('B','O','S',' ')},	/* Bosnian */
    225   {"btb",	HB_TAG('B','T','I',' ')},	/* Beti (Cameroon) */
    226   {"bxr",	HB_TAG('R','B','U',' ')},	/* Russian Buriat */
    227   {"byn",	HB_TAG('B','I','L',' ')},	/* Bilen */
    228   {"ca",	HB_TAG('C','A','T',' ')},	/* Catalan */
    229   {"ce",	HB_TAG('C','H','E',' ')},	/* Chechen */
    230   {"ceb",	HB_TAG('C','E','B',' ')},	/* Cebuano */
    231   {"chp",	HB_TAG('C','H','P',' ')},	/* Chipewyan */
    232   {"chr",	HB_TAG('C','H','R',' ')},	/* Cherokee */
    233   {"ckt",	HB_TAG('C','H','K',' ')},	/* Chukchi */
    234   {"cop",	HB_TAG('C','O','P',' ')},	/* Coptic */
    235   {"cr",	HB_TAG('C','R','E',' ')},	/* Cree */
    236   {"crh",	HB_TAG('C','R','T',' ')},	/* Crimean Tatar */
    237   {"crj",	HB_TAG('E','C','R',' ')},	/* [Southern] East Cree */
    238   {"crl",	HB_TAG('E','C','R',' ')},	/* [Northern] East Cree */
    239   {"crm",	HB_TAG('M','C','R',' ')},	/* Moose Cree */
    240   {"crx",	HB_TAG('C','R','R',' ')},	/* Carrier */
    241   {"cs",	HB_TAG('C','S','Y',' ')},	/* Czech */
    242   {"cu",	HB_TAG('C','S','L',' ')},	/* Church Slavic */
    243   {"cv",	HB_TAG('C','H','U',' ')},	/* Chuvash */
    244   {"cwd",	HB_TAG('D','C','R',' ')},	/* Woods Cree */
    245   {"cy",	HB_TAG('W','E','L',' ')},	/* Welsh */
    246   {"da",	HB_TAG('D','A','N',' ')},	/* Danish */
    247   {"dap",	HB_TAG('N','I','S',' ')},	/* Nisi (India) */
    248   {"dar",	HB_TAG('D','A','R',' ')},	/* Dargwa */
    249   {"de",	HB_TAG('D','E','U',' ')},	/* German */
    250   {"din",	HB_TAG('D','N','K',' ')},	/* Dinka */
    251   {"dje",	HB_TAG('D','J','R',' ')},	/* Djerma */
    252   {"dng",	HB_TAG('D','U','N',' ')},	/* Dungan */
    253   {"doi",	HB_TAG('D','G','R',' ')},	/* Dogri */
    254   {"dsb",	HB_TAG('L','S','B',' ')},	/* Lower Sorbian */
    255   {"dv",	HB_TAG('D','I','V',' ')},	/* Dhivehi */
    256   {"dyu",	HB_TAG('J','U','L',' ')},	/* Jula */
    257   {"dz",	HB_TAG('D','Z','N',' ')},	/* Dzongkha */
    258   {"ee",	HB_TAG('E','W','E',' ')},	/* Ewe */
    259   {"efi",	HB_TAG('E','F','I',' ')},	/* Efik */
    260   {"el",	HB_TAG('E','L','L',' ')},	/* Modern Greek (1453-) */
    261   {"en",	HB_TAG('E','N','G',' ')},	/* English */
    262   {"eo",	HB_TAG('N','T','O',' ')},	/* Esperanto */
    263   {"eot",	HB_TAG('B','T','I',' ')},	/* Beti (Cte d'Ivoire) */
    264   {"es",	HB_TAG('E','S','P',' ')},	/* Spanish */
    265   {"et",	HB_TAG('E','T','I',' ')},	/* Estonian */
    266   {"eu",	HB_TAG('E','U','Q',' ')},	/* Basque */
    267   {"eve",	HB_TAG('E','V','N',' ')},	/* Even */
    268   {"evn",	HB_TAG('E','V','K',' ')},	/* Evenki */
    269   {"fa",	HB_TAG('F','A','R',' ')},	/* Persian */
    270   {"ff",	HB_TAG('F','U','L',' ')},	/* Fulah */
    271   {"fi",	HB_TAG('F','I','N',' ')},	/* Finnish */
    272   {"fil",	HB_TAG('P','I','L',' ')},	/* Filipino */
    273   {"fj",	HB_TAG('F','J','I',' ')},	/* Fijian */
    274   {"fo",	HB_TAG('F','O','S',' ')},	/* Faroese */
    275   {"fon",	HB_TAG('F','O','N',' ')},	/* Fon */
    276   {"fr",	HB_TAG('F','R','A',' ')},	/* French */
    277   {"fur",	HB_TAG('F','R','L',' ')},	/* Friulian */
    278   {"fy",	HB_TAG('F','R','I',' ')},	/* Western Frisian */
    279   {"ga",	HB_TAG('I','R','I',' ')},	/* Irish */
    280   {"gaa",	HB_TAG('G','A','D',' ')},	/* Ga */
    281   {"gag",	HB_TAG('G','A','G',' ')},	/* Gagauz */
    282   {"gbm",	HB_TAG('G','A','W',' ')},	/* Garhwali */
    283   {"gd",	HB_TAG('G','A','E',' ')},	/* Scottish Gaelic */
    284   {"gez",	HB_TAG('G','E','Z',' ')},	/* Ge'ez */
    285   {"gl",	HB_TAG('G','A','L',' ')},	/* Galician */
    286   {"gld",	HB_TAG('N','A','N',' ')},	/* Nanai */
    287   {"gn",	HB_TAG('G','U','A',' ')},	/* Guarani */
    288   {"gon",	HB_TAG('G','O','N',' ')},	/* Gondi */
    289   {"grt",	HB_TAG('G','R','O',' ')},	/* Garo */
    290   {"gru",	HB_TAG('S','O','G',' ')},	/* Sodo Gurage */
    291   {"gu",	HB_TAG('G','U','J',' ')},	/* Gujarati */
    292   {"guk",	HB_TAG('G','M','Z',' ')},	/* Gumuz */
    293   {"gv",	HB_TAG('M','N','X',' ')},	/* Manx Gaelic */
    294   {"ha",	HB_TAG('H','A','U',' ')},	/* Hausa */
    295   {"har",	HB_TAG('H','R','I',' ')},	/* Harari */
    296   {"haw",	HB_TAG('H','A','W',' ')},  	/* Hawaiin */
    297   {"he",	HB_TAG('I','W','R',' ')},	/* Hebrew */
    298   {"hi",	HB_TAG('H','I','N',' ')},	/* Hindi */
    299   {"hil",	HB_TAG('H','I','L',' ')},	/* Hiligaynon */
    300   {"hnd",	HB_TAG('H','N','D',' ')},	/* [Southern] Hindko */
    301   {"hne",	HB_TAG('C','H','H',' ')},	/* Chattisgarhi */
    302   {"hno",	HB_TAG('H','N','D',' ')},	/* [Northern] Hindko */
    303   {"hoc",	HB_TAG('H','O',' ',' ')},	/* Ho */
    304   {"hoj",	HB_TAG('H','A','R',' ')},	/* Harauti */
    305   {"hr",	HB_TAG('H','R','V',' ')},	/* Croatian */
    306   {"hsb",	HB_TAG('U','S','B',' ')},	/* Upper Sorbian */
    307   {"ht",	HB_TAG('H','A','I',' ')},	/* Haitian */
    308   {"hu",	HB_TAG('H','U','N',' ')},	/* Hungarian */
    309   {"hy",	HB_TAG('H','Y','E',' ')},	/* Armenian */
    310   {"id",	HB_TAG('I','N','D',' ')},	/* Indonesian */
    311   {"ig",	HB_TAG('I','B','O',' ')},	/* Igbo */
    312   {"igb",	HB_TAG('E','B','I',' ')},	/* Ebira */
    313   {"ijo",	HB_TAG('I','J','O',' ')},	/* Ijo [family] */
    314   {"ilo",	HB_TAG('I','L','O',' ')},	/* Ilokano */
    315   {"inh",	HB_TAG('I','N','G',' ')},	/* Ingush */
    316   {"is",	HB_TAG('I','S','L',' ')},	/* Icelandic */
    317   {"it",	HB_TAG('I','T','A',' ')},	/* Italian */
    318   {"iu",	HB_TAG('I','N','U',' ')},	/* Inuktitut */
    319   {"ja",	HB_TAG('J','A','N',' ')},	/* Japanese */
    320   {"jv",	HB_TAG('J','A','V',' ')},	/* Javanese */
    321   {"ka",	HB_TAG('K','A','T',' ')},	/* Georgian */
    322   {"kaa",	HB_TAG('K','R','K',' ')},	/* Karakalpak */
    323   {"kam",	HB_TAG('K','M','B',' ')},	/* Kamba (Kenya) */
    324   {"kar",	HB_TAG('K','R','N',' ')},	/* Karen [family] */
    325   {"kbd",	HB_TAG('K','A','B',' ')},	/* Kabardian */
    326   {"kdr",	HB_TAG('K','R','M',' ')},	/* Karaim */
    327   {"kdt",	HB_TAG('K','U','Y',' ')},	/* Kuy */
    328   {"kex",	HB_TAG('K','K','N',' ')},	/* Kokni */
    329   {"kfr",	HB_TAG('K','A','C',' ')},	/* Kachchi */
    330   {"kfy",	HB_TAG('K','M','N',' ')},	/* Kumaoni */
    331   {"kha",	HB_TAG('K','S','I',' ')},	/* Khasi */
    332   {"khb",	HB_TAG('X','B','D',' ')},	/* Tai Lue */
    333   {"khw",	HB_TAG('K','H','W',' ')},	/* Khowar */
    334   {"ki",	HB_TAG('K','I','K',' ')},	/* Kikuyu */
    335   {"kjh",	HB_TAG('K','H','A',' ')},	/* Khakass */
    336   {"kk",	HB_TAG('K','A','Z',' ')},	/* Kazakh */
    337   {"kl",	HB_TAG('G','R','N',' ')},	/* Kalaallisut */
    338   {"kln",	HB_TAG('K','A','L',' ')},	/* Kalenjin */
    339   {"km",	HB_TAG('K','H','M',' ')},	/* Central Khmer */
    340   {"kmb",	HB_TAG('M','B','N',' ')},	/* [North] Mbundu */
    341   {"kmw",	HB_TAG('K','M','O',' ')},	/* Komo (Democratic Republic of Congo) */
    342   {"kn",	HB_TAG('K','A','N',' ')},	/* Kannada */
    343   {"ko",	HB_TAG('K','O','R',' ')},	/* Korean */
    344   {"koi",	HB_TAG('K','O','P',' ')},	/* Komi-Permyak */
    345   {"kok",	HB_TAG('K','O','K',' ')},	/* Konkani */
    346   {"kpe",	HB_TAG('K','P','L',' ')},	/* Kpelle */
    347   {"kpv",	HB_TAG('K','O','Z',' ')},	/* Komi-Zyrian */
    348   {"kpy",	HB_TAG('K','Y','K',' ')},	/* Koryak */
    349   {"kqy",	HB_TAG('K','R','T',' ')},	/* Koorete */
    350   {"kr",	HB_TAG('K','N','R',' ')},	/* Kanuri */
    351   {"kri",	HB_TAG('K','R','I',' ')},	/* Krio */
    352   {"krl",	HB_TAG('K','R','L',' ')},	/* Karelian */
    353   {"kru",	HB_TAG('K','U','U',' ')},	/* Kurukh */
    354   {"ks",	HB_TAG('K','S','H',' ')},	/* Kashmiri */
    355   {"ku",	HB_TAG('K','U','R',' ')},	/* Kurdish */
    356   {"kum",	HB_TAG('K','U','M',' ')},	/* Kumyk */
    357   {"kvd",	HB_TAG('K','U','I',' ')},	/* Kui (Indonesia) */
    358   {"kxc",	HB_TAG('K','M','S',' ')},	/* Komso */
    359   {"kxu",	HB_TAG('K','U','I',' ')},	/* Kui (India) */
    360   {"ky",	HB_TAG('K','I','R',' ')},	/* Kirghiz */
    361   {"la",	HB_TAG('L','A','T',' ')},	/* Latin */
    362   {"lad",	HB_TAG('J','U','D',' ')},	/* Ladino */
    363   {"lb",	HB_TAG('L','T','Z',' ')},	/* Luxembourgish */
    364   {"lbe",	HB_TAG('L','A','K',' ')},	/* Lak */
    365   {"lbj",	HB_TAG('L','D','K',' ')},	/* Ladakhi */
    366   {"lez",	HB_TAG('L','E','Z',' ')},	/* Lezgi */
    367   {"lg",	HB_TAG('L','U','G',' ')},	/* Luganda */
    368   {"lif",	HB_TAG('L','M','B',' ')},	/* Limbu */
    369   {"lld",	HB_TAG('L','A','D',' ')},	/* Ladin */
    370   {"lmn",	HB_TAG('L','A','M',' ')},	/* Lambani */
    371   {"ln",	HB_TAG('L','I','N',' ')},	/* Lingala */
    372   {"lo",	HB_TAG('L','A','O',' ')},	/* Lao */
    373   {"lt",	HB_TAG('L','T','H',' ')},	/* Lithuanian */
    374   {"lu",	HB_TAG('L','U','B',' ')},	/* Luba-Katanga */
    375   {"lua",	HB_TAG('L','U','B',' ')},	/* Luba-Kasai */
    376   {"luo",	HB_TAG('L','U','O',' ')},	/* Luo (Kenya and Tanzania) */
    377   {"lus",	HB_TAG('M','I','Z',' ')},	/* Mizo */
    378   {"luy",	HB_TAG('L','U','H',' ')},	/* Luhya [macrolanguage] */
    379   {"lv",	HB_TAG('L','V','I',' ')},	/* Latvian */
    380   {"lzz",	HB_TAG('L','A','Z',' ')},	/* Laz */
    381   {"mai",	HB_TAG('M','T','H',' ')},	/* Maithili */
    382   {"mdc",	HB_TAG('M','L','E',' ')},	/* Male (Papua New Guinea) */
    383   {"mdf",	HB_TAG('M','O','K',' ')},	/* Moksha */
    384   {"mdy",	HB_TAG('M','L','E',' ')},	/* Male (Ethiopia) */
    385   {"men",	HB_TAG('M','D','E',' ')},	/* Mende (Sierra Leone) */
    386   {"mg",	HB_TAG('M','L','G',' ')},	/* Malagasy */
    387   {"mhr",	HB_TAG('L','M','A',' ')},	/* Low Mari */
    388   {"mi",	HB_TAG('M','R','I',' ')},	/* Maori */
    389   {"mk",	HB_TAG('M','K','D',' ')},	/* Macedonian */
    390   {"ml",	HB_TAG('M','L','R',' ')},	/* Malayalam */
    391   {"mn",	HB_TAG('M','N','G',' ')},	/* Mongolian */
    392   {"mnc",	HB_TAG('M','C','H',' ')},	/* Manchu */
    393   {"mni",	HB_TAG('M','N','I',' ')},	/* Manipuri */
    394   {"mnk",	HB_TAG('M','N','D',' ')},	/* Mandinka */
    395   {"mns",	HB_TAG('M','A','N',' ')},	/* Mansi */
    396   {"mnw",	HB_TAG('M','O','N',' ')},	/* Mon */
    397   {"mo",	HB_TAG('M','O','L',' ')},	/* Moldavian */
    398   {"moh",	HB_TAG('M','O','H',' ')},	/* Mohawk */
    399   {"mpe",	HB_TAG('M','A','J',' ')},	/* Majang */
    400   {"mr",	HB_TAG('M','A','R',' ')},	/* Marathi */
    401   {"mrj",	HB_TAG('H','M','A',' ')},	/* High Mari */
    402   {"ms",	HB_TAG('M','L','Y',' ')},	/* Malay */
    403   {"mt",	HB_TAG('M','T','S',' ')},	/* Maltese */
    404   {"mwr",	HB_TAG('M','A','W',' ')},	/* Marwari */
    405   {"my",	HB_TAG('B','R','M',' ')},	/* Burmese */
    406   {"mym",	HB_TAG('M','E','N',' ')},	/* Me'en */
    407   {"myv",	HB_TAG('E','R','Z',' ')},	/* Erzya */
    408   {"nag",	HB_TAG('N','A','G',' ')},	/* Naga-Assamese */
    409   {"nb",	HB_TAG('N','O','R',' ')},	/* Norwegian Bokml */
    410   {"nco",	HB_TAG('S','I','B',' ')},	/* Sibe */
    411   {"nd",	HB_TAG('N','D','B',' ')},	/* [North] Ndebele */
    412   {"ne",	HB_TAG('N','E','P',' ')},	/* Nepali */
    413   {"new",	HB_TAG('N','E','W',' ')},	/* Newari */
    414   {"ng",	HB_TAG('N','D','G',' ')},	/* Ndonga */
    415   {"ngl",	HB_TAG('L','M','W',' ')},	/* Lomwe */
    416   {"niu",	HB_TAG('N','I','U',' ')},	/* Niuean */
    417   {"niv",	HB_TAG('G','I','L',' ')},	/* Gilyak */
    418   {"nl",	HB_TAG('N','L','D',' ')},	/* Dutch */
    419   {"nn",	HB_TAG('N','Y','N',' ')},	/* Norwegian Nynorsk */
    420   {"no",	HB_TAG('N','O','R',' ')},	/* Norwegian (deprecated) */
    421   {"nod",	HB_TAG('N','T','A',' ')},	/* Northern Tai */
    422   {"nog",	HB_TAG('N','O','G',' ')},	/* Nogai */
    423   {"nqo",	HB_TAG('N','K','O',' ')},	/* N'Ko */
    424   {"nr",	HB_TAG('N','D','B',' ')},	/* [South] Ndebele */
    425   {"nsk",	HB_TAG('N','A','S',' ')},	/* Naskapi */
    426   {"nso",	HB_TAG('S','O','T',' ')},	/* [Northern] Sotho */
    427   {"ny",	HB_TAG('C','H','I',' ')},	/* Nyanja */
    428   {"nyn",	HB_TAG('N','K','L',' ')},	/* Nkole */
    429   {"oc",	HB_TAG('O','C','I',' ')},	/* Occitan (post 1500) */
    430   {"oj",	HB_TAG('O','J','B',' ')},	/* Ojibwa */
    431   {"ojs",	HB_TAG('O','C','R',' ')},	/* Oji-Cree */
    432   {"om",	HB_TAG('O','R','O',' ')},	/* Oromo */
    433   {"or",	HB_TAG('O','R','I',' ')},	/* Oriya */
    434   {"os",	HB_TAG('O','S','S',' ')},	/* Ossetian */
    435   {"pa",	HB_TAG('P','A','N',' ')},	/* Panjabi */
    436   {"pce",	HB_TAG('P','L','G',' ')},	/* [Ruching] Palaung */
    437   {"pi",	HB_TAG('P','A','L',' ')},	/* Pali */
    438   {"pl",	HB_TAG('P','L','K',' ')},	/* Polish */
    439   {"pll",	HB_TAG('P','L','G',' ')},	/* [Shwe] Palaung */
    440   {"plp",	HB_TAG('P','A','P',' ')},	/* Palpa */
    441   {"prs",	HB_TAG('D','R','I',' ')},	/* Dari */
    442   {"ps",	HB_TAG('P','A','S',' ')},	/* Pushto */
    443   {"pt",	HB_TAG('P','T','G',' ')},	/* Portuguese */
    444   {"raj",	HB_TAG('R','A','J',' ')},	/* Rajasthani */
    445   {"rbb",	HB_TAG('P','L','G',' ')},	/* [Rumai] Palaung */
    446   {"ria",	HB_TAG('R','I','A',' ')},	/* Riang (India) */
    447   {"ril",	HB_TAG('R','I','A',' ')},	/* Riang (Myanmar) */
    448   {"rki",	HB_TAG('A','R','K',' ')},	/* Arakanese */
    449   {"rm",	HB_TAG('R','M','S',' ')},	/* Rhaeto-Romanic */
    450   {"ro",	HB_TAG('R','O','M',' ')},	/* Romanian */
    451   {"rom",	HB_TAG('R','O','Y',' ')},	/* Romany */
    452   {"ru",	HB_TAG('R','U','S',' ')},	/* Russian */
    453   {"rue",	HB_TAG('R','S','Y',' ')},	/* Rusyn */
    454   {"rw",	HB_TAG('R','U','A',' ')},	/* Ruanda */
    455   {"sa",	HB_TAG('S','A','N',' ')},	/* Sanskrit */
    456   {"sah",	HB_TAG('Y','A','K',' ')},	/* Yakut */
    457   {"sat",	HB_TAG('S','A','T',' ')},	/* Santali */
    458   {"sck",	HB_TAG('S','A','D',' ')},	/* Sadri */
    459   {"scs",	HB_TAG('S','L','A',' ')},	/* [North] Slavey */
    460   {"sd",	HB_TAG('S','N','D',' ')},	/* Sindhi */
    461   {"se",	HB_TAG('N','S','M',' ')},	/* Northern Sami */
    462   {"seh",	HB_TAG('S','N','A',' ')},	/* Sena */
    463   {"sel",	HB_TAG('S','E','L',' ')},	/* Selkup */
    464   {"sg",	HB_TAG('S','G','O',' ')},	/* Sango */
    465   {"shn",	HB_TAG('S','H','N',' ')},	/* Shan */
    466   {"si",	HB_TAG('S','N','H',' ')},	/* Sinhala */
    467   {"sid",	HB_TAG('S','I','D',' ')},	/* Sidamo */
    468   {"sjd",	HB_TAG('K','S','M',' ')},	/* Kildin Sami */
    469   {"sk",	HB_TAG('S','K','Y',' ')},	/* Slovak */
    470   {"skr",	HB_TAG('S','R','K',' ')},	/* Seraiki */
    471   {"sl",	HB_TAG('S','L','V',' ')},	/* Slovenian */
    472   {"sm",	HB_TAG('S','M','O',' ')},	/* Samoan */
    473   {"sma",	HB_TAG('S','S','M',' ')},	/* Southern Sami */
    474   {"smj",	HB_TAG('L','S','M',' ')},	/* Lule Sami */
    475   {"smn",	HB_TAG('I','S','M',' ')},	/* Inari Sami */
    476   {"sms",	HB_TAG('S','K','S',' ')},	/* Skolt Sami */
    477   {"snk",	HB_TAG('S','N','K',' ')},	/* Soninke */
    478   {"so",	HB_TAG('S','M','L',' ')},	/* Somali */
    479   {"sq",	HB_TAG('S','Q','I',' ')},	/* Albanian */
    480   {"sr",	HB_TAG('S','R','B',' ')},	/* Serbian */
    481   {"srr",	HB_TAG('S','R','R',' ')},	/* Serer */
    482   {"ss",	HB_TAG('S','W','Z',' ')},	/* Swazi */
    483   {"st",	HB_TAG('S','O','T',' ')},	/* [Southern] Sotho */
    484   {"suq",	HB_TAG('S','U','R',' ')},	/* Suri */
    485   {"sv",	HB_TAG('S','V','E',' ')},	/* Swedish */
    486   {"sva",	HB_TAG('S','V','A',' ')},	/* Svan */
    487   {"sw",	HB_TAG('S','W','K',' ')},	/* Swahili */
    488   {"swb",	HB_TAG('C','M','R',' ')},	/* Comorian */
    489   {"syr",	HB_TAG('S','Y','R',' ')},	/* Syriac */
    490   {"ta",	HB_TAG('T','A','M',' ')},	/* Tamil */
    491   {"tab",	HB_TAG('T','A','B',' ')},	/* Tabasaran */
    492   {"tcy",	HB_TAG('T','U','L',' ')},	/* Tulu */
    493   {"te",	HB_TAG('T','E','L',' ')},	/* Telugu */
    494   {"tem",	HB_TAG('T','M','N',' ')},	/* Temne */
    495   {"tg",	HB_TAG('T','A','J',' ')},	/* Tajik */
    496   {"th",	HB_TAG('T','H','A',' ')},	/* Thai */
    497   {"ti",	HB_TAG('T','G','Y',' ')},	/* Tigrinya */
    498   {"tig",	HB_TAG('T','G','R',' ')},	/* Tigre */
    499   {"tk",	HB_TAG('T','K','M',' ')},	/* Turkmen */
    500   {"tn",	HB_TAG('T','N','A',' ')},	/* Tswana */
    501   {"to",	HB_TAG('T','G','N',' ')},	/* Tonga (Tonga Islands) */
    502   {"tr",	HB_TAG('T','R','K',' ')},	/* Turkish */
    503   {"tru",	HB_TAG('T','U','A',' ')},	/* Turoyo Aramaic */
    504   {"ts",	HB_TAG('T','S','G',' ')},	/* Tsonga */
    505   {"tt",	HB_TAG('T','A','T',' ')},	/* Tatar */
    506   {"tw",	HB_TAG('T','W','I',' ')},	/* Twi */
    507   {"ty",	HB_TAG('T','H','T',' ')},	/* Tahitian */
    508   {"tyv",	HB_TAG('T','U','V',' ')},	/* Tuvin */
    509   {"udm",	HB_TAG('U','D','M',' ')},	/* Udmurt */
    510   {"ug",	HB_TAG('U','Y','G',' ')},	/* Uighur */
    511   {"uk",	HB_TAG('U','K','R',' ')},	/* Ukrainian */
    512   {"umb",	HB_TAG('M','B','N',' ')},	/* [South] Mbundu */
    513   {"unr",	HB_TAG('M','U','N',' ')},	/* Mundari */
    514   {"ur",	HB_TAG('U','R','D',' ')},	/* Urdu */
    515   {"uz",	HB_TAG('U','Z','B',' ')},	/* Uzbek */
    516   {"ve",	HB_TAG('V','E','N',' ')},	/* Venda */
    517   {"vi",	HB_TAG('V','I','T',' ')},	/* Vietnamese */
    518   {"vmw",	HB_TAG('M','A','K',' ')},	/* Makua */
    519   {"wbm",	HB_TAG('W','A',' ',' ')},	/* Wa */
    520   {"wbr",	HB_TAG('W','A','G',' ')},	/* Wagdi */
    521   {"wo",	HB_TAG('W','L','F',' ')},	/* Wolof */
    522   {"xal",	HB_TAG('K','L','M',' ')},	/* Kalmyk */
    523   {"xh",	HB_TAG('X','H','S',' ')},	/* Xhosa */
    524   {"xom",	HB_TAG('K','M','O',' ')},	/* Komo (Sudan) */
    525   {"xsl",	HB_TAG('S','S','L',' ')},	/* South Slavey */
    526   {"yi",	HB_TAG('J','I','I',' ')},	/* Yiddish */
    527   {"yo",	HB_TAG('Y','B','A',' ')},	/* Yoruba */
    528   {"yso",	HB_TAG('N','I','S',' ')},	/* Nisi (China) */
    529   {"zne",	HB_TAG('Z','N','D',' ')},	/* Zande */
    530   {"zu",	HB_TAG('Z','U','L',' ')} 	/* Zulu */
    531 
    532   /* The corresponding languages IDs for the following IDs are unclear,
    533    * overlap, or are architecturally weird. Needs more research. */
    534 
    535 /*{"ahg/awn/xan?",	HB_TAG('A','G','W',' ')},*/	/* Agaw */
    536 /*{"gsw?/gsw-FR?",	HB_TAG('A','L','S',' ')},*/	/* Alsatian */
    537 /*{"krc",	HB_TAG('B','A','L',' ')},*/	/* Balkar */
    538 /*{"??",	HB_TAG('B','C','R',' ')},*/	/* Bible Cree */
    539 /*{"sgw?",	HB_TAG('C','H','G',' ')},*/	/* Chaha Gurage */
    540 /*{"acf/gcf?",	HB_TAG('F','A','N',' ')},*/	/* French Antillean */
    541 /*{"vls/nl-be",	HB_TAG('F','L','E',' ')},*/	/* Flemish */
    542 /*{"enf?/yrk?",	HB_TAG('F','N','E',' ')},*/	/* Forest Nenets */
    543 /*{"fuf?",	HB_TAG('F','T','A',' ')},*/	/* Futa */
    544 /*{"ar-Syrc?",	HB_TAG('G','A','R',' ')},*/	/* Garshuni */
    545 /*{"cfm/rnl?",	HB_TAG('H','A','L',' ')},*/	/* Halam */
    546 /*{"ga-Latg?/Latg?",	HB_TAG('I','R','T',' ')},*/	/* Irish Traditional */
    547 /*{"krc",	HB_TAG('K','A','R',' ')},*/	/* Karachay */
    548 /*{"alw?/ktb?",	HB_TAG('K','E','B',' ')},*/	/* Kebena */
    549 /*{"Geok",	HB_TAG('K','G','E',' ')},*/	/* Khutsuri Georgian */
    550 /*{"kca",	HB_TAG('K','H','K',' ')},*/	/* Khanty-Kazim */
    551 /*{"kca",	HB_TAG('K','H','S',' ')},*/	/* Khanty-Shurishkar */
    552 /*{"kca",	HB_TAG('K','H','V',' ')},*/	/* Khanty-Vakhi */
    553 /*{"guz?/kqs?/kss?",	HB_TAG('K','I','S',' ')},*/	/* Kisii */
    554 /*{"kfa/kfi?/kpb?/xua?/xuj?",	HB_TAG('K','O','D',' ')},*/	/* Kodagu */
    555 /*{"okm?/oko?",	HB_TAG('K','O','H',' ')},*/	/* Korean Old Hangul */
    556 /*{"kon?/ktu?/...",	HB_TAG('K','O','N',' ')},*/	/* Kikongo */
    557 /*{"kfx?",	HB_TAG('K','U','L',' ')},*/	/* Kulvi */
    558 /*{"??",	HB_TAG('L','A','H',' ')},*/	/* Lahuli */
    559 /*{"??",	HB_TAG('L','C','R',' ')},*/	/* L-Cree */
    560 /*{"??",	HB_TAG('M','A','L',' ')},*/	/* Malayalam Traditional */
    561 /*{"mnk?/mlq?/...",	HB_TAG('M','L','N',' ')},*/	/* Malinke */
    562 /*{"man?/myq?/mku?/msc?/...",	HB_TAG('M','N','K',' ')},*/	/* Maninka */
    563 /*{"??",	HB_TAG('M','O','R',' ')},*/	/* Moroccan */
    564 /*{"??",	HB_TAG('N','C','R',' ')},*/	/* N-Cree */
    565 /*{"??",	HB_TAG('N','H','C',' ')},*/	/* Norway House Cree */
    566 /*{"jpa?/sam?",	HB_TAG('P','A','A',' ')},*/	/* Palestinian Aramaic */
    567 /*{"polyton",	HB_TAG('P','G','R',' ')},*/	/* Polytonic Greek */
    568 /*{"??",	HB_TAG('Q','I','N',' ')},*/	/* Asho Chin */
    569 /*{"??",	HB_TAG('R','C','R',' ')},*/	/* R-Cree */
    570 /*{"chp?",	HB_TAG('S','A','Y',' ')},*/	/* Sayisi */
    571 /*{"xan?",	HB_TAG('S','E','K',' ')},*/	/* Sekota */
    572 /*{"stv/wle?/xst?",	HB_TAG('S','I','G',' ')},*/	/* Silte Gurage */
    573 /*{"ngo?",	HB_TAG('S','X','T',' ')},*/	/* Sutu */
    574 /*{"??",	HB_TAG('T','C','R',' ')},*/	/* TH-Cree */
    575 /*{"tnz?/tog?/toi?",	HB_TAG('T','N','G',' ')},*/	/* Tonga */
    576 /*{"enh?/yrk?",	HB_TAG('T','N','E',' ')},*/	/* Tundra Nenets */
    577 /*{"??",	HB_TAG('T','O','D',' ')},*/	/* Todo */
    578 /*{"??",	HB_TAG('W','C','R',' ')},*/	/* West-Cree */
    579 /*{"??",	HB_TAG('Y','C','R',' ')},*/	/* Y-Cree */
    580 /*{"??",	HB_TAG('Y','I','C',' ')},*/	/* Yi Classic */
    581 /*{"ii?/Yiii?",	HB_TAG('Y','I','M',' ')},*/	/* Yi Modern */
    582 /*{"??",	HB_TAG('Z','H','P',' ')},*/	/* Chinese Phonetic */
    583 };
    584 
    585 static const LangTag ot_languages_zh[] = {
    586   {"zh-cn",	HB_TAG('Z','H','S',' ')},	/* Chinese (China) */
    587   {"zh-hk",	HB_TAG('Z','H','H',' ')},	/* Chinese (Hong Kong) */
    588   {"zh-mo",	HB_TAG('Z','H','T',' ')},	/* Chinese (Macao) */
    589   {"zh-sg",	HB_TAG('Z','H','S',' ')},	/* Chinese (Singapore) */
    590   {"zh-tw",	HB_TAG('Z','H','T',' ')} 	/* Chinese (Taiwan) */
    591 };
    592 
    593 static int
    594 lang_compare_first_component (const char *a,
    595 			      const char *b)
    596 {
    597   unsigned int da, db;
    598   const char *p;
    599 
    600   p = strchr (a, '-');
    601   da = p ? (unsigned int) (p - a) : strlen (a);
    602 
    603   p = strchr (b, '-');
    604   db = p ? (unsigned int) (p - b) : strlen (b);
    605 
    606   return strncmp (a, b, MAX (da, db));
    607 }
    608 
    609 static hb_bool_t
    610 lang_matches (const char *lang_str, const char *spec)
    611 {
    612   unsigned int len = strlen (spec);
    613 
    614   return strncmp (lang_str, spec, len) == 0 &&
    615 	 (lang_str[len] == '\0' || lang_str[len] == '-');
    616 }
    617 
    618 hb_tag_t
    619 hb_ot_tag_from_language (hb_language_t language)
    620 {
    621   const char *lang_str, *s;
    622   const LangTag *lang_tag;
    623 
    624   if (language == HB_LANGUAGE_INVALID)
    625     return HB_OT_TAG_DEFAULT_LANGUAGE;
    626 
    627   lang_str = hb_language_to_string (language);
    628 
    629   s = strstr (lang_str, "x-hbot");
    630   if (s) {
    631     char tag[4];
    632     int i;
    633     s += 6;
    634     for (i = 0; i < 4 && ISALPHA (s[i]); i++)
    635       tag[i] = TOUPPER (s[i]);
    636     if (i) {
    637       for (; i < 4; i++)
    638 	tag[i] = ' ';
    639       return HB_TAG_CHAR4 (tag);
    640     }
    641   }
    642 
    643   /* Find a language matching in the first component */
    644   lang_tag = (LangTag *) bsearch (lang_str, ot_languages,
    645 				  ARRAY_LENGTH (ot_languages), sizeof (LangTag),
    646 				  (hb_compare_func_t) lang_compare_first_component);
    647   if (lang_tag)
    648     return lang_tag->tag;
    649 
    650   /* Otherwise, check the Chinese ones */
    651   if (0 == lang_compare_first_component (lang_str, "zh"))
    652   {
    653     unsigned int i;
    654 
    655     for (i = 0; i < ARRAY_LENGTH (ot_languages_zh); i++)
    656     {
    657       lang_tag = &ot_languages_zh[i];
    658       if (lang_matches (lang_tag->language, lang_str))
    659 	return lang_tag->tag;
    660     }
    661 
    662     /* Otherwise just return 'ZHS ' */
    663     return HB_TAG('Z','H','S',' ');
    664   }
    665 
    666   s = strchr (lang_str, '-');
    667   if (!s)
    668     s = lang_str + strlen (lang_str);
    669   if (s - lang_str == 3) {
    670     /* Assume it's ISO-639-3 and upper-case and use it. */
    671     return hb_tag_from_string (lang_str, s - lang_str) & ~0x20202000;
    672   }
    673 
    674   return HB_OT_TAG_DEFAULT_LANGUAGE;
    675 }
    676 
    677 hb_language_t
    678 hb_ot_tag_to_language (hb_tag_t tag)
    679 {
    680   unsigned int i;
    681 
    682   if (tag == HB_OT_TAG_DEFAULT_LANGUAGE)
    683     return NULL;
    684 
    685   for (i = 0; i < ARRAY_LENGTH (ot_languages); i++)
    686     if (ot_languages[i].tag == tag)
    687       return hb_language_from_string (ot_languages[i].language, -1);
    688 
    689   /* If tag starts with ZH, it's Chinese */
    690   if ((tag & 0xFFFF0000)  == 0x5A480000) {
    691     switch (tag) {
    692       case HB_TAG('Z','H','H',' '): return hb_language_from_string ("zh-hk", -1); /* Hong Kong */
    693       default: {
    694         /* Encode the tag... */
    695 	unsigned char buf[14] = "zh-x-hbot";
    696 	buf[9] = tag >> 24;
    697 	buf[10] = (tag >> 16) & 0xFF;
    698 	buf[11] = (tag >> 8) & 0xFF;
    699 	buf[12] = tag & 0xFF;
    700 	if (buf[12] == 0x20)
    701 	  buf[12] = '\0';
    702 	buf[13] = '\0';
    703 	return hb_language_from_string ((char *) buf, -1);
    704       }
    705     }
    706   }
    707 
    708   /* Else return a custom language in the form of "x-hbotABCD" */
    709   {
    710     unsigned char buf[11] = "x-hbot";
    711     buf[6] = tag >> 24;
    712     buf[7] = (tag >> 16) & 0xFF;
    713     buf[8] = (tag >> 8) & 0xFF;
    714     buf[9] = tag & 0xFF;
    715     if (buf[9] == 0x20)
    716       buf[9] = '\0';
    717     buf[10] = '\0';
    718     return hb_language_from_string ((char *) buf, -1);
    719   }
    720 }
    721 
    722 
    723