Coverage Report

Created: 2025-07-23 07:04

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