Behdad Esfahbod | 8a3511a | 2009-11-04 19:45:39 -0500 | [diff] [blame] | 1 | /* |
Behdad Esfahbod | 2409d5f | 2011-04-21 17:14:28 -0400 | [diff] [blame] | 2 | * Copyright © 2009 Red Hat, Inc. |
| 3 | * Copyright © 2011 Google, Inc. |
Behdad Esfahbod | 8a3511a | 2009-11-04 19:45:39 -0500 | [diff] [blame] | 4 | * |
Behdad Esfahbod | c755cb3 | 2010-04-22 00:11:43 -0400 | [diff] [blame] | 5 | * This is part of HarfBuzz, a text shaping library. |
Behdad Esfahbod | 8a3511a | 2009-11-04 19:45:39 -0500 | [diff] [blame] | 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 |
Behdad Esfahbod | ba2d543 | 2012-12-08 19:28:41 -0500 | [diff] [blame] | 26 | * Google Author(s): Behdad Esfahbod, Roozbeh Pournader |
Behdad Esfahbod | 8a3511a | 2009-11-04 19:45:39 -0500 | [diff] [blame] | 27 | */ |
| 28 | |
Behdad Esfahbod | c77ae40 | 2018-08-25 22:36:36 -0700 | [diff] [blame] | 29 | #include "hb.hh" |
Behdad Esfahbod | 8a3511a | 2009-11-04 19:45:39 -0500 | [diff] [blame] | 30 | |
Behdad Esfahbod | 8a3511a | 2009-11-04 19:45:39 -0500 | [diff] [blame] | 31 | |
Behdad Esfahbod | 62879ee | 2011-04-18 23:40:21 -0400 | [diff] [blame] | 32 | /* hb_script_t */ |
| 33 | |
| 34 | static hb_tag_t |
| 35 | hb_ot_old_tag_from_script (hb_script_t script) |
| 36 | { |
Behdad Esfahbod | 10a3329 | 2012-11-02 13:38:55 -0700 | [diff] [blame] | 37 | /* This seems to be accurate as of end of 2012. */ |
| 38 | |
Behdad Esfahbod | 5646dcb | 2018-10-11 19:39:07 -0400 | [diff] [blame] | 39 | switch ((hb_tag_t) script) |
| 40 | { |
Behdad Esfahbod | 40b5c2e | 2011-05-13 22:46:36 -0400 | [diff] [blame] | 41 | case HB_SCRIPT_INVALID: return HB_OT_TAG_DEFAULT_SCRIPT; |
| 42 | |
| 43 | /* KATAKANA and HIRAGANA both map to 'kana' */ |
Behdad Esfahbod | 62879ee | 2011-04-18 23:40:21 -0400 | [diff] [blame] | 44 | case HB_SCRIPT_HIRAGANA: return HB_TAG('k','a','n','a'); |
Behdad Esfahbod | 40b5c2e | 2011-05-13 22:46:36 -0400 | [diff] [blame] | 45 | |
| 46 | /* Spaces at the end are preserved, unlike ISO 15924 */ |
Behdad Esfahbod | 62879ee | 2011-04-18 23:40:21 -0400 | [diff] [blame] | 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',' '); |
Behdad Esfahbod | 62879ee | 2011-04-18 23:40:21 -0400 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | /* Else, just change first char to lowercase and return */ |
Behdad Esfahbod | 7627100 | 2014-07-11 14:54:42 -0400 | [diff] [blame] | 56 | return ((hb_tag_t) script) | 0x20000000u; |
Behdad Esfahbod | 62879ee | 2011-04-18 23:40:21 -0400 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | static hb_script_t |
| 60 | hb_ot_old_tag_to_script (hb_tag_t tag) |
| 61 | { |
Behdad Esfahbod | 40b5c2e | 2011-05-13 22:46:36 -0400 | [diff] [blame] | 62 | if (unlikely (tag == HB_OT_TAG_DEFAULT_SCRIPT)) |
| 63 | return HB_SCRIPT_INVALID; |
Behdad Esfahbod | 62879ee | 2011-04-18 23:40:21 -0400 | [diff] [blame] | 64 | |
Behdad Esfahbod | 40b5c2e | 2011-05-13 22:46:36 -0400 | [diff] [blame] | 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' */ |
Behdad Esfahbod | 7627100 | 2014-07-11 14:54:42 -0400 | [diff] [blame] | 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 */ |
Behdad Esfahbod | 40b5c2e | 2011-05-13 22:46:36 -0400 | [diff] [blame] | 73 | |
| 74 | /* Change first char to uppercase and return */ |
Behdad Esfahbod | 7627100 | 2014-07-11 14:54:42 -0400 | [diff] [blame] | 75 | return (hb_script_t) (tag & ~0x20000000u); |
Behdad Esfahbod | 62879ee | 2011-04-18 23:40:21 -0400 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | static hb_tag_t |
| 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'); |
Behdad Esfahbod | de796a6 | 2012-11-12 17:27:51 -0800 | [diff] [blame] | 91 | case HB_SCRIPT_MYANMAR: return HB_TAG('m','y','m','2'); |
Behdad Esfahbod | 62879ee | 2011-04-18 23:40:21 -0400 | [diff] [blame] | 92 | } |
| 93 | |
Behdad Esfahbod | 40b5c2e | 2011-05-13 22:46:36 -0400 | [diff] [blame] | 94 | return HB_OT_TAG_DEFAULT_SCRIPT; |
Behdad Esfahbod | 62879ee | 2011-04-18 23:40:21 -0400 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | static hb_script_t |
| 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; |
Behdad Esfahbod | de796a6 | 2012-11-12 17:27:51 -0800 | [diff] [blame] | 110 | case HB_TAG('m','y','m','2'): return HB_SCRIPT_MYANMAR; |
Behdad Esfahbod | 62879ee | 2011-04-18 23:40:21 -0400 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | return HB_SCRIPT_UNKNOWN; |
| 114 | } |
| 115 | |
David Corbett | 9106771 | 2017-12-08 11:21:14 -0500 | [diff] [blame] | 116 | void |
| 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]; |
David Corbett | 7f1fbfe | 2018-07-23 21:19:23 -0400 | [diff] [blame] | 123 | hb_ot_tags_from_script_and_language (script, HB_LANGUAGE_INVALID, &count, tags, nullptr, nullptr); |
David Corbett | 9106771 | 2017-12-08 11:21:14 -0500 | [diff] [blame] | 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 | |
Behdad Esfahbod | 8a3511a | 2009-11-04 19:45:39 -0500 | [diff] [blame] | 128 | /* |
| 129 | * Complete list at: |
Ebrahim Byagowi | f24b0b9 | 2018-04-12 13:40:45 +0430 | [diff] [blame] | 130 | * https://docs.microsoft.com/en-us/typography/opentype/spec/scripttags |
Behdad Esfahbod | 62879ee | 2011-04-18 23:40:21 -0400 | [diff] [blame] | 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. |
Behdad Esfahbod | 8a3511a | 2009-11-04 19:45:39 -0500 | [diff] [blame] | 134 | */ |
Behdad Esfahbod | 8a3511a | 2009-11-04 19:45:39 -0500 | [diff] [blame] | 135 | |
David Corbett | 9106771 | 2017-12-08 11:21:14 -0500 | [diff] [blame] | 136 | static void |
| 137 | hb_ot_all_tags_from_script (hb_script_t script, |
| 138 | unsigned int *count /* IN/OUT */, |
| 139 | hb_tag_t *tags /* OUT */) |
Behdad Esfahbod | 8a3511a | 2009-11-04 19:45:39 -0500 | [diff] [blame] | 140 | { |
David Corbett | 9106771 | 2017-12-08 11:21:14 -0500 | [diff] [blame] | 141 | unsigned int i = 0; |
Behdad Esfahbod | 8a3511a | 2009-11-04 19:45:39 -0500 | [diff] [blame] | 142 | |
David Corbett | 9106771 | 2017-12-08 11:21:14 -0500 | [diff] [blame] | 143 | hb_tag_t new_tag = hb_ot_new_tag_from_script (script); |
| 144 | if (unlikely (new_tag != HB_OT_TAG_DEFAULT_SCRIPT)) |
David Corbett | 28d091d | 2018-10-11 17:15:22 -0400 | [diff] [blame] | 145 | { |
| 146 | tags[i++] = new_tag | '3'; |
| 147 | if (*count > i) |
| 148 | tags[i++] = new_tag; |
| 149 | } |
Behdad Esfahbod | 5d91c3d | 2011-03-16 17:36:32 -0300 | [diff] [blame] | 150 | |
David Corbett | 9106771 | 2017-12-08 11:21:14 -0500 | [diff] [blame] | 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; |
Behdad Esfahbod | 62879ee | 2011-04-18 23:40:21 -0400 | [diff] [blame] | 156 | } |
David Corbett | 9106771 | 2017-12-08 11:21:14 -0500 | [diff] [blame] | 157 | |
| 158 | *count = i; |
Behdad Esfahbod | 8a3511a | 2009-11-04 19:45:39 -0500 | [diff] [blame] | 159 | } |
| 160 | |
Behdad Esfahbod | b490fa3 | 2010-05-21 11:15:07 +0100 | [diff] [blame] | 161 | hb_script_t |
| 162 | hb_ot_tag_to_script (hb_tag_t tag) |
| 163 | { |
David Corbett | 28d091d | 2018-10-11 17:15:22 -0400 | [diff] [blame] | 164 | unsigned char digit = tag & 0x000000FFu; |
| 165 | if (unlikely (digit == '2' || digit == '3')) |
| 166 | return hb_ot_new_tag_to_script (tag & 0xFFFFFF32); |
Behdad Esfahbod | b490fa3 | 2010-05-21 11:15:07 +0100 | [diff] [blame] | 167 | |
Behdad Esfahbod | 62879ee | 2011-04-18 23:40:21 -0400 | [diff] [blame] | 168 | return hb_ot_old_tag_to_script (tag); |
Behdad Esfahbod | b490fa3 | 2010-05-21 11:15:07 +0100 | [diff] [blame] | 169 | } |
Behdad Esfahbod | 8a3511a | 2009-11-04 19:45:39 -0500 | [diff] [blame] | 170 | |
Behdad Esfahbod | 62879ee | 2011-04-18 23:40:21 -0400 | [diff] [blame] | 171 | |
| 172 | /* hb_language_t */ |
| 173 | |
Behdad Esfahbod | 8a3511a | 2009-11-04 19:45:39 -0500 | [diff] [blame] | 174 | static int |
Behdad Esfahbod | 0712e91 | 2017-10-29 17:01:47 -0600 | [diff] [blame] | 175 | lang_compare_first_component (const void *pa, |
| 176 | const void *pb) |
Behdad Esfahbod | 8a3511a | 2009-11-04 19:45:39 -0500 | [diff] [blame] | 177 | { |
Behdad Esfahbod | 0712e91 | 2017-10-29 17:01:47 -0600 | [diff] [blame] | 178 | const char *a = (const char *) pa; |
| 179 | const char *b = (const char *) pb; |
Behdad Esfahbod | 8a3511a | 2009-11-04 19:45:39 -0500 | [diff] [blame] | 180 | unsigned int da, db; |
| 181 | const char *p; |
| 182 | |
Behdad Esfahbod | e3693b7 | 2011-06-15 09:33:52 -0400 | [diff] [blame] | 183 | p = strchr (a, '-'); |
Behdad Esfahbod | 8a3511a | 2009-11-04 19:45:39 -0500 | [diff] [blame] | 184 | da = p ? (unsigned int) (p - a) : strlen (a); |
| 185 | |
Behdad Esfahbod | e3693b7 | 2011-06-15 09:33:52 -0400 | [diff] [blame] | 186 | p = strchr (b, '-'); |
Behdad Esfahbod | 8a3511a | 2009-11-04 19:45:39 -0500 | [diff] [blame] | 187 | db = p ? (unsigned int) (p - b) : strlen (b); |
| 188 | |
| 189 | return strncmp (a, b, MAX (da, db)); |
| 190 | } |
| 191 | |
David Corbett | 9106771 | 2017-12-08 11:21:14 -0500 | [diff] [blame] | 192 | static bool |
David Corbett | 2f1f961 | 2017-12-08 22:45:52 -0500 | [diff] [blame] | 193 | subtag_matches (const char *lang_str, |
| 194 | const char *limit, |
| 195 | const char *subtag) |
David Corbett | 9106771 | 2017-12-08 11:21:14 -0500 | [diff] [blame] | 196 | { |
| 197 | do { |
| 198 | const char *s = strstr (lang_str, subtag); |
David Corbett | 2f1f961 | 2017-12-08 22:45:52 -0500 | [diff] [blame] | 199 | if (!s || s >= limit) |
David Corbett | 9106771 | 2017-12-08 11:21:14 -0500 | [diff] [blame] | 200 | return false; |
| 201 | if (!ISALNUM (s[strlen (subtag)])) |
David Corbett | 9106771 | 2017-12-08 11:21:14 -0500 | [diff] [blame] | 202 | return true; |
David Corbett | 9106771 | 2017-12-08 11:21:14 -0500 | [diff] [blame] | 203 | lang_str = s + strlen (subtag); |
Bruce Mitchener | 8d1e479 | 2018-10-18 22:18:42 +0700 | [diff] [blame] | 204 | } while (true); |
David Corbett | 9106771 | 2017-12-08 11:21:14 -0500 | [diff] [blame] | 205 | } |
| 206 | |
Behdad Esfahbod | 8a3511a | 2009-11-04 19:45:39 -0500 | [diff] [blame] | 207 | static hb_bool_t |
| 208 | lang_matches (const char *lang_str, const char *spec) |
| 209 | { |
| 210 | unsigned int len = strlen (spec); |
| 211 | |
Behdad Esfahbod | 40b5c2e | 2011-05-13 22:46:36 -0400 | [diff] [blame] | 212 | return strncmp (lang_str, spec, len) == 0 && |
Behdad Esfahbod | 8a3511a | 2009-11-04 19:45:39 -0500 | [diff] [blame] | 213 | (lang_str[len] == '\0' || lang_str[len] == '-'); |
| 214 | } |
| 215 | |
David Corbett | 2f1f961 | 2017-12-08 22:45:52 -0500 | [diff] [blame] | 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 | |
Behdad Esfahbod | 8a3511a | 2009-11-04 19:45:39 -0500 | [diff] [blame] | 233 | hb_tag_t |
| 234 | hb_ot_tag_from_language (hb_language_t language) |
| 235 | { |
David Corbett | 9106771 | 2017-12-08 11:21:14 -0500 | [diff] [blame] | 236 | unsigned int count = 1; |
| 237 | hb_tag_t tags[1]; |
David Corbett | 7f1fbfe | 2018-07-23 21:19:23 -0400 | [diff] [blame] | 238 | hb_ot_tags_from_script_and_language (HB_SCRIPT_UNKNOWN, language, nullptr, nullptr, &count, tags); |
David Corbett | 9106771 | 2017-12-08 11:21:14 -0500 | [diff] [blame] | 239 | return count > 0 ? tags[0] : HB_OT_TAG_DEFAULT_LANGUAGE; |
| 240 | } |
Behdad Esfahbod | 8a3511a | 2009-11-04 19:45:39 -0500 | [diff] [blame] | 241 | |
David Corbett | 9106771 | 2017-12-08 11:21:14 -0500 | [diff] [blame] | 242 | static void |
| 243 | hb_ot_tags_from_language (const char *lang_str, |
| 244 | const char *limit, |
| 245 | const char *private_use_subtag, |
| 246 | unsigned int *count, |
| 247 | hb_tag_t *tags) |
| 248 | { |
| 249 | const char *s; |
Behdad Esfahbod | 8a3511a | 2009-11-04 19:45:39 -0500 | [diff] [blame] | 250 | |
David Corbett | 2f1f961 | 2017-12-08 22:45:52 -0500 | [diff] [blame] | 251 | /* Check for matches of multiple subtags. */ |
| 252 | if (hb_ot_tags_from_complex_language (lang_str, limit, count, tags)) |
| 253 | return; |
Sascha Brawer | 1337428 | 2017-01-18 13:51:02 +0100 | [diff] [blame] | 254 | |
David Corbett | 2f1f961 | 2017-12-08 22:45:52 -0500 | [diff] [blame] | 255 | /* Find a language matching in the first component. */ |
David Corbett | 7c7cb2a | 2018-01-20 15:53:09 -0500 | [diff] [blame] | 256 | s = strchr (lang_str, '-'); |
Behdad Esfahbod | ee5350d | 2014-07-10 19:06:45 -0400 | [diff] [blame] | 257 | { |
| 258 | const LangTag *lang_tag; |
David Corbett | 7c7cb2a | 2018-01-20 15:53:09 -0500 | [diff] [blame] | 259 | if (s && limit - lang_str >= 6) |
| 260 | { |
| 261 | const char *extlang_end = strchr (s + 1, '-'); |
| 262 | /* If there is an extended language tag, use it. */ |
| 263 | if (3 == (extlang_end ? extlang_end - s - 1 : strlen (s + 1)) && |
| 264 | ISALPHA (s[1])) |
| 265 | lang_str = s + 1; |
| 266 | } |
Behdad Esfahbod | ee5350d | 2014-07-10 19:06:45 -0400 | [diff] [blame] | 267 | lang_tag = (LangTag *) bsearch (lang_str, ot_languages, |
| 268 | ARRAY_LENGTH (ot_languages), sizeof (LangTag), |
Behdad Esfahbod | 0712e91 | 2017-10-29 17:01:47 -0600 | [diff] [blame] | 269 | lang_compare_first_component); |
Behdad Esfahbod | ee5350d | 2014-07-10 19:06:45 -0400 | [diff] [blame] | 270 | if (lang_tag) |
David Corbett | 9106771 | 2017-12-08 11:21:14 -0500 | [diff] [blame] | 271 | { |
| 272 | unsigned int i; |
| 273 | for (i = 0; i < *count && lang_tag->tags[i] != HB_TAG_NONE; i++) |
| 274 | tags[i] = lang_tag->tags[i]; |
| 275 | *count = i; |
| 276 | return; |
| 277 | } |
Behdad Esfahbod | ee5350d | 2014-07-10 19:06:45 -0400 | [diff] [blame] | 278 | } |
Behdad Esfahbod | 8a3511a | 2009-11-04 19:45:39 -0500 | [diff] [blame] | 279 | |
Behdad Esfahbod | 738d096 | 2011-09-02 13:31:19 -0400 | [diff] [blame] | 280 | if (!s) |
| 281 | s = lang_str + strlen (lang_str); |
| 282 | if (s - lang_str == 3) { |
| 283 | /* Assume it's ISO-639-3 and upper-case and use it. */ |
David Corbett | 9106771 | 2017-12-08 11:21:14 -0500 | [diff] [blame] | 284 | tags[0] = hb_tag_from_string (lang_str, s - lang_str) & ~0x20202000u; |
| 285 | *count = 1; |
| 286 | return; |
Behdad Esfahbod | 738d096 | 2011-09-02 13:31:19 -0400 | [diff] [blame] | 287 | } |
| 288 | |
David Corbett | 9106771 | 2017-12-08 11:21:14 -0500 | [diff] [blame] | 289 | *count = 0; |
| 290 | } |
| 291 | |
| 292 | static bool |
| 293 | parse_private_use_subtag (const char *private_use_subtag, |
| 294 | unsigned int *count, |
| 295 | hb_tag_t *tags, |
| 296 | const char *prefix, |
| 297 | unsigned char (*normalize) (unsigned char)) |
| 298 | { |
| 299 | if (private_use_subtag && count && tags && *count) |
| 300 | { |
| 301 | const char *s = strstr (private_use_subtag, prefix); |
| 302 | if (s) |
| 303 | { |
| 304 | char tag[4]; |
| 305 | int i; |
| 306 | s += strlen (prefix); |
| 307 | for (i = 0; i < 4 && ISALNUM (s[i]); i++) |
| 308 | tag[i] = normalize (s[i]); |
| 309 | if (i) |
| 310 | { |
| 311 | for (; i < 4; i++) |
| 312 | tag[i] = ' '; |
| 313 | tags[0] = HB_TAG (tag[0], tag[1], tag[2], tag[3]); |
David Corbett | 7f1fbfe | 2018-07-23 21:19:23 -0400 | [diff] [blame] | 314 | if ((tags[0] & 0xDFDFDFDF) == HB_OT_TAG_DEFAULT_SCRIPT) |
| 315 | tags[0] ^= ~0xDFDFDFDF; |
David Corbett | 9106771 | 2017-12-08 11:21:14 -0500 | [diff] [blame] | 316 | *count = 1; |
| 317 | return false; |
| 318 | } |
| 319 | } |
| 320 | } |
| 321 | return true; |
| 322 | } |
| 323 | |
| 324 | /** |
David Corbett | 7f1fbfe | 2018-07-23 21:19:23 -0400 | [diff] [blame] | 325 | * hb_ot_tags_from_script_and_language: |
David Corbett | 9106771 | 2017-12-08 11:21:14 -0500 | [diff] [blame] | 326 | * @script: an #hb_script_t to convert. |
| 327 | * @language: an #hb_language_t to convert. |
| 328 | * @script_count: (allow-none): maximum number of script tags to retrieve (IN) |
| 329 | * and actual number of script tags retrieved (OUT) |
HinTak | 26092bb | 2018-10-17 00:54:39 +0100 | [diff] [blame] | 330 | * @script_tags: (out) (allow-none): array of size at least @script_count to store the |
David Corbett | 9106771 | 2017-12-08 11:21:14 -0500 | [diff] [blame] | 331 | * script tag results |
| 332 | * @language_count: (allow-none): maximum number of language tags to retrieve |
| 333 | * (IN) and actual number of language tags retrieved (OUT) |
HinTak | 26092bb | 2018-10-17 00:54:39 +0100 | [diff] [blame] | 334 | * @language_tags: (out) (allow-none): array of size at least @language_count to store |
David Corbett | 9106771 | 2017-12-08 11:21:14 -0500 | [diff] [blame] | 335 | * the language tag results |
| 336 | * |
| 337 | * Converts an #hb_script_t and an #hb_language_t to script and language tags. |
| 338 | * |
Behdad Esfahbod | 3d9a030 | 2018-10-18 05:58:17 -0700 | [diff] [blame] | 339 | * Since: 2.0.0 |
David Corbett | 9106771 | 2017-12-08 11:21:14 -0500 | [diff] [blame] | 340 | **/ |
| 341 | void |
David Corbett | 7f1fbfe | 2018-07-23 21:19:23 -0400 | [diff] [blame] | 342 | hb_ot_tags_from_script_and_language (hb_script_t script, |
| 343 | hb_language_t language, |
| 344 | unsigned int *script_count /* IN/OUT */, |
| 345 | hb_tag_t *script_tags /* OUT */, |
| 346 | unsigned int *language_count /* IN/OUT */, |
| 347 | hb_tag_t *language_tags /* OUT */) |
David Corbett | 9106771 | 2017-12-08 11:21:14 -0500 | [diff] [blame] | 348 | { |
| 349 | bool needs_script = true; |
| 350 | |
| 351 | if (language == HB_LANGUAGE_INVALID) |
| 352 | { |
| 353 | if (language_count && language_tags && *language_count) |
| 354 | *language_count = 0; |
Behdad Esfahbod | da591f2 | 2018-10-11 14:30:15 -0400 | [diff] [blame] | 355 | } |
| 356 | else |
| 357 | { |
David Corbett | 9106771 | 2017-12-08 11:21:14 -0500 | [diff] [blame] | 358 | const char *lang_str, *s, *limit, *private_use_subtag; |
| 359 | bool needs_language; |
| 360 | |
| 361 | lang_str = hb_language_to_string (language); |
| 362 | limit = nullptr; |
| 363 | private_use_subtag = nullptr; |
| 364 | if (lang_str[0] == 'x' && lang_str[1] == '-') |
| 365 | { |
| 366 | private_use_subtag = lang_str; |
| 367 | } else { |
| 368 | for (s = lang_str + 1; *s; s++) |
| 369 | { |
| 370 | if (s[-1] == '-' && s[1] == '-') |
| 371 | { |
| 372 | if (s[0] == 'x') |
| 373 | { |
| 374 | private_use_subtag = s; |
| 375 | if (!limit) |
| 376 | limit = s - 1; |
| 377 | break; |
| 378 | } else if (!limit) |
| 379 | { |
| 380 | limit = s - 1; |
| 381 | } |
| 382 | } |
| 383 | } |
| 384 | if (!limit) |
| 385 | limit = s; |
| 386 | } |
| 387 | |
| 388 | needs_script = parse_private_use_subtag (private_use_subtag, script_count, script_tags, "-hbsc", TOLOWER); |
| 389 | needs_language = parse_private_use_subtag (private_use_subtag, language_count, language_tags, "-hbot", TOUPPER); |
| 390 | |
| 391 | if (needs_language && language_count && language_tags && *language_count) |
| 392 | hb_ot_tags_from_language (lang_str, limit, private_use_subtag, language_count, language_tags); |
| 393 | } |
| 394 | |
| 395 | if (needs_script && script_count && script_tags && *script_count) |
| 396 | hb_ot_all_tags_from_script (script, script_count, script_tags); |
Behdad Esfahbod | 8a3511a | 2009-11-04 19:45:39 -0500 | [diff] [blame] | 397 | } |
| 398 | |
Behdad Esfahbod | 351f68f | 2015-06-12 17:46:06 -0700 | [diff] [blame] | 399 | /** |
| 400 | * hb_ot_tag_to_language: |
| 401 | * |
Ebrahim Byagowi | f24b0b9 | 2018-04-12 13:40:45 +0430 | [diff] [blame] | 402 | * |
Behdad Esfahbod | 351f68f | 2015-06-12 17:46:06 -0700 | [diff] [blame] | 403 | * |
| 404 | * Return value: (transfer none): |
| 405 | * |
Behdad Esfahbod | b881142 | 2015-09-03 15:53:22 +0430 | [diff] [blame] | 406 | * Since: 0.9.2 |
Behdad Esfahbod | 351f68f | 2015-06-12 17:46:06 -0700 | [diff] [blame] | 407 | **/ |
Behdad Esfahbod | 8a3511a | 2009-11-04 19:45:39 -0500 | [diff] [blame] | 408 | hb_language_t |
| 409 | hb_ot_tag_to_language (hb_tag_t tag) |
| 410 | { |
| 411 | unsigned int i; |
Behdad Esfahbod | 40b5c2e | 2011-05-13 22:46:36 -0400 | [diff] [blame] | 412 | |
| 413 | if (tag == HB_OT_TAG_DEFAULT_LANGUAGE) |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 414 | return nullptr; |
Behdad Esfahbod | 8a3511a | 2009-11-04 19:45:39 -0500 | [diff] [blame] | 415 | |
David Corbett | 2f1f961 | 2017-12-08 22:45:52 -0500 | [diff] [blame] | 416 | { |
| 417 | hb_language_t disambiguated_tag = hb_ot_ambiguous_tag_to_language (tag); |
| 418 | if (disambiguated_tag != HB_LANGUAGE_INVALID) |
| 419 | return disambiguated_tag; |
Sascha Brawer | 1337428 | 2017-01-18 13:51:02 +0100 | [diff] [blame] | 420 | } |
| 421 | |
Behdad Esfahbod | 8a3511a | 2009-11-04 19:45:39 -0500 | [diff] [blame] | 422 | for (i = 0; i < ARRAY_LENGTH (ot_languages); i++) |
David Corbett | 9106771 | 2017-12-08 11:21:14 -0500 | [diff] [blame] | 423 | if (ot_languages[i].tags[0] == tag) |
Behdad Esfahbod | 4c9fe88 | 2011-08-26 09:18:53 +0200 | [diff] [blame] | 424 | return hb_language_from_string (ot_languages[i].language, -1); |
Behdad Esfahbod | 8a3511a | 2009-11-04 19:45:39 -0500 | [diff] [blame] | 425 | |
Behdad Esfahbod | 744970a | 2011-05-16 18:15:37 -0400 | [diff] [blame] | 426 | /* Else return a custom language in the form of "x-hbotABCD" */ |
Behdad Esfahbod | 40b5c2e | 2011-05-13 22:46:36 -0400 | [diff] [blame] | 427 | { |
| 428 | unsigned char buf[11] = "x-hbot"; |
| 429 | buf[6] = tag >> 24; |
| 430 | buf[7] = (tag >> 16) & 0xFF; |
| 431 | buf[8] = (tag >> 8) & 0xFF; |
| 432 | buf[9] = tag & 0xFF; |
| 433 | if (buf[9] == 0x20) |
| 434 | buf[9] = '\0'; |
| 435 | buf[10] = '\0'; |
Behdad Esfahbod | 4c9fe88 | 2011-08-26 09:18:53 +0200 | [diff] [blame] | 436 | return hb_language_from_string ((char *) buf, -1); |
Behdad Esfahbod | 40b5c2e | 2011-05-13 22:46:36 -0400 | [diff] [blame] | 437 | } |
Behdad Esfahbod | 8a3511a | 2009-11-04 19:45:39 -0500 | [diff] [blame] | 438 | } |
Behdad Esfahbod | acdba3f | 2010-07-23 15:11:18 -0400 | [diff] [blame] | 439 | |
David Corbett | 7f1fbfe | 2018-07-23 21:19:23 -0400 | [diff] [blame] | 440 | /** |
| 441 | * hb_ot_tags_to_script_and_language: |
| 442 | * @script_tag: a script tag |
| 443 | * @language_tag: a language tag |
David Corbett | c551000 | 2018-10-11 22:08:14 -0400 | [diff] [blame] | 444 | * @script: (allow-none): the #hb_script_t corresponding to @script_tag (OUT). |
| 445 | * @language: (allow-none): the #hb_language_t corresponding to @script_tag and |
David Corbett | 7f1fbfe | 2018-07-23 21:19:23 -0400 | [diff] [blame] | 446 | * @language_tag (OUT). |
| 447 | * |
| 448 | * Converts a script tag and a language tag to an #hb_script_t and an |
| 449 | * #hb_language_t. |
| 450 | * |
Behdad Esfahbod | 3d9a030 | 2018-10-18 05:58:17 -0700 | [diff] [blame] | 451 | * Since: 2.0.0 |
David Corbett | 7f1fbfe | 2018-07-23 21:19:23 -0400 | [diff] [blame] | 452 | **/ |
| 453 | void |
| 454 | hb_ot_tags_to_script_and_language (hb_tag_t script_tag, |
| 455 | hb_tag_t language_tag, |
| 456 | hb_script_t *script /* OUT */, |
| 457 | hb_language_t *language /* OUT */) |
| 458 | { |
| 459 | hb_script_t script_out = hb_ot_tag_to_script (script_tag); |
| 460 | if (script) |
| 461 | *script = script_out; |
Behdad Esfahbod | 8061664 | 2018-10-11 14:16:55 -0400 | [diff] [blame] | 462 | if (language) |
| 463 | { |
David Corbett | 7f1fbfe | 2018-07-23 21:19:23 -0400 | [diff] [blame] | 464 | unsigned int script_count = 1; |
| 465 | hb_tag_t primary_script_tag[1]; |
Behdad Esfahbod | 8061664 | 2018-10-11 14:16:55 -0400 | [diff] [blame] | 466 | hb_ot_tags_from_script_and_language (script_out, |
| 467 | HB_LANGUAGE_INVALID, |
| 468 | &script_count, |
| 469 | primary_script_tag, |
| 470 | nullptr, nullptr); |
David Corbett | 7f1fbfe | 2018-07-23 21:19:23 -0400 | [diff] [blame] | 471 | *language = hb_ot_tag_to_language (language_tag); |
Behdad Esfahbod | 8061664 | 2018-10-11 14:16:55 -0400 | [diff] [blame] | 472 | if (script_count == 0 || primary_script_tag[0] != script_tag) |
| 473 | { |
David Corbett | 7f1fbfe | 2018-07-23 21:19:23 -0400 | [diff] [blame] | 474 | unsigned char *buf; |
| 475 | const char *lang_str = hb_language_to_string (*language); |
| 476 | size_t len = strlen (lang_str); |
| 477 | buf = (unsigned char *) malloc (len + 11); |
Behdad Esfahbod | 8061664 | 2018-10-11 14:16:55 -0400 | [diff] [blame] | 478 | if (unlikely (!buf)) |
| 479 | { |
David Corbett | 7f1fbfe | 2018-07-23 21:19:23 -0400 | [diff] [blame] | 480 | *language = nullptr; |
Behdad Esfahbod | 8061664 | 2018-10-11 14:16:55 -0400 | [diff] [blame] | 481 | } |
| 482 | else |
| 483 | { |
David Corbett | 7f1fbfe | 2018-07-23 21:19:23 -0400 | [diff] [blame] | 484 | memcpy (buf, lang_str, len); |
| 485 | if (lang_str[0] != 'x' || lang_str[1] != '-') { |
| 486 | buf[len++] = '-'; |
| 487 | buf[len++] = 'x'; |
| 488 | } |
| 489 | buf[len++] = '-'; |
| 490 | buf[len++] = 'h'; |
| 491 | buf[len++] = 'b'; |
| 492 | buf[len++] = 's'; |
| 493 | buf[len++] = 'c'; |
| 494 | buf[len++] = script_tag >> 24; |
| 495 | buf[len++] = (script_tag >> 16) & 0xFF; |
| 496 | buf[len++] = (script_tag >> 8) & 0xFF; |
| 497 | buf[len++] = script_tag & 0xFF; |
| 498 | *language = hb_language_from_string ((char *) buf, len); |
| 499 | free (buf); |
| 500 | } |
| 501 | } |
| 502 | } |
| 503 | } |
| 504 | |
Behdad Esfahbod | d05b783 | 2016-01-12 16:17:21 +0000 | [diff] [blame] | 505 | #ifdef MAIN |
Behdad Esfahbod | 70952dd | 2015-12-07 10:28:46 +0100 | [diff] [blame] | 506 | static inline void |
| 507 | test_langs_sorted (void) |
| 508 | { |
| 509 | for (unsigned int i = 1; i < ARRAY_LENGTH (ot_languages); i++) |
| 510 | { |
| 511 | int c = lang_compare_first_component (ot_languages[i-1].language, ot_languages[i].language); |
| 512 | if (c >= 0) |
| 513 | { |
| 514 | fprintf (stderr, "ot_languages not sorted at index %d: %s %d %s\n", |
| 515 | i, ot_languages[i-1].language, c, ot_languages[i].language); |
| 516 | abort(); |
| 517 | } |
| 518 | } |
| 519 | } |
Behdad Esfahbod | acdba3f | 2010-07-23 15:11:18 -0400 | [diff] [blame] | 520 | |
Behdad Esfahbod | 70952dd | 2015-12-07 10:28:46 +0100 | [diff] [blame] | 521 | int |
| 522 | main (void) |
| 523 | { |
| 524 | test_langs_sorted (); |
| 525 | return 0; |
| 526 | } |
| 527 | |
| 528 | #endif |