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 | c77ae40 | 2018-08-25 22:36:36 -0700 | [diff] [blame] | 30 | #include "hb-machinery.hh" |
Behdad Esfahbod | b8d6183 | 2011-05-05 15:14:04 -0400 | [diff] [blame] | 31 | |
Behdad Esfahbod | acdba3f | 2010-07-23 15:11:18 -0400 | [diff] [blame] | 32 | |
Behdad Esfahbod | 00cf4e5 | 2018-10-27 04:07:33 -0700 | [diff] [blame] | 33 | /** |
| 34 | * SECTION:hb-common |
Behdad Esfahbod | cf5fa57 | 2018-10-27 04:50:38 -0700 | [diff] [blame] | 35 | * @title: hb-common |
Behdad Esfahbod | 00cf4e5 | 2018-10-27 04:07:33 -0700 | [diff] [blame] | 36 | * @short_description: Common data types |
| 37 | * @include: hb.h |
| 38 | * |
| 39 | * Common data types used across HarfBuzz are defined here. |
| 40 | **/ |
| 41 | |
| 42 | |
Behdad Esfahbod | bab02d3 | 2013-02-12 15:26:45 -0500 | [diff] [blame] | 43 | /* hb_options_t */ |
| 44 | |
Behdad Esfahbod | 4bc16ac | 2018-07-31 21:05:51 -0700 | [diff] [blame] | 45 | hb_atomic_int_t _hb_options; |
Behdad Esfahbod | bab02d3 | 2013-02-12 15:26:45 -0500 | [diff] [blame] | 46 | |
| 47 | void |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 48 | _hb_options_init () |
Behdad Esfahbod | bab02d3 | 2013-02-12 15:26:45 -0500 | [diff] [blame] | 49 | { |
| 50 | hb_options_union_t u; |
| 51 | u.i = 0; |
Bruce Mitchener | 8d1e479 | 2018-10-18 22:18:42 +0700 | [diff] [blame] | 52 | u.opts.initialized = true; |
Behdad Esfahbod | bab02d3 | 2013-02-12 15:26:45 -0500 | [diff] [blame] | 53 | |
Behdad Esfahbod | 38a7a8a | 2018-10-10 17:44:46 -0400 | [diff] [blame] | 54 | const char *c = getenv ("HB_OPTIONS"); |
| 55 | if (c) |
| 56 | { |
| 57 | while (*c) |
| 58 | { |
| 59 | const char *p = strchr (c, ':'); |
| 60 | if (!p) |
Ebrahim Byagowi | a0b4ac4 | 2019-08-24 17:57:14 +0430 | [diff] [blame] | 61 | p = c + strlen (c); |
Behdad Esfahbod | 38a7a8a | 2018-10-10 17:44:46 -0400 | [diff] [blame] | 62 | |
| 63 | #define OPTION(name, symbol) \ |
GaryQian | ccf1448 | 2019-06-24 12:57:13 -0700 | [diff] [blame] | 64 | if (0 == strncmp (c, name, p - c) && strlen (name) == static_cast<size_t>(p - c)) do { u.opts.symbol = true; } while (0) |
Behdad Esfahbod | 38a7a8a | 2018-10-10 17:44:46 -0400 | [diff] [blame] | 65 | |
| 66 | OPTION ("uniscribe-bug-compatible", uniscribe_bug_compatible); |
Behdad Esfahbod | 38a7a8a | 2018-10-10 17:44:46 -0400 | [diff] [blame] | 67 | |
| 68 | #undef OPTION |
| 69 | |
| 70 | c = *p ? p + 1 : p; |
| 71 | } |
| 72 | |
| 73 | } |
Behdad Esfahbod | bab02d3 | 2013-02-12 15:26:45 -0500 | [diff] [blame] | 74 | |
| 75 | /* This is idempotent and threadsafe. */ |
Behdad Esfahbod | f73c15c | 2022-08-03 12:54:03 -0600 | [diff] [blame] | 76 | _hb_options = u.i; |
Behdad Esfahbod | bab02d3 | 2013-02-12 15:26:45 -0500 | [diff] [blame] | 77 | } |
| 78 | |
Behdad Esfahbod | acdba3f | 2010-07-23 15:11:18 -0400 | [diff] [blame] | 79 | |
Behdad Esfahbod | 8e4bb3c | 2011-04-11 17:55:58 -0400 | [diff] [blame] | 80 | /* hb_tag_t */ |
| 81 | |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 82 | /** |
| 83 | * hb_tag_from_string: |
Nathan Willis | 4cdb12a | 2019-04-21 20:31:09 +0100 | [diff] [blame] | 84 | * @str: (array length=len) (element-type uint8_t): String to convert |
Khaled Hosny | 98e90cc | 2022-06-30 08:43:57 +0200 | [diff] [blame] | 85 | * @len: Length of @str, or -1 if it is `NULL`-terminated |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 86 | * |
Nathan Willis | 4cdb12a | 2019-04-21 20:31:09 +0100 | [diff] [blame] | 87 | * Converts a string into an #hb_tag_t. Valid tags |
| 88 | * are four characters. Shorter input strings will be |
| 89 | * padded with spaces. Longer input strings will be |
| 90 | * truncated. |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 91 | * |
Nathan Willis | 4cdb12a | 2019-04-21 20:31:09 +0100 | [diff] [blame] | 92 | * Return value: The #hb_tag_t corresponding to @str |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 93 | * |
Behdad Esfahbod | b881142 | 2015-09-03 15:53:22 +0430 | [diff] [blame] | 94 | * Since: 0.9.2 |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 95 | **/ |
Behdad Esfahbod | 83f3467 | 2010-05-21 13:43:49 +0100 | [diff] [blame] | 96 | hb_tag_t |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 97 | hb_tag_from_string (const char *str, int len) |
Behdad Esfahbod | 83f3467 | 2010-05-21 13:43:49 +0100 | [diff] [blame] | 98 | { |
| 99 | char tag[4]; |
| 100 | unsigned int i; |
| 101 | |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 102 | if (!str || !len || !*str) |
Behdad Esfahbod | 7ff7401 | 2011-04-11 13:27:30 -0400 | [diff] [blame] | 103 | return HB_TAG_NONE; |
| 104 | |
Behdad Esfahbod | 4c9fe88 | 2011-08-26 09:18:53 +0200 | [diff] [blame] | 105 | if (len < 0 || len > 4) |
| 106 | len = 4; |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 107 | for (i = 0; i < (unsigned) len && str[i]; i++) |
| 108 | tag[i] = str[i]; |
Behdad Esfahbod | 83f3467 | 2010-05-21 13:43:49 +0100 | [diff] [blame] | 109 | for (; i < 4; i++) |
| 110 | tag[i] = ' '; |
| 111 | |
Behdad Esfahbod | 8eaff98 | 2017-10-31 15:30:06 -0600 | [diff] [blame] | 112 | return HB_TAG (tag[0], tag[1], tag[2], tag[3]); |
Behdad Esfahbod | 83f3467 | 2010-05-21 13:43:49 +0100 | [diff] [blame] | 113 | } |
Behdad Esfahbod | acdba3f | 2010-07-23 15:11:18 -0400 | [diff] [blame] | 114 | |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 115 | /** |
| 116 | * hb_tag_to_string: |
Nathan Willis | 4cdb12a | 2019-04-21 20:31:09 +0100 | [diff] [blame] | 117 | * @tag: #hb_tag_t to convert |
| 118 | * @buf: (out caller-allocates) (array fixed-size=4) (element-type uint8_t): Converted string |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 119 | * |
Behdad Esfahbod | a45a630 | 2022-01-08 15:47:33 -0800 | [diff] [blame] | 120 | * Converts an #hb_tag_t to a string and returns it in @buf. |
Nathan Willis | 4cdb12a | 2019-04-21 20:31:09 +0100 | [diff] [blame] | 121 | * Strings will be four characters long. |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 122 | * |
Sascha Brawer | 01c3a88 | 2015-06-01 13:22:01 +0200 | [diff] [blame] | 123 | * Since: 0.9.5 |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 124 | **/ |
Behdad Esfahbod | e30ebd2 | 2012-09-06 22:09:06 -0400 | [diff] [blame] | 125 | void |
| 126 | hb_tag_to_string (hb_tag_t tag, char *buf) |
| 127 | { |
| 128 | buf[0] = (char) (uint8_t) (tag >> 24); |
| 129 | buf[1] = (char) (uint8_t) (tag >> 16); |
| 130 | buf[2] = (char) (uint8_t) (tag >> 8); |
| 131 | buf[3] = (char) (uint8_t) (tag >> 0); |
| 132 | } |
| 133 | |
Behdad Esfahbod | acdba3f | 2010-07-23 15:11:18 -0400 | [diff] [blame] | 134 | |
Behdad Esfahbod | 39a840a | 2011-04-27 14:48:19 -0400 | [diff] [blame] | 135 | /* hb_direction_t */ |
| 136 | |
Behdad Esfahbod | c859cbf | 2022-06-16 13:55:12 -0600 | [diff] [blame] | 137 | static const char direction_strings[][4] = { |
Behdad Esfahbod | 39a840a | 2011-04-27 14:48:19 -0400 | [diff] [blame] | 138 | "ltr", |
| 139 | "rtl", |
| 140 | "ttb", |
| 141 | "btt" |
| 142 | }; |
| 143 | |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 144 | /** |
| 145 | * hb_direction_from_string: |
Nathan Willis | 4cdb12a | 2019-04-21 20:31:09 +0100 | [diff] [blame] | 146 | * @str: (array length=len) (element-type uint8_t): String to convert |
Khaled Hosny | 98e90cc | 2022-06-30 08:43:57 +0200 | [diff] [blame] | 147 | * @len: Length of @str, or -1 if it is `NULL`-terminated |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 148 | * |
Behdad Esfahbod | a45a630 | 2022-01-08 15:47:33 -0800 | [diff] [blame] | 149 | * Converts a string to an #hb_direction_t. |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 150 | * |
Nathan Willis | 4cdb12a | 2019-04-21 20:31:09 +0100 | [diff] [blame] | 151 | * Matching is loose and applies only to the first letter. For |
| 152 | * examples, "LTR" and "left-to-right" will both return #HB_DIRECTION_LTR. |
Ebrahim Byagowi | 70d3654 | 2018-03-30 05:00:28 +0430 | [diff] [blame] | 153 | * |
Nathan Willis | 4cdb12a | 2019-04-21 20:31:09 +0100 | [diff] [blame] | 154 | * Unmatched strings will return #HB_DIRECTION_INVALID. |
Behdad Esfahbod | a45a630 | 2022-01-08 15:47:33 -0800 | [diff] [blame] | 155 | * |
Nathan Willis | 4cdb12a | 2019-04-21 20:31:09 +0100 | [diff] [blame] | 156 | * Return value: The #hb_direction_t matching @str |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 157 | * |
Behdad Esfahbod | b881142 | 2015-09-03 15:53:22 +0430 | [diff] [blame] | 158 | * Since: 0.9.2 |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 159 | **/ |
Behdad Esfahbod | 39a840a | 2011-04-27 14:48:19 -0400 | [diff] [blame] | 160 | hb_direction_t |
Behdad Esfahbod | 4c9fe88 | 2011-08-26 09:18:53 +0200 | [diff] [blame] | 161 | hb_direction_from_string (const char *str, int len) |
Behdad Esfahbod | 39a840a | 2011-04-27 14:48:19 -0400 | [diff] [blame] | 162 | { |
Behdad Esfahbod | 4c9fe88 | 2011-08-26 09:18:53 +0200 | [diff] [blame] | 163 | if (unlikely (!str || !len || !*str)) |
Behdad Esfahbod | 39a840a | 2011-04-27 14:48:19 -0400 | [diff] [blame] | 164 | return HB_DIRECTION_INVALID; |
| 165 | |
| 166 | /* Lets match loosely: just match the first letter, such that |
| 167 | * all of "ltr", "left-to-right", etc work! |
| 168 | */ |
| 169 | char c = TOLOWER (str[0]); |
| 170 | for (unsigned int i = 0; i < ARRAY_LENGTH (direction_strings); i++) |
| 171 | if (c == direction_strings[i][0]) |
Behdad Esfahbod | 4bf90f6 | 2012-04-12 17:38:23 -0400 | [diff] [blame] | 172 | return (hb_direction_t) (HB_DIRECTION_LTR + i); |
Behdad Esfahbod | 39a840a | 2011-04-27 14:48:19 -0400 | [diff] [blame] | 173 | |
| 174 | return HB_DIRECTION_INVALID; |
| 175 | } |
| 176 | |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 177 | /** |
| 178 | * hb_direction_to_string: |
Nathan Willis | 4cdb12a | 2019-04-21 20:31:09 +0100 | [diff] [blame] | 179 | * @direction: The #hb_direction_t to convert |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 180 | * |
Nathan Willis | 4cdb12a | 2019-04-21 20:31:09 +0100 | [diff] [blame] | 181 | * Converts an #hb_direction_t to a string. |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 182 | * |
Nathan Willis | 4cdb12a | 2019-04-21 20:31:09 +0100 | [diff] [blame] | 183 | * Return value: (transfer none): The string corresponding to @direction |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 184 | * |
Behdad Esfahbod | b881142 | 2015-09-03 15:53:22 +0430 | [diff] [blame] | 185 | * Since: 0.9.2 |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 186 | **/ |
Behdad Esfahbod | 39a840a | 2011-04-27 14:48:19 -0400 | [diff] [blame] | 187 | const char * |
| 188 | hb_direction_to_string (hb_direction_t direction) |
| 189 | { |
Behdad Esfahbod | 4bf90f6 | 2012-04-12 17:38:23 -0400 | [diff] [blame] | 190 | if (likely ((unsigned int) (direction - HB_DIRECTION_LTR) |
| 191 | < ARRAY_LENGTH (direction_strings))) |
| 192 | return direction_strings[direction - HB_DIRECTION_LTR]; |
Behdad Esfahbod | 39a840a | 2011-04-27 14:48:19 -0400 | [diff] [blame] | 193 | |
| 194 | return "invalid"; |
| 195 | } |
| 196 | |
| 197 | |
Behdad Esfahbod | 8e4bb3c | 2011-04-11 17:55:58 -0400 | [diff] [blame] | 198 | /* hb_language_t */ |
| 199 | |
Behdad Esfahbod | 1bc1cb3 | 2012-06-16 15:21:55 -0400 | [diff] [blame] | 200 | struct hb_language_impl_t { |
Behdad Esfahbod | 3cbdf70 | 2011-04-15 12:32:06 -0400 | [diff] [blame] | 201 | const char s[1]; |
| 202 | }; |
| 203 | |
Behdad Esfahbod | 8e4bb3c | 2011-04-11 17:55:58 -0400 | [diff] [blame] | 204 | static const char canon_map[256] = { |
| 205 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 206 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 207 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '-', 0, 0, |
| 208 | '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 0, 0, 0, 0, 0, 0, |
David Corbett | 018ba46 | 2018-11-23 13:21:22 -0500 | [diff] [blame] | 209 | 0, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', |
Behdad Esfahbod | 8e4bb3c | 2011-04-11 17:55:58 -0400 | [diff] [blame] | 210 | 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 0, 0, 0, 0, '-', |
| 211 | 0, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', |
| 212 | 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 0, 0, 0, 0, 0 |
| 213 | }; |
| 214 | |
Behdad Esfahbod | f3b170b | 2015-04-08 16:26:24 -0700 | [diff] [blame] | 215 | static bool |
Behdad Esfahbod | 093171c | 2012-06-05 18:00:45 -0400 | [diff] [blame] | 216 | lang_equal (hb_language_t v1, |
| 217 | const void *v2) |
Behdad Esfahbod | 8e4bb3c | 2011-04-11 17:55:58 -0400 | [diff] [blame] | 218 | { |
Behdad Esfahbod | c57d454 | 2011-04-20 18:50:27 -0400 | [diff] [blame] | 219 | const unsigned char *p1 = (const unsigned char *) v1; |
| 220 | const unsigned char *p2 = (const unsigned char *) v2; |
Behdad Esfahbod | 8e4bb3c | 2011-04-11 17:55:58 -0400 | [diff] [blame] | 221 | |
Chris Peterson | aacca37 | 2017-04-17 23:25:24 -0700 | [diff] [blame] | 222 | while (*p1 && *p1 == canon_map[*p2]) { |
| 223 | p1++; |
| 224 | p2++; |
| 225 | } |
Behdad Esfahbod | 8e4bb3c | 2011-04-11 17:55:58 -0400 | [diff] [blame] | 226 | |
Behdad Esfahbod | 093171c | 2012-06-05 18:00:45 -0400 | [diff] [blame] | 227 | return *p1 == canon_map[*p2]; |
Behdad Esfahbod | 8e4bb3c | 2011-04-11 17:55:58 -0400 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | #if 0 |
| 231 | static unsigned int |
| 232 | lang_hash (const void *key) |
| 233 | { |
| 234 | const unsigned char *p = key; |
| 235 | unsigned int h = 0; |
| 236 | while (canon_map[*p]) |
| 237 | { |
| 238 | h = (h << 5) - h + canon_map[*p]; |
| 239 | p++; |
| 240 | } |
| 241 | |
| 242 | return h; |
| 243 | } |
| 244 | #endif |
| 245 | |
| 246 | |
Behdad Esfahbod | b45f32e | 2011-05-05 15:00:43 -0400 | [diff] [blame] | 247 | struct hb_language_item_t { |
| 248 | |
Behdad Esfahbod | 093171c | 2012-06-05 18:00:45 -0400 | [diff] [blame] | 249 | struct hb_language_item_t *next; |
Behdad Esfahbod | b45f32e | 2011-05-05 15:00:43 -0400 | [diff] [blame] | 250 | hb_language_t lang; |
| 251 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 252 | bool operator == (const char *s) const |
| 253 | { return lang_equal (lang, s); } |
Behdad Esfahbod | b45f32e | 2011-05-05 15:00:43 -0400 | [diff] [blame] | 254 | |
Behdad Esfahbod | bb48bf5 | 2021-07-08 10:53:45 -0600 | [diff] [blame] | 255 | hb_language_item_t & operator = (const char *s) |
| 256 | { |
| 257 | /* We can't call strdup(), because we allow custom allocators. */ |
Sebastian Rasmussen | 92e2c4b | 2017-05-29 12:53:30 -0500 | [diff] [blame] | 258 | size_t len = strlen(s) + 1; |
Behdad Esfahbod | 2337f0d | 2021-07-08 10:58:50 -0600 | [diff] [blame] | 259 | lang = (hb_language_t) hb_malloc(len); |
Behdad Esfahbod | 7dba306 | 2017-06-01 11:44:42 -0400 | [diff] [blame] | 260 | if (likely (lang)) |
| 261 | { |
Behdad Esfahbod | 59c45f6 | 2022-11-22 12:54:50 -0700 | [diff] [blame] | 262 | hb_memcpy((unsigned char *) lang, s, len); |
Behdad Esfahbod | 7dba306 | 2017-06-01 11:44:42 -0400 | [diff] [blame] | 263 | for (unsigned char *p = (unsigned char *) lang; *p; p++) |
| 264 | *p = canon_map[*p]; |
| 265 | } |
Behdad Esfahbod | b45f32e | 2011-05-05 15:00:43 -0400 | [diff] [blame] | 266 | |
| 267 | return *this; |
| 268 | } |
| 269 | |
Behdad Esfahbod | 2337f0d | 2021-07-08 10:58:50 -0600 | [diff] [blame] | 270 | void fini () { hb_free ((void *) lang); } |
Behdad Esfahbod | b45f32e | 2011-05-05 15:00:43 -0400 | [diff] [blame] | 271 | }; |
| 272 | |
Behdad Esfahbod | 093171c | 2012-06-05 18:00:45 -0400 | [diff] [blame] | 273 | |
Behdad Esfahbod | bb48bf5 | 2021-07-08 10:53:45 -0600 | [diff] [blame] | 274 | /* Thread-safe lockfree language list */ |
Behdad Esfahbod | 093171c | 2012-06-05 18:00:45 -0400 | [diff] [blame] | 275 | |
Behdad Esfahbod | 1f73809 | 2018-08-09 00:22:37 -0700 | [diff] [blame] | 276 | static hb_atomic_ptr_t <hb_language_item_t> langs; |
Behdad Esfahbod | 093171c | 2012-06-05 18:00:45 -0400 | [diff] [blame] | 277 | |
Behdad Esfahbod | ed11632 | 2021-09-14 07:09:54 -0400 | [diff] [blame] | 278 | static inline void |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 279 | free_langs () |
Behdad Esfahbod | 093171c | 2012-06-05 18:00:45 -0400 | [diff] [blame] | 280 | { |
Behdad Esfahbod | 5aa2c6e | 2018-03-28 15:33:51 -0700 | [diff] [blame] | 281 | retry: |
Behdad Esfahbod | f6fc557 | 2018-11-05 13:23:54 -0500 | [diff] [blame] | 282 | hb_language_item_t *first_lang = langs; |
Behdad Esfahbod | 1f73809 | 2018-08-09 00:22:37 -0700 | [diff] [blame] | 283 | if (unlikely (!langs.cmpexch (first_lang, nullptr))) |
Behdad Esfahbod | 5aa2c6e | 2018-03-28 15:33:51 -0700 | [diff] [blame] | 284 | goto retry; |
| 285 | |
| 286 | while (first_lang) { |
| 287 | hb_language_item_t *next = first_lang->next; |
Behdad Esfahbod | a60ba79 | 2018-05-01 19:01:25 -0400 | [diff] [blame] | 288 | first_lang->fini (); |
Behdad Esfahbod | 2337f0d | 2021-07-08 10:58:50 -0600 | [diff] [blame] | 289 | hb_free (first_lang); |
Behdad Esfahbod | 5aa2c6e | 2018-03-28 15:33:51 -0700 | [diff] [blame] | 290 | first_lang = next; |
Behdad Esfahbod | 093171c | 2012-06-05 18:00:45 -0400 | [diff] [blame] | 291 | } |
| 292 | } |
| 293 | |
| 294 | static hb_language_item_t * |
| 295 | lang_find_or_insert (const char *key) |
| 296 | { |
Behdad Esfahbod | 093171c | 2012-06-05 18:00:45 -0400 | [diff] [blame] | 297 | retry: |
Behdad Esfahbod | f6fc557 | 2018-11-05 13:23:54 -0500 | [diff] [blame] | 298 | hb_language_item_t *first_lang = langs; |
Behdad Esfahbod | 093171c | 2012-06-05 18:00:45 -0400 | [diff] [blame] | 299 | |
| 300 | for (hb_language_item_t *lang = first_lang; lang; lang = lang->next) |
| 301 | if (*lang == key) |
| 302 | return lang; |
| 303 | |
| 304 | /* Not found; allocate one. */ |
Behdad Esfahbod | 2337f0d | 2021-07-08 10:58:50 -0600 | [diff] [blame] | 305 | hb_language_item_t *lang = (hb_language_item_t *) hb_calloc (1, sizeof (hb_language_item_t)); |
Behdad Esfahbod | 093171c | 2012-06-05 18:00:45 -0400 | [diff] [blame] | 306 | if (unlikely (!lang)) |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 307 | return nullptr; |
Behdad Esfahbod | 093171c | 2012-06-05 18:00:45 -0400 | [diff] [blame] | 308 | lang->next = first_lang; |
| 309 | *lang = key; |
Behdad Esfahbod | 7dba306 | 2017-06-01 11:44:42 -0400 | [diff] [blame] | 310 | if (unlikely (!lang->lang)) |
| 311 | { |
Behdad Esfahbod | 2337f0d | 2021-07-08 10:58:50 -0600 | [diff] [blame] | 312 | hb_free (lang); |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 313 | return nullptr; |
Behdad Esfahbod | 7dba306 | 2017-06-01 11:44:42 -0400 | [diff] [blame] | 314 | } |
Behdad Esfahbod | 093171c | 2012-06-05 18:00:45 -0400 | [diff] [blame] | 315 | |
Behdad Esfahbod | 1f73809 | 2018-08-09 00:22:37 -0700 | [diff] [blame] | 316 | if (unlikely (!langs.cmpexch (first_lang, lang))) |
| 317 | { |
Behdad Esfahbod | a60ba79 | 2018-05-01 19:01:25 -0400 | [diff] [blame] | 318 | lang->fini (); |
Behdad Esfahbod | 2337f0d | 2021-07-08 10:58:50 -0600 | [diff] [blame] | 319 | hb_free (lang); |
Behdad Esfahbod | 093171c | 2012-06-05 18:00:45 -0400 | [diff] [blame] | 320 | goto retry; |
| 321 | } |
| 322 | |
Behdad Esfahbod | 04aed57 | 2012-06-05 18:30:19 -0400 | [diff] [blame] | 323 | if (!first_lang) |
Behdad Esfahbod | ed11632 | 2021-09-14 07:09:54 -0400 | [diff] [blame] | 324 | hb_atexit (free_langs); /* First person registers atexit() callback. */ |
Behdad Esfahbod | 093171c | 2012-06-05 18:00:45 -0400 | [diff] [blame] | 325 | |
| 326 | return lang; |
| 327 | } |
| 328 | |
Behdad Esfahbod | b8d6183 | 2011-05-05 15:14:04 -0400 | [diff] [blame] | 329 | |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 330 | /** |
| 331 | * hb_language_from_string: |
Khaled Hosny | 8ab797c | 2015-12-29 17:42:16 +0400 | [diff] [blame] | 332 | * @str: (array length=len) (element-type uint8_t): a string representing |
Evgeniy Reizner | b618e0a | 2019-12-15 16:26:50 +0200 | [diff] [blame] | 333 | * a BCP 47 language tag |
Khaled Hosny | 98e90cc | 2022-06-30 08:43:57 +0200 | [diff] [blame] | 334 | * @len: length of the @str, or -1 if it is `NULL`-terminated. |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 335 | * |
Evgeniy Reizner | b618e0a | 2019-12-15 16:26:50 +0200 | [diff] [blame] | 336 | * Converts @str representing a BCP 47 language tag to the corresponding |
Khaled Hosny | 8ab797c | 2015-12-29 17:42:16 +0400 | [diff] [blame] | 337 | * #hb_language_t. |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 338 | * |
Khaled Hosny | 04f89e8 | 2015-04-10 17:49:01 +0200 | [diff] [blame] | 339 | * Return value: (transfer none): |
Evgeniy Reizner | b618e0a | 2019-12-15 16:26:50 +0200 | [diff] [blame] | 340 | * The #hb_language_t corresponding to the BCP 47 language tag. |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 341 | * |
Behdad Esfahbod | b881142 | 2015-09-03 15:53:22 +0430 | [diff] [blame] | 342 | * Since: 0.9.2 |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 343 | **/ |
Behdad Esfahbod | 8e4bb3c | 2011-04-11 17:55:58 -0400 | [diff] [blame] | 344 | hb_language_t |
Behdad Esfahbod | 4c9fe88 | 2011-08-26 09:18:53 +0200 | [diff] [blame] | 345 | hb_language_from_string (const char *str, int len) |
Behdad Esfahbod | 8e4bb3c | 2011-04-11 17:55:58 -0400 | [diff] [blame] | 346 | { |
Behdad Esfahbod | 4c9fe88 | 2011-08-26 09:18:53 +0200 | [diff] [blame] | 347 | if (!str || !len || !*str) |
Behdad Esfahbod | 1a64f6e | 2011-05-13 22:55:32 -0400 | [diff] [blame] | 348 | return HB_LANGUAGE_INVALID; |
Behdad Esfahbod | 8e4bb3c | 2011-04-11 17:55:58 -0400 | [diff] [blame] | 349 | |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 350 | hb_language_item_t *item = nullptr; |
Behdad Esfahbod | 48360ec | 2013-09-26 16:48:42 -0400 | [diff] [blame] | 351 | if (len >= 0) |
| 352 | { |
Behdad Esfahbod | dac8602 | 2014-06-03 17:57:00 -0400 | [diff] [blame] | 353 | /* NUL-terminate it. */ |
Behdad Esfahbod | f3159ba | 2015-09-29 14:34:56 +0100 | [diff] [blame] | 354 | char strbuf[64]; |
Behdad Esfahbod | 41248cc | 2019-05-07 20:54:31 -0700 | [diff] [blame] | 355 | len = hb_min (len, (int) sizeof (strbuf) - 1); |
Behdad Esfahbod | 59c45f6 | 2022-11-22 12:54:50 -0700 | [diff] [blame] | 356 | hb_memcpy (strbuf, str, len); |
Behdad Esfahbod | 4c9fe88 | 2011-08-26 09:18:53 +0200 | [diff] [blame] | 357 | strbuf[len] = '\0'; |
Behdad Esfahbod | f3159ba | 2015-09-29 14:34:56 +0100 | [diff] [blame] | 358 | item = lang_find_or_insert (strbuf); |
Behdad Esfahbod | 4c9fe88 | 2011-08-26 09:18:53 +0200 | [diff] [blame] | 359 | } |
Behdad Esfahbod | f3159ba | 2015-09-29 14:34:56 +0100 | [diff] [blame] | 360 | else |
| 361 | item = lang_find_or_insert (str); |
Behdad Esfahbod | 8e4bb3c | 2011-04-11 17:55:58 -0400 | [diff] [blame] | 362 | |
Behdad Esfahbod | 1a64f6e | 2011-05-13 22:55:32 -0400 | [diff] [blame] | 363 | return likely (item) ? item->lang : HB_LANGUAGE_INVALID; |
Behdad Esfahbod | 8e4bb3c | 2011-04-11 17:55:58 -0400 | [diff] [blame] | 364 | } |
| 365 | |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 366 | /** |
| 367 | * hb_language_to_string: |
Nathan Willis | 4cdb12a | 2019-04-21 20:31:09 +0100 | [diff] [blame] | 368 | * @language: The #hb_language_t to convert |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 369 | * |
Nathan Willis | 4cdb12a | 2019-04-21 20:31:09 +0100 | [diff] [blame] | 370 | * Converts an #hb_language_t to a string. |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 371 | * |
Khaled Hosny | 8ab797c | 2015-12-29 17:42:16 +0400 | [diff] [blame] | 372 | * Return value: (transfer none): |
Khaled Hosny | 98e90cc | 2022-06-30 08:43:57 +0200 | [diff] [blame] | 373 | * A `NULL`-terminated string representing the @language. Must not be freed by |
Khaled Hosny | 8ab797c | 2015-12-29 17:42:16 +0400 | [diff] [blame] | 374 | * the caller. |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 375 | * |
Behdad Esfahbod | b881142 | 2015-09-03 15:53:22 +0430 | [diff] [blame] | 376 | * Since: 0.9.2 |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 377 | **/ |
Behdad Esfahbod | 8e4bb3c | 2011-04-11 17:55:58 -0400 | [diff] [blame] | 378 | const char * |
| 379 | hb_language_to_string (hb_language_t language) |
| 380 | { |
Ebrahim Byagowi | e6909ee | 2019-09-18 22:12:25 +0430 | [diff] [blame] | 381 | if (unlikely (!language)) return nullptr; |
Ebrahim Byagowi | d8af9ee | 2019-09-18 00:47:55 +0430 | [diff] [blame] | 382 | |
Behdad Esfahbod | 3cbdf70 | 2011-04-15 12:32:06 -0400 | [diff] [blame] | 383 | return language->s; |
Behdad Esfahbod | 8e4bb3c | 2011-04-11 17:55:58 -0400 | [diff] [blame] | 384 | } |
| 385 | |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 386 | /** |
| 387 | * hb_language_get_default: |
| 388 | * |
Nathan Willis | 4cdb12a | 2019-04-21 20:31:09 +0100 | [diff] [blame] | 389 | * Fetch the default language from current locale. |
Ebrahim Byagowi | 70d3654 | 2018-03-30 05:00:28 +0430 | [diff] [blame] | 390 | * |
Nathan Willis | 4cdb12a | 2019-04-21 20:31:09 +0100 | [diff] [blame] | 391 | * <note>Note that the first time this function is called, it calls |
Behdad Esfahbod | ba0f0f1 | 2018-09-30 03:49:52 -0400 | [diff] [blame] | 392 | * "setlocale (LC_CTYPE, nullptr)" to fetch current locale. The underlying |
| 393 | * setlocale function is, in many implementations, NOT threadsafe. To avoid |
| 394 | * problems, call this function once before multiple threads can call it. |
| 395 | * This function is only used from hb_buffer_guess_segment_properties() by |
Nathan Willis | 4cdb12a | 2019-04-21 20:31:09 +0100 | [diff] [blame] | 396 | * HarfBuzz itself.</note> |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 397 | * |
Nathan Willis | 4cdb12a | 2019-04-21 20:31:09 +0100 | [diff] [blame] | 398 | * Return value: (transfer none): The default language of the locale as |
| 399 | * an #hb_language_t |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 400 | * |
Behdad Esfahbod | b881142 | 2015-09-03 15:53:22 +0430 | [diff] [blame] | 401 | * Since: 0.9.2 |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 402 | **/ |
Behdad Esfahbod | 34fb552 | 2011-05-06 00:04:28 -0400 | [diff] [blame] | 403 | hb_language_t |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 404 | hb_language_get_default () |
Behdad Esfahbod | 34fb552 | 2011-05-06 00:04:28 -0400 | [diff] [blame] | 405 | { |
Behdad Esfahbod | 1f73809 | 2018-08-09 00:22:37 -0700 | [diff] [blame] | 406 | static hb_atomic_ptr_t <hb_language_t> default_language; |
Behdad Esfahbod | 34fb552 | 2011-05-06 00:04:28 -0400 | [diff] [blame] | 407 | |
Behdad Esfahbod | f6fc557 | 2018-11-05 13:23:54 -0500 | [diff] [blame] | 408 | hb_language_t language = default_language; |
Behdad Esfahbod | 1f73809 | 2018-08-09 00:22:37 -0700 | [diff] [blame] | 409 | if (unlikely (language == HB_LANGUAGE_INVALID)) |
| 410 | { |
Behdad Esfahbod | b97e4f7 | 2022-01-15 17:47:51 -0700 | [diff] [blame] | 411 | language = hb_language_from_string (hb_setlocale (LC_CTYPE, nullptr), -1); |
Behdad Esfahbod | 1f73809 | 2018-08-09 00:22:37 -0700 | [diff] [blame] | 412 | (void) default_language.cmpexch (HB_LANGUAGE_INVALID, language); |
Behdad Esfahbod | 34fb552 | 2011-05-06 00:04:28 -0400 | [diff] [blame] | 413 | } |
| 414 | |
Behdad Esfahbod | 1f73809 | 2018-08-09 00:22:37 -0700 | [diff] [blame] | 415 | return language; |
Behdad Esfahbod | 34fb552 | 2011-05-06 00:04:28 -0400 | [diff] [blame] | 416 | } |
| 417 | |
Behdad Esfahbod | f7f6d27 | 2022-07-17 22:15:42 -0600 | [diff] [blame] | 418 | /** |
| 419 | * hb_language_matches: |
| 420 | * @language: The #hb_language_t to work on |
| 421 | * @specific: Another #hb_language_t |
| 422 | * |
| 423 | * Check whether a second language tag is the same or a more |
| 424 | * specific version of the provided language tag. For example, |
| 425 | * "fa_IR.utf8" is a more specific tag for "fa" or for "fa_IR". |
| 426 | * |
| 427 | * Return value: `true` if languages match, `false` otherwise. |
| 428 | * |
Khaled Hosny | 40b21ed | 2022-07-23 16:45:32 +0200 | [diff] [blame] | 429 | * Since: 5.0.0 |
Behdad Esfahbod | f7f6d27 | 2022-07-17 22:15:42 -0600 | [diff] [blame] | 430 | **/ |
| 431 | hb_bool_t |
| 432 | hb_language_matches (hb_language_t language, |
| 433 | hb_language_t specific) |
| 434 | { |
| 435 | if (language == specific) return true; |
| 436 | if (!language || !specific) return false; |
| 437 | |
| 438 | const char *l = language->s; |
| 439 | const char *s = specific->s; |
| 440 | unsigned ll = strlen (l); |
| 441 | unsigned sl = strlen (s); |
| 442 | |
| 443 | if (ll > sl) |
| 444 | return false; |
| 445 | |
| 446 | return strncmp (l, s, ll) == 0 && |
| 447 | (s[ll] == '\0' || s[ll] == '-'); |
| 448 | } |
| 449 | |
Behdad Esfahbod | 8e4bb3c | 2011-04-11 17:55:58 -0400 | [diff] [blame] | 450 | |
Behdad Esfahbod | 00bec2c | 2011-04-15 19:16:54 -0400 | [diff] [blame] | 451 | /* hb_script_t */ |
| 452 | |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 453 | /** |
| 454 | * hb_script_from_iso15924_tag: |
Evgeniy Reizner | b618e0a | 2019-12-15 16:26:50 +0200 | [diff] [blame] | 455 | * @tag: an #hb_tag_t representing an ISO 15924 tag. |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 456 | * |
Evgeniy Reizner | b618e0a | 2019-12-15 16:26:50 +0200 | [diff] [blame] | 457 | * Converts an ISO 15924 script tag to a corresponding #hb_script_t. |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 458 | * |
Ebrahim Byagowi | 70d3654 | 2018-03-30 05:00:28 +0430 | [diff] [blame] | 459 | * Return value: |
Evgeniy Reizner | b618e0a | 2019-12-15 16:26:50 +0200 | [diff] [blame] | 460 | * An #hb_script_t corresponding to the ISO 15924 tag. |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 461 | * |
Behdad Esfahbod | b881142 | 2015-09-03 15:53:22 +0430 | [diff] [blame] | 462 | * Since: 0.9.2 |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 463 | **/ |
Behdad Esfahbod | 00bec2c | 2011-04-15 19:16:54 -0400 | [diff] [blame] | 464 | hb_script_t |
| 465 | hb_script_from_iso15924_tag (hb_tag_t tag) |
| 466 | { |
Behdad Esfahbod | f144a8e | 2011-04-20 02:54:42 -0400 | [diff] [blame] | 467 | if (unlikely (tag == HB_TAG_NONE)) |
| 468 | return HB_SCRIPT_INVALID; |
| 469 | |
Behdad Esfahbod | 00bec2c | 2011-04-15 19:16:54 -0400 | [diff] [blame] | 470 | /* Be lenient, adjust case (one capital letter followed by three small letters) */ |
Behdad Esfahbod | 7627100 | 2014-07-11 14:54:42 -0400 | [diff] [blame] | 471 | tag = (tag & 0xDFDFDFDFu) | 0x00202020u; |
Behdad Esfahbod | 00bec2c | 2011-04-15 19:16:54 -0400 | [diff] [blame] | 472 | |
Behdad Esfahbod | 62879ee | 2011-04-18 23:40:21 -0400 | [diff] [blame] | 473 | switch (tag) { |
Behdad Esfahbod | d02985e | 2011-05-02 12:35:14 -0400 | [diff] [blame] | 474 | |
| 475 | /* These graduated from the 'Q' private-area codes, but |
| 476 | * the old code is still aliased by Unicode, and the Qaai |
| 477 | * one in use by ICU. */ |
| 478 | case HB_TAG('Q','a','a','i'): return HB_SCRIPT_INHERITED; |
| 479 | case HB_TAG('Q','a','a','c'): return HB_SCRIPT_COPTIC; |
| 480 | |
Ebrahim Byagowi | f24b0b9 | 2018-04-12 13:40:45 +0430 | [diff] [blame] | 481 | /* Script variants from https://unicode.org/iso15924/ */ |
David Corbett | 3bd43bd | 2020-11-16 21:55:02 -0500 | [diff] [blame] | 482 | case HB_TAG('A','r','a','n'): return HB_SCRIPT_ARABIC; |
Behdad Esfahbod | 62879ee | 2011-04-18 23:40:21 -0400 | [diff] [blame] | 483 | case HB_TAG('C','y','r','s'): return HB_SCRIPT_CYRILLIC; |
David Corbett | 3bd43bd | 2020-11-16 21:55:02 -0500 | [diff] [blame] | 484 | case HB_TAG('G','e','o','k'): return HB_SCRIPT_GEORGIAN; |
| 485 | case HB_TAG('H','a','n','s'): return HB_SCRIPT_HAN; |
| 486 | case HB_TAG('H','a','n','t'): return HB_SCRIPT_HAN; |
| 487 | case HB_TAG('J','a','m','o'): return HB_SCRIPT_HANGUL; |
Behdad Esfahbod | 62879ee | 2011-04-18 23:40:21 -0400 | [diff] [blame] | 488 | case HB_TAG('L','a','t','f'): return HB_SCRIPT_LATIN; |
| 489 | case HB_TAG('L','a','t','g'): return HB_SCRIPT_LATIN; |
| 490 | case HB_TAG('S','y','r','e'): return HB_SCRIPT_SYRIAC; |
| 491 | case HB_TAG('S','y','r','j'): return HB_SCRIPT_SYRIAC; |
| 492 | case HB_TAG('S','y','r','n'): return HB_SCRIPT_SYRIAC; |
| 493 | } |
Behdad Esfahbod | 00bec2c | 2011-04-15 19:16:54 -0400 | [diff] [blame] | 494 | |
| 495 | /* If it looks right, just use the tag as a script */ |
Behdad Esfahbod | 7627100 | 2014-07-11 14:54:42 -0400 | [diff] [blame] | 496 | if (((uint32_t) tag & 0xE0E0E0E0u) == 0x40606060u) |
Behdad Esfahbod | 00bec2c | 2011-04-15 19:16:54 -0400 | [diff] [blame] | 497 | return (hb_script_t) tag; |
| 498 | |
| 499 | /* Otherwise, return unknown */ |
| 500 | return HB_SCRIPT_UNKNOWN; |
| 501 | } |
| 502 | |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 503 | /** |
| 504 | * hb_script_from_string: |
Khaled Hosny | 8ab797c | 2015-12-29 17:42:16 +0400 | [diff] [blame] | 505 | * @str: (array length=len) (element-type uint8_t): a string representing an |
Evgeniy Reizner | b618e0a | 2019-12-15 16:26:50 +0200 | [diff] [blame] | 506 | * ISO 15924 tag. |
Khaled Hosny | 98e90cc | 2022-06-30 08:43:57 +0200 | [diff] [blame] | 507 | * @len: length of the @str, or -1 if it is `NULL`-terminated. |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 508 | * |
Evgeniy Reizner | b618e0a | 2019-12-15 16:26:50 +0200 | [diff] [blame] | 509 | * Converts a string @str representing an ISO 15924 script tag to a |
Khaled Hosny | 8ab797c | 2015-12-29 17:42:16 +0400 | [diff] [blame] | 510 | * corresponding #hb_script_t. Shorthand for hb_tag_from_string() then |
| 511 | * hb_script_from_iso15924_tag(). |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 512 | * |
Ebrahim Byagowi | 70d3654 | 2018-03-30 05:00:28 +0430 | [diff] [blame] | 513 | * Return value: |
Evgeniy Reizner | b618e0a | 2019-12-15 16:26:50 +0200 | [diff] [blame] | 514 | * An #hb_script_t corresponding to the ISO 15924 tag. |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 515 | * |
Behdad Esfahbod | b881142 | 2015-09-03 15:53:22 +0430 | [diff] [blame] | 516 | * Since: 0.9.2 |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 517 | **/ |
Behdad Esfahbod | 00bec2c | 2011-04-15 19:16:54 -0400 | [diff] [blame] | 518 | hb_script_t |
Khaled Hosny | 8ab797c | 2015-12-29 17:42:16 +0400 | [diff] [blame] | 519 | hb_script_from_string (const char *str, int len) |
Behdad Esfahbod | 00bec2c | 2011-04-15 19:16:54 -0400 | [diff] [blame] | 520 | { |
Khaled Hosny | 8ab797c | 2015-12-29 17:42:16 +0400 | [diff] [blame] | 521 | return hb_script_from_iso15924_tag (hb_tag_from_string (str, len)); |
Behdad Esfahbod | 00bec2c | 2011-04-15 19:16:54 -0400 | [diff] [blame] | 522 | } |
| 523 | |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 524 | /** |
| 525 | * hb_script_to_iso15924_tag: |
David Corbett | 5daeff3 | 2019-04-17 09:11:44 -0400 | [diff] [blame] | 526 | * @script: an #hb_script_t to convert. |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 527 | * |
Nathan Willis | 4cdb12a | 2019-04-21 20:31:09 +0100 | [diff] [blame] | 528 | * Converts an #hb_script_t to a corresponding ISO 15924 script tag. |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 529 | * |
Khaled Hosny | 8ab797c | 2015-12-29 17:42:16 +0400 | [diff] [blame] | 530 | * Return value: |
Evgeniy Reizner | b618e0a | 2019-12-15 16:26:50 +0200 | [diff] [blame] | 531 | * An #hb_tag_t representing an ISO 15924 script tag. |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 532 | * |
Behdad Esfahbod | b881142 | 2015-09-03 15:53:22 +0430 | [diff] [blame] | 533 | * Since: 0.9.2 |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 534 | **/ |
Behdad Esfahbod | 00bec2c | 2011-04-15 19:16:54 -0400 | [diff] [blame] | 535 | hb_tag_t |
| 536 | hb_script_to_iso15924_tag (hb_script_t script) |
| 537 | { |
Behdad Esfahbod | 62879ee | 2011-04-18 23:40:21 -0400 | [diff] [blame] | 538 | return (hb_tag_t) script; |
Behdad Esfahbod | 00bec2c | 2011-04-15 19:16:54 -0400 | [diff] [blame] | 539 | } |
| 540 | |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 541 | /** |
| 542 | * hb_script_get_horizontal_direction: |
Nathan Willis | 4cdb12a | 2019-04-21 20:31:09 +0100 | [diff] [blame] | 543 | * @script: The #hb_script_t to query |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 544 | * |
Nathan Willis | 4cdb12a | 2019-04-21 20:31:09 +0100 | [diff] [blame] | 545 | * Fetches the #hb_direction_t of a script when it is |
| 546 | * set horizontally. All right-to-left scripts will return |
| 547 | * #HB_DIRECTION_RTL. All left-to-right scripts will return |
Khaled Hosny | 8586f15 | 2020-12-24 22:23:47 +0200 | [diff] [blame] | 548 | * #HB_DIRECTION_LTR. Scripts that can be written either |
| 549 | * horizontally or vertically will return #HB_DIRECTION_INVALID. |
| 550 | * Unknown scripts will return #HB_DIRECTION_LTR. |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 551 | * |
Nathan Willis | 4cdb12a | 2019-04-21 20:31:09 +0100 | [diff] [blame] | 552 | * Return value: The horizontal #hb_direction_t of @script |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 553 | * |
Behdad Esfahbod | b881142 | 2015-09-03 15:53:22 +0430 | [diff] [blame] | 554 | * Since: 0.9.2 |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 555 | **/ |
Behdad Esfahbod | 00bec2c | 2011-04-15 19:16:54 -0400 | [diff] [blame] | 556 | hb_direction_t |
| 557 | hb_script_get_horizontal_direction (hb_script_t script) |
| 558 | { |
Ebrahim Byagowi | f24b0b9 | 2018-04-12 13:40:45 +0430 | [diff] [blame] | 559 | /* https://docs.google.com/spreadsheets/d/1Y90M0Ie3MUJ6UVCRDOypOtijlMDLNNyyLk36T6iMu0o */ |
Behdad Esfahbod | 62879ee | 2011-04-18 23:40:21 -0400 | [diff] [blame] | 560 | switch ((hb_tag_t) script) |
| 561 | { |
Behdad Esfahbod | fa2673c | 2012-03-07 15:52:02 -0500 | [diff] [blame] | 562 | /* Unicode-1.1 additions */ |
Behdad Esfahbod | 62879ee | 2011-04-18 23:40:21 -0400 | [diff] [blame] | 563 | case HB_SCRIPT_ARABIC: |
| 564 | case HB_SCRIPT_HEBREW: |
Behdad Esfahbod | fa2673c | 2012-03-07 15:52:02 -0500 | [diff] [blame] | 565 | |
| 566 | /* Unicode-3.0 additions */ |
Behdad Esfahbod | 62879ee | 2011-04-18 23:40:21 -0400 | [diff] [blame] | 567 | case HB_SCRIPT_SYRIAC: |
| 568 | case HB_SCRIPT_THAANA: |
Behdad Esfahbod | 00bec2c | 2011-04-15 19:16:54 -0400 | [diff] [blame] | 569 | |
Behdad Esfahbod | 62879ee | 2011-04-18 23:40:21 -0400 | [diff] [blame] | 570 | /* Unicode-4.0 additions */ |
| 571 | case HB_SCRIPT_CYPRIOT: |
| 572 | |
Behdad Esfahbod | 50e810c | 2012-03-07 12:49:08 -0500 | [diff] [blame] | 573 | /* Unicode-4.1 additions */ |
| 574 | case HB_SCRIPT_KHAROSHTHI: |
| 575 | |
Behdad Esfahbod | 62879ee | 2011-04-18 23:40:21 -0400 | [diff] [blame] | 576 | /* Unicode-5.0 additions */ |
| 577 | case HB_SCRIPT_PHOENICIAN: |
| 578 | case HB_SCRIPT_NKO: |
| 579 | |
Behdad Esfahbod | 50e810c | 2012-03-07 12:49:08 -0500 | [diff] [blame] | 580 | /* Unicode-5.1 additions */ |
| 581 | case HB_SCRIPT_LYDIAN: |
| 582 | |
Behdad Esfahbod | 62879ee | 2011-04-18 23:40:21 -0400 | [diff] [blame] | 583 | /* Unicode-5.2 additions */ |
| 584 | case HB_SCRIPT_AVESTAN: |
| 585 | case HB_SCRIPT_IMPERIAL_ARAMAIC: |
| 586 | case HB_SCRIPT_INSCRIPTIONAL_PAHLAVI: |
| 587 | case HB_SCRIPT_INSCRIPTIONAL_PARTHIAN: |
| 588 | case HB_SCRIPT_OLD_SOUTH_ARABIAN: |
| 589 | case HB_SCRIPT_OLD_TURKIC: |
| 590 | case HB_SCRIPT_SAMARITAN: |
| 591 | |
| 592 | /* Unicode-6.0 additions */ |
| 593 | case HB_SCRIPT_MANDAIC: |
| 594 | |
Behdad Esfahbod | fa2673c | 2012-03-07 15:52:02 -0500 | [diff] [blame] | 595 | /* Unicode-6.1 additions */ |
| 596 | case HB_SCRIPT_MEROITIC_CURSIVE: |
| 597 | case HB_SCRIPT_MEROITIC_HIEROGLYPHS: |
| 598 | |
Behdad Esfahbod | a4a7899 | 2014-04-28 15:06:42 -0700 | [diff] [blame] | 599 | /* Unicode-7.0 additions */ |
| 600 | case HB_SCRIPT_MANICHAEAN: |
| 601 | case HB_SCRIPT_MENDE_KIKAKUI: |
| 602 | case HB_SCRIPT_NABATAEAN: |
| 603 | case HB_SCRIPT_OLD_NORTH_ARABIAN: |
| 604 | case HB_SCRIPT_PALMYRENE: |
| 605 | case HB_SCRIPT_PSALTER_PAHLAVI: |
| 606 | |
Behdad Esfahbod | 64a2726 | 2015-07-15 01:36:39 +0100 | [diff] [blame] | 607 | /* Unicode-8.0 additions */ |
Cosimo Lupo | c8f2a4f | 2018-01-18 22:49:40 +0100 | [diff] [blame] | 608 | case HB_SCRIPT_HATRAN: |
Behdad Esfahbod | 64a2726 | 2015-07-15 01:36:39 +0100 | [diff] [blame] | 609 | |
Behdad Esfahbod | 691086f | 2016-05-06 12:08:18 +0100 | [diff] [blame] | 610 | /* Unicode-9.0 additions */ |
| 611 | case HB_SCRIPT_ADLAM: |
| 612 | |
Behdad Esfahbod | 060e6b4 | 2018-06-05 17:31:46 -0700 | [diff] [blame] | 613 | /* Unicode-11.0 additions */ |
| 614 | case HB_SCRIPT_HANIFI_ROHINGYA: |
| 615 | case HB_SCRIPT_OLD_SOGDIAN: |
| 616 | case HB_SCRIPT_SOGDIAN: |
| 617 | |
David Corbett | 665483c | 2020-04-01 17:28:12 -0400 | [diff] [blame] | 618 | /* Unicode-12.0 additions */ |
| 619 | case HB_SCRIPT_ELYMAIC: |
| 620 | |
David Corbett | fd748fa | 2020-03-15 15:59:31 -0400 | [diff] [blame] | 621 | /* Unicode-13.0 additions */ |
| 622 | case HB_SCRIPT_CHORASMIAN: |
| 623 | case HB_SCRIPT_YEZIDI: |
| 624 | |
David Corbett | 7b05eec | 2021-09-14 17:51:11 -0400 | [diff] [blame] | 625 | /* Unicode-14.0 additions */ |
| 626 | case HB_SCRIPT_OLD_UYGHUR: |
| 627 | |
Behdad Esfahbod | 62879ee | 2011-04-18 23:40:21 -0400 | [diff] [blame] | 628 | return HB_DIRECTION_RTL; |
Behdad Esfahbod | f673cfb | 2018-05-07 13:58:32 -0700 | [diff] [blame] | 629 | |
| 630 | |
| 631 | /* https://github.com/harfbuzz/harfbuzz/issues/1000 */ |
David Corbett | d8d1e7d | 2018-09-17 11:09:51 -0400 | [diff] [blame] | 632 | case HB_SCRIPT_OLD_HUNGARIAN: |
Behdad Esfahbod | f673cfb | 2018-05-07 13:58:32 -0700 | [diff] [blame] | 633 | case HB_SCRIPT_OLD_ITALIC: |
David Corbett | 46d8f0d | 2018-07-06 15:47:03 -0400 | [diff] [blame] | 634 | case HB_SCRIPT_RUNIC: |
Simon Cozens | 2d86346 | 2023-03-10 14:32:39 +0000 | [diff] [blame] | 635 | case HB_SCRIPT_TIFINAGH: |
Behdad Esfahbod | f673cfb | 2018-05-07 13:58:32 -0700 | [diff] [blame] | 636 | |
| 637 | return HB_DIRECTION_INVALID; |
Behdad Esfahbod | 62879ee | 2011-04-18 23:40:21 -0400 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | return HB_DIRECTION_LTR; |
Behdad Esfahbod | 00bec2c | 2011-04-15 19:16:54 -0400 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | |
Behdad Esfahbod | c78f448 | 2011-05-05 21:31:04 -0400 | [diff] [blame] | 644 | /* hb_version */ |
| 645 | |
Behdad Esfahbod | 00cf4e5 | 2018-10-27 04:07:33 -0700 | [diff] [blame] | 646 | |
| 647 | /** |
| 648 | * SECTION:hb-version |
Behdad Esfahbod | cf5fa57 | 2018-10-27 04:50:38 -0700 | [diff] [blame] | 649 | * @title: hb-version |
Behdad Esfahbod | 00cf4e5 | 2018-10-27 04:07:33 -0700 | [diff] [blame] | 650 | * @short_description: Information about the version of HarfBuzz in use |
| 651 | * @include: hb.h |
| 652 | * |
| 653 | * These functions and macros allow accessing version of the HarfBuzz |
| 654 | * library used at compile- as well as run-time, and to direct code |
| 655 | * conditionally based on those versions, again, at compile- or run-time. |
| 656 | **/ |
| 657 | |
| 658 | |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 659 | /** |
| 660 | * hb_version: |
Nathan Willis | 4cdb12a | 2019-04-21 20:31:09 +0100 | [diff] [blame] | 661 | * @major: (out): Library major version component |
| 662 | * @minor: (out): Library minor version component |
| 663 | * @micro: (out): Library micro version component |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 664 | * |
| 665 | * Returns library version as three integer components. |
| 666 | * |
Behdad Esfahbod | b881142 | 2015-09-03 15:53:22 +0430 | [diff] [blame] | 667 | * Since: 0.9.2 |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 668 | **/ |
Behdad Esfahbod | c78f448 | 2011-05-05 21:31:04 -0400 | [diff] [blame] | 669 | void |
| 670 | hb_version (unsigned int *major, |
| 671 | unsigned int *minor, |
| 672 | unsigned int *micro) |
| 673 | { |
| 674 | *major = HB_VERSION_MAJOR; |
| 675 | *minor = HB_VERSION_MINOR; |
| 676 | *micro = HB_VERSION_MICRO; |
| 677 | } |
| 678 | |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 679 | /** |
| 680 | * hb_version_string: |
| 681 | * |
| 682 | * Returns library version as a string with three components. |
| 683 | * |
Nathan Willis | 4cdb12a | 2019-04-21 20:31:09 +0100 | [diff] [blame] | 684 | * Return value: Library version string |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 685 | * |
Behdad Esfahbod | b881142 | 2015-09-03 15:53:22 +0430 | [diff] [blame] | 686 | * Since: 0.9.2 |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 687 | **/ |
Behdad Esfahbod | c78f448 | 2011-05-05 21:31:04 -0400 | [diff] [blame] | 688 | const char * |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 689 | hb_version_string () |
Behdad Esfahbod | c78f448 | 2011-05-05 21:31:04 -0400 | [diff] [blame] | 690 | { |
| 691 | return HB_VERSION_STRING; |
| 692 | } |
| 693 | |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 694 | /** |
Behdad Esfahbod | 2b051c6 | 2014-06-20 14:09:57 -0400 | [diff] [blame] | 695 | * hb_version_atleast: |
Nathan Willis | 4cdb12a | 2019-04-21 20:31:09 +0100 | [diff] [blame] | 696 | * @major: Library major version component |
| 697 | * @minor: Library minor version component |
| 698 | * @micro: Library micro version component |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 699 | * |
Nathan Willis | 4cdb12a | 2019-04-21 20:31:09 +0100 | [diff] [blame] | 700 | * Tests the library version against a minimum value, |
| 701 | * as three integer components. |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 702 | * |
Khaled Hosny | 98e90cc | 2022-06-30 08:43:57 +0200 | [diff] [blame] | 703 | * Return value: `true` if the library is equal to or greater than |
| 704 | * the test value, `false` otherwise |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 705 | * |
Sascha Brawer | 01c3a88 | 2015-06-01 13:22:01 +0200 | [diff] [blame] | 706 | * Since: 0.9.30 |
Behdad Esfahbod | 70303cf | 2013-09-06 17:35:57 -0400 | [diff] [blame] | 707 | **/ |
Behdad Esfahbod | c78f448 | 2011-05-05 21:31:04 -0400 | [diff] [blame] | 708 | hb_bool_t |
Behdad Esfahbod | 2b051c6 | 2014-06-20 14:09:57 -0400 | [diff] [blame] | 709 | hb_version_atleast (unsigned int major, |
| 710 | unsigned int minor, |
| 711 | unsigned int micro) |
Behdad Esfahbod | c78f448 | 2011-05-05 21:31:04 -0400 | [diff] [blame] | 712 | { |
Behdad Esfahbod | 2b051c6 | 2014-06-20 14:09:57 -0400 | [diff] [blame] | 713 | return HB_VERSION_ATLEAST (major, minor, micro); |
Behdad Esfahbod | c78f448 | 2011-05-05 21:31:04 -0400 | [diff] [blame] | 714 | } |
Behdad Esfahbod | 7236410 | 2017-01-20 20:16:53 -0800 | [diff] [blame] | 715 | |
| 716 | |
Behdad Esfahbod | b3c0714 | 2017-01-20 20:30:03 -0800 | [diff] [blame] | 717 | |
Behdad Esfahbod | bb1e192 | 2017-01-21 17:41:37 -0800 | [diff] [blame] | 718 | /* hb_feature_t and hb_variation_t */ |
Behdad Esfahbod | 7236410 | 2017-01-20 20:16:53 -0800 | [diff] [blame] | 719 | |
| 720 | static bool |
| 721 | parse_space (const char **pp, const char *end) |
| 722 | { |
| 723 | while (*pp < end && ISSPACE (**pp)) |
| 724 | (*pp)++; |
| 725 | return true; |
| 726 | } |
| 727 | |
| 728 | static bool |
| 729 | parse_char (const char **pp, const char *end, char c) |
| 730 | { |
| 731 | parse_space (pp, end); |
| 732 | |
| 733 | if (*pp == end || **pp != c) |
| 734 | return false; |
| 735 | |
| 736 | (*pp)++; |
| 737 | return true; |
| 738 | } |
| 739 | |
| 740 | static bool |
| 741 | parse_uint (const char **pp, const char *end, unsigned int *pv) |
| 742 | { |
Ebrahim Byagowi | b5e6805 | 2019-09-03 15:23:40 +0430 | [diff] [blame] | 743 | /* Intentionally use hb_parse_int inside instead of hb_parse_uint, |
| 744 | * such that -1 turns into "big number"... */ |
Ebrahim Byagowi | 43372fb | 2019-09-03 01:02:40 +0430 | [diff] [blame] | 745 | int v; |
Ebrahim Byagowi | b5e6805 | 2019-09-03 15:23:40 +0430 | [diff] [blame] | 746 | if (unlikely (!hb_parse_int (pp, end, &v))) return false; |
Behdad Esfahbod | 7236410 | 2017-01-20 20:16:53 -0800 | [diff] [blame] | 747 | |
| 748 | *pv = v; |
Behdad Esfahbod | 7236410 | 2017-01-20 20:16:53 -0800 | [diff] [blame] | 749 | return true; |
| 750 | } |
| 751 | |
| 752 | static bool |
Ebrahim Byagowi | 3b0e47c | 2017-06-19 14:47:09 +0430 | [diff] [blame] | 753 | parse_uint32 (const char **pp, const char *end, uint32_t *pv) |
| 754 | { |
Ebrahim Byagowi | b5e6805 | 2019-09-03 15:23:40 +0430 | [diff] [blame] | 755 | /* Intentionally use hb_parse_int inside instead of hb_parse_uint, |
| 756 | * such that -1 turns into "big number"... */ |
Ebrahim Byagowi | 43372fb | 2019-09-03 01:02:40 +0430 | [diff] [blame] | 757 | int v; |
Ebrahim Byagowi | b5e6805 | 2019-09-03 15:23:40 +0430 | [diff] [blame] | 758 | if (unlikely (!hb_parse_int (pp, end, &v))) return false; |
Ebrahim Byagowi | 3b0e47c | 2017-06-19 14:47:09 +0430 | [diff] [blame] | 759 | |
| 760 | *pv = v; |
Behdad Esfahbod | b3c0714 | 2017-01-20 20:30:03 -0800 | [diff] [blame] | 761 | return true; |
| 762 | } |
| 763 | |
| 764 | static bool |
Ebrahim Byagowi | 3b0e47c | 2017-06-19 14:47:09 +0430 | [diff] [blame] | 765 | parse_bool (const char **pp, const char *end, uint32_t *pv) |
Behdad Esfahbod | 7236410 | 2017-01-20 20:16:53 -0800 | [diff] [blame] | 766 | { |
| 767 | parse_space (pp, end); |
| 768 | |
| 769 | const char *p = *pp; |
| 770 | while (*pp < end && ISALPHA(**pp)) |
| 771 | (*pp)++; |
| 772 | |
| 773 | /* CSS allows on/off as aliases 1/0. */ |
David Corbett | 45adc18 | 2019-02-18 22:30:40 -0500 | [diff] [blame] | 774 | if (*pp - p == 2 |
| 775 | && TOLOWER (p[0]) == 'o' |
| 776 | && TOLOWER (p[1]) == 'n') |
Behdad Esfahbod | 7236410 | 2017-01-20 20:16:53 -0800 | [diff] [blame] | 777 | *pv = 1; |
David Corbett | 45adc18 | 2019-02-18 22:30:40 -0500 | [diff] [blame] | 778 | else if (*pp - p == 3 |
| 779 | && TOLOWER (p[0]) == 'o' |
| 780 | && TOLOWER (p[1]) == 'f' |
| 781 | && TOLOWER (p[2]) == 'f') |
Behdad Esfahbod | 7236410 | 2017-01-20 20:16:53 -0800 | [diff] [blame] | 782 | *pv = 0; |
| 783 | else |
| 784 | return false; |
| 785 | |
| 786 | return true; |
| 787 | } |
| 788 | |
Behdad Esfahbod | b3c0714 | 2017-01-20 20:30:03 -0800 | [diff] [blame] | 789 | /* hb_feature_t */ |
| 790 | |
Behdad Esfahbod | 7236410 | 2017-01-20 20:16:53 -0800 | [diff] [blame] | 791 | static bool |
| 792 | parse_feature_value_prefix (const char **pp, const char *end, hb_feature_t *feature) |
| 793 | { |
| 794 | if (parse_char (pp, end, '-')) |
| 795 | feature->value = 0; |
| 796 | else { |
| 797 | parse_char (pp, end, '+'); |
| 798 | feature->value = 1; |
| 799 | } |
| 800 | |
| 801 | return true; |
| 802 | } |
| 803 | |
| 804 | static bool |
Behdad Esfahbod | b3c0714 | 2017-01-20 20:30:03 -0800 | [diff] [blame] | 805 | parse_tag (const char **pp, const char *end, hb_tag_t *tag) |
Behdad Esfahbod | 7236410 | 2017-01-20 20:16:53 -0800 | [diff] [blame] | 806 | { |
| 807 | parse_space (pp, end); |
| 808 | |
| 809 | char quote = 0; |
| 810 | |
| 811 | if (*pp < end && (**pp == '\'' || **pp == '"')) |
| 812 | { |
| 813 | quote = **pp; |
| 814 | (*pp)++; |
| 815 | } |
| 816 | |
| 817 | const char *p = *pp; |
Behdad Esfahbod | e8eb1dc | 2023-08-09 15:35:59 -0600 | [diff] [blame] | 818 | while (*pp < end && (**pp != ' ' && **pp != '=' && **pp != '[')) |
Behdad Esfahbod | 7236410 | 2017-01-20 20:16:53 -0800 | [diff] [blame] | 819 | (*pp)++; |
| 820 | |
| 821 | if (p == *pp || *pp - p > 4) |
| 822 | return false; |
| 823 | |
Behdad Esfahbod | b3c0714 | 2017-01-20 20:30:03 -0800 | [diff] [blame] | 824 | *tag = hb_tag_from_string (p, *pp - p); |
Behdad Esfahbod | 7236410 | 2017-01-20 20:16:53 -0800 | [diff] [blame] | 825 | |
| 826 | if (quote) |
| 827 | { |
| 828 | /* CSS expects exactly four bytes. And we only allow quotations for |
| 829 | * CSS compatibility. So, enforce the length. */ |
| 830 | if (*pp - p != 4) |
| 831 | return false; |
| 832 | if (*pp == end || **pp != quote) |
| 833 | return false; |
| 834 | (*pp)++; |
| 835 | } |
| 836 | |
| 837 | return true; |
| 838 | } |
| 839 | |
| 840 | static bool |
| 841 | parse_feature_indices (const char **pp, const char *end, hb_feature_t *feature) |
| 842 | { |
| 843 | parse_space (pp, end); |
| 844 | |
| 845 | bool has_start; |
| 846 | |
Behdad Esfahbod | becd84a | 2018-09-11 01:26:18 +0200 | [diff] [blame] | 847 | feature->start = HB_FEATURE_GLOBAL_START; |
| 848 | feature->end = HB_FEATURE_GLOBAL_END; |
Behdad Esfahbod | 7236410 | 2017-01-20 20:16:53 -0800 | [diff] [blame] | 849 | |
| 850 | if (!parse_char (pp, end, '[')) |
| 851 | return true; |
| 852 | |
| 853 | has_start = parse_uint (pp, end, &feature->start); |
| 854 | |
Behdad Esfahbod | 8869436 | 2018-10-23 03:07:48 -0700 | [diff] [blame] | 855 | if (parse_char (pp, end, ':') || parse_char (pp, end, ';')) { |
Behdad Esfahbod | 7236410 | 2017-01-20 20:16:53 -0800 | [diff] [blame] | 856 | parse_uint (pp, end, &feature->end); |
| 857 | } else { |
| 858 | if (has_start) |
| 859 | feature->end = feature->start + 1; |
| 860 | } |
| 861 | |
| 862 | return parse_char (pp, end, ']'); |
| 863 | } |
| 864 | |
| 865 | static bool |
| 866 | parse_feature_value_postfix (const char **pp, const char *end, hb_feature_t *feature) |
| 867 | { |
| 868 | bool had_equal = parse_char (pp, end, '='); |
Ebrahim Byagowi | 3b0e47c | 2017-06-19 14:47:09 +0430 | [diff] [blame] | 869 | bool had_value = parse_uint32 (pp, end, &feature->value) || |
Ebrahim Byagowi | a0b4ac4 | 2019-08-24 17:57:14 +0430 | [diff] [blame] | 870 | parse_bool (pp, end, &feature->value); |
Behdad Esfahbod | 7236410 | 2017-01-20 20:16:53 -0800 | [diff] [blame] | 871 | /* CSS doesn't use equal-sign between tag and value. |
| 872 | * If there was an equal-sign, then there *must* be a value. |
Bruce Mitchener | 90218fa | 2018-01-31 20:44:45 +0700 | [diff] [blame] | 873 | * A value without an equal-sign is ok, but not required. */ |
Behdad Esfahbod | 7236410 | 2017-01-20 20:16:53 -0800 | [diff] [blame] | 874 | return !had_equal || had_value; |
| 875 | } |
| 876 | |
Behdad Esfahbod | 7236410 | 2017-01-20 20:16:53 -0800 | [diff] [blame] | 877 | static bool |
| 878 | parse_one_feature (const char **pp, const char *end, hb_feature_t *feature) |
| 879 | { |
| 880 | return parse_feature_value_prefix (pp, end, feature) && |
Behdad Esfahbod | b3c0714 | 2017-01-20 20:30:03 -0800 | [diff] [blame] | 881 | parse_tag (pp, end, &feature->tag) && |
Behdad Esfahbod | 7236410 | 2017-01-20 20:16:53 -0800 | [diff] [blame] | 882 | parse_feature_indices (pp, end, feature) && |
| 883 | parse_feature_value_postfix (pp, end, feature) && |
| 884 | parse_space (pp, end) && |
| 885 | *pp == end; |
| 886 | } |
| 887 | |
| 888 | /** |
| 889 | * hb_feature_from_string: |
| 890 | * @str: (array length=len) (element-type uint8_t): a string to parse |
Khaled Hosny | 98e90cc | 2022-06-30 08:43:57 +0200 | [diff] [blame] | 891 | * @len: length of @str, or -1 if string is `NULL` terminated |
Behdad Esfahbod | 7236410 | 2017-01-20 20:16:53 -0800 | [diff] [blame] | 892 | * @feature: (out): the #hb_feature_t to initialize with the parsed values |
| 893 | * |
| 894 | * Parses a string into a #hb_feature_t. |
| 895 | * |
Khaled Hosny | 6bd4c08 | 2019-02-19 02:23:58 +0200 | [diff] [blame] | 896 | * The format for specifying feature strings follows. All valid CSS |
David Corbett | 45adc18 | 2019-02-18 22:30:40 -0500 | [diff] [blame] | 897 | * font-feature-settings values other than 'normal' and the global values are |
| 898 | * also accepted, though not documented below. CSS string escapes are not |
| 899 | * supported. |
Khaled Hosny | 6bd4c08 | 2019-02-19 02:23:58 +0200 | [diff] [blame] | 900 | * |
| 901 | * The range indices refer to the positions between Unicode characters. The |
| 902 | * position before the first character is always 0. |
| 903 | * |
| 904 | * The format is Python-esque. Here is how it all works: |
| 905 | * |
| 906 | * <informaltable pgwide='1' align='left' frame='none'> |
| 907 | * <tgroup cols='5'> |
| 908 | * <thead> |
| 909 | * <row><entry>Syntax</entry> <entry>Value</entry> <entry>Start</entry> <entry>End</entry></row> |
| 910 | * </thead> |
| 911 | * <tbody> |
| 912 | * <row><entry>Setting value:</entry></row> |
| 913 | * <row><entry>kern</entry> <entry>1</entry> <entry>0</entry> <entry>∞</entry> <entry>Turn feature on</entry></row> |
| 914 | * <row><entry>+kern</entry> <entry>1</entry> <entry>0</entry> <entry>∞</entry> <entry>Turn feature on</entry></row> |
| 915 | * <row><entry>-kern</entry> <entry>0</entry> <entry>0</entry> <entry>∞</entry> <entry>Turn feature off</entry></row> |
| 916 | * <row><entry>kern=0</entry> <entry>0</entry> <entry>0</entry> <entry>∞</entry> <entry>Turn feature off</entry></row> |
| 917 | * <row><entry>kern=1</entry> <entry>1</entry> <entry>0</entry> <entry>∞</entry> <entry>Turn feature on</entry></row> |
| 918 | * <row><entry>aalt=2</entry> <entry>2</entry> <entry>0</entry> <entry>∞</entry> <entry>Choose 2nd alternate</entry></row> |
| 919 | * <row><entry>Setting index:</entry></row> |
| 920 | * <row><entry>kern[]</entry> <entry>1</entry> <entry>0</entry> <entry>∞</entry> <entry>Turn feature on</entry></row> |
| 921 | * <row><entry>kern[:]</entry> <entry>1</entry> <entry>0</entry> <entry>∞</entry> <entry>Turn feature on</entry></row> |
| 922 | * <row><entry>kern[5:]</entry> <entry>1</entry> <entry>5</entry> <entry>∞</entry> <entry>Turn feature on, partial</entry></row> |
| 923 | * <row><entry>kern[:5]</entry> <entry>1</entry> <entry>0</entry> <entry>5</entry> <entry>Turn feature on, partial</entry></row> |
| 924 | * <row><entry>kern[3:5]</entry> <entry>1</entry> <entry>3</entry> <entry>5</entry> <entry>Turn feature on, range</entry></row> |
| 925 | * <row><entry>kern[3]</entry> <entry>1</entry> <entry>3</entry> <entry>3+1</entry> <entry>Turn feature on, single char</entry></row> |
| 926 | * <row><entry>Mixing it all:</entry></row> |
| 927 | * <row><entry>aalt[3:5]=2</entry> <entry>2</entry> <entry>3</entry> <entry>5</entry> <entry>Turn 2nd alternate on for range</entry></row> |
| 928 | * </tbody> |
| 929 | * </tgroup> |
| 930 | * </informaltable> |
Behdad Esfahbod | 7236410 | 2017-01-20 20:16:53 -0800 | [diff] [blame] | 931 | * |
| 932 | * Return value: |
Khaled Hosny | 98e90cc | 2022-06-30 08:43:57 +0200 | [diff] [blame] | 933 | * `true` if @str is successfully parsed, `false` otherwise |
Behdad Esfahbod | 7236410 | 2017-01-20 20:16:53 -0800 | [diff] [blame] | 934 | * |
| 935 | * Since: 0.9.5 |
| 936 | **/ |
| 937 | hb_bool_t |
| 938 | hb_feature_from_string (const char *str, int len, |
| 939 | hb_feature_t *feature) |
| 940 | { |
| 941 | hb_feature_t feat; |
| 942 | |
| 943 | if (len < 0) |
| 944 | len = strlen (str); |
| 945 | |
| 946 | if (likely (parse_one_feature (&str, str + len, &feat))) |
| 947 | { |
| 948 | if (feature) |
| 949 | *feature = feat; |
| 950 | return true; |
| 951 | } |
| 952 | |
| 953 | if (feature) |
Behdad Esfahbod | ac0efaf | 2022-11-22 12:50:36 -0700 | [diff] [blame] | 954 | hb_memset (feature, 0, sizeof (*feature)); |
Behdad Esfahbod | 7236410 | 2017-01-20 20:16:53 -0800 | [diff] [blame] | 955 | return false; |
| 956 | } |
| 957 | |
| 958 | /** |
| 959 | * hb_feature_to_string: |
| 960 | * @feature: an #hb_feature_t to convert |
| 961 | * @buf: (array length=size) (out): output string |
| 962 | * @size: the allocated size of @buf |
| 963 | * |
Khaled Hosny | 98e90cc | 2022-06-30 08:43:57 +0200 | [diff] [blame] | 964 | * Converts a #hb_feature_t into a `NULL`-terminated string in the format |
Behdad Esfahbod | 7236410 | 2017-01-20 20:16:53 -0800 | [diff] [blame] | 965 | * understood by hb_feature_from_string(). The client in responsible for |
| 966 | * allocating big enough size for @buf, 128 bytes is more than enough. |
| 967 | * |
| 968 | * Since: 0.9.5 |
| 969 | **/ |
| 970 | void |
| 971 | hb_feature_to_string (hb_feature_t *feature, |
| 972 | char *buf, unsigned int size) |
| 973 | { |
| 974 | if (unlikely (!size)) return; |
| 975 | |
| 976 | char s[128]; |
| 977 | unsigned int len = 0; |
| 978 | if (feature->value == 0) |
| 979 | s[len++] = '-'; |
| 980 | hb_tag_to_string (feature->tag, s + len); |
| 981 | len += 4; |
| 982 | while (len && s[len - 1] == ' ') |
| 983 | len--; |
Evgeniy Reizner | b79ceac | 2019-12-15 16:50:01 +0200 | [diff] [blame] | 984 | if (feature->start != HB_FEATURE_GLOBAL_START || feature->end != HB_FEATURE_GLOBAL_END) |
Behdad Esfahbod | 7236410 | 2017-01-20 20:16:53 -0800 | [diff] [blame] | 985 | { |
| 986 | s[len++] = '['; |
| 987 | if (feature->start) |
Behdad Esfahbod | 41248cc | 2019-05-07 20:54:31 -0700 | [diff] [blame] | 988 | len += hb_max (0, snprintf (s + len, ARRAY_LENGTH (s) - len, "%u", feature->start)); |
Behdad Esfahbod | 7236410 | 2017-01-20 20:16:53 -0800 | [diff] [blame] | 989 | if (feature->end != feature->start + 1) { |
| 990 | s[len++] = ':'; |
Evgeniy Reizner | b79ceac | 2019-12-15 16:50:01 +0200 | [diff] [blame] | 991 | if (feature->end != HB_FEATURE_GLOBAL_END) |
Behdad Esfahbod | 41248cc | 2019-05-07 20:54:31 -0700 | [diff] [blame] | 992 | len += hb_max (0, snprintf (s + len, ARRAY_LENGTH (s) - len, "%u", feature->end)); |
Behdad Esfahbod | 7236410 | 2017-01-20 20:16:53 -0800 | [diff] [blame] | 993 | } |
| 994 | s[len++] = ']'; |
| 995 | } |
| 996 | if (feature->value > 1) |
| 997 | { |
| 998 | s[len++] = '='; |
Behdad Esfahbod | 41248cc | 2019-05-07 20:54:31 -0700 | [diff] [blame] | 999 | len += hb_max (0, snprintf (s + len, ARRAY_LENGTH (s) - len, "%u", feature->value)); |
Behdad Esfahbod | 7236410 | 2017-01-20 20:16:53 -0800 | [diff] [blame] | 1000 | } |
| 1001 | assert (len < ARRAY_LENGTH (s)); |
Behdad Esfahbod | 41248cc | 2019-05-07 20:54:31 -0700 | [diff] [blame] | 1002 | len = hb_min (len, size - 1); |
Behdad Esfahbod | 59c45f6 | 2022-11-22 12:54:50 -0700 | [diff] [blame] | 1003 | hb_memcpy (buf, s, len); |
Behdad Esfahbod | 7236410 | 2017-01-20 20:16:53 -0800 | [diff] [blame] | 1004 | buf[len] = '\0'; |
| 1005 | } |
Behdad Esfahbod | b3c0714 | 2017-01-20 20:30:03 -0800 | [diff] [blame] | 1006 | |
Behdad Esfahbod | bb1e192 | 2017-01-21 17:41:37 -0800 | [diff] [blame] | 1007 | /* hb_variation_t */ |
Behdad Esfahbod | b3c0714 | 2017-01-20 20:30:03 -0800 | [diff] [blame] | 1008 | |
| 1009 | static bool |
Behdad Esfahbod | bb1e192 | 2017-01-21 17:41:37 -0800 | [diff] [blame] | 1010 | 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] | 1011 | { |
| 1012 | parse_char (pp, end, '='); /* Optional. */ |
Ebrahim Byagowi | 57f88e1 | 2019-09-04 01:20:50 +0430 | [diff] [blame] | 1013 | double v; |
| 1014 | if (unlikely (!hb_parse_double (pp, end, &v))) return false; |
| 1015 | |
| 1016 | variation->value = v; |
| 1017 | return true; |
Behdad Esfahbod | b3c0714 | 2017-01-20 20:30:03 -0800 | [diff] [blame] | 1018 | } |
| 1019 | |
| 1020 | static bool |
Behdad Esfahbod | bb1e192 | 2017-01-21 17:41:37 -0800 | [diff] [blame] | 1021 | 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] | 1022 | { |
Behdad Esfahbod | bb1e192 | 2017-01-21 17:41:37 -0800 | [diff] [blame] | 1023 | return parse_tag (pp, end, &variation->tag) && |
| 1024 | parse_variation_value (pp, end, variation) && |
Behdad Esfahbod | b3c0714 | 2017-01-20 20:30:03 -0800 | [diff] [blame] | 1025 | parse_space (pp, end) && |
| 1026 | *pp == end; |
| 1027 | } |
| 1028 | |
Behdad Esfahbod | d2f249e | 2017-01-22 17:42:33 -0800 | [diff] [blame] | 1029 | /** |
| 1030 | * hb_variation_from_string: |
Khaled Hosny | 9b602e8 | 2020-12-30 23:28:54 +0200 | [diff] [blame] | 1031 | * @str: (array length=len) (element-type uint8_t): a string to parse |
Khaled Hosny | 98e90cc | 2022-06-30 08:43:57 +0200 | [diff] [blame] | 1032 | * @len: length of @str, or -1 if string is `NULL` terminated |
Khaled Hosny | 9b602e8 | 2020-12-30 23:28:54 +0200 | [diff] [blame] | 1033 | * @variation: (out): the #hb_variation_t to initialize with the parsed values |
| 1034 | * |
| 1035 | * Parses a string into a #hb_variation_t. |
| 1036 | * |
| 1037 | * The format for specifying variation settings follows. All valid CSS |
| 1038 | * font-variation-settings values other than 'normal' and 'inherited' are also |
| 1039 | * accepted, though, not documented below. |
| 1040 | * |
| 1041 | * The format is a tag, optionally followed by an equals sign, followed by a |
| 1042 | * number. For example `wght=500`, or `slnt=-7.5`. |
| 1043 | * |
| 1044 | * Return value: |
Khaled Hosny | 98e90cc | 2022-06-30 08:43:57 +0200 | [diff] [blame] | 1045 | * `true` if @str is successfully parsed, `false` otherwise |
Behdad Esfahbod | d2f249e | 2017-01-22 17:42:33 -0800 | [diff] [blame] | 1046 | * |
| 1047 | * Since: 1.4.2 |
| 1048 | */ |
Behdad Esfahbod | b3c0714 | 2017-01-20 20:30:03 -0800 | [diff] [blame] | 1049 | hb_bool_t |
Behdad Esfahbod | bb1e192 | 2017-01-21 17:41:37 -0800 | [diff] [blame] | 1050 | hb_variation_from_string (const char *str, int len, |
| 1051 | hb_variation_t *variation) |
Behdad Esfahbod | b3c0714 | 2017-01-20 20:30:03 -0800 | [diff] [blame] | 1052 | { |
Behdad Esfahbod | bb1e192 | 2017-01-21 17:41:37 -0800 | [diff] [blame] | 1053 | hb_variation_t var; |
Behdad Esfahbod | b3c0714 | 2017-01-20 20:30:03 -0800 | [diff] [blame] | 1054 | |
| 1055 | if (len < 0) |
| 1056 | len = strlen (str); |
| 1057 | |
Behdad Esfahbod | bb1e192 | 2017-01-21 17:41:37 -0800 | [diff] [blame] | 1058 | if (likely (parse_one_variation (&str, str + len, &var))) |
Behdad Esfahbod | b3c0714 | 2017-01-20 20:30:03 -0800 | [diff] [blame] | 1059 | { |
Behdad Esfahbod | bb1e192 | 2017-01-21 17:41:37 -0800 | [diff] [blame] | 1060 | if (variation) |
| 1061 | *variation = var; |
Behdad Esfahbod | b3c0714 | 2017-01-20 20:30:03 -0800 | [diff] [blame] | 1062 | return true; |
| 1063 | } |
| 1064 | |
Behdad Esfahbod | bb1e192 | 2017-01-21 17:41:37 -0800 | [diff] [blame] | 1065 | if (variation) |
Behdad Esfahbod | ac0efaf | 2022-11-22 12:50:36 -0700 | [diff] [blame] | 1066 | hb_memset (variation, 0, sizeof (*variation)); |
Behdad Esfahbod | b3c0714 | 2017-01-20 20:30:03 -0800 | [diff] [blame] | 1067 | return false; |
| 1068 | } |
| 1069 | |
Behdad Esfahbod | 1364393 | 2022-01-12 10:54:28 -0700 | [diff] [blame] | 1070 | #ifndef HB_NO_SETLOCALE |
Behdad Esfahbod | a45a630 | 2022-01-08 15:47:33 -0800 | [diff] [blame] | 1071 | |
| 1072 | static inline void free_static_C_locale (); |
| 1073 | |
Behdad Esfahbod | 1364393 | 2022-01-12 10:54:28 -0700 | [diff] [blame] | 1074 | static struct hb_C_locale_lazy_loader_t : hb_lazy_loader_t<hb_remove_pointer<hb_locale_t>, |
Behdad Esfahbod | 6893723 | 2022-02-11 13:16:25 -0600 | [diff] [blame] | 1075 | hb_C_locale_lazy_loader_t> |
Behdad Esfahbod | a45a630 | 2022-01-08 15:47:33 -0800 | [diff] [blame] | 1076 | { |
Behdad Esfahbod | 1364393 | 2022-01-12 10:54:28 -0700 | [diff] [blame] | 1077 | static hb_locale_t create () |
Behdad Esfahbod | a45a630 | 2022-01-08 15:47:33 -0800 | [diff] [blame] | 1078 | { |
Behdad Esfahbod | 1364393 | 2022-01-12 10:54:28 -0700 | [diff] [blame] | 1079 | hb_locale_t l = newlocale (LC_ALL_MASK, "C", NULL); |
Behdad Esfahbod | a45a630 | 2022-01-08 15:47:33 -0800 | [diff] [blame] | 1080 | if (!l) |
| 1081 | return l; |
| 1082 | |
| 1083 | hb_atexit (free_static_C_locale); |
| 1084 | |
| 1085 | return l; |
| 1086 | } |
Behdad Esfahbod | 1364393 | 2022-01-12 10:54:28 -0700 | [diff] [blame] | 1087 | static void destroy (hb_locale_t l) |
Behdad Esfahbod | a45a630 | 2022-01-08 15:47:33 -0800 | [diff] [blame] | 1088 | { |
| 1089 | freelocale (l); |
| 1090 | } |
Behdad Esfahbod | 1364393 | 2022-01-12 10:54:28 -0700 | [diff] [blame] | 1091 | static hb_locale_t get_null () |
Behdad Esfahbod | a45a630 | 2022-01-08 15:47:33 -0800 | [diff] [blame] | 1092 | { |
Behdad Esfahbod | 1364393 | 2022-01-12 10:54:28 -0700 | [diff] [blame] | 1093 | return (hb_locale_t) 0; |
Behdad Esfahbod | a45a630 | 2022-01-08 15:47:33 -0800 | [diff] [blame] | 1094 | } |
| 1095 | } static_C_locale; |
| 1096 | |
| 1097 | static inline |
| 1098 | void free_static_C_locale () |
| 1099 | { |
| 1100 | static_C_locale.free_instance (); |
| 1101 | } |
| 1102 | |
Behdad Esfahbod | 1364393 | 2022-01-12 10:54:28 -0700 | [diff] [blame] | 1103 | static hb_locale_t |
Behdad Esfahbod | a45a630 | 2022-01-08 15:47:33 -0800 | [diff] [blame] | 1104 | get_C_locale () |
| 1105 | { |
| 1106 | return static_C_locale.get_unconst (); |
| 1107 | } |
| 1108 | |
Behdad Esfahbod | a45a630 | 2022-01-08 15:47:33 -0800 | [diff] [blame] | 1109 | #endif |
| 1110 | |
Behdad Esfahbod | d2f249e | 2017-01-22 17:42:33 -0800 | [diff] [blame] | 1111 | /** |
| 1112 | * hb_variation_to_string: |
Khaled Hosny | 9b602e8 | 2020-12-30 23:28:54 +0200 | [diff] [blame] | 1113 | * @variation: an #hb_variation_t to convert |
Lorenz Wildberg | 2ee42bb | 2022-11-01 13:00:18 +0100 | [diff] [blame] | 1114 | * @buf: (array length=size) (out caller-allocates): output string |
Khaled Hosny | 9b602e8 | 2020-12-30 23:28:54 +0200 | [diff] [blame] | 1115 | * @size: the allocated size of @buf |
| 1116 | * |
Khaled Hosny | 98e90cc | 2022-06-30 08:43:57 +0200 | [diff] [blame] | 1117 | * Converts an #hb_variation_t into a `NULL`-terminated string in the format |
Khaled Hosny | 9b602e8 | 2020-12-30 23:28:54 +0200 | [diff] [blame] | 1118 | * understood by hb_variation_from_string(). The client in responsible for |
| 1119 | * allocating big enough size for @buf, 128 bytes is more than enough. |
Behdad Esfahbod | d2f249e | 2017-01-22 17:42:33 -0800 | [diff] [blame] | 1120 | * |
| 1121 | * Since: 1.4.2 |
| 1122 | */ |
Behdad Esfahbod | b3c0714 | 2017-01-20 20:30:03 -0800 | [diff] [blame] | 1123 | void |
Behdad Esfahbod | bb1e192 | 2017-01-21 17:41:37 -0800 | [diff] [blame] | 1124 | hb_variation_to_string (hb_variation_t *variation, |
Behdad Esfahbod | b3c0714 | 2017-01-20 20:30:03 -0800 | [diff] [blame] | 1125 | char *buf, unsigned int size) |
| 1126 | { |
| 1127 | if (unlikely (!size)) return; |
| 1128 | |
| 1129 | char s[128]; |
| 1130 | unsigned int len = 0; |
Behdad Esfahbod | bb1e192 | 2017-01-21 17:41:37 -0800 | [diff] [blame] | 1131 | hb_tag_to_string (variation->tag, s + len); |
Behdad Esfahbod | b3c0714 | 2017-01-20 20:30:03 -0800 | [diff] [blame] | 1132 | len += 4; |
| 1133 | while (len && s[len - 1] == ' ') |
| 1134 | len--; |
| 1135 | s[len++] = '='; |
Behdad Esfahbod | a45a630 | 2022-01-08 15:47:33 -0800 | [diff] [blame] | 1136 | |
Behdad Esfahbod | 1364393 | 2022-01-12 10:54:28 -0700 | [diff] [blame] | 1137 | hb_locale_t oldlocale HB_UNUSED; |
Behdad Esfahbod | b97e4f7 | 2022-01-15 17:47:51 -0700 | [diff] [blame] | 1138 | oldlocale = hb_uselocale (get_C_locale ()); |
Behdad Esfahbod | 41248cc | 2019-05-07 20:54:31 -0700 | [diff] [blame] | 1139 | len += hb_max (0, snprintf (s + len, ARRAY_LENGTH (s) - len, "%g", (double) variation->value)); |
Behdad Esfahbod | b97e4f7 | 2022-01-15 17:47:51 -0700 | [diff] [blame] | 1140 | (void) hb_uselocale (oldlocale); |
Behdad Esfahbod | b3c0714 | 2017-01-20 20:30:03 -0800 | [diff] [blame] | 1141 | |
| 1142 | assert (len < ARRAY_LENGTH (s)); |
Behdad Esfahbod | 41248cc | 2019-05-07 20:54:31 -0700 | [diff] [blame] | 1143 | len = hb_min (len, size - 1); |
Behdad Esfahbod | 59c45f6 | 2022-11-22 12:54:50 -0700 | [diff] [blame] | 1144 | hb_memcpy (buf, s, len); |
Behdad Esfahbod | b3c0714 | 2017-01-20 20:30:03 -0800 | [diff] [blame] | 1145 | buf[len] = '\0'; |
| 1146 | } |
Behdad Esfahbod | e22a48a | 2018-07-23 13:24:26 -0700 | [diff] [blame] | 1147 | |
Ebrahim Byagowi | 9542bdd | 2019-04-29 14:52:28 -0700 | [diff] [blame] | 1148 | /** |
| 1149 | * hb_color_get_alpha: |
Khaled Hosny | 9b602e8 | 2020-12-30 23:28:54 +0200 | [diff] [blame] | 1150 | * @color: an #hb_color_t we are interested in its channels. |
Ebrahim Byagowi | 9542bdd | 2019-04-29 14:52:28 -0700 | [diff] [blame] | 1151 | * |
Khaled Hosny | 4bfa0b3 | 2020-12-31 16:30:05 +0200 | [diff] [blame] | 1152 | * Fetches the alpha channel of the given @color. |
| 1153 | * |
| 1154 | * Return value: Alpha channel value |
Ebrahim Byagowi | 9542bdd | 2019-04-29 14:52:28 -0700 | [diff] [blame] | 1155 | * |
Behdad Esfahbod | 1da0891 | 2019-05-24 15:41:34 -0400 | [diff] [blame] | 1156 | * Since: 2.1.0 |
Ebrahim Byagowi | 9542bdd | 2019-04-29 14:52:28 -0700 | [diff] [blame] | 1157 | */ |
| 1158 | uint8_t |
| 1159 | (hb_color_get_alpha) (hb_color_t color) |
| 1160 | { |
| 1161 | return hb_color_get_alpha (color); |
| 1162 | } |
| 1163 | |
| 1164 | /** |
| 1165 | * hb_color_get_red: |
Khaled Hosny | 9b602e8 | 2020-12-30 23:28:54 +0200 | [diff] [blame] | 1166 | * @color: an #hb_color_t we are interested in its channels. |
Ebrahim Byagowi | 9542bdd | 2019-04-29 14:52:28 -0700 | [diff] [blame] | 1167 | * |
Khaled Hosny | 4bfa0b3 | 2020-12-31 16:30:05 +0200 | [diff] [blame] | 1168 | * Fetches the red channel of the given @color. |
| 1169 | * |
| 1170 | * Return value: Red channel value |
Ebrahim Byagowi | 9542bdd | 2019-04-29 14:52:28 -0700 | [diff] [blame] | 1171 | * |
Behdad Esfahbod | 1da0891 | 2019-05-24 15:41:34 -0400 | [diff] [blame] | 1172 | * Since: 2.1.0 |
Ebrahim Byagowi | 9542bdd | 2019-04-29 14:52:28 -0700 | [diff] [blame] | 1173 | */ |
| 1174 | uint8_t |
| 1175 | (hb_color_get_red) (hb_color_t color) |
| 1176 | { |
| 1177 | return hb_color_get_red (color); |
| 1178 | } |
| 1179 | |
| 1180 | /** |
| 1181 | * hb_color_get_green: |
Khaled Hosny | 9b602e8 | 2020-12-30 23:28:54 +0200 | [diff] [blame] | 1182 | * @color: an #hb_color_t we are interested in its channels. |
Ebrahim Byagowi | 9542bdd | 2019-04-29 14:52:28 -0700 | [diff] [blame] | 1183 | * |
Khaled Hosny | 4bfa0b3 | 2020-12-31 16:30:05 +0200 | [diff] [blame] | 1184 | * Fetches the green channel of the given @color. |
| 1185 | * |
| 1186 | * Return value: Green channel value |
Ebrahim Byagowi | 9542bdd | 2019-04-29 14:52:28 -0700 | [diff] [blame] | 1187 | * |
Behdad Esfahbod | 1da0891 | 2019-05-24 15:41:34 -0400 | [diff] [blame] | 1188 | * Since: 2.1.0 |
Ebrahim Byagowi | 9542bdd | 2019-04-29 14:52:28 -0700 | [diff] [blame] | 1189 | */ |
| 1190 | uint8_t |
| 1191 | (hb_color_get_green) (hb_color_t color) |
| 1192 | { |
| 1193 | return hb_color_get_green (color); |
| 1194 | } |
| 1195 | |
| 1196 | /** |
| 1197 | * hb_color_get_blue: |
Khaled Hosny | 9b602e8 | 2020-12-30 23:28:54 +0200 | [diff] [blame] | 1198 | * @color: an #hb_color_t we are interested in its channels. |
Ebrahim Byagowi | 9542bdd | 2019-04-29 14:52:28 -0700 | [diff] [blame] | 1199 | * |
Khaled Hosny | 4bfa0b3 | 2020-12-31 16:30:05 +0200 | [diff] [blame] | 1200 | * Fetches the blue channel of the given @color. |
| 1201 | * |
| 1202 | * Return value: Blue channel value |
Ebrahim Byagowi | 9542bdd | 2019-04-29 14:52:28 -0700 | [diff] [blame] | 1203 | * |
Behdad Esfahbod | 1da0891 | 2019-05-24 15:41:34 -0400 | [diff] [blame] | 1204 | * Since: 2.1.0 |
Ebrahim Byagowi | 9542bdd | 2019-04-29 14:52:28 -0700 | [diff] [blame] | 1205 | */ |
| 1206 | uint8_t |
| 1207 | (hb_color_get_blue) (hb_color_t color) |
| 1208 | { |
| 1209 | return hb_color_get_blue (color); |
| 1210 | } |
| 1211 | |
| 1212 | |
Behdad Esfahbod | e22a48a | 2018-07-23 13:24:26 -0700 | [diff] [blame] | 1213 | /* If there is no visibility control, then hb-static.cc will NOT |
| 1214 | * define anything. Instead, we get it to define one set in here |
| 1215 | * only, so only libharfbuzz.so defines them, not other libs. */ |
| 1216 | #ifdef HB_NO_VISIBILITY |
| 1217 | #undef HB_NO_VISIBILITY |
| 1218 | #include "hb-static.cc" |
| 1219 | #define HB_NO_VISIBILITY 1 |
| 1220 | #endif |