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