Behdad Esfahbod | 83f3467 | 2010-05-21 13:43:49 +0100 | [diff] [blame] | 1 | /* |
Behdad Esfahbod | 2409d5f | 2011-04-21 17:14:28 -0400 | [diff] [blame] | 2 | * Copyright © 2009,2010 Red Hat, Inc. |
Behdad Esfahbod | cad3821 | 2012-03-07 17:13:25 -0500 | [diff] [blame] | 3 | * Copyright © 2011,2012 Google, Inc. |
Behdad Esfahbod | 83f3467 | 2010-05-21 13:43:49 +0100 | [diff] [blame] | 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 |
Behdad Esfahbod | 2409d5f | 2011-04-21 17:14:28 -0400 | [diff] [blame] | 26 | * Google Author(s): Behdad Esfahbod |
Behdad Esfahbod | 83f3467 | 2010-05-21 13:43:49 +0100 | [diff] [blame] | 27 | */ |
| 28 | |
Behdad Esfahbod | c77ae40 | 2018-08-25 22:36:36 -0700 | [diff] [blame] | 29 | #include "hb.hh" |
Behdad Esfahbod | 83f3467 | 2010-05-21 13:43:49 +0100 | [diff] [blame] | 30 | |
Behdad Esfahbod | c77ae40 | 2018-08-25 22:36:36 -0700 | [diff] [blame] | 31 | #include "hb-machinery.hh" |
Behdad Esfahbod | b8d6183 | 2011-05-05 15:14:04 -0400 | [diff] [blame] | 32 | |
Behdad Esfahbod | 34fb552 | 2011-05-06 00:04:28 -0400 | [diff] [blame] | 33 | #include <locale.h> |
Behdad Esfahbod | 3ca69c8 | 2017-09-14 20:50:35 -0400 | [diff] [blame] | 34 | #ifdef HAVE_XLOCALE_H |
| 35 | #include <xlocale.h> |
| 36 | #endif |
Behdad Esfahbod | 34fb552 | 2011-05-06 00:04:28 -0400 | [diff] [blame] | 37 | |
Behdad Esfahbod | acdba3f | 2010-07-23 15:11:18 -0400 | [diff] [blame] | 38 | |
Behdad Esfahbod | bab02d3 | 2013-02-12 15:26:45 -0500 | [diff] [blame] | 39 | /* hb_options_t */ |
| 40 | |
Behdad Esfahbod | 4bc16ac | 2018-07-31 21:05:51 -0700 | [diff] [blame] | 41 | hb_atomic_int_t _hb_options; |
Behdad Esfahbod | bab02d3 | 2013-02-12 15:26:45 -0500 | [diff] [blame] | 42 | |
| 43 | void |
| 44 | _hb_options_init (void) |
| 45 | { |
| 46 | hb_options_union_t u; |
| 47 | u.i = 0; |
| 48 | u.opts.initialized = 1; |
| 49 | |
Behdad Esfahbod | 38a7a8a | 2018-10-10 17:44:46 -0400 | [diff] [blame^] | 50 | const char *c = getenv ("HB_OPTIONS"); |
| 51 | if (c) |
| 52 | { |
| 53 | while (*c) |
| 54 | { |
| 55 | const char *p = strchr (c, ':'); |
| 56 | if (!p) |
| 57 | p = c + strlen (c); |
| 58 | |
| 59 | #define OPTION(name, symbol) \ |
| 60 | if (0 == strncmp (c, name, p - c)) u.opts.symbol = true; |
| 61 | |
| 62 | OPTION ("uniscribe-bug-compatible", uniscribe_bug_compatible); |
| 63 | OPTION ("aat", aat); |
| 64 | |
| 65 | #undef OPTION |
| 66 | |
| 67 | c = *p ? p + 1 : p; |
| 68 | } |
| 69 | |
| 70 | } |
Behdad Esfahbod | bab02d3 | 2013-02-12 15:26:45 -0500 | [diff] [blame] | 71 | |
| 72 | /* This is idempotent and threadsafe. */ |
Behdad Esfahbod | 4bc16ac | 2018-07-31 21:05:51 -0700 | [diff] [blame] | 73 | _hb_options.set_relaxed (u.i); |
Behdad Esfahbod | bab02d3 | 2013-02-12 15:26:45 -0500 | [diff] [blame] | 74 | } |
| 75 | |
Behdad Esfahbod | acdba3f | 2010-07-23 15:11:18 -0400 | [diff] [blame] | 76 | |
Behdad Esfahbod | 8e4bb3c | 2011-04-11 17:55:58 -0400 | [diff] [blame] | 77 | /* hb_tag_t */ |
| 78 | |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 79 | /** |
| 80 | * hb_tag_from_string: |
Ebrahim Byagowi | 70d3654 | 2018-03-30 05:00:28 +0430 | [diff] [blame] | 81 | * @str: (array length=len) (element-type uint8_t): |
| 82 | * @len: |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 83 | * |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 84 | * |
Ebrahim Byagowi | 70d3654 | 2018-03-30 05:00:28 +0430 | [diff] [blame] | 85 | * |
| 86 | * Return value: |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 87 | * |
Behdad Esfahbod | b881142 | 2015-09-03 15:53:22 +0430 | [diff] [blame] | 88 | * Since: 0.9.2 |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 89 | **/ |
Behdad Esfahbod | 83f3467 | 2010-05-21 13:43:49 +0100 | [diff] [blame] | 90 | hb_tag_t |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 91 | hb_tag_from_string (const char *str, int len) |
Behdad Esfahbod | 83f3467 | 2010-05-21 13:43:49 +0100 | [diff] [blame] | 92 | { |
| 93 | char tag[4]; |
| 94 | unsigned int i; |
| 95 | |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 96 | if (!str || !len || !*str) |
Behdad Esfahbod | 7ff7401 | 2011-04-11 13:27:30 -0400 | [diff] [blame] | 97 | return HB_TAG_NONE; |
| 98 | |
Behdad Esfahbod | 4c9fe88 | 2011-08-26 09:18:53 +0200 | [diff] [blame] | 99 | if (len < 0 || len > 4) |
| 100 | len = 4; |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 101 | for (i = 0; i < (unsigned) len && str[i]; i++) |
| 102 | tag[i] = str[i]; |
Behdad Esfahbod | 83f3467 | 2010-05-21 13:43:49 +0100 | [diff] [blame] | 103 | for (; i < 4; i++) |
| 104 | tag[i] = ' '; |
| 105 | |
Behdad Esfahbod | 8eaff98 | 2017-10-31 15:30:06 -0600 | [diff] [blame] | 106 | return HB_TAG (tag[0], tag[1], tag[2], tag[3]); |
Behdad Esfahbod | 83f3467 | 2010-05-21 13:43:49 +0100 | [diff] [blame] | 107 | } |
Behdad Esfahbod | acdba3f | 2010-07-23 15:11:18 -0400 | [diff] [blame] | 108 | |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 109 | /** |
| 110 | * hb_tag_to_string: |
Ebrahim Byagowi | 70d3654 | 2018-03-30 05:00:28 +0430 | [diff] [blame] | 111 | * @tag: |
| 112 | * @buf: (out caller-allocates) (array fixed-size=4) (element-type uint8_t): |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 113 | * |
Ebrahim Byagowi | 70d3654 | 2018-03-30 05:00:28 +0430 | [diff] [blame] | 114 | * |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 115 | * |
Sascha Brawer | 01c3a88 | 2015-06-01 13:22:01 +0200 | [diff] [blame] | 116 | * Since: 0.9.5 |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 117 | **/ |
Behdad Esfahbod | e30ebd2 | 2012-09-06 22:09:06 -0400 | [diff] [blame] | 118 | void |
| 119 | hb_tag_to_string (hb_tag_t tag, char *buf) |
| 120 | { |
| 121 | buf[0] = (char) (uint8_t) (tag >> 24); |
| 122 | buf[1] = (char) (uint8_t) (tag >> 16); |
| 123 | buf[2] = (char) (uint8_t) (tag >> 8); |
| 124 | buf[3] = (char) (uint8_t) (tag >> 0); |
| 125 | } |
| 126 | |
Behdad Esfahbod | acdba3f | 2010-07-23 15:11:18 -0400 | [diff] [blame] | 127 | |
Behdad Esfahbod | 39a840a | 2011-04-27 14:48:19 -0400 | [diff] [blame] | 128 | /* hb_direction_t */ |
| 129 | |
| 130 | const char direction_strings[][4] = { |
| 131 | "ltr", |
| 132 | "rtl", |
| 133 | "ttb", |
| 134 | "btt" |
| 135 | }; |
| 136 | |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 137 | /** |
| 138 | * hb_direction_from_string: |
Ebrahim Byagowi | 70d3654 | 2018-03-30 05:00:28 +0430 | [diff] [blame] | 139 | * @str: (array length=len) (element-type uint8_t): |
| 140 | * @len: |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 141 | * |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 142 | * |
Ebrahim Byagowi | 70d3654 | 2018-03-30 05:00:28 +0430 | [diff] [blame] | 143 | * |
| 144 | * Return value: |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 145 | * |
Behdad Esfahbod | b881142 | 2015-09-03 15:53:22 +0430 | [diff] [blame] | 146 | * Since: 0.9.2 |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 147 | **/ |
Behdad Esfahbod | 39a840a | 2011-04-27 14:48:19 -0400 | [diff] [blame] | 148 | hb_direction_t |
Behdad Esfahbod | 4c9fe88 | 2011-08-26 09:18:53 +0200 | [diff] [blame] | 149 | hb_direction_from_string (const char *str, int len) |
Behdad Esfahbod | 39a840a | 2011-04-27 14:48:19 -0400 | [diff] [blame] | 150 | { |
Behdad Esfahbod | 4c9fe88 | 2011-08-26 09:18:53 +0200 | [diff] [blame] | 151 | if (unlikely (!str || !len || !*str)) |
Behdad Esfahbod | 39a840a | 2011-04-27 14:48:19 -0400 | [diff] [blame] | 152 | return HB_DIRECTION_INVALID; |
| 153 | |
| 154 | /* Lets match loosely: just match the first letter, such that |
| 155 | * all of "ltr", "left-to-right", etc work! |
| 156 | */ |
| 157 | char c = TOLOWER (str[0]); |
| 158 | for (unsigned int i = 0; i < ARRAY_LENGTH (direction_strings); i++) |
| 159 | if (c == direction_strings[i][0]) |
Behdad Esfahbod | 4bf90f6 | 2012-04-12 17:38:23 -0400 | [diff] [blame] | 160 | return (hb_direction_t) (HB_DIRECTION_LTR + i); |
Behdad Esfahbod | 39a840a | 2011-04-27 14:48:19 -0400 | [diff] [blame] | 161 | |
| 162 | return HB_DIRECTION_INVALID; |
| 163 | } |
| 164 | |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 165 | /** |
| 166 | * hb_direction_to_string: |
Ebrahim Byagowi | 70d3654 | 2018-03-30 05:00:28 +0430 | [diff] [blame] | 167 | * @direction: |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 168 | * |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 169 | * |
Ebrahim Byagowi | 70d3654 | 2018-03-30 05:00:28 +0430 | [diff] [blame] | 170 | * |
| 171 | * Return value: (transfer none): |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 172 | * |
Behdad Esfahbod | b881142 | 2015-09-03 15:53:22 +0430 | [diff] [blame] | 173 | * Since: 0.9.2 |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 174 | **/ |
Behdad Esfahbod | 39a840a | 2011-04-27 14:48:19 -0400 | [diff] [blame] | 175 | const char * |
| 176 | hb_direction_to_string (hb_direction_t direction) |
| 177 | { |
Behdad Esfahbod | 4bf90f6 | 2012-04-12 17:38:23 -0400 | [diff] [blame] | 178 | if (likely ((unsigned int) (direction - HB_DIRECTION_LTR) |
| 179 | < ARRAY_LENGTH (direction_strings))) |
| 180 | return direction_strings[direction - HB_DIRECTION_LTR]; |
Behdad Esfahbod | 39a840a | 2011-04-27 14:48:19 -0400 | [diff] [blame] | 181 | |
| 182 | return "invalid"; |
| 183 | } |
| 184 | |
| 185 | |
Behdad Esfahbod | 8e4bb3c | 2011-04-11 17:55:58 -0400 | [diff] [blame] | 186 | /* hb_language_t */ |
| 187 | |
Behdad Esfahbod | 1bc1cb3 | 2012-06-16 15:21:55 -0400 | [diff] [blame] | 188 | struct hb_language_impl_t { |
Behdad Esfahbod | 3cbdf70 | 2011-04-15 12:32:06 -0400 | [diff] [blame] | 189 | const char s[1]; |
| 190 | }; |
| 191 | |
Behdad Esfahbod | 8e4bb3c | 2011-04-11 17:55:58 -0400 | [diff] [blame] | 192 | static const char canon_map[256] = { |
| 193 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 194 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 195 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '-', 0, 0, |
| 196 | '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 0, 0, 0, 0, 0, 0, |
| 197 | '-', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', |
| 198 | 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 0, 0, 0, 0, '-', |
| 199 | 0, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', |
| 200 | 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 0, 0, 0, 0, 0 |
| 201 | }; |
| 202 | |
Behdad Esfahbod | f3b170b | 2015-04-08 16:26:24 -0700 | [diff] [blame] | 203 | static bool |
Behdad Esfahbod | 093171c | 2012-06-05 18:00:45 -0400 | [diff] [blame] | 204 | lang_equal (hb_language_t v1, |
| 205 | const void *v2) |
Behdad Esfahbod | 8e4bb3c | 2011-04-11 17:55:58 -0400 | [diff] [blame] | 206 | { |
Behdad Esfahbod | c57d454 | 2011-04-20 18:50:27 -0400 | [diff] [blame] | 207 | const unsigned char *p1 = (const unsigned char *) v1; |
| 208 | const unsigned char *p2 = (const unsigned char *) v2; |
Behdad Esfahbod | 8e4bb3c | 2011-04-11 17:55:58 -0400 | [diff] [blame] | 209 | |
Chris Peterson | aacca37 | 2017-04-17 23:25:24 -0700 | [diff] [blame] | 210 | while (*p1 && *p1 == canon_map[*p2]) { |
| 211 | p1++; |
| 212 | p2++; |
| 213 | } |
Behdad Esfahbod | 8e4bb3c | 2011-04-11 17:55:58 -0400 | [diff] [blame] | 214 | |
Behdad Esfahbod | 093171c | 2012-06-05 18:00:45 -0400 | [diff] [blame] | 215 | return *p1 == canon_map[*p2]; |
Behdad Esfahbod | 8e4bb3c | 2011-04-11 17:55:58 -0400 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | #if 0 |
| 219 | static unsigned int |
| 220 | lang_hash (const void *key) |
| 221 | { |
| 222 | const unsigned char *p = key; |
| 223 | unsigned int h = 0; |
| 224 | while (canon_map[*p]) |
| 225 | { |
| 226 | h = (h << 5) - h + canon_map[*p]; |
| 227 | p++; |
| 228 | } |
| 229 | |
| 230 | return h; |
| 231 | } |
| 232 | #endif |
| 233 | |
| 234 | |
Behdad Esfahbod | b45f32e | 2011-05-05 15:00:43 -0400 | [diff] [blame] | 235 | struct hb_language_item_t { |
| 236 | |
Behdad Esfahbod | 093171c | 2012-06-05 18:00:45 -0400 | [diff] [blame] | 237 | struct hb_language_item_t *next; |
Behdad Esfahbod | b45f32e | 2011-05-05 15:00:43 -0400 | [diff] [blame] | 238 | hb_language_t lang; |
| 239 | |
| 240 | inline bool operator == (const char *s) const { |
| 241 | return lang_equal (lang, s); |
| 242 | } |
| 243 | |
| 244 | inline hb_language_item_t & operator = (const char *s) { |
Sebastian Rasmussen | 92e2c4b | 2017-05-29 12:53:30 -0500 | [diff] [blame] | 245 | /* If a custom allocated is used calling strdup() pairs |
Behdad Esfahbod | a60ba79 | 2018-05-01 19:01:25 -0400 | [diff] [blame] | 246 | badly with a call to the custom free() in fini() below. |
Sebastian Rasmussen | 92e2c4b | 2017-05-29 12:53:30 -0500 | [diff] [blame] | 247 | Therefore don't call strdup(), implement its behavior. |
| 248 | */ |
| 249 | size_t len = strlen(s) + 1; |
| 250 | lang = (hb_language_t) malloc(len); |
Behdad Esfahbod | 7dba306 | 2017-06-01 11:44:42 -0400 | [diff] [blame] | 251 | if (likely (lang)) |
| 252 | { |
| 253 | memcpy((unsigned char *) lang, s, len); |
| 254 | for (unsigned char *p = (unsigned char *) lang; *p; p++) |
| 255 | *p = canon_map[*p]; |
| 256 | } |
Behdad Esfahbod | b45f32e | 2011-05-05 15:00:43 -0400 | [diff] [blame] | 257 | |
| 258 | return *this; |
| 259 | } |
| 260 | |
Behdad Esfahbod | a60ba79 | 2018-05-01 19:01:25 -0400 | [diff] [blame] | 261 | void fini (void) { free ((void *) lang); } |
Behdad Esfahbod | b45f32e | 2011-05-05 15:00:43 -0400 | [diff] [blame] | 262 | }; |
| 263 | |
Behdad Esfahbod | 093171c | 2012-06-05 18:00:45 -0400 | [diff] [blame] | 264 | |
| 265 | /* Thread-safe lock-free language list */ |
| 266 | |
Behdad Esfahbod | 1f73809 | 2018-08-09 00:22:37 -0700 | [diff] [blame] | 267 | static hb_atomic_ptr_t <hb_language_item_t> langs; |
Behdad Esfahbod | 093171c | 2012-06-05 18:00:45 -0400 | [diff] [blame] | 268 | |
Behdad Esfahbod | 38fb30d | 2014-08-06 13:34:49 -0400 | [diff] [blame] | 269 | #ifdef HB_USE_ATEXIT |
Behdad Esfahbod | 3ca69c8 | 2017-09-14 20:50:35 -0400 | [diff] [blame] | 270 | static void |
| 271 | free_langs (void) |
Behdad Esfahbod | 093171c | 2012-06-05 18:00:45 -0400 | [diff] [blame] | 272 | { |
Behdad Esfahbod | 5aa2c6e | 2018-03-28 15:33:51 -0700 | [diff] [blame] | 273 | retry: |
Behdad Esfahbod | 1f73809 | 2018-08-09 00:22:37 -0700 | [diff] [blame] | 274 | hb_language_item_t *first_lang = langs.get (); |
| 275 | if (unlikely (!langs.cmpexch (first_lang, nullptr))) |
Behdad Esfahbod | 5aa2c6e | 2018-03-28 15:33:51 -0700 | [diff] [blame] | 276 | goto retry; |
| 277 | |
| 278 | while (first_lang) { |
| 279 | hb_language_item_t *next = first_lang->next; |
Behdad Esfahbod | a60ba79 | 2018-05-01 19:01:25 -0400 | [diff] [blame] | 280 | first_lang->fini (); |
Behdad Esfahbod | 5aa2c6e | 2018-03-28 15:33:51 -0700 | [diff] [blame] | 281 | free (first_lang); |
| 282 | first_lang = next; |
Behdad Esfahbod | 093171c | 2012-06-05 18:00:45 -0400 | [diff] [blame] | 283 | } |
| 284 | } |
jfkthame | 0082dbe | 2014-03-16 08:25:17 +0000 | [diff] [blame] | 285 | #endif |
Behdad Esfahbod | 093171c | 2012-06-05 18:00:45 -0400 | [diff] [blame] | 286 | |
| 287 | static hb_language_item_t * |
| 288 | lang_find_or_insert (const char *key) |
| 289 | { |
Behdad Esfahbod | 093171c | 2012-06-05 18:00:45 -0400 | [diff] [blame] | 290 | retry: |
Behdad Esfahbod | 1f73809 | 2018-08-09 00:22:37 -0700 | [diff] [blame] | 291 | hb_language_item_t *first_lang = langs.get (); |
Behdad Esfahbod | 093171c | 2012-06-05 18:00:45 -0400 | [diff] [blame] | 292 | |
| 293 | for (hb_language_item_t *lang = first_lang; lang; lang = lang->next) |
| 294 | if (*lang == key) |
| 295 | return lang; |
| 296 | |
| 297 | /* Not found; allocate one. */ |
| 298 | hb_language_item_t *lang = (hb_language_item_t *) calloc (1, sizeof (hb_language_item_t)); |
| 299 | if (unlikely (!lang)) |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 300 | return nullptr; |
Behdad Esfahbod | 093171c | 2012-06-05 18:00:45 -0400 | [diff] [blame] | 301 | lang->next = first_lang; |
| 302 | *lang = key; |
Behdad Esfahbod | 7dba306 | 2017-06-01 11:44:42 -0400 | [diff] [blame] | 303 | if (unlikely (!lang->lang)) |
| 304 | { |
| 305 | free (lang); |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 306 | return nullptr; |
Behdad Esfahbod | 7dba306 | 2017-06-01 11:44:42 -0400 | [diff] [blame] | 307 | } |
Behdad Esfahbod | 093171c | 2012-06-05 18:00:45 -0400 | [diff] [blame] | 308 | |
Behdad Esfahbod | 1f73809 | 2018-08-09 00:22:37 -0700 | [diff] [blame] | 309 | if (unlikely (!langs.cmpexch (first_lang, lang))) |
| 310 | { |
Behdad Esfahbod | a60ba79 | 2018-05-01 19:01:25 -0400 | [diff] [blame] | 311 | lang->fini (); |
Behdad Esfahbod | 093171c | 2012-06-05 18:00:45 -0400 | [diff] [blame] | 312 | free (lang); |
| 313 | goto retry; |
| 314 | } |
| 315 | |
Behdad Esfahbod | 38fb30d | 2014-08-06 13:34:49 -0400 | [diff] [blame] | 316 | #ifdef HB_USE_ATEXIT |
Behdad Esfahbod | 04aed57 | 2012-06-05 18:30:19 -0400 | [diff] [blame] | 317 | if (!first_lang) |
| 318 | atexit (free_langs); /* First person registers atexit() callback. */ |
Behdad Esfahbod | 093171c | 2012-06-05 18:00:45 -0400 | [diff] [blame] | 319 | #endif |
| 320 | |
| 321 | return lang; |
| 322 | } |
| 323 | |
Behdad Esfahbod | b8d6183 | 2011-05-05 15:14:04 -0400 | [diff] [blame] | 324 | |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 325 | /** |
| 326 | * hb_language_from_string: |
Khaled Hosny | 8ab797c | 2015-12-29 17:42:16 +0400 | [diff] [blame] | 327 | * @str: (array length=len) (element-type uint8_t): a string representing |
| 328 | * ISO 639 language code |
| 329 | * @len: length of the @str, or -1 if it is %NULL-terminated. |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 330 | * |
Khaled Hosny | 8ab797c | 2015-12-29 17:42:16 +0400 | [diff] [blame] | 331 | * Converts @str representing an ISO 639 language code to the corresponding |
| 332 | * #hb_language_t. |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 333 | * |
Khaled Hosny | 04f89e8 | 2015-04-10 17:49:01 +0200 | [diff] [blame] | 334 | * Return value: (transfer none): |
Khaled Hosny | 8ab797c | 2015-12-29 17:42:16 +0400 | [diff] [blame] | 335 | * The #hb_language_t corresponding to the ISO 639 language code. |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 336 | * |
Behdad Esfahbod | b881142 | 2015-09-03 15:53:22 +0430 | [diff] [blame] | 337 | * Since: 0.9.2 |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 338 | **/ |
Behdad Esfahbod | 8e4bb3c | 2011-04-11 17:55:58 -0400 | [diff] [blame] | 339 | hb_language_t |
Behdad Esfahbod | 4c9fe88 | 2011-08-26 09:18:53 +0200 | [diff] [blame] | 340 | hb_language_from_string (const char *str, int len) |
Behdad Esfahbod | 8e4bb3c | 2011-04-11 17:55:58 -0400 | [diff] [blame] | 341 | { |
Behdad Esfahbod | 4c9fe88 | 2011-08-26 09:18:53 +0200 | [diff] [blame] | 342 | if (!str || !len || !*str) |
Behdad Esfahbod | 1a64f6e | 2011-05-13 22:55:32 -0400 | [diff] [blame] | 343 | return HB_LANGUAGE_INVALID; |
Behdad Esfahbod | 8e4bb3c | 2011-04-11 17:55:58 -0400 | [diff] [blame] | 344 | |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 345 | hb_language_item_t *item = nullptr; |
Behdad Esfahbod | 48360ec | 2013-09-26 16:48:42 -0400 | [diff] [blame] | 346 | if (len >= 0) |
| 347 | { |
Behdad Esfahbod | dac8602 | 2014-06-03 17:57:00 -0400 | [diff] [blame] | 348 | /* NUL-terminate it. */ |
Behdad Esfahbod | f3159ba | 2015-09-29 14:34:56 +0100 | [diff] [blame] | 349 | char strbuf[64]; |
Behdad Esfahbod | 4c9fe88 | 2011-08-26 09:18:53 +0200 | [diff] [blame] | 350 | len = MIN (len, (int) sizeof (strbuf) - 1); |
Behdad Esfahbod | dac8602 | 2014-06-03 17:57:00 -0400 | [diff] [blame] | 351 | memcpy (strbuf, str, len); |
Behdad Esfahbod | 4c9fe88 | 2011-08-26 09:18:53 +0200 | [diff] [blame] | 352 | strbuf[len] = '\0'; |
Behdad Esfahbod | f3159ba | 2015-09-29 14:34:56 +0100 | [diff] [blame] | 353 | item = lang_find_or_insert (strbuf); |
Behdad Esfahbod | 4c9fe88 | 2011-08-26 09:18:53 +0200 | [diff] [blame] | 354 | } |
Behdad Esfahbod | f3159ba | 2015-09-29 14:34:56 +0100 | [diff] [blame] | 355 | else |
| 356 | item = lang_find_or_insert (str); |
Behdad Esfahbod | 8e4bb3c | 2011-04-11 17:55:58 -0400 | [diff] [blame] | 357 | |
Behdad Esfahbod | 1a64f6e | 2011-05-13 22:55:32 -0400 | [diff] [blame] | 358 | return likely (item) ? item->lang : HB_LANGUAGE_INVALID; |
Behdad Esfahbod | 8e4bb3c | 2011-04-11 17:55:58 -0400 | [diff] [blame] | 359 | } |
| 360 | |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 361 | /** |
| 362 | * hb_language_to_string: |
Khaled Hosny | 8ab797c | 2015-12-29 17:42:16 +0400 | [diff] [blame] | 363 | * @language: an #hb_language_t to convert. |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 364 | * |
Khaled Hosny | 8ab797c | 2015-12-29 17:42:16 +0400 | [diff] [blame] | 365 | * See hb_language_from_string(). |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 366 | * |
Khaled Hosny | 8ab797c | 2015-12-29 17:42:16 +0400 | [diff] [blame] | 367 | * Return value: (transfer none): |
| 368 | * A %NULL-terminated string representing the @language. Must not be freed by |
| 369 | * the caller. |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 370 | * |
Behdad Esfahbod | b881142 | 2015-09-03 15:53:22 +0430 | [diff] [blame] | 371 | * Since: 0.9.2 |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 372 | **/ |
Behdad Esfahbod | 8e4bb3c | 2011-04-11 17:55:58 -0400 | [diff] [blame] | 373 | const char * |
| 374 | hb_language_to_string (hb_language_t language) |
| 375 | { |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 376 | /* This is actually nullptr-safe! */ |
Behdad Esfahbod | 3cbdf70 | 2011-04-15 12:32:06 -0400 | [diff] [blame] | 377 | return language->s; |
Behdad Esfahbod | 8e4bb3c | 2011-04-11 17:55:58 -0400 | [diff] [blame] | 378 | } |
| 379 | |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 380 | /** |
| 381 | * hb_language_get_default: |
| 382 | * |
Behdad Esfahbod | ba0f0f1 | 2018-09-30 03:49:52 -0400 | [diff] [blame] | 383 | * Get default language from current locale. |
Ebrahim Byagowi | 70d3654 | 2018-03-30 05:00:28 +0430 | [diff] [blame] | 384 | * |
Behdad Esfahbod | ba0f0f1 | 2018-09-30 03:49:52 -0400 | [diff] [blame] | 385 | * Note that the first time this function is called, it calls |
| 386 | * "setlocale (LC_CTYPE, nullptr)" to fetch current locale. The underlying |
| 387 | * setlocale function is, in many implementations, NOT threadsafe. To avoid |
| 388 | * problems, call this function once before multiple threads can call it. |
| 389 | * This function is only used from hb_buffer_guess_segment_properties() by |
| 390 | * HarfBuzz itself. |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 391 | * |
Behdad Esfahbod | 351f68f | 2015-06-12 17:46:06 -0700 | [diff] [blame] | 392 | * Return value: (transfer none): |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 393 | * |
Behdad Esfahbod | b881142 | 2015-09-03 15:53:22 +0430 | [diff] [blame] | 394 | * Since: 0.9.2 |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 395 | **/ |
Behdad Esfahbod | 34fb552 | 2011-05-06 00:04:28 -0400 | [diff] [blame] | 396 | hb_language_t |
| 397 | hb_language_get_default (void) |
| 398 | { |
Behdad Esfahbod | 1f73809 | 2018-08-09 00:22:37 -0700 | [diff] [blame] | 399 | static hb_atomic_ptr_t <hb_language_t> default_language; |
Behdad Esfahbod | 34fb552 | 2011-05-06 00:04:28 -0400 | [diff] [blame] | 400 | |
Behdad Esfahbod | 1f73809 | 2018-08-09 00:22:37 -0700 | [diff] [blame] | 401 | hb_language_t language = default_language.get (); |
| 402 | if (unlikely (language == HB_LANGUAGE_INVALID)) |
| 403 | { |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 404 | language = hb_language_from_string (setlocale (LC_CTYPE, nullptr), -1); |
Behdad Esfahbod | 1f73809 | 2018-08-09 00:22:37 -0700 | [diff] [blame] | 405 | (void) default_language.cmpexch (HB_LANGUAGE_INVALID, language); |
Behdad Esfahbod | 34fb552 | 2011-05-06 00:04:28 -0400 | [diff] [blame] | 406 | } |
| 407 | |
Behdad Esfahbod | 1f73809 | 2018-08-09 00:22:37 -0700 | [diff] [blame] | 408 | return language; |
Behdad Esfahbod | 34fb552 | 2011-05-06 00:04:28 -0400 | [diff] [blame] | 409 | } |
| 410 | |
Behdad Esfahbod | 8e4bb3c | 2011-04-11 17:55:58 -0400 | [diff] [blame] | 411 | |
Behdad Esfahbod | 00bec2c | 2011-04-15 19:16:54 -0400 | [diff] [blame] | 412 | /* hb_script_t */ |
| 413 | |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 414 | /** |
| 415 | * hb_script_from_iso15924_tag: |
Khaled Hosny | 8ab797c | 2015-12-29 17:42:16 +0400 | [diff] [blame] | 416 | * @tag: an #hb_tag_t representing an ISO 15924 tag. |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 417 | * |
Khaled Hosny | 8ab797c | 2015-12-29 17:42:16 +0400 | [diff] [blame] | 418 | * Converts an ISO 15924 script tag to a corresponding #hb_script_t. |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 419 | * |
Ebrahim Byagowi | 70d3654 | 2018-03-30 05:00:28 +0430 | [diff] [blame] | 420 | * Return value: |
Khaled Hosny | 8ab797c | 2015-12-29 17:42:16 +0400 | [diff] [blame] | 421 | * An #hb_script_t corresponding to the ISO 15924 tag. |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 422 | * |
Behdad Esfahbod | b881142 | 2015-09-03 15:53:22 +0430 | [diff] [blame] | 423 | * Since: 0.9.2 |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 424 | **/ |
Behdad Esfahbod | 00bec2c | 2011-04-15 19:16:54 -0400 | [diff] [blame] | 425 | hb_script_t |
| 426 | hb_script_from_iso15924_tag (hb_tag_t tag) |
| 427 | { |
Behdad Esfahbod | f144a8e | 2011-04-20 02:54:42 -0400 | [diff] [blame] | 428 | if (unlikely (tag == HB_TAG_NONE)) |
| 429 | return HB_SCRIPT_INVALID; |
| 430 | |
Behdad Esfahbod | 00bec2c | 2011-04-15 19:16:54 -0400 | [diff] [blame] | 431 | /* Be lenient, adjust case (one capital letter followed by three small letters) */ |
Behdad Esfahbod | 7627100 | 2014-07-11 14:54:42 -0400 | [diff] [blame] | 432 | tag = (tag & 0xDFDFDFDFu) | 0x00202020u; |
Behdad Esfahbod | 00bec2c | 2011-04-15 19:16:54 -0400 | [diff] [blame] | 433 | |
Behdad Esfahbod | 62879ee | 2011-04-18 23:40:21 -0400 | [diff] [blame] | 434 | switch (tag) { |
Behdad Esfahbod | d02985e | 2011-05-02 12:35:14 -0400 | [diff] [blame] | 435 | |
| 436 | /* These graduated from the 'Q' private-area codes, but |
| 437 | * the old code is still aliased by Unicode, and the Qaai |
| 438 | * one in use by ICU. */ |
| 439 | case HB_TAG('Q','a','a','i'): return HB_SCRIPT_INHERITED; |
| 440 | case HB_TAG('Q','a','a','c'): return HB_SCRIPT_COPTIC; |
| 441 | |
Ebrahim Byagowi | f24b0b9 | 2018-04-12 13:40:45 +0430 | [diff] [blame] | 442 | /* Script variants from https://unicode.org/iso15924/ */ |
Behdad Esfahbod | 62879ee | 2011-04-18 23:40:21 -0400 | [diff] [blame] | 443 | case HB_TAG('C','y','r','s'): return HB_SCRIPT_CYRILLIC; |
Behdad Esfahbod | 62879ee | 2011-04-18 23:40:21 -0400 | [diff] [blame] | 444 | case HB_TAG('L','a','t','f'): return HB_SCRIPT_LATIN; |
| 445 | case HB_TAG('L','a','t','g'): return HB_SCRIPT_LATIN; |
| 446 | case HB_TAG('S','y','r','e'): return HB_SCRIPT_SYRIAC; |
| 447 | case HB_TAG('S','y','r','j'): return HB_SCRIPT_SYRIAC; |
| 448 | case HB_TAG('S','y','r','n'): return HB_SCRIPT_SYRIAC; |
| 449 | } |
Behdad Esfahbod | 00bec2c | 2011-04-15 19:16:54 -0400 | [diff] [blame] | 450 | |
| 451 | /* If it looks right, just use the tag as a script */ |
Behdad Esfahbod | 7627100 | 2014-07-11 14:54:42 -0400 | [diff] [blame] | 452 | if (((uint32_t) tag & 0xE0E0E0E0u) == 0x40606060u) |
Behdad Esfahbod | 00bec2c | 2011-04-15 19:16:54 -0400 | [diff] [blame] | 453 | return (hb_script_t) tag; |
| 454 | |
| 455 | /* Otherwise, return unknown */ |
| 456 | return HB_SCRIPT_UNKNOWN; |
| 457 | } |
| 458 | |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 459 | /** |
| 460 | * hb_script_from_string: |
Khaled Hosny | 8ab797c | 2015-12-29 17:42:16 +0400 | [diff] [blame] | 461 | * @str: (array length=len) (element-type uint8_t): a string representing an |
| 462 | * ISO 15924 tag. |
| 463 | * @len: length of the @str, or -1 if it is %NULL-terminated. |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 464 | * |
Khaled Hosny | 8ab797c | 2015-12-29 17:42:16 +0400 | [diff] [blame] | 465 | * Converts a string @str representing an ISO 15924 script tag to a |
| 466 | * corresponding #hb_script_t. Shorthand for hb_tag_from_string() then |
| 467 | * hb_script_from_iso15924_tag(). |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 468 | * |
Ebrahim Byagowi | 70d3654 | 2018-03-30 05:00:28 +0430 | [diff] [blame] | 469 | * Return value: |
Khaled Hosny | 8ab797c | 2015-12-29 17:42:16 +0400 | [diff] [blame] | 470 | * An #hb_script_t corresponding to the ISO 15924 tag. |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 471 | * |
Behdad Esfahbod | b881142 | 2015-09-03 15:53:22 +0430 | [diff] [blame] | 472 | * Since: 0.9.2 |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 473 | **/ |
Behdad Esfahbod | 00bec2c | 2011-04-15 19:16:54 -0400 | [diff] [blame] | 474 | hb_script_t |
Khaled Hosny | 8ab797c | 2015-12-29 17:42:16 +0400 | [diff] [blame] | 475 | hb_script_from_string (const char *str, int len) |
Behdad Esfahbod | 00bec2c | 2011-04-15 19:16:54 -0400 | [diff] [blame] | 476 | { |
Khaled Hosny | 8ab797c | 2015-12-29 17:42:16 +0400 | [diff] [blame] | 477 | return hb_script_from_iso15924_tag (hb_tag_from_string (str, len)); |
Behdad Esfahbod | 00bec2c | 2011-04-15 19:16:54 -0400 | [diff] [blame] | 478 | } |
| 479 | |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 480 | /** |
| 481 | * hb_script_to_iso15924_tag: |
Khaled Hosny | 8ab797c | 2015-12-29 17:42:16 +0400 | [diff] [blame] | 482 | * @script: an #hb_script_ to convert. |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 483 | * |
Khaled Hosny | 8ab797c | 2015-12-29 17:42:16 +0400 | [diff] [blame] | 484 | * See hb_script_from_iso15924_tag(). |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 485 | * |
Khaled Hosny | 8ab797c | 2015-12-29 17:42:16 +0400 | [diff] [blame] | 486 | * Return value: |
| 487 | * An #hb_tag_t representing an ISO 15924 script tag. |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 488 | * |
Behdad Esfahbod | b881142 | 2015-09-03 15:53:22 +0430 | [diff] [blame] | 489 | * Since: 0.9.2 |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 490 | **/ |
Behdad Esfahbod | 00bec2c | 2011-04-15 19:16:54 -0400 | [diff] [blame] | 491 | hb_tag_t |
| 492 | hb_script_to_iso15924_tag (hb_script_t script) |
| 493 | { |
Behdad Esfahbod | 62879ee | 2011-04-18 23:40:21 -0400 | [diff] [blame] | 494 | return (hb_tag_t) script; |
Behdad Esfahbod | 00bec2c | 2011-04-15 19:16:54 -0400 | [diff] [blame] | 495 | } |
| 496 | |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 497 | /** |
| 498 | * hb_script_get_horizontal_direction: |
Ebrahim Byagowi | 70d3654 | 2018-03-30 05:00:28 +0430 | [diff] [blame] | 499 | * @script: |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 500 | * |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 501 | * |
Ebrahim Byagowi | 70d3654 | 2018-03-30 05:00:28 +0430 | [diff] [blame] | 502 | * |
| 503 | * Return value: |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 504 | * |
Behdad Esfahbod | b881142 | 2015-09-03 15:53:22 +0430 | [diff] [blame] | 505 | * Since: 0.9.2 |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 506 | **/ |
Behdad Esfahbod | 00bec2c | 2011-04-15 19:16:54 -0400 | [diff] [blame] | 507 | hb_direction_t |
| 508 | hb_script_get_horizontal_direction (hb_script_t script) |
| 509 | { |
Ebrahim Byagowi | f24b0b9 | 2018-04-12 13:40:45 +0430 | [diff] [blame] | 510 | /* https://docs.google.com/spreadsheets/d/1Y90M0Ie3MUJ6UVCRDOypOtijlMDLNNyyLk36T6iMu0o */ |
Behdad Esfahbod | 62879ee | 2011-04-18 23:40:21 -0400 | [diff] [blame] | 511 | switch ((hb_tag_t) script) |
| 512 | { |
Behdad Esfahbod | fa2673c | 2012-03-07 15:52:02 -0500 | [diff] [blame] | 513 | /* Unicode-1.1 additions */ |
Behdad Esfahbod | 62879ee | 2011-04-18 23:40:21 -0400 | [diff] [blame] | 514 | case HB_SCRIPT_ARABIC: |
| 515 | case HB_SCRIPT_HEBREW: |
Behdad Esfahbod | fa2673c | 2012-03-07 15:52:02 -0500 | [diff] [blame] | 516 | |
| 517 | /* Unicode-3.0 additions */ |
Behdad Esfahbod | 62879ee | 2011-04-18 23:40:21 -0400 | [diff] [blame] | 518 | case HB_SCRIPT_SYRIAC: |
| 519 | case HB_SCRIPT_THAANA: |
Behdad Esfahbod | 00bec2c | 2011-04-15 19:16:54 -0400 | [diff] [blame] | 520 | |
Behdad Esfahbod | 62879ee | 2011-04-18 23:40:21 -0400 | [diff] [blame] | 521 | /* Unicode-4.0 additions */ |
| 522 | case HB_SCRIPT_CYPRIOT: |
| 523 | |
Behdad Esfahbod | 50e810c | 2012-03-07 12:49:08 -0500 | [diff] [blame] | 524 | /* Unicode-4.1 additions */ |
| 525 | case HB_SCRIPT_KHAROSHTHI: |
| 526 | |
Behdad Esfahbod | 62879ee | 2011-04-18 23:40:21 -0400 | [diff] [blame] | 527 | /* Unicode-5.0 additions */ |
| 528 | case HB_SCRIPT_PHOENICIAN: |
| 529 | case HB_SCRIPT_NKO: |
| 530 | |
Behdad Esfahbod | 50e810c | 2012-03-07 12:49:08 -0500 | [diff] [blame] | 531 | /* Unicode-5.1 additions */ |
| 532 | case HB_SCRIPT_LYDIAN: |
| 533 | |
Behdad Esfahbod | 62879ee | 2011-04-18 23:40:21 -0400 | [diff] [blame] | 534 | /* Unicode-5.2 additions */ |
| 535 | case HB_SCRIPT_AVESTAN: |
| 536 | case HB_SCRIPT_IMPERIAL_ARAMAIC: |
| 537 | case HB_SCRIPT_INSCRIPTIONAL_PAHLAVI: |
| 538 | case HB_SCRIPT_INSCRIPTIONAL_PARTHIAN: |
| 539 | case HB_SCRIPT_OLD_SOUTH_ARABIAN: |
| 540 | case HB_SCRIPT_OLD_TURKIC: |
| 541 | case HB_SCRIPT_SAMARITAN: |
| 542 | |
| 543 | /* Unicode-6.0 additions */ |
| 544 | case HB_SCRIPT_MANDAIC: |
| 545 | |
Behdad Esfahbod | fa2673c | 2012-03-07 15:52:02 -0500 | [diff] [blame] | 546 | /* Unicode-6.1 additions */ |
| 547 | case HB_SCRIPT_MEROITIC_CURSIVE: |
| 548 | case HB_SCRIPT_MEROITIC_HIEROGLYPHS: |
| 549 | |
Behdad Esfahbod | a4a7899 | 2014-04-28 15:06:42 -0700 | [diff] [blame] | 550 | /* Unicode-7.0 additions */ |
| 551 | case HB_SCRIPT_MANICHAEAN: |
| 552 | case HB_SCRIPT_MENDE_KIKAKUI: |
| 553 | case HB_SCRIPT_NABATAEAN: |
| 554 | case HB_SCRIPT_OLD_NORTH_ARABIAN: |
| 555 | case HB_SCRIPT_PALMYRENE: |
| 556 | case HB_SCRIPT_PSALTER_PAHLAVI: |
| 557 | |
Behdad Esfahbod | 64a2726 | 2015-07-15 01:36:39 +0100 | [diff] [blame] | 558 | /* Unicode-8.0 additions */ |
Cosimo Lupo | c8f2a4f | 2018-01-18 22:49:40 +0100 | [diff] [blame] | 559 | case HB_SCRIPT_HATRAN: |
Behdad Esfahbod | 64a2726 | 2015-07-15 01:36:39 +0100 | [diff] [blame] | 560 | |
Behdad Esfahbod | 691086f | 2016-05-06 12:08:18 +0100 | [diff] [blame] | 561 | /* Unicode-9.0 additions */ |
| 562 | case HB_SCRIPT_ADLAM: |
| 563 | |
Behdad Esfahbod | 060e6b4 | 2018-06-05 17:31:46 -0700 | [diff] [blame] | 564 | /* Unicode-11.0 additions */ |
| 565 | case HB_SCRIPT_HANIFI_ROHINGYA: |
| 566 | case HB_SCRIPT_OLD_SOGDIAN: |
| 567 | case HB_SCRIPT_SOGDIAN: |
| 568 | |
Behdad Esfahbod | 62879ee | 2011-04-18 23:40:21 -0400 | [diff] [blame] | 569 | return HB_DIRECTION_RTL; |
Behdad Esfahbod | f673cfb | 2018-05-07 13:58:32 -0700 | [diff] [blame] | 570 | |
| 571 | |
| 572 | /* https://github.com/harfbuzz/harfbuzz/issues/1000 */ |
David Corbett | d8d1e7d | 2018-09-17 11:09:51 -0400 | [diff] [blame] | 573 | case HB_SCRIPT_OLD_HUNGARIAN: |
Behdad Esfahbod | f673cfb | 2018-05-07 13:58:32 -0700 | [diff] [blame] | 574 | case HB_SCRIPT_OLD_ITALIC: |
David Corbett | 46d8f0d | 2018-07-06 15:47:03 -0400 | [diff] [blame] | 575 | case HB_SCRIPT_RUNIC: |
Behdad Esfahbod | f673cfb | 2018-05-07 13:58:32 -0700 | [diff] [blame] | 576 | |
| 577 | return HB_DIRECTION_INVALID; |
Behdad Esfahbod | 62879ee | 2011-04-18 23:40:21 -0400 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | return HB_DIRECTION_LTR; |
Behdad Esfahbod | 00bec2c | 2011-04-15 19:16:54 -0400 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | |
Behdad Esfahbod | 218e67b | 2011-05-05 15:28:37 -0400 | [diff] [blame] | 584 | /* hb_user_data_array_t */ |
| 585 | |
| 586 | bool |
| 587 | hb_user_data_array_t::set (hb_user_data_key_t *key, |
| 588 | void * data, |
Behdad Esfahbod | 33ccc77 | 2011-08-09 00:43:24 +0200 | [diff] [blame] | 589 | hb_destroy_func_t destroy, |
Behdad Esfahbod | 7babfe5 | 2012-12-04 00:35:54 +0200 | [diff] [blame] | 590 | hb_bool_t replace) |
Behdad Esfahbod | 218e67b | 2011-05-05 15:28:37 -0400 | [diff] [blame] | 591 | { |
| 592 | if (!key) |
| 593 | return false; |
Behdad Esfahbod | 46df682 | 2011-05-05 15:33:19 -0400 | [diff] [blame] | 594 | |
Behdad Esfahbod | 33ccc77 | 2011-08-09 00:43:24 +0200 | [diff] [blame] | 595 | if (replace) { |
| 596 | if (!data && !destroy) { |
Behdad Esfahbod | 0e253e9 | 2012-06-05 15:37:19 -0400 | [diff] [blame] | 597 | items.remove (key, lock); |
Behdad Esfahbod | 33ccc77 | 2011-08-09 00:43:24 +0200 | [diff] [blame] | 598 | return true; |
| 599 | } |
Behdad Esfahbod | 218e67b | 2011-05-05 15:28:37 -0400 | [diff] [blame] | 600 | } |
| 601 | hb_user_data_item_t item = {key, data, destroy}; |
Behdad Esfahbod | ea512f7 | 2015-11-26 19:22:22 -0500 | [diff] [blame] | 602 | bool ret = !!items.replace_or_insert (item, lock, (bool) replace); |
Behdad Esfahbod | 46df682 | 2011-05-05 15:33:19 -0400 | [diff] [blame] | 603 | |
| 604 | return ret; |
Behdad Esfahbod | 218e67b | 2011-05-05 15:28:37 -0400 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | void * |
Behdad Esfahbod | 7babfe5 | 2012-12-04 00:35:54 +0200 | [diff] [blame] | 608 | hb_user_data_array_t::get (hb_user_data_key_t *key) |
Behdad Esfahbod | 218e67b | 2011-05-05 15:28:37 -0400 | [diff] [blame] | 609 | { |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 610 | hb_user_data_item_t item = {nullptr, nullptr, nullptr}; |
Behdad Esfahbod | 46df682 | 2011-05-05 15:33:19 -0400 | [diff] [blame] | 611 | |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 612 | return items.find (key, &item, lock) ? item.data : nullptr; |
Behdad Esfahbod | 45bfa99 | 2011-05-10 19:12:49 -0400 | [diff] [blame] | 613 | } |
Behdad Esfahbod | 46df682 | 2011-05-05 15:33:19 -0400 | [diff] [blame] | 614 | |
Behdad Esfahbod | 218e67b | 2011-05-05 15:28:37 -0400 | [diff] [blame] | 615 | |
Behdad Esfahbod | c78f448 | 2011-05-05 21:31:04 -0400 | [diff] [blame] | 616 | /* hb_version */ |
| 617 | |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 618 | /** |
| 619 | * hb_version: |
| 620 | * @major: (out): Library major version component. |
| 621 | * @minor: (out): Library minor version component. |
| 622 | * @micro: (out): Library micro version component. |
| 623 | * |
| 624 | * Returns library version as three integer components. |
| 625 | * |
Behdad Esfahbod | b881142 | 2015-09-03 15:53:22 +0430 | [diff] [blame] | 626 | * Since: 0.9.2 |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 627 | **/ |
Behdad Esfahbod | c78f448 | 2011-05-05 21:31:04 -0400 | [diff] [blame] | 628 | void |
| 629 | hb_version (unsigned int *major, |
| 630 | unsigned int *minor, |
| 631 | unsigned int *micro) |
| 632 | { |
| 633 | *major = HB_VERSION_MAJOR; |
| 634 | *minor = HB_VERSION_MINOR; |
| 635 | *micro = HB_VERSION_MICRO; |
| 636 | } |
| 637 | |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 638 | /** |
| 639 | * hb_version_string: |
| 640 | * |
| 641 | * Returns library version as a string with three components. |
| 642 | * |
| 643 | * Return value: library version string. |
| 644 | * |
Behdad Esfahbod | b881142 | 2015-09-03 15:53:22 +0430 | [diff] [blame] | 645 | * Since: 0.9.2 |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 646 | **/ |
Behdad Esfahbod | c78f448 | 2011-05-05 21:31:04 -0400 | [diff] [blame] | 647 | const char * |
| 648 | hb_version_string (void) |
| 649 | { |
| 650 | return HB_VERSION_STRING; |
| 651 | } |
| 652 | |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 653 | /** |
Behdad Esfahbod | 2b051c6 | 2014-06-20 14:09:57 -0400 | [diff] [blame] | 654 | * hb_version_atleast: |
Ebrahim Byagowi | 70d3654 | 2018-03-30 05:00:28 +0430 | [diff] [blame] | 655 | * @major: |
| 656 | * @minor: |
| 657 | * @micro: |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 658 | * |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 659 | * |
Ebrahim Byagowi | 70d3654 | 2018-03-30 05:00:28 +0430 | [diff] [blame] | 660 | * |
| 661 | * Return value: |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 662 | * |
Sascha Brawer | 01c3a88 | 2015-06-01 13:22:01 +0200 | [diff] [blame] | 663 | * Since: 0.9.30 |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 664 | **/ |
Behdad Esfahbod | c78f448 | 2011-05-05 21:31:04 -0400 | [diff] [blame] | 665 | hb_bool_t |
Behdad Esfahbod | 2b051c6 | 2014-06-20 14:09:57 -0400 | [diff] [blame] | 666 | hb_version_atleast (unsigned int major, |
| 667 | unsigned int minor, |
| 668 | unsigned int micro) |
Behdad Esfahbod | c78f448 | 2011-05-05 21:31:04 -0400 | [diff] [blame] | 669 | { |
Behdad Esfahbod | 2b051c6 | 2014-06-20 14:09:57 -0400 | [diff] [blame] | 670 | return HB_VERSION_ATLEAST (major, minor, micro); |
Behdad Esfahbod | c78f448 | 2011-05-05 21:31:04 -0400 | [diff] [blame] | 671 | } |
Behdad Esfahbod | 7236410 | 2017-01-20 20:16:53 -0800 | [diff] [blame] | 672 | |
| 673 | |
Behdad Esfahbod | b3c0714 | 2017-01-20 20:30:03 -0800 | [diff] [blame] | 674 | |
Behdad Esfahbod | bb1e192 | 2017-01-21 17:41:37 -0800 | [diff] [blame] | 675 | /* hb_feature_t and hb_variation_t */ |
Behdad Esfahbod | 7236410 | 2017-01-20 20:16:53 -0800 | [diff] [blame] | 676 | |
| 677 | static bool |
| 678 | parse_space (const char **pp, const char *end) |
| 679 | { |
| 680 | while (*pp < end && ISSPACE (**pp)) |
| 681 | (*pp)++; |
| 682 | return true; |
| 683 | } |
| 684 | |
| 685 | static bool |
| 686 | parse_char (const char **pp, const char *end, char c) |
| 687 | { |
| 688 | parse_space (pp, end); |
| 689 | |
| 690 | if (*pp == end || **pp != c) |
| 691 | return false; |
| 692 | |
| 693 | (*pp)++; |
| 694 | return true; |
| 695 | } |
| 696 | |
| 697 | static bool |
| 698 | parse_uint (const char **pp, const char *end, unsigned int *pv) |
| 699 | { |
| 700 | char buf[32]; |
| 701 | unsigned int len = MIN (ARRAY_LENGTH (buf) - 1, (unsigned int) (end - *pp)); |
| 702 | strncpy (buf, *pp, len); |
| 703 | buf[len] = '\0'; |
| 704 | |
| 705 | char *p = buf; |
| 706 | char *pend = p; |
| 707 | unsigned int v; |
| 708 | |
| 709 | /* Intentionally use strtol instead of strtoul, such that |
| 710 | * -1 turns into "big number"... */ |
| 711 | errno = 0; |
| 712 | v = strtol (p, &pend, 0); |
| 713 | if (errno || p == pend) |
| 714 | return false; |
| 715 | |
| 716 | *pv = v; |
| 717 | *pp += pend - p; |
| 718 | return true; |
| 719 | } |
| 720 | |
| 721 | static bool |
Ebrahim Byagowi | 3b0e47c | 2017-06-19 14:47:09 +0430 | [diff] [blame] | 722 | parse_uint32 (const char **pp, const char *end, uint32_t *pv) |
| 723 | { |
| 724 | char buf[32]; |
| 725 | unsigned int len = MIN (ARRAY_LENGTH (buf) - 1, (unsigned int) (end - *pp)); |
| 726 | strncpy (buf, *pp, len); |
| 727 | buf[len] = '\0'; |
| 728 | |
| 729 | char *p = buf; |
| 730 | char *pend = p; |
| 731 | unsigned int v; |
| 732 | |
| 733 | /* Intentionally use strtol instead of strtoul, such that |
| 734 | * -1 turns into "big number"... */ |
| 735 | errno = 0; |
| 736 | v = strtol (p, &pend, 0); |
| 737 | if (errno || p == pend) |
| 738 | return false; |
| 739 | |
| 740 | *pv = v; |
| 741 | *pp += pend - p; |
| 742 | return true; |
| 743 | } |
| 744 | |
Behdad Esfahbod | 3ca69c8 | 2017-09-14 20:50:35 -0400 | [diff] [blame] | 745 | #if defined (HAVE_NEWLOCALE) && defined (HAVE_STRTOD_L) |
| 746 | #define USE_XLOCALE 1 |
Behdad Esfahbod | 7f39f57 | 2017-11-13 15:04:13 -0800 | [diff] [blame] | 747 | #define HB_LOCALE_T locale_t |
| 748 | #define HB_CREATE_LOCALE(locName) newlocale (LC_ALL_MASK, locName, nullptr) |
| 749 | #define HB_FREE_LOCALE(loc) freelocale (loc) |
| 750 | #elif defined(_MSC_VER) |
| 751 | #define USE_XLOCALE 1 |
| 752 | #define HB_LOCALE_T _locale_t |
| 753 | #define HB_CREATE_LOCALE(locName) _create_locale (LC_ALL, locName) |
| 754 | #define HB_FREE_LOCALE(loc) _free_locale (loc) |
| 755 | #define strtod_l(a, b, c) _strtod_l ((a), (b), (c)) |
Behdad Esfahbod | 3ca69c8 | 2017-09-14 20:50:35 -0400 | [diff] [blame] | 756 | #endif |
| 757 | |
| 758 | #ifdef USE_XLOCALE |
| 759 | |
Emil A Eklund | 22defe0 | 2018-08-14 14:47:20 -0700 | [diff] [blame] | 760 | #ifdef HB_USE_ATEXIT |
Behdad Esfahbod | 6750ec6 | 2018-08-12 17:42:16 -0700 | [diff] [blame] | 761 | static void free_static_C_locale (void); |
Emil A Eklund | 22defe0 | 2018-08-14 14:47:20 -0700 | [diff] [blame] | 762 | #endif |
Behdad Esfahbod | 6750ec6 | 2018-08-12 17:42:16 -0700 | [diff] [blame] | 763 | |
| 764 | static struct hb_C_locale_lazy_loader_t : hb_lazy_loader_t<hb_remove_ptr_t<HB_LOCALE_T>::value, |
| 765 | hb_C_locale_lazy_loader_t> |
| 766 | { |
| 767 | static inline HB_LOCALE_T create (void) |
| 768 | { |
| 769 | HB_LOCALE_T C_locale = HB_CREATE_LOCALE ("C"); |
Behdad Esfahbod | 3ca69c8 | 2017-09-14 20:50:35 -0400 | [diff] [blame] | 770 | |
| 771 | #ifdef HB_USE_ATEXIT |
Behdad Esfahbod | 6750ec6 | 2018-08-12 17:42:16 -0700 | [diff] [blame] | 772 | atexit (free_static_C_locale); |
| 773 | #endif |
| 774 | |
| 775 | return C_locale; |
| 776 | } |
| 777 | static inline void destroy (HB_LOCALE_T p) |
| 778 | { |
| 779 | HB_FREE_LOCALE (p); |
| 780 | } |
| 781 | static inline HB_LOCALE_T get_null (void) |
| 782 | { |
| 783 | return nullptr; |
| 784 | } |
| 785 | } static_C_locale; |
| 786 | |
| 787 | #ifdef HB_USE_ATEXIT |
| 788 | static |
| 789 | void free_static_C_locale (void) |
Behdad Esfahbod | 3ca69c8 | 2017-09-14 20:50:35 -0400 | [diff] [blame] | 790 | { |
Behdad Esfahbod | 6750ec6 | 2018-08-12 17:42:16 -0700 | [diff] [blame] | 791 | static_C_locale.free_instance (); |
Behdad Esfahbod | 3ca69c8 | 2017-09-14 20:50:35 -0400 | [diff] [blame] | 792 | } |
| 793 | #endif |
| 794 | |
Behdad Esfahbod | 7f39f57 | 2017-11-13 15:04:13 -0800 | [diff] [blame] | 795 | static HB_LOCALE_T |
Behdad Esfahbod | 3ca69c8 | 2017-09-14 20:50:35 -0400 | [diff] [blame] | 796 | get_C_locale (void) |
| 797 | { |
Behdad Esfahbod | 6750ec6 | 2018-08-12 17:42:16 -0700 | [diff] [blame] | 798 | return static_C_locale.get_unconst (); |
Behdad Esfahbod | 3ca69c8 | 2017-09-14 20:50:35 -0400 | [diff] [blame] | 799 | } |
Behdad Esfahbod | 6750ec6 | 2018-08-12 17:42:16 -0700 | [diff] [blame] | 800 | #endif /* USE_XLOCALE */ |
Behdad Esfahbod | 3ca69c8 | 2017-09-14 20:50:35 -0400 | [diff] [blame] | 801 | |
Ebrahim Byagowi | 3b0e47c | 2017-06-19 14:47:09 +0430 | [diff] [blame] | 802 | static bool |
Behdad Esfahbod | b3c0714 | 2017-01-20 20:30:03 -0800 | [diff] [blame] | 803 | parse_float (const char **pp, const char *end, float *pv) |
| 804 | { |
| 805 | char buf[32]; |
| 806 | unsigned int len = MIN (ARRAY_LENGTH (buf) - 1, (unsigned int) (end - *pp)); |
| 807 | strncpy (buf, *pp, len); |
| 808 | buf[len] = '\0'; |
| 809 | |
| 810 | char *p = buf; |
| 811 | char *pend = p; |
| 812 | float v; |
| 813 | |
| 814 | errno = 0; |
Behdad Esfahbod | 3ca69c8 | 2017-09-14 20:50:35 -0400 | [diff] [blame] | 815 | #ifdef USE_XLOCALE |
| 816 | v = strtod_l (p, &pend, get_C_locale ()); |
| 817 | #else |
Chun-wei Fan | 539571c | 2017-02-24 17:58:25 +0800 | [diff] [blame] | 818 | v = strtod (p, &pend); |
Behdad Esfahbod | 3ca69c8 | 2017-09-14 20:50:35 -0400 | [diff] [blame] | 819 | #endif |
Behdad Esfahbod | b3c0714 | 2017-01-20 20:30:03 -0800 | [diff] [blame] | 820 | if (errno || p == pend) |
| 821 | return false; |
| 822 | |
| 823 | *pv = v; |
| 824 | *pp += pend - p; |
| 825 | return true; |
| 826 | } |
| 827 | |
| 828 | static bool |
Ebrahim Byagowi | 3b0e47c | 2017-06-19 14:47:09 +0430 | [diff] [blame] | 829 | parse_bool (const char **pp, const char *end, uint32_t *pv) |
Behdad Esfahbod | 7236410 | 2017-01-20 20:16:53 -0800 | [diff] [blame] | 830 | { |
| 831 | parse_space (pp, end); |
| 832 | |
| 833 | const char *p = *pp; |
| 834 | while (*pp < end && ISALPHA(**pp)) |
| 835 | (*pp)++; |
| 836 | |
| 837 | /* CSS allows on/off as aliases 1/0. */ |
David Corbett | 85bb89a | 2017-12-04 15:15:27 -0500 | [diff] [blame] | 838 | if (*pp - p == 2 && 0 == strncmp (p, "on", 2)) |
Behdad Esfahbod | 7236410 | 2017-01-20 20:16:53 -0800 | [diff] [blame] | 839 | *pv = 1; |
David Corbett | 85bb89a | 2017-12-04 15:15:27 -0500 | [diff] [blame] | 840 | else if (*pp - p == 3 && 0 == strncmp (p, "off", 3)) |
Behdad Esfahbod | 7236410 | 2017-01-20 20:16:53 -0800 | [diff] [blame] | 841 | *pv = 0; |
| 842 | else |
| 843 | return false; |
| 844 | |
| 845 | return true; |
| 846 | } |
| 847 | |
Behdad Esfahbod | b3c0714 | 2017-01-20 20:30:03 -0800 | [diff] [blame] | 848 | /* hb_feature_t */ |
| 849 | |
Behdad Esfahbod | 7236410 | 2017-01-20 20:16:53 -0800 | [diff] [blame] | 850 | static bool |
| 851 | parse_feature_value_prefix (const char **pp, const char *end, hb_feature_t *feature) |
| 852 | { |
| 853 | if (parse_char (pp, end, '-')) |
| 854 | feature->value = 0; |
| 855 | else { |
| 856 | parse_char (pp, end, '+'); |
| 857 | feature->value = 1; |
| 858 | } |
| 859 | |
| 860 | return true; |
| 861 | } |
| 862 | |
| 863 | static bool |
Behdad Esfahbod | b3c0714 | 2017-01-20 20:30:03 -0800 | [diff] [blame] | 864 | parse_tag (const char **pp, const char *end, hb_tag_t *tag) |
Behdad Esfahbod | 7236410 | 2017-01-20 20:16:53 -0800 | [diff] [blame] | 865 | { |
| 866 | parse_space (pp, end); |
| 867 | |
| 868 | char quote = 0; |
| 869 | |
| 870 | if (*pp < end && (**pp == '\'' || **pp == '"')) |
| 871 | { |
| 872 | quote = **pp; |
| 873 | (*pp)++; |
| 874 | } |
| 875 | |
| 876 | const char *p = *pp; |
Martin Hosken | 39607dc | 2018-08-09 15:16:32 +0700 | [diff] [blame] | 877 | while (*pp < end && (ISALNUM(**pp) || **pp == '_')) |
Behdad Esfahbod | 7236410 | 2017-01-20 20:16:53 -0800 | [diff] [blame] | 878 | (*pp)++; |
| 879 | |
| 880 | if (p == *pp || *pp - p > 4) |
| 881 | return false; |
| 882 | |
Behdad Esfahbod | b3c0714 | 2017-01-20 20:30:03 -0800 | [diff] [blame] | 883 | *tag = hb_tag_from_string (p, *pp - p); |
Behdad Esfahbod | 7236410 | 2017-01-20 20:16:53 -0800 | [diff] [blame] | 884 | |
| 885 | if (quote) |
| 886 | { |
| 887 | /* CSS expects exactly four bytes. And we only allow quotations for |
| 888 | * CSS compatibility. So, enforce the length. */ |
| 889 | if (*pp - p != 4) |
| 890 | return false; |
| 891 | if (*pp == end || **pp != quote) |
| 892 | return false; |
| 893 | (*pp)++; |
| 894 | } |
| 895 | |
| 896 | return true; |
| 897 | } |
| 898 | |
| 899 | static bool |
| 900 | parse_feature_indices (const char **pp, const char *end, hb_feature_t *feature) |
| 901 | { |
| 902 | parse_space (pp, end); |
| 903 | |
| 904 | bool has_start; |
| 905 | |
Behdad Esfahbod | becd84a | 2018-09-11 01:26:18 +0200 | [diff] [blame] | 906 | feature->start = HB_FEATURE_GLOBAL_START; |
| 907 | feature->end = HB_FEATURE_GLOBAL_END; |
Behdad Esfahbod | 7236410 | 2017-01-20 20:16:53 -0800 | [diff] [blame] | 908 | |
| 909 | if (!parse_char (pp, end, '[')) |
| 910 | return true; |
| 911 | |
| 912 | has_start = parse_uint (pp, end, &feature->start); |
| 913 | |
| 914 | if (parse_char (pp, end, ':')) { |
| 915 | parse_uint (pp, end, &feature->end); |
| 916 | } else { |
| 917 | if (has_start) |
| 918 | feature->end = feature->start + 1; |
| 919 | } |
| 920 | |
| 921 | return parse_char (pp, end, ']'); |
| 922 | } |
| 923 | |
| 924 | static bool |
| 925 | parse_feature_value_postfix (const char **pp, const char *end, hb_feature_t *feature) |
| 926 | { |
| 927 | bool had_equal = parse_char (pp, end, '='); |
Ebrahim Byagowi | 3b0e47c | 2017-06-19 14:47:09 +0430 | [diff] [blame] | 928 | bool had_value = parse_uint32 (pp, end, &feature->value) || |
Behdad Esfahbod | 7236410 | 2017-01-20 20:16:53 -0800 | [diff] [blame] | 929 | parse_bool (pp, end, &feature->value); |
| 930 | /* CSS doesn't use equal-sign between tag and value. |
| 931 | * If there was an equal-sign, then there *must* be a value. |
Bruce Mitchener | 90218fa | 2018-01-31 20:44:45 +0700 | [diff] [blame] | 932 | * A value without an equal-sign is ok, but not required. */ |
Behdad Esfahbod | 7236410 | 2017-01-20 20:16:53 -0800 | [diff] [blame] | 933 | return !had_equal || had_value; |
| 934 | } |
| 935 | |
Behdad Esfahbod | 7236410 | 2017-01-20 20:16:53 -0800 | [diff] [blame] | 936 | static bool |
| 937 | parse_one_feature (const char **pp, const char *end, hb_feature_t *feature) |
| 938 | { |
| 939 | return parse_feature_value_prefix (pp, end, feature) && |
Behdad Esfahbod | b3c0714 | 2017-01-20 20:30:03 -0800 | [diff] [blame] | 940 | parse_tag (pp, end, &feature->tag) && |
Behdad Esfahbod | 7236410 | 2017-01-20 20:16:53 -0800 | [diff] [blame] | 941 | parse_feature_indices (pp, end, feature) && |
| 942 | parse_feature_value_postfix (pp, end, feature) && |
| 943 | parse_space (pp, end) && |
| 944 | *pp == end; |
| 945 | } |
| 946 | |
| 947 | /** |
| 948 | * hb_feature_from_string: |
| 949 | * @str: (array length=len) (element-type uint8_t): a string to parse |
| 950 | * @len: length of @str, or -1 if string is %NULL terminated |
| 951 | * @feature: (out): the #hb_feature_t to initialize with the parsed values |
| 952 | * |
| 953 | * Parses a string into a #hb_feature_t. |
| 954 | * |
| 955 | * TODO: document the syntax here. |
| 956 | * |
| 957 | * Return value: |
| 958 | * %true if @str is successfully parsed, %false otherwise. |
| 959 | * |
| 960 | * Since: 0.9.5 |
| 961 | **/ |
| 962 | hb_bool_t |
| 963 | hb_feature_from_string (const char *str, int len, |
| 964 | hb_feature_t *feature) |
| 965 | { |
| 966 | hb_feature_t feat; |
| 967 | |
| 968 | if (len < 0) |
| 969 | len = strlen (str); |
| 970 | |
| 971 | if (likely (parse_one_feature (&str, str + len, &feat))) |
| 972 | { |
| 973 | if (feature) |
| 974 | *feature = feat; |
| 975 | return true; |
| 976 | } |
| 977 | |
| 978 | if (feature) |
| 979 | memset (feature, 0, sizeof (*feature)); |
| 980 | return false; |
| 981 | } |
| 982 | |
| 983 | /** |
| 984 | * hb_feature_to_string: |
| 985 | * @feature: an #hb_feature_t to convert |
| 986 | * @buf: (array length=size) (out): output string |
| 987 | * @size: the allocated size of @buf |
| 988 | * |
| 989 | * Converts a #hb_feature_t into a %NULL-terminated string in the format |
| 990 | * understood by hb_feature_from_string(). The client in responsible for |
| 991 | * allocating big enough size for @buf, 128 bytes is more than enough. |
| 992 | * |
| 993 | * Since: 0.9.5 |
| 994 | **/ |
| 995 | void |
| 996 | hb_feature_to_string (hb_feature_t *feature, |
| 997 | char *buf, unsigned int size) |
| 998 | { |
| 999 | if (unlikely (!size)) return; |
| 1000 | |
| 1001 | char s[128]; |
| 1002 | unsigned int len = 0; |
| 1003 | if (feature->value == 0) |
| 1004 | s[len++] = '-'; |
| 1005 | hb_tag_to_string (feature->tag, s + len); |
| 1006 | len += 4; |
| 1007 | while (len && s[len - 1] == ' ') |
| 1008 | len--; |
| 1009 | if (feature->start != 0 || feature->end != (unsigned int) -1) |
| 1010 | { |
| 1011 | s[len++] = '['; |
| 1012 | if (feature->start) |
| 1013 | len += MAX (0, snprintf (s + len, ARRAY_LENGTH (s) - len, "%u", feature->start)); |
| 1014 | if (feature->end != feature->start + 1) { |
| 1015 | s[len++] = ':'; |
| 1016 | if (feature->end != (unsigned int) -1) |
| 1017 | len += MAX (0, snprintf (s + len, ARRAY_LENGTH (s) - len, "%u", feature->end)); |
| 1018 | } |
| 1019 | s[len++] = ']'; |
| 1020 | } |
| 1021 | if (feature->value > 1) |
| 1022 | { |
| 1023 | s[len++] = '='; |
| 1024 | len += MAX (0, snprintf (s + len, ARRAY_LENGTH (s) - len, "%u", feature->value)); |
| 1025 | } |
| 1026 | assert (len < ARRAY_LENGTH (s)); |
| 1027 | len = MIN (len, size - 1); |
| 1028 | memcpy (buf, s, len); |
| 1029 | buf[len] = '\0'; |
| 1030 | } |
Behdad Esfahbod | b3c0714 | 2017-01-20 20:30:03 -0800 | [diff] [blame] | 1031 | |
Behdad Esfahbod | bb1e192 | 2017-01-21 17:41:37 -0800 | [diff] [blame] | 1032 | /* hb_variation_t */ |
Behdad Esfahbod | b3c0714 | 2017-01-20 20:30:03 -0800 | [diff] [blame] | 1033 | |
| 1034 | static bool |
Behdad Esfahbod | bb1e192 | 2017-01-21 17:41:37 -0800 | [diff] [blame] | 1035 | parse_variation_value (const char **pp, const char *end, hb_variation_t *variation) |
Behdad Esfahbod | b3c0714 | 2017-01-20 20:30:03 -0800 | [diff] [blame] | 1036 | { |
| 1037 | parse_char (pp, end, '='); /* Optional. */ |
Behdad Esfahbod | bb1e192 | 2017-01-21 17:41:37 -0800 | [diff] [blame] | 1038 | return parse_float (pp, end, &variation->value); |
Behdad Esfahbod | b3c0714 | 2017-01-20 20:30:03 -0800 | [diff] [blame] | 1039 | } |
| 1040 | |
| 1041 | static bool |
Behdad Esfahbod | bb1e192 | 2017-01-21 17:41:37 -0800 | [diff] [blame] | 1042 | parse_one_variation (const char **pp, const char *end, hb_variation_t *variation) |
Behdad Esfahbod | b3c0714 | 2017-01-20 20:30:03 -0800 | [diff] [blame] | 1043 | { |
Behdad Esfahbod | bb1e192 | 2017-01-21 17:41:37 -0800 | [diff] [blame] | 1044 | return parse_tag (pp, end, &variation->tag) && |
| 1045 | parse_variation_value (pp, end, variation) && |
Behdad Esfahbod | b3c0714 | 2017-01-20 20:30:03 -0800 | [diff] [blame] | 1046 | parse_space (pp, end) && |
| 1047 | *pp == end; |
| 1048 | } |
| 1049 | |
Behdad Esfahbod | d2f249e | 2017-01-22 17:42:33 -0800 | [diff] [blame] | 1050 | /** |
| 1051 | * hb_variation_from_string: |
| 1052 | * |
| 1053 | * Since: 1.4.2 |
| 1054 | */ |
Behdad Esfahbod | b3c0714 | 2017-01-20 20:30:03 -0800 | [diff] [blame] | 1055 | hb_bool_t |
Behdad Esfahbod | bb1e192 | 2017-01-21 17:41:37 -0800 | [diff] [blame] | 1056 | hb_variation_from_string (const char *str, int len, |
| 1057 | hb_variation_t *variation) |
Behdad Esfahbod | b3c0714 | 2017-01-20 20:30:03 -0800 | [diff] [blame] | 1058 | { |
Behdad Esfahbod | bb1e192 | 2017-01-21 17:41:37 -0800 | [diff] [blame] | 1059 | hb_variation_t var; |
Behdad Esfahbod | b3c0714 | 2017-01-20 20:30:03 -0800 | [diff] [blame] | 1060 | |
| 1061 | if (len < 0) |
| 1062 | len = strlen (str); |
| 1063 | |
Behdad Esfahbod | bb1e192 | 2017-01-21 17:41:37 -0800 | [diff] [blame] | 1064 | if (likely (parse_one_variation (&str, str + len, &var))) |
Behdad Esfahbod | b3c0714 | 2017-01-20 20:30:03 -0800 | [diff] [blame] | 1065 | { |
Behdad Esfahbod | bb1e192 | 2017-01-21 17:41:37 -0800 | [diff] [blame] | 1066 | if (variation) |
| 1067 | *variation = var; |
Behdad Esfahbod | b3c0714 | 2017-01-20 20:30:03 -0800 | [diff] [blame] | 1068 | return true; |
| 1069 | } |
| 1070 | |
Behdad Esfahbod | bb1e192 | 2017-01-21 17:41:37 -0800 | [diff] [blame] | 1071 | if (variation) |
| 1072 | memset (variation, 0, sizeof (*variation)); |
Behdad Esfahbod | b3c0714 | 2017-01-20 20:30:03 -0800 | [diff] [blame] | 1073 | return false; |
| 1074 | } |
| 1075 | |
Behdad Esfahbod | d2f249e | 2017-01-22 17:42:33 -0800 | [diff] [blame] | 1076 | /** |
| 1077 | * hb_variation_to_string: |
| 1078 | * |
| 1079 | * Since: 1.4.2 |
| 1080 | */ |
Behdad Esfahbod | b3c0714 | 2017-01-20 20:30:03 -0800 | [diff] [blame] | 1081 | void |
Behdad Esfahbod | bb1e192 | 2017-01-21 17:41:37 -0800 | [diff] [blame] | 1082 | hb_variation_to_string (hb_variation_t *variation, |
Behdad Esfahbod | b3c0714 | 2017-01-20 20:30:03 -0800 | [diff] [blame] | 1083 | char *buf, unsigned int size) |
| 1084 | { |
| 1085 | if (unlikely (!size)) return; |
| 1086 | |
| 1087 | char s[128]; |
| 1088 | unsigned int len = 0; |
Behdad Esfahbod | bb1e192 | 2017-01-21 17:41:37 -0800 | [diff] [blame] | 1089 | hb_tag_to_string (variation->tag, s + len); |
Behdad Esfahbod | b3c0714 | 2017-01-20 20:30:03 -0800 | [diff] [blame] | 1090 | len += 4; |
| 1091 | while (len && s[len - 1] == ' ') |
| 1092 | len--; |
| 1093 | s[len++] = '='; |
Ebrahim Byagowi | 5de2d9c | 2018-10-04 02:14:18 +0330 | [diff] [blame] | 1094 | len += MAX (0, snprintf (s + len, ARRAY_LENGTH (s) - len, "%g", (double) variation->value)); |
Behdad Esfahbod | b3c0714 | 2017-01-20 20:30:03 -0800 | [diff] [blame] | 1095 | |
| 1096 | assert (len < ARRAY_LENGTH (s)); |
| 1097 | len = MIN (len, size - 1); |
| 1098 | memcpy (buf, s, len); |
| 1099 | buf[len] = '\0'; |
| 1100 | } |
Behdad Esfahbod | e22a48a | 2018-07-23 13:24:26 -0700 | [diff] [blame] | 1101 | |
| 1102 | /* If there is no visibility control, then hb-static.cc will NOT |
| 1103 | * define anything. Instead, we get it to define one set in here |
| 1104 | * only, so only libharfbuzz.so defines them, not other libs. */ |
| 1105 | #ifdef HB_NO_VISIBILITY |
| 1106 | #undef HB_NO_VISIBILITY |
| 1107 | #include "hb-static.cc" |
| 1108 | #define HB_NO_VISIBILITY 1 |
| 1109 | #endif |