/src/harfbuzz/src/hb-common.cc
Line | Count | Source |
1 | | /* |
2 | | * Copyright © 2009,2010 Red Hat, Inc. |
3 | | * Copyright © 2011,2012 Google, Inc. |
4 | | * |
5 | | * This is part of HarfBuzz, a text shaping library. |
6 | | * |
7 | | * Permission is hereby granted, without written agreement and without |
8 | | * license or royalty fees, to use, copy, modify, and distribute this |
9 | | * software and its documentation for any purpose, provided that the |
10 | | * above copyright notice and the following two paragraphs appear in |
11 | | * all copies of this software. |
12 | | * |
13 | | * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR |
14 | | * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES |
15 | | * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN |
16 | | * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH |
17 | | * DAMAGE. |
18 | | * |
19 | | * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, |
20 | | * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND |
21 | | * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS |
22 | | * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO |
23 | | * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. |
24 | | * |
25 | | * Red Hat Author(s): Behdad Esfahbod |
26 | | * Google Author(s): Behdad Esfahbod |
27 | | */ |
28 | | |
29 | | #include "hb.hh" |
30 | | #include "hb-machinery.hh" |
31 | | |
32 | | |
33 | | /** |
34 | | * SECTION:hb-common |
35 | | * @title: hb-common |
36 | | * @short_description: Common data types |
37 | | * @include: hb.h |
38 | | * |
39 | | * Common data types used across HarfBuzz are defined here. |
40 | | **/ |
41 | | |
42 | | |
43 | | /* hb_tag_t */ |
44 | | |
45 | | /** |
46 | | * hb_tag_from_string: |
47 | | * @str: (array length=len) (element-type uint8_t): String to convert |
48 | | * @len: Length of @str, or -1 if it is `NULL`-terminated |
49 | | * |
50 | | * Converts a string into an #hb_tag_t. Valid tags |
51 | | * are four characters. Shorter input strings will be |
52 | | * padded with spaces. Longer input strings will be |
53 | | * truncated. |
54 | | * |
55 | | * Return value: The #hb_tag_t corresponding to @str |
56 | | * |
57 | | * Since: 0.9.2 |
58 | | **/ |
59 | | hb_tag_t |
60 | | hb_tag_from_string (const char *str, int len) |
61 | 682k | { |
62 | 682k | char tag[4]; |
63 | 682k | unsigned int i; |
64 | | |
65 | 682k | if (!str || !len || !*str) |
66 | 2.01k | return HB_TAG_NONE; |
67 | | |
68 | 680k | if (len < 0 || len > 4) |
69 | 0 | len = 4; |
70 | 1.84M | for (i = 0; i < (unsigned) len && str[i]; i++) |
71 | 1.16M | tag[i] = str[i]; |
72 | 2.23M | for (; i < 4; i++) |
73 | 1.55M | tag[i] = ' '; |
74 | | |
75 | 680k | return HB_TAG (tag[0], tag[1], tag[2], tag[3]); |
76 | 682k | } |
77 | | |
78 | | /** |
79 | | * hb_tag_to_string: |
80 | | * @tag: #hb_tag_t to convert |
81 | | * @buf: (out caller-allocates) (array fixed-size=4) (element-type uint8_t): Converted string |
82 | | * |
83 | | * Converts an #hb_tag_t to a string and returns it in @buf. |
84 | | * Strings will be four characters long. |
85 | | * |
86 | | * Since: 0.9.5 |
87 | | **/ |
88 | | void |
89 | | hb_tag_to_string (hb_tag_t tag, char *buf) |
90 | 4.84M | { |
91 | 4.84M | buf[0] = (char) (uint8_t) (tag >> 24); |
92 | 4.84M | buf[1] = (char) (uint8_t) (tag >> 16); |
93 | 4.84M | buf[2] = (char) (uint8_t) (tag >> 8); |
94 | 4.84M | buf[3] = (char) (uint8_t) (tag >> 0); |
95 | 4.84M | } |
96 | | |
97 | | |
98 | | /* hb_direction_t */ |
99 | | |
100 | | static const char direction_strings[][4] = { |
101 | | "ltr", |
102 | | "rtl", |
103 | | "ttb", |
104 | | "btt" |
105 | | }; |
106 | | |
107 | | /** |
108 | | * hb_direction_from_string: |
109 | | * @str: (array length=len) (element-type uint8_t): String to convert |
110 | | * @len: Length of @str, or -1 if it is `NULL`-terminated |
111 | | * |
112 | | * Converts a string to an #hb_direction_t. |
113 | | * |
114 | | * Matching is loose and applies only to the first letter. For |
115 | | * examples, "LTR" and "left-to-right" will both return #HB_DIRECTION_LTR. |
116 | | * |
117 | | * Unmatched strings will return #HB_DIRECTION_INVALID. |
118 | | * |
119 | | * Return value: The #hb_direction_t matching @str |
120 | | * |
121 | | * Since: 0.9.2 |
122 | | **/ |
123 | | hb_direction_t |
124 | | hb_direction_from_string (const char *str, int len) |
125 | 0 | { |
126 | 0 | if (unlikely (!str || !len || !*str)) |
127 | 0 | return HB_DIRECTION_INVALID; |
128 | | |
129 | | /* Lets match loosely: just match the first letter, such that |
130 | | * all of "ltr", "left-to-right", etc work! |
131 | | */ |
132 | 0 | char c = TOLOWER (str[0]); |
133 | 0 | for (unsigned int i = 0; i < ARRAY_LENGTH (direction_strings); i++) |
134 | 0 | if (c == direction_strings[i][0]) |
135 | 0 | return (hb_direction_t) (HB_DIRECTION_LTR + i); |
136 | | |
137 | 0 | return HB_DIRECTION_INVALID; |
138 | 0 | } |
139 | | |
140 | | /** |
141 | | * hb_direction_to_string: |
142 | | * @direction: The #hb_direction_t to convert |
143 | | * |
144 | | * Converts an #hb_direction_t to a string. |
145 | | * |
146 | | * Return value: (transfer none): The string corresponding to @direction |
147 | | * |
148 | | * Since: 0.9.2 |
149 | | **/ |
150 | | const char * |
151 | | hb_direction_to_string (hb_direction_t direction) |
152 | 0 | { |
153 | 0 | if (likely ((unsigned int) (direction - HB_DIRECTION_LTR) |
154 | 0 | < ARRAY_LENGTH (direction_strings))) |
155 | 0 | return direction_strings[direction - HB_DIRECTION_LTR]; |
156 | | |
157 | 0 | return "invalid"; |
158 | 0 | } |
159 | | |
160 | | |
161 | | /* hb_language_t */ |
162 | | |
163 | | struct hb_language_impl_t { |
164 | | const char s[1]; |
165 | | }; |
166 | | |
167 | | static const char canon_map[256] = { |
168 | | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
169 | | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
170 | | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '-', 0, 0, |
171 | | '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 0, 0, 0, 0, 0, 0, |
172 | | 0, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', |
173 | | 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 0, 0, 0, 0, '-', |
174 | | 0, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', |
175 | | 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 0, 0, 0, 0, 0 |
176 | | }; |
177 | | |
178 | | /* Maximum number of bytes interned for a language tag. */ |
179 | 1.47M | #define LANG_MAX_LEN 255 |
180 | | |
181 | | static bool |
182 | | lang_equal (hb_language_t v1, |
183 | | const void *v2) |
184 | 5.75M | { |
185 | 5.75M | const unsigned char *p1 = (const unsigned char *) v1; |
186 | 5.75M | const unsigned char *p2 = (const unsigned char *) v2; |
187 | | |
188 | 8.86M | while (*p1 && *p1 == canon_map[*p2]) { |
189 | 3.10M | p1++; |
190 | 3.10M | p2++; |
191 | 3.10M | } |
192 | | |
193 | 5.75M | return *p1 == canon_map[*p2]; |
194 | 5.75M | } |
195 | | |
196 | | #if 0 |
197 | | static unsigned int |
198 | | lang_hash (const void *key) |
199 | | { |
200 | | const unsigned char *p = key; |
201 | | unsigned int h = 0; |
202 | | while (canon_map[*p]) |
203 | | { |
204 | | h = (h << 5) - h + canon_map[*p]; |
205 | | p++; |
206 | | } |
207 | | |
208 | | return h; |
209 | | } |
210 | | #endif |
211 | | |
212 | | |
213 | | struct hb_language_item_t { |
214 | | |
215 | | struct hb_language_item_t *next; |
216 | | hb_language_t lang; |
217 | | |
218 | | bool operator == (const char *s) const |
219 | 5.75M | { return lang_equal (lang, s); } |
220 | | |
221 | | hb_language_item_t & operator = (const char *s) |
222 | 46 | { |
223 | | /* We can't call strdup(), because we allow custom allocators. */ |
224 | 46 | size_t len = strlen(s) + 1; |
225 | 46 | lang = (hb_language_t) hb_malloc(len); |
226 | 46 | if (likely (lang)) |
227 | 46 | { |
228 | 46 | hb_memcpy((unsigned char *) lang, s, len); |
229 | 143 | for (unsigned char *p = (unsigned char *) lang; *p; p++) |
230 | 97 | *p = canon_map[*p]; |
231 | 46 | } |
232 | | |
233 | 46 | return *this; |
234 | 46 | } |
235 | | |
236 | 46 | void fini () { hb_free ((void *) lang); } |
237 | | }; |
238 | | |
239 | | |
240 | | /* Thread-safe lockfree language list */ |
241 | | |
242 | | static hb_atomic_t<hb_language_item_t *> langs; |
243 | | /* One-entry cache of the most recently returned language, so that repeated |
244 | | * lookups of the same tag (the common case: every run of a mostly-one-language |
245 | | * document) return without walking the whole list. Interned languages are |
246 | | * never freed, so a cached pointer stays valid; a stale or raced read just |
247 | | * falls through to the list scan below. */ |
248 | | static hb_atomic_t<hb_language_item_t *> last_found_lang; |
249 | | |
250 | | static inline void |
251 | | free_langs () |
252 | 1 | { |
253 | 1 | last_found_lang.set_relaxed (nullptr); |
254 | 1 | retry: |
255 | 1 | hb_language_item_t *first_lang = langs; |
256 | 1 | if (unlikely (!langs.cmpexch (first_lang, nullptr))) |
257 | 0 | goto retry; |
258 | | |
259 | 47 | while (first_lang) { |
260 | 46 | hb_language_item_t *next = first_lang->next; |
261 | 46 | first_lang->fini (); |
262 | 46 | hb_free (first_lang); |
263 | 46 | first_lang = next; |
264 | 46 | } |
265 | 1 | } |
266 | | |
267 | | static hb_language_item_t * |
268 | | lang_find_or_insert (const char *key) |
269 | 1.47M | { |
270 | | /* Fast path: the most recently returned language. Repeated lookups of the |
271 | | * same tag hit here without walking the list. */ |
272 | 1.47M | hb_language_item_t *cached = last_found_lang.get_acquire (); |
273 | 1.47M | if (cached && *cached == key) |
274 | 1.33M | return cached; |
275 | | |
276 | 141k | retry: |
277 | 141k | hb_language_item_t *first_lang = langs; |
278 | | |
279 | 4.27M | for (hb_language_item_t *lang = first_lang; lang; lang = lang->next) |
280 | 4.27M | if (*lang == key) |
281 | 141k | { |
282 | 141k | last_found_lang.set_release (lang); |
283 | 141k | return lang; |
284 | 141k | } |
285 | | |
286 | | /* Not found; allocate one. */ |
287 | 46 | hb_language_item_t *lang = (hb_language_item_t *) hb_calloc (1, sizeof (hb_language_item_t)); |
288 | 46 | if (unlikely (!lang)) |
289 | 0 | return nullptr; |
290 | 46 | lang->next = first_lang; |
291 | 46 | *lang = key; |
292 | 46 | if (unlikely (!lang->lang)) |
293 | 0 | { |
294 | 0 | hb_free (lang); |
295 | 0 | return nullptr; |
296 | 0 | } |
297 | | |
298 | 46 | if (unlikely (!langs.cmpexch (first_lang, lang))) |
299 | 0 | { |
300 | 0 | lang->fini (); |
301 | 0 | hb_free (lang); |
302 | 0 | goto retry; |
303 | 0 | } |
304 | | |
305 | 46 | if (!first_lang) |
306 | 1 | hb_atexit (free_langs); /* First person registers atexit() callback. */ |
307 | | |
308 | 46 | last_found_lang.set_release (lang); |
309 | 46 | return lang; |
310 | 46 | } |
311 | | |
312 | | |
313 | | /** |
314 | | * hb_language_from_string: |
315 | | * @str: (array length=len) (element-type uint8_t): a string representing |
316 | | * a BCP 47 language tag |
317 | | * @len: length of the @str, or -1 if it is `NULL`-terminated. |
318 | | * |
319 | | * Converts @str representing a BCP 47 language tag to the corresponding |
320 | | * #hb_language_t. At most 255 bytes of the tag are used. |
321 | | * |
322 | | * Return value: (transfer none): |
323 | | * The #hb_language_t corresponding to the BCP 47 language tag. |
324 | | * |
325 | | * Since: 0.9.2 |
326 | | **/ |
327 | | hb_language_t |
328 | | hb_language_from_string (const char *str, int len) |
329 | 1.47M | { |
330 | 1.47M | if (!str || !len || !*str) |
331 | 92 | return HB_LANGUAGE_INVALID; |
332 | | |
333 | 1.47M | char strbuf[LANG_MAX_LEN + 1]; |
334 | 1.47M | unsigned limit = len < 0 ? LANG_MAX_LEN : hb_min (len, LANG_MAX_LEN); |
335 | 1.47M | unsigned length = 0; |
336 | 4.48M | while (length < limit && str[length]) |
337 | 3.00M | { |
338 | 3.00M | strbuf[length] = str[length]; |
339 | 3.00M | length++; |
340 | 3.00M | } |
341 | 1.47M | strbuf[length] = '\0'; |
342 | 1.47M | hb_language_item_t *item = lang_find_or_insert (strbuf); |
343 | | |
344 | 1.47M | return likely (item) ? item->lang : HB_LANGUAGE_INVALID; |
345 | 1.47M | } |
346 | | |
347 | | /** |
348 | | * hb_language_to_string: |
349 | | * @language: The #hb_language_t to convert |
350 | | * |
351 | | * Converts an #hb_language_t to a string. |
352 | | * |
353 | | * Return value: (transfer none): |
354 | | * A `NULL`-terminated string representing the @language. Must not be freed by |
355 | | * the caller. |
356 | | * |
357 | | * Since: 0.9.2 |
358 | | **/ |
359 | | const char * |
360 | | hb_language_to_string (hb_language_t language) |
361 | 682k | { |
362 | 682k | if (unlikely (!language)) return nullptr; |
363 | | |
364 | 682k | return language->s; |
365 | 682k | } |
366 | | |
367 | | /** |
368 | | * hb_language_get_default: |
369 | | * |
370 | | * Fetch the default language from current locale. |
371 | | * |
372 | | * <note>Note that the first time this function is called, it calls |
373 | | * "setlocale (LC_CTYPE, nullptr)" to fetch current locale. The underlying |
374 | | * setlocale function is, in many implementations, NOT threadsafe. To avoid |
375 | | * problems, call this function once before multiple threads can call it. |
376 | | * This function is only used from hb_buffer_guess_segment_properties() by |
377 | | * HarfBuzz itself.</note> |
378 | | * |
379 | | * Return value: (transfer none): The default language of the locale as |
380 | | * an #hb_language_t |
381 | | * |
382 | | * Since: 0.9.2 |
383 | | **/ |
384 | | hb_language_t |
385 | | hb_language_get_default () |
386 | 748k | { |
387 | 748k | static hb_atomic_t<hb_language_t> default_language; |
388 | | |
389 | 748k | hb_language_t language = default_language; |
390 | 748k | if (unlikely (language == HB_LANGUAGE_INVALID)) |
391 | 1 | { |
392 | 1 | language = hb_language_from_string (hb_setlocale (LC_CTYPE, nullptr), -1); |
393 | 1 | (void) default_language.cmpexch (HB_LANGUAGE_INVALID, language); |
394 | 1 | } |
395 | | |
396 | 748k | return language; |
397 | 748k | } |
398 | | |
399 | | /** |
400 | | * hb_language_matches: |
401 | | * @language: The #hb_language_t to work on |
402 | | * @specific: Another #hb_language_t |
403 | | * |
404 | | * Check whether a second language tag is the same or a more |
405 | | * specific version of the provided language tag. For example, |
406 | | * "fa_IR.utf8" is a more specific tag for "fa" or for "fa_IR". |
407 | | * |
408 | | * Return value: `true` if languages match, `false` otherwise. |
409 | | * |
410 | | * Since: 5.0.0 |
411 | | **/ |
412 | | hb_bool_t |
413 | | hb_language_matches (hb_language_t language, |
414 | | hb_language_t specific) |
415 | 0 | { |
416 | 0 | if (language == specific) return true; |
417 | 0 | if (!language || !specific) return false; |
418 | | |
419 | 0 | const char *l = language->s; |
420 | 0 | const char *s = specific->s; |
421 | 0 | unsigned ll = strlen (l); |
422 | 0 | unsigned sl = strlen (s); |
423 | |
|
424 | 0 | if (ll > sl) |
425 | 0 | return false; |
426 | | |
427 | 0 | return strncmp (l, s, ll) == 0 && |
428 | 0 | (s[ll] == '\0' || s[ll] == '-'); |
429 | 0 | } |
430 | | |
431 | | /* Like hb_language_matches (hb_language_from_string (str, len), specific), |
432 | | * without interning @str; hb_language_from_string() would add it to the |
433 | | * process-wide list of languages forever. */ |
434 | | int |
435 | | hb_language_matching_prefix (const char *str, |
436 | | unsigned int len, |
437 | | hb_language_t specific) |
438 | 0 | { |
439 | 0 | if (!str || !len || !*str) return specific == HB_LANGUAGE_INVALID ? 0 : -1; |
440 | 0 | if (!specific) return -1; |
441 | | |
442 | 0 | const unsigned char *p1 = (const unsigned char *) str; |
443 | 0 | const char *s = specific->s; |
444 | 0 | len = hb_min (len, (unsigned) LANG_MAX_LEN); |
445 | 0 | unsigned l = 0; |
446 | 0 | for (; l < len && canon_map[p1[l]]; l++) |
447 | 0 | if (canon_map[p1[l]] != s[l]) return -1; |
448 | | |
449 | 0 | return s[l] == '\0' || s[l] == '-' ? (int) l : -1; |
450 | 0 | } |
451 | | |
452 | | |
453 | | /* hb_script_t */ |
454 | | |
455 | | /** |
456 | | * hb_script_from_iso15924_tag: |
457 | | * @tag: an #hb_tag_t representing an ISO 15924 tag. |
458 | | * |
459 | | * Converts an ISO 15924 script tag to a corresponding #hb_script_t. |
460 | | * |
461 | | * Return value: |
462 | | * An #hb_script_t corresponding to the ISO 15924 tag. |
463 | | * |
464 | | * Since: 0.9.2 |
465 | | **/ |
466 | | hb_script_t |
467 | | hb_script_from_iso15924_tag (hb_tag_t tag) |
468 | 0 | { |
469 | 0 | if (unlikely (tag == HB_TAG_NONE)) |
470 | 0 | return HB_SCRIPT_INVALID; |
471 | | |
472 | | /* Be lenient, adjust case (one capital letter followed by three small letters) */ |
473 | 0 | tag = (tag & 0xDFDFDFDFu) | 0x00202020u; |
474 | |
|
475 | 0 | switch (tag) |
476 | 0 | { |
477 | | /* Script variants from https://unicode.org/iso15924/ */ |
478 | 0 | case HB_TAG('A','r','a','n'): return HB_SCRIPT_ARABIC; |
479 | 0 | case HB_TAG('C','y','r','s'): return HB_SCRIPT_CYRILLIC; |
480 | 0 | case HB_TAG('G','e','o','k'): return HB_SCRIPT_GEORGIAN; |
481 | 0 | case HB_TAG('H','a','n','s'): return HB_SCRIPT_HAN; |
482 | 0 | case HB_TAG('H','a','n','t'): return HB_SCRIPT_HAN; |
483 | 0 | case HB_TAG('J','a','m','o'): return HB_SCRIPT_HANGUL; |
484 | 0 | case HB_TAG('L','a','t','f'): return HB_SCRIPT_LATIN; |
485 | 0 | case HB_TAG('L','a','t','g'): return HB_SCRIPT_LATIN; |
486 | 0 | case HB_TAG('S','y','r','e'): return HB_SCRIPT_SYRIAC; |
487 | 0 | case HB_TAG('S','y','r','j'): return HB_SCRIPT_SYRIAC; |
488 | 0 | case HB_TAG('S','y','r','n'): return HB_SCRIPT_SYRIAC; |
489 | 0 | } |
490 | | |
491 | | /* If it looks right, just use the tag as a script */ |
492 | 0 | if (((uint32_t) tag & 0xE0E0E0E0u) == 0x40606060u) |
493 | 0 | return (hb_script_t) tag; |
494 | | |
495 | | /* Otherwise, return unknown */ |
496 | 0 | return HB_SCRIPT_UNKNOWN; |
497 | 0 | } |
498 | | |
499 | | /** |
500 | | * hb_script_from_string: |
501 | | * @str: (array length=len) (element-type uint8_t): a string representing an |
502 | | * ISO 15924 tag. |
503 | | * @len: length of the @str, or -1 if it is `NULL`-terminated. |
504 | | * |
505 | | * Converts a string @str representing an ISO 15924 script tag to a |
506 | | * corresponding #hb_script_t. Shorthand for hb_tag_from_string() then |
507 | | * hb_script_from_iso15924_tag(). |
508 | | * |
509 | | * Return value: |
510 | | * An #hb_script_t corresponding to the ISO 15924 tag. |
511 | | * |
512 | | * Since: 0.9.2 |
513 | | **/ |
514 | | hb_script_t |
515 | | hb_script_from_string (const char *str, int len) |
516 | 0 | { |
517 | 0 | return hb_script_from_iso15924_tag (hb_tag_from_string (str, len)); |
518 | 0 | } |
519 | | |
520 | | /** |
521 | | * hb_script_to_iso15924_tag: |
522 | | * @script: an #hb_script_t to convert. |
523 | | * |
524 | | * Converts an #hb_script_t to a corresponding ISO 15924 script tag. |
525 | | * |
526 | | * Return value: |
527 | | * An #hb_tag_t representing an ISO 15924 script tag. |
528 | | * |
529 | | * Since: 0.9.2 |
530 | | **/ |
531 | | hb_tag_t |
532 | | hb_script_to_iso15924_tag (hb_script_t script) |
533 | 0 | { |
534 | 0 | return (hb_tag_t) script; |
535 | 0 | } |
536 | | |
537 | | /** |
538 | | * hb_script_get_horizontal_direction: |
539 | | * @script: The #hb_script_t to query |
540 | | * |
541 | | * Fetches the #hb_direction_t of a script when it is |
542 | | * set horizontally. All right-to-left scripts will return |
543 | | * #HB_DIRECTION_RTL. All left-to-right scripts will return |
544 | | * #HB_DIRECTION_LTR. |
545 | | * |
546 | | * Scripts that can be written either right-to-left or |
547 | | * left-to-right will return #HB_DIRECTION_INVALID. |
548 | | * |
549 | | * Unknown scripts will return #HB_DIRECTION_LTR. |
550 | | * |
551 | | * Return value: The horizontal #hb_direction_t of @script |
552 | | * |
553 | | * Since: 0.9.2 |
554 | | **/ |
555 | | hb_direction_t |
556 | | hb_script_get_horizontal_direction (hb_script_t script) |
557 | 2.42M | { |
558 | | /* https://docs.google.com/spreadsheets/d/1Y90M0Ie3MUJ6UVCRDOypOtijlMDLNNyyLk36T6iMu0o */ |
559 | 2.42M | switch ((hb_tag_t) script) |
560 | 2.42M | { |
561 | | /* Unicode-1.1 additions */ |
562 | 18.5k | case HB_SCRIPT_ARABIC: |
563 | 23.6k | case HB_SCRIPT_HEBREW: |
564 | | |
565 | | /* Unicode-3.0 additions */ |
566 | 23.9k | case HB_SCRIPT_SYRIAC: |
567 | 24.0k | case HB_SCRIPT_THAANA: |
568 | | |
569 | | /* Unicode-4.0 additions */ |
570 | 24.2k | case HB_SCRIPT_CYPRIOT: |
571 | | |
572 | | /* Unicode-4.1 additions */ |
573 | 24.3k | case HB_SCRIPT_KHAROSHTHI: |
574 | | |
575 | | /* Unicode-5.0 additions */ |
576 | 24.4k | case HB_SCRIPT_PHOENICIAN: |
577 | 74.3k | case HB_SCRIPT_NKO: |
578 | | |
579 | | /* Unicode-5.1 additions */ |
580 | 74.5k | case HB_SCRIPT_LYDIAN: |
581 | | |
582 | | /* Unicode-5.2 additions */ |
583 | 74.9k | case HB_SCRIPT_AVESTAN: |
584 | 75.0k | case HB_SCRIPT_IMPERIAL_ARAMAIC: |
585 | 75.0k | case HB_SCRIPT_INSCRIPTIONAL_PAHLAVI: |
586 | 75.0k | case HB_SCRIPT_INSCRIPTIONAL_PARTHIAN: |
587 | 75.1k | case HB_SCRIPT_OLD_SOUTH_ARABIAN: |
588 | 75.7k | case HB_SCRIPT_OLD_TURKIC: |
589 | 76.0k | case HB_SCRIPT_SAMARITAN: |
590 | | |
591 | | /* Unicode-6.0 additions */ |
592 | 76.0k | case HB_SCRIPT_MANDAIC: |
593 | | |
594 | | /* Unicode-6.1 additions */ |
595 | 76.0k | case HB_SCRIPT_MEROITIC_CURSIVE: |
596 | 76.0k | case HB_SCRIPT_MEROITIC_HIEROGLYPHS: |
597 | | |
598 | | /* Unicode-7.0 additions */ |
599 | 76.4k | case HB_SCRIPT_MANICHAEAN: |
600 | 76.4k | case HB_SCRIPT_MENDE_KIKAKUI: |
601 | 76.5k | case HB_SCRIPT_NABATAEAN: |
602 | 76.5k | case HB_SCRIPT_OLD_NORTH_ARABIAN: |
603 | 76.5k | case HB_SCRIPT_PALMYRENE: |
604 | 76.9k | case HB_SCRIPT_PSALTER_PAHLAVI: |
605 | | |
606 | | /* Unicode-8.0 additions */ |
607 | 77.0k | case HB_SCRIPT_HATRAN: |
608 | | |
609 | | /* Unicode-9.0 additions */ |
610 | 77.0k | case HB_SCRIPT_ADLAM: |
611 | | |
612 | | /* Unicode-11.0 additions */ |
613 | 77.3k | case HB_SCRIPT_HANIFI_ROHINGYA: |
614 | 77.3k | case HB_SCRIPT_OLD_SOGDIAN: |
615 | 77.3k | case HB_SCRIPT_SOGDIAN: |
616 | | |
617 | | /* Unicode-12.0 additions */ |
618 | 77.3k | case HB_SCRIPT_ELYMAIC: |
619 | | |
620 | | /* Unicode-13.0 additions */ |
621 | 77.4k | case HB_SCRIPT_CHORASMIAN: |
622 | 77.4k | case HB_SCRIPT_YEZIDI: |
623 | | |
624 | | /* Unicode-14.0 additions */ |
625 | 77.4k | case HB_SCRIPT_OLD_UYGHUR: |
626 | | |
627 | | /* Unicode-16.0 additions */ |
628 | 77.4k | case HB_SCRIPT_GARAY: |
629 | | |
630 | | /* Unicode-17.0 additions */ |
631 | 77.4k | case HB_SCRIPT_SIDETIC: |
632 | | |
633 | 77.4k | return HB_DIRECTION_RTL; |
634 | | |
635 | | |
636 | | /* https://github.com/harfbuzz/harfbuzz/issues/1000 */ |
637 | 211 | case HB_SCRIPT_OLD_HUNGARIAN: |
638 | 241 | case HB_SCRIPT_OLD_ITALIC: |
639 | 286 | case HB_SCRIPT_RUNIC: |
640 | 286 | case HB_SCRIPT_TIFINAGH: |
641 | | |
642 | 286 | return HB_DIRECTION_INVALID; |
643 | 2.42M | } |
644 | | |
645 | 2.34M | return HB_DIRECTION_LTR; |
646 | 2.42M | } |
647 | | |
648 | | |
649 | | /* hb_version */ |
650 | | |
651 | | |
652 | | /** |
653 | | * SECTION:hb-version |
654 | | * @title: hb-version |
655 | | * @short_description: Information about the version of HarfBuzz in use |
656 | | * @include: hb.h |
657 | | * |
658 | | * These functions and macros allow accessing version of the HarfBuzz |
659 | | * library used at compile- as well as run-time, and to direct code |
660 | | * conditionally based on those versions, again, at compile- or run-time. |
661 | | **/ |
662 | | |
663 | | |
664 | | /** |
665 | | * hb_version: |
666 | | * @major: (out): Library major version component |
667 | | * @minor: (out): Library minor version component |
668 | | * @micro: (out): Library micro version component |
669 | | * |
670 | | * Returns library version as three integer components. |
671 | | * |
672 | | * Since: 0.9.2 |
673 | | **/ |
674 | | void |
675 | | hb_version (unsigned int *major, |
676 | | unsigned int *minor, |
677 | | unsigned int *micro) |
678 | 0 | { |
679 | 0 | *major = HB_VERSION_MAJOR; |
680 | 0 | *minor = HB_VERSION_MINOR; |
681 | 0 | *micro = HB_VERSION_MICRO; |
682 | 0 | } |
683 | | |
684 | | /** |
685 | | * hb_version_string: |
686 | | * |
687 | | * Returns library version as a string with three components. |
688 | | * |
689 | | * Return value: Library version string |
690 | | * |
691 | | * Since: 0.9.2 |
692 | | **/ |
693 | | const char * |
694 | | hb_version_string () |
695 | 996 | { |
696 | 996 | return HB_VERSION_STRING; |
697 | 996 | } |
698 | | |
699 | | /** |
700 | | * hb_version_atleast: |
701 | | * @major: Library major version component |
702 | | * @minor: Library minor version component |
703 | | * @micro: Library micro version component |
704 | | * |
705 | | * Tests the library version against a minimum value, |
706 | | * as three integer components. |
707 | | * |
708 | | * Return value: `true` if the library is equal to or greater than |
709 | | * the test value, `false` otherwise |
710 | | * |
711 | | * Since: 0.9.30 |
712 | | **/ |
713 | | hb_bool_t |
714 | | hb_version_atleast (unsigned int major, |
715 | | unsigned int minor, |
716 | | unsigned int micro) |
717 | 0 | { |
718 | 0 | return HB_VERSION_ATLEAST (major, minor, micro); |
719 | 0 | } |
720 | | |
721 | | |
722 | | |
723 | | /* hb_feature_t and hb_variation_t */ |
724 | | |
725 | | static bool |
726 | | parse_space (const char **pp, const char *end) |
727 | 0 | { |
728 | 0 | while (*pp < end && ISSPACE (**pp)) |
729 | 0 | (*pp)++; |
730 | 0 | return true; |
731 | 0 | } |
732 | | |
733 | | static bool |
734 | | parse_char (const char **pp, const char *end, char c) |
735 | 0 | { |
736 | 0 | parse_space (pp, end); |
737 | |
|
738 | 0 | if (*pp == end || **pp != c) |
739 | 0 | return false; |
740 | | |
741 | 0 | (*pp)++; |
742 | 0 | return true; |
743 | 0 | } |
744 | | |
745 | | static bool |
746 | | parse_uint (const char **pp, const char *end, unsigned int *pv) |
747 | 0 | { |
748 | | /* Intentionally use hb_parse_int inside instead of hb_parse_uint, |
749 | | * such that -1 turns into "big number"... */ |
750 | 0 | int v; |
751 | 0 | if (unlikely (!hb_parse_int (pp, end, &v))) return false; |
752 | | |
753 | 0 | *pv = v; |
754 | 0 | return true; |
755 | 0 | } |
756 | | |
757 | | static bool |
758 | | parse_uint32 (const char **pp, const char *end, uint32_t *pv) |
759 | 0 | { |
760 | | /* Intentionally use hb_parse_int inside instead of hb_parse_uint, |
761 | | * such that -1 turns into "big number"... */ |
762 | 0 | int v; |
763 | 0 | if (unlikely (!hb_parse_int (pp, end, &v))) return false; |
764 | | |
765 | 0 | *pv = v; |
766 | 0 | return true; |
767 | 0 | } |
768 | | |
769 | | static bool |
770 | | parse_bool (const char **pp, const char *end, uint32_t *pv) |
771 | 0 | { |
772 | 0 | parse_space (pp, end); |
773 | |
|
774 | 0 | const char *p = *pp; |
775 | 0 | while (*pp < end && ISALPHA(**pp)) |
776 | 0 | (*pp)++; |
777 | | |
778 | | /* CSS allows on/off as aliases 1/0. */ |
779 | 0 | if (*pp - p == 2 |
780 | 0 | && TOLOWER (p[0]) == 'o' |
781 | 0 | && TOLOWER (p[1]) == 'n') |
782 | 0 | *pv = 1; |
783 | 0 | else if (*pp - p == 3 |
784 | 0 | && TOLOWER (p[0]) == 'o' |
785 | 0 | && TOLOWER (p[1]) == 'f' |
786 | 0 | && TOLOWER (p[2]) == 'f') |
787 | 0 | *pv = 0; |
788 | 0 | else |
789 | 0 | return false; |
790 | | |
791 | 0 | return true; |
792 | 0 | } |
793 | | |
794 | | /* hb_feature_t */ |
795 | | |
796 | | static bool |
797 | | parse_feature_value_prefix (const char **pp, const char *end, hb_feature_t *feature) |
798 | 0 | { |
799 | 0 | if (parse_char (pp, end, '-')) |
800 | 0 | feature->value = 0; |
801 | 0 | else { |
802 | 0 | parse_char (pp, end, '+'); |
803 | 0 | feature->value = 1; |
804 | 0 | } |
805 | |
|
806 | 0 | return true; |
807 | 0 | } |
808 | | |
809 | | static bool |
810 | | parse_tag (const char **pp, const char *end, hb_tag_t *tag) |
811 | 0 | { |
812 | 0 | parse_space (pp, end); |
813 | |
|
814 | 0 | char quote = 0; |
815 | |
|
816 | 0 | if (*pp < end && (**pp == '\'' || **pp == '"')) |
817 | 0 | { |
818 | 0 | quote = **pp; |
819 | 0 | (*pp)++; |
820 | 0 | } |
821 | |
|
822 | 0 | const char *p = *pp; |
823 | 0 | while (*pp < end && (**pp != ' ' && **pp != '=' && **pp != '[' && **pp != quote)) |
824 | 0 | (*pp)++; |
825 | |
|
826 | 0 | if (p == *pp || *pp - p > 4) |
827 | 0 | return false; |
828 | | |
829 | 0 | *tag = hb_tag_from_string (p, *pp - p); |
830 | |
|
831 | 0 | if (quote) |
832 | 0 | { |
833 | | /* CSS expects exactly four bytes. And we only allow quotations for |
834 | | * CSS compatibility. So, enforce the length. */ |
835 | 0 | if (*pp - p != 4) |
836 | 0 | return false; |
837 | 0 | if (*pp == end || **pp != quote) |
838 | 0 | return false; |
839 | 0 | (*pp)++; |
840 | 0 | } |
841 | | |
842 | 0 | return true; |
843 | 0 | } |
844 | | |
845 | | static bool |
846 | | parse_feature_indices (const char **pp, const char *end, hb_feature_t *feature) |
847 | 0 | { |
848 | 0 | parse_space (pp, end); |
849 | |
|
850 | 0 | bool has_start; |
851 | |
|
852 | 0 | feature->start = HB_FEATURE_GLOBAL_START; |
853 | 0 | feature->end = HB_FEATURE_GLOBAL_END; |
854 | |
|
855 | 0 | if (!parse_char (pp, end, '[')) |
856 | 0 | return true; |
857 | | |
858 | 0 | has_start = parse_uint (pp, end, &feature->start); |
859 | |
|
860 | 0 | if (parse_char (pp, end, ':') || parse_char (pp, end, ';')) { |
861 | 0 | parse_uint (pp, end, &feature->end); |
862 | 0 | } else { |
863 | 0 | if (has_start) |
864 | 0 | feature->end = feature->start + 1; |
865 | 0 | } |
866 | |
|
867 | 0 | return parse_char (pp, end, ']'); |
868 | 0 | } |
869 | | |
870 | | static bool |
871 | | parse_feature_value_postfix (const char **pp, const char *end, hb_feature_t *feature) |
872 | 0 | { |
873 | 0 | bool had_equal = parse_char (pp, end, '='); |
874 | 0 | bool had_value = parse_uint32 (pp, end, &feature->value) || |
875 | 0 | parse_bool (pp, end, &feature->value); |
876 | | /* CSS doesn't use equal-sign between tag and value. |
877 | | * If there was an equal-sign, then there *must* be a value. |
878 | | * A value without an equal-sign is ok, but not required. */ |
879 | 0 | return !had_equal || had_value; |
880 | 0 | } |
881 | | |
882 | | static bool |
883 | | parse_one_feature (const char **pp, const char *end, hb_feature_t *feature) |
884 | 0 | { |
885 | 0 | return parse_feature_value_prefix (pp, end, feature) && |
886 | 0 | parse_tag (pp, end, &feature->tag) && |
887 | 0 | parse_feature_indices (pp, end, feature) && |
888 | 0 | parse_feature_value_postfix (pp, end, feature) && |
889 | 0 | parse_space (pp, end) && |
890 | 0 | *pp == end; |
891 | 0 | } |
892 | | |
893 | | /** |
894 | | * hb_feature_from_string: |
895 | | * @str: (array length=len) (element-type uint8_t): a string to parse |
896 | | * @len: length of @str, or -1 if string is `NULL` terminated |
897 | | * @feature: (out): the #hb_feature_t to initialize with the parsed values |
898 | | * |
899 | | * Parses a string into a #hb_feature_t. |
900 | | * |
901 | | * The format for specifying feature strings follows. All valid CSS |
902 | | * font-feature-settings values other than 'normal' and the global values are |
903 | | * also accepted, though not documented below. CSS string escapes are not |
904 | | * supported. |
905 | | * |
906 | | * The range indices refer to the positions between Unicode characters. The |
907 | | * position before the first character is always 0. |
908 | | * |
909 | | * The format is Python-esque. Here is how it all works: |
910 | | * |
911 | | * <informaltable pgwide='1' align='left' frame='none'> |
912 | | * <tgroup cols='5'> |
913 | | * <thead> |
914 | | * <row><entry>Syntax</entry> <entry>Value</entry> <entry>Start</entry> <entry>End</entry></row> |
915 | | * </thead> |
916 | | * <tbody> |
917 | | * <row><entry>Setting value:</entry></row> |
918 | | * <row><entry>kern</entry> <entry>1</entry> <entry>0</entry> <entry>∞</entry> <entry>Turn feature on</entry></row> |
919 | | * <row><entry>+kern</entry> <entry>1</entry> <entry>0</entry> <entry>∞</entry> <entry>Turn feature on</entry></row> |
920 | | * <row><entry>-kern</entry> <entry>0</entry> <entry>0</entry> <entry>∞</entry> <entry>Turn feature off</entry></row> |
921 | | * <row><entry>kern=0</entry> <entry>0</entry> <entry>0</entry> <entry>∞</entry> <entry>Turn feature off</entry></row> |
922 | | * <row><entry>kern=1</entry> <entry>1</entry> <entry>0</entry> <entry>∞</entry> <entry>Turn feature on</entry></row> |
923 | | * <row><entry>aalt=2</entry> <entry>2</entry> <entry>0</entry> <entry>∞</entry> <entry>Choose 2nd alternate</entry></row> |
924 | | * <row><entry>Setting index:</entry></row> |
925 | | * <row><entry>kern[]</entry> <entry>1</entry> <entry>0</entry> <entry>∞</entry> <entry>Turn feature on</entry></row> |
926 | | * <row><entry>kern[:]</entry> <entry>1</entry> <entry>0</entry> <entry>∞</entry> <entry>Turn feature on</entry></row> |
927 | | * <row><entry>kern[5:]</entry> <entry>1</entry> <entry>5</entry> <entry>∞</entry> <entry>Turn feature on, partial</entry></row> |
928 | | * <row><entry>kern[:5]</entry> <entry>1</entry> <entry>0</entry> <entry>5</entry> <entry>Turn feature on, partial</entry></row> |
929 | | * <row><entry>kern[3:5]</entry> <entry>1</entry> <entry>3</entry> <entry>5</entry> <entry>Turn feature on, range</entry></row> |
930 | | * <row><entry>kern[3]</entry> <entry>1</entry> <entry>3</entry> <entry>3+1</entry> <entry>Turn feature on, single char</entry></row> |
931 | | * <row><entry>Mixing it all:</entry></row> |
932 | | * <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> |
933 | | * </tbody> |
934 | | * </tgroup> |
935 | | * </informaltable> |
936 | | * |
937 | | * Return value: |
938 | | * `true` if @str is successfully parsed, `false` otherwise |
939 | | * |
940 | | * Since: 0.9.5 |
941 | | **/ |
942 | | hb_bool_t |
943 | | hb_feature_from_string (const char *str, int len, |
944 | | hb_feature_t *feature) |
945 | 0 | { |
946 | 0 | hb_feature_t feat; |
947 | |
|
948 | 0 | if (len < 0) |
949 | 0 | len = strlen (str); |
950 | |
|
951 | 0 | if (likely (parse_one_feature (&str, str + len, &feat))) |
952 | 0 | { |
953 | 0 | if (feature) |
954 | 0 | *feature = feat; |
955 | 0 | return true; |
956 | 0 | } |
957 | | |
958 | 0 | if (feature) |
959 | 0 | hb_memset (feature, 0, sizeof (*feature)); |
960 | 0 | return false; |
961 | 0 | } |
962 | | |
963 | | /** |
964 | | * hb_feature_to_string: |
965 | | * @feature: an #hb_feature_t to convert |
966 | | * @buf: (array length=size) (out): output string |
967 | | * @size: the allocated size of @buf |
968 | | * |
969 | | * Converts a #hb_feature_t into a `NULL`-terminated string in the format |
970 | | * understood by hb_feature_from_string(). The client in responsible for |
971 | | * allocating big enough size for @buf, 128 bytes is more than enough. |
972 | | * |
973 | | * Note that the feature value will be omitted if it is '1', but the |
974 | | * string won't include any whitespace. |
975 | | * |
976 | | * Since: 0.9.5 |
977 | | **/ |
978 | | void |
979 | | hb_feature_to_string (hb_feature_t *feature, |
980 | | char *buf, unsigned int size) |
981 | 0 | { |
982 | 0 | if (unlikely (!size)) return; |
983 | | |
984 | 0 | char s[128]; |
985 | 0 | unsigned int len = 0; |
986 | 0 | if (feature->value == 0) |
987 | 0 | s[len++] = '-'; |
988 | 0 | hb_tag_to_string (feature->tag, s + len); |
989 | 0 | len += 4; |
990 | 0 | while (len && s[len - 1] == ' ') |
991 | 0 | len--; |
992 | 0 | if (feature->start != HB_FEATURE_GLOBAL_START || feature->end != HB_FEATURE_GLOBAL_END) |
993 | 0 | { |
994 | 0 | s[len++] = '['; |
995 | 0 | if (feature->start) |
996 | 0 | len += hb_max (0, snprintf (s + len, ARRAY_LENGTH (s) - len, "%u", feature->start)); |
997 | 0 | if (feature->end != feature->start + 1) { |
998 | 0 | s[len++] = ':'; |
999 | 0 | if (feature->end != HB_FEATURE_GLOBAL_END) |
1000 | 0 | len += hb_max (0, snprintf (s + len, ARRAY_LENGTH (s) - len, "%u", feature->end)); |
1001 | 0 | } |
1002 | 0 | s[len++] = ']'; |
1003 | 0 | } |
1004 | 0 | if (feature->value > 1) |
1005 | 0 | { |
1006 | 0 | s[len++] = '='; |
1007 | 0 | len += hb_max (0, snprintf (s + len, ARRAY_LENGTH (s) - len, "%" PRIu32, feature->value)); |
1008 | 0 | } |
1009 | 0 | assert (len < ARRAY_LENGTH (s)); |
1010 | 0 | len = hb_min (len, size - 1); |
1011 | 0 | hb_memcpy (buf, s, len); |
1012 | 0 | buf[len] = '\0'; |
1013 | 0 | } |
1014 | | |
1015 | | /* hb_variation_t */ |
1016 | | |
1017 | | static bool |
1018 | | parse_variation_value (const char **pp, const char *end, hb_variation_t *variation) |
1019 | 0 | { |
1020 | 0 | parse_char (pp, end, '='); /* Optional. */ |
1021 | 0 | double v; |
1022 | 0 | if (unlikely (!hb_parse_double (pp, end, &v))) return false; |
1023 | | |
1024 | 0 | variation->value = v; |
1025 | 0 | return true; |
1026 | 0 | } |
1027 | | |
1028 | | static bool |
1029 | | parse_one_variation (const char **pp, const char *end, hb_variation_t *variation) |
1030 | 0 | { |
1031 | 0 | return parse_tag (pp, end, &variation->tag) && |
1032 | 0 | parse_variation_value (pp, end, variation) && |
1033 | 0 | parse_space (pp, end) && |
1034 | 0 | *pp == end; |
1035 | 0 | } |
1036 | | |
1037 | | /** |
1038 | | * hb_variation_from_string: |
1039 | | * @str: (array length=len) (element-type uint8_t): a string to parse |
1040 | | * @len: length of @str, or -1 if string is `NULL` terminated |
1041 | | * @variation: (out): the #hb_variation_t to initialize with the parsed values |
1042 | | * |
1043 | | * Parses a string into a #hb_variation_t. |
1044 | | * |
1045 | | * The format for specifying variation settings follows. All valid CSS |
1046 | | * font-variation-settings values other than 'normal' and 'inherited' are also |
1047 | | * accepted, though, not documented below. |
1048 | | * |
1049 | | * The format is a tag, optionally followed by an equals sign, followed by a |
1050 | | * number. For example `wght=500`, or `slnt=-7.5`. |
1051 | | * |
1052 | | * Return value: |
1053 | | * `true` if @str is successfully parsed, `false` otherwise |
1054 | | * |
1055 | | * Since: 1.4.2 |
1056 | | */ |
1057 | | hb_bool_t |
1058 | | hb_variation_from_string (const char *str, int len, |
1059 | | hb_variation_t *variation) |
1060 | 0 | { |
1061 | 0 | hb_variation_t var; |
1062 | |
|
1063 | 0 | if (len < 0) |
1064 | 0 | len = strlen (str); |
1065 | |
|
1066 | 0 | if (likely (parse_one_variation (&str, str + len, &var))) |
1067 | 0 | { |
1068 | 0 | if (variation) |
1069 | 0 | *variation = var; |
1070 | 0 | return true; |
1071 | 0 | } |
1072 | | |
1073 | 0 | if (variation) |
1074 | 0 | hb_memset (variation, 0, sizeof (*variation)); |
1075 | 0 | return false; |
1076 | 0 | } |
1077 | | |
1078 | | #ifndef HB_NO_SETLOCALE |
1079 | | |
1080 | | static inline void free_static_C_locale (); |
1081 | | |
1082 | | static struct hb_C_locale_lazy_loader_t : hb_lazy_loader_t<hb_remove_pointer<hb_locale_t>, |
1083 | | hb_C_locale_lazy_loader_t> |
1084 | | { |
1085 | | static hb_locale_t create () |
1086 | 0 | { |
1087 | 0 | hb_locale_t l = newlocale (LC_ALL_MASK, "C", NULL); |
1088 | 0 | if (!l) |
1089 | 0 | return l; |
1090 | | |
1091 | 0 | hb_atexit (free_static_C_locale); |
1092 | |
|
1093 | 0 | return l; |
1094 | 0 | } |
1095 | | static void destroy (hb_locale_t l) |
1096 | 0 | { |
1097 | 0 | freelocale (l); |
1098 | 0 | } |
1099 | | static hb_locale_t get_null () |
1100 | 0 | { |
1101 | 0 | return (hb_locale_t) 0; |
1102 | 0 | } |
1103 | | } static_C_locale; |
1104 | | |
1105 | | static inline |
1106 | | void free_static_C_locale () |
1107 | 0 | { |
1108 | 0 | static_C_locale.free_instance (); |
1109 | 0 | } |
1110 | | |
1111 | | static hb_locale_t |
1112 | | get_C_locale () |
1113 | 0 | { |
1114 | 0 | return static_C_locale.get_unconst (); |
1115 | 0 | } |
1116 | | |
1117 | | #endif |
1118 | | |
1119 | | /** |
1120 | | * hb_variation_to_string: |
1121 | | * @variation: an #hb_variation_t to convert |
1122 | | * @buf: (array length=size) (out caller-allocates): output string |
1123 | | * @size: the allocated size of @buf |
1124 | | * |
1125 | | * Converts an #hb_variation_t into a `NULL`-terminated string in the format |
1126 | | * understood by hb_variation_from_string(). The client in responsible for |
1127 | | * allocating big enough size for @buf, 128 bytes is more than enough. |
1128 | | * |
1129 | | * Note that the string won't include any whitespace. |
1130 | | * |
1131 | | * Since: 1.4.2 |
1132 | | */ |
1133 | | void |
1134 | | hb_variation_to_string (hb_variation_t *variation, |
1135 | | char *buf, unsigned int size) |
1136 | 0 | { |
1137 | 0 | if (unlikely (!size)) return; |
1138 | | |
1139 | 0 | char s[128]; |
1140 | 0 | unsigned int len = 0; |
1141 | 0 | hb_tag_to_string (variation->tag, s + len); |
1142 | 0 | len += 4; |
1143 | 0 | while (len && s[len - 1] == ' ') |
1144 | 0 | len--; |
1145 | 0 | s[len++] = '='; |
1146 | |
|
1147 | 0 | hb_locale_t oldlocale HB_UNUSED; |
1148 | 0 | oldlocale = hb_uselocale (get_C_locale ()); |
1149 | 0 | len += hb_max (0, snprintf (s + len, ARRAY_LENGTH (s) - len, "%g", (double) variation->value)); |
1150 | 0 | (void) hb_uselocale (oldlocale); |
1151 | |
|
1152 | 0 | assert (len < ARRAY_LENGTH (s)); |
1153 | 0 | len = hb_min (len, size - 1); |
1154 | 0 | hb_memcpy (buf, s, len); |
1155 | 0 | buf[len] = '\0'; |
1156 | 0 | } |
1157 | | |
1158 | | /** |
1159 | | * hb_color_get_alpha: |
1160 | | * @color: an #hb_color_t we are interested in its channels. |
1161 | | * |
1162 | | * Fetches the alpha channel of the given @color. |
1163 | | * |
1164 | | * Return value: Alpha channel value |
1165 | | * |
1166 | | * Since: 2.1.0 |
1167 | | */ |
1168 | | uint8_t |
1169 | | (hb_color_get_alpha) (hb_color_t color) |
1170 | 0 | { |
1171 | 0 | return hb_color_get_alpha (color); |
1172 | 0 | } |
1173 | | |
1174 | | /** |
1175 | | * hb_color_get_red: |
1176 | | * @color: an #hb_color_t we are interested in its channels. |
1177 | | * |
1178 | | * Fetches the red channel of the given @color. |
1179 | | * |
1180 | | * Return value: Red channel value |
1181 | | * |
1182 | | * Since: 2.1.0 |
1183 | | */ |
1184 | | uint8_t |
1185 | | (hb_color_get_red) (hb_color_t color) |
1186 | 0 | { |
1187 | 0 | return hb_color_get_red (color); |
1188 | 0 | } |
1189 | | |
1190 | | /** |
1191 | | * hb_color_get_green: |
1192 | | * @color: an #hb_color_t we are interested in its channels. |
1193 | | * |
1194 | | * Fetches the green channel of the given @color. |
1195 | | * |
1196 | | * Return value: Green channel value |
1197 | | * |
1198 | | * Since: 2.1.0 |
1199 | | */ |
1200 | | uint8_t |
1201 | | (hb_color_get_green) (hb_color_t color) |
1202 | 0 | { |
1203 | 0 | return hb_color_get_green (color); |
1204 | 0 | } |
1205 | | |
1206 | | /** |
1207 | | * hb_color_get_blue: |
1208 | | * @color: an #hb_color_t we are interested in its channels. |
1209 | | * |
1210 | | * Fetches the blue channel of the given @color. |
1211 | | * |
1212 | | * Return value: Blue channel value |
1213 | | * |
1214 | | * Since: 2.1.0 |
1215 | | */ |
1216 | | uint8_t |
1217 | | (hb_color_get_blue) (hb_color_t color) |
1218 | 0 | { |
1219 | 0 | return hb_color_get_blue (color); |
1220 | 0 | } |
1221 | | |
1222 | | /** |
1223 | | * hb_malloc: |
1224 | | * @size: The size of the memory to allocate. |
1225 | | * |
1226 | | * Allocates @size bytes of memory, using the allocator set at |
1227 | | * compile-time. Typically just malloc(). |
1228 | | * |
1229 | | * Return value: A pointer to the allocated memory. |
1230 | | * |
1231 | | * Since: 11.0.0 |
1232 | | **/ |
1233 | 50.9k | void* hb_malloc(size_t size) { return hb_malloc_impl (size); } |
1234 | | |
1235 | | /** |
1236 | | * hb_calloc: |
1237 | | * @nmemb: The number of elements to allocate. |
1238 | | * @size: The size of each element. |
1239 | | * |
1240 | | * Allocates @nmemb elements of @size bytes each, initialized to zero, |
1241 | | * using the allocator set at compile-time. Typically just calloc(). |
1242 | | * |
1243 | | * Return value: A pointer to the allocated memory. |
1244 | | * |
1245 | | * Since: 11.0.0 |
1246 | | **/ |
1247 | 424k | void* hb_calloc(size_t nmemb, size_t size) { return hb_calloc_impl (nmemb, size); } |
1248 | | |
1249 | | /** |
1250 | | * hb_realloc: |
1251 | | * @ptr: The pointer to the memory to reallocate. |
1252 | | * @size: The new size of the memory. |
1253 | | * |
1254 | | * Reallocates the memory pointed to by @ptr to @size bytes, using the |
1255 | | * allocator set at compile-time. Typically just realloc(). |
1256 | | * |
1257 | | * Return value: A pointer to the reallocated memory. |
1258 | | * |
1259 | | * Since: 11.0.0 |
1260 | | **/ |
1261 | 211k | void* hb_realloc(void *ptr, size_t size) { return hb_realloc_impl (ptr, size); } |
1262 | | |
1263 | | /** |
1264 | | * hb_free: |
1265 | | * @ptr: The pointer to the memory to free. |
1266 | | * |
1267 | | * Frees the memory pointed to by @ptr, using the allocator set at |
1268 | | * compile-time. Typically just free(). |
1269 | | * |
1270 | | * Since: 11.0.0 |
1271 | | **/ |
1272 | 1.33M | void hb_free(void *ptr) { hb_free_impl (ptr); } |
1273 | | |
1274 | | |
1275 | | /* If there is no visibility control, then hb-static.cc will NOT |
1276 | | * define anything. Instead, we get it to define one set in here |
1277 | | * only, so only libharfbuzz.so defines them, not other libs. */ |
1278 | | #ifdef HB_NO_VISIBILITY |
1279 | | #undef HB_NO_VISIBILITY |
1280 | | #include "hb-static.cc" |
1281 | | #define HB_NO_VISIBILITY 1 |
1282 | | #endif |