Coverage Report

Created: 2025-11-16 07:45

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/pango/subprojects/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_options_t */
44
45
hb_atomic_int_t _hb_options;
46
47
void
48
_hb_options_init ()
49
0
{
50
0
  hb_options_union_t u;
51
0
  u.i = 0;
52
0
  u.opts.initialized = true;
53
54
0
  const char *c = getenv ("HB_OPTIONS");
55
0
  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
0
  _hb_options = u.i;
77
0
}
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
0
{
99
0
  char tag[4];
100
0
  unsigned int i;
101
102
0
  if (!str || !len || !*str)
103
0
    return HB_TAG_NONE;
104
105
0
  if (len < 0 || len > 4)
106
0
    len = 4;
107
0
  for (i = 0; i < (unsigned) len && str[i]; i++)
108
0
    tag[i] = str[i];
109
0
  for (; i < 4; i++)
110
0
    tag[i] = ' ';
111
112
0
  return HB_TAG (tag[0], tag[1], tag[2], tag[3]);
113
0
}
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
0
{
219
0
  const unsigned char *p1 = (const unsigned char *) v1;
220
0
  const unsigned char *p2 = (const unsigned char *) v2;
221
222
0
  while (*p1 && *p1 == canon_map[*p2]) {
223
0
    p1++;
224
0
    p2++;
225
0
  }
226
227
0
  return *p1 == canon_map[*p2];
228
0
}
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
0
  { return lang_equal (lang, s); }
254
255
  hb_language_item_t & operator = (const char *s)
256
0
  {
257
    /* We can't call strdup(), because we allow custom allocators. */
258
0
    size_t len = strlen(s) + 1;
259
0
    lang = (hb_language_t) hb_malloc(len);
260
0
    if (likely (lang))
261
0
    {
262
0
      hb_memcpy((unsigned char *) lang, s, len);
263
0
      for (unsigned char *p = (unsigned char *) lang; *p; p++)
264
0
  *p = canon_map[*p];
265
0
    }
266
267
0
    return *this;
268
0
  }
269
270
0
  void fini () { hb_free ((void *) lang); }
271
};
272
273
274
/* Thread-safe lockfree language list */
275
276
static hb_atomic_ptr_t <hb_language_item_t> langs;
277
278
static inline void
279
free_langs ()
280
0
{
281
0
retry:
282
0
  hb_language_item_t *first_lang = langs;
283
0
  if (unlikely (!langs.cmpexch (first_lang, nullptr)))
284
0
    goto retry;
285
286
0
  while (first_lang) {
287
0
    hb_language_item_t *next = first_lang->next;
288
0
    first_lang->fini ();
289
0
    hb_free (first_lang);
290
0
    first_lang = next;
291
0
  }
292
0
}
293
294
static hb_language_item_t *
295
lang_find_or_insert (const char *key)
296
0
{
297
0
retry:
298
0
  hb_language_item_t *first_lang = langs;
299
300
0
  for (hb_language_item_t *lang = first_lang; lang; lang = lang->next)
301
0
    if (*lang == key)
302
0
      return lang;
303
304
  /* Not found; allocate one. */
305
0
  hb_language_item_t *lang = (hb_language_item_t *) hb_calloc (1, sizeof (hb_language_item_t));
306
0
  if (unlikely (!lang))
307
0
    return nullptr;
308
0
  lang->next = first_lang;
309
0
  *lang = key;
310
0
  if (unlikely (!lang->lang))
311
0
  {
312
0
    hb_free (lang);
313
0
    return nullptr;
314
0
  }
315
316
0
  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
0
  if (!first_lang)
324
0
    hb_atexit (free_langs); /* First person registers atexit() callback. */
325
326
0
  return lang;
327
0
}
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
0
{
347
0
  if (!str || !len || !*str)
348
0
    return HB_LANGUAGE_INVALID;
349
350
0
  hb_language_item_t *item = nullptr;
351
0
  if (len >= 0)
352
0
  {
353
    /* NUL-terminate it. */
354
0
    char strbuf[64];
355
0
    len = hb_min (len, (int) sizeof (strbuf) - 1);
356
0
    hb_memcpy (strbuf, str, len);
357
0
    strbuf[len] = '\0';
358
0
    item = lang_find_or_insert (strbuf);
359
0
  }
360
0
  else
361
0
    item = lang_find_or_insert (str);
362
363
0
  return likely (item) ? item->lang : HB_LANGUAGE_INVALID;
364
0
}
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
0
{
381
0
  if (unlikely (!language)) return nullptr;
382
383
0
  return language->s;
384
0
}
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
0
{
406
0
  static hb_atomic_ptr_t <hb_language_t> default_language;
407
408
0
  hb_language_t language = default_language;
409
0
  if (unlikely (language == HB_LANGUAGE_INVALID))
410
0
  {
411
0
    language = hb_language_from_string (hb_setlocale (LC_CTYPE, nullptr), -1);
412
0
    (void) default_language.cmpexch (HB_LANGUAGE_INVALID, language);
413
0
  }
414
415
0
  return language;
416
0
}
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
0
{
435
0
  if (language == specific) return true;
436
0
  if (!language || !specific) return false;
437
438
0
  const char *l = language->s;
439
0
  const char *s = specific->s;
440
0
  unsigned ll = strlen (l);
441
0
  unsigned sl = strlen (s);
442
443
0
  if (ll > sl)
444
0
    return false;
445
446
0
  return strncmp (l, s, ll) == 0 &&
447
0
   (s[ll] == '\0' || s[ll] == '-');
448
0
}
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.  Scripts that can be written either
549
 * horizontally or vertically will return #HB_DIRECTION_INVALID.
550
 * Unknown scripts will return #HB_DIRECTION_LTR.
551
 *
552
 * Return value: The horizontal #hb_direction_t of @script
553
 *
554
 * Since: 0.9.2
555
 **/
556
hb_direction_t
557
hb_script_get_horizontal_direction (hb_script_t script)
558
0
{
559
  /* https://docs.google.com/spreadsheets/d/1Y90M0Ie3MUJ6UVCRDOypOtijlMDLNNyyLk36T6iMu0o */
560
0
  switch ((hb_tag_t) script)
561
0
  {
562
    /* Unicode-1.1 additions */
563
0
    case HB_SCRIPT_ARABIC:
564
0
    case HB_SCRIPT_HEBREW:
565
566
    /* Unicode-3.0 additions */
567
0
    case HB_SCRIPT_SYRIAC:
568
0
    case HB_SCRIPT_THAANA:
569
570
    /* Unicode-4.0 additions */
571
0
    case HB_SCRIPT_CYPRIOT:
572
573
    /* Unicode-4.1 additions */
574
0
    case HB_SCRIPT_KHAROSHTHI:
575
576
    /* Unicode-5.0 additions */
577
0
    case HB_SCRIPT_PHOENICIAN:
578
0
    case HB_SCRIPT_NKO:
579
580
    /* Unicode-5.1 additions */
581
0
    case HB_SCRIPT_LYDIAN:
582
583
    /* Unicode-5.2 additions */
584
0
    case HB_SCRIPT_AVESTAN:
585
0
    case HB_SCRIPT_IMPERIAL_ARAMAIC:
586
0
    case HB_SCRIPT_INSCRIPTIONAL_PAHLAVI:
587
0
    case HB_SCRIPT_INSCRIPTIONAL_PARTHIAN:
588
0
    case HB_SCRIPT_OLD_SOUTH_ARABIAN:
589
0
    case HB_SCRIPT_OLD_TURKIC:
590
0
    case HB_SCRIPT_SAMARITAN:
591
592
    /* Unicode-6.0 additions */
593
0
    case HB_SCRIPT_MANDAIC:
594
595
    /* Unicode-6.1 additions */
596
0
    case HB_SCRIPT_MEROITIC_CURSIVE:
597
0
    case HB_SCRIPT_MEROITIC_HIEROGLYPHS:
598
599
    /* Unicode-7.0 additions */
600
0
    case HB_SCRIPT_MANICHAEAN:
601
0
    case HB_SCRIPT_MENDE_KIKAKUI:
602
0
    case HB_SCRIPT_NABATAEAN:
603
0
    case HB_SCRIPT_OLD_NORTH_ARABIAN:
604
0
    case HB_SCRIPT_PALMYRENE:
605
0
    case HB_SCRIPT_PSALTER_PAHLAVI:
606
607
    /* Unicode-8.0 additions */
608
0
    case HB_SCRIPT_HATRAN:
609
610
    /* Unicode-9.0 additions */
611
0
    case HB_SCRIPT_ADLAM:
612
613
    /* Unicode-11.0 additions */
614
0
    case HB_SCRIPT_HANIFI_ROHINGYA:
615
0
    case HB_SCRIPT_OLD_SOGDIAN:
616
0
    case HB_SCRIPT_SOGDIAN:
617
618
    /* Unicode-12.0 additions */
619
0
    case HB_SCRIPT_ELYMAIC:
620
621
    /* Unicode-13.0 additions */
622
0
    case HB_SCRIPT_CHORASMIAN:
623
0
    case HB_SCRIPT_YEZIDI:
624
625
    /* Unicode-14.0 additions */
626
0
    case HB_SCRIPT_OLD_UYGHUR:
627
628
0
      return HB_DIRECTION_RTL;
629
630
631
    /* https://github.com/harfbuzz/harfbuzz/issues/1000 */
632
0
    case HB_SCRIPT_OLD_HUNGARIAN:
633
0
    case HB_SCRIPT_OLD_ITALIC:
634
0
    case HB_SCRIPT_RUNIC:
635
0
    case HB_SCRIPT_TIFINAGH:
636
637
0
      return HB_DIRECTION_INVALID;
638
0
  }
639
640
0
  return HB_DIRECTION_LTR;
641
0
}
642
643
644
/* hb_version */
645
646
647
/**
648
 * SECTION:hb-version
649
 * @title: hb-version
650
 * @short_description: Information about the version of HarfBuzz in use
651
 * @include: hb.h
652
 *
653
 * These functions and macros allow accessing version of the HarfBuzz
654
 * library used at compile- as well as run-time, and to direct code
655
 * conditionally based on those versions, again, at compile- or run-time.
656
 **/
657
658
659
/**
660
 * hb_version:
661
 * @major: (out): Library major version component
662
 * @minor: (out): Library minor version component
663
 * @micro: (out): Library micro version component
664
 *
665
 * Returns library version as three integer components.
666
 *
667
 * Since: 0.9.2
668
 **/
669
void
670
hb_version (unsigned int *major,
671
      unsigned int *minor,
672
      unsigned int *micro)
673
0
{
674
0
  *major = HB_VERSION_MAJOR;
675
0
  *minor = HB_VERSION_MINOR;
676
0
  *micro = HB_VERSION_MICRO;
677
0
}
678
679
/**
680
 * hb_version_string:
681
 *
682
 * Returns library version as a string with three components.
683
 *
684
 * Return value: Library version string
685
 *
686
 * Since: 0.9.2
687
 **/
688
const char *
689
hb_version_string ()
690
0
{
691
0
  return HB_VERSION_STRING;
692
0
}
693
694
/**
695
 * hb_version_atleast:
696
 * @major: Library major version component
697
 * @minor: Library minor version component
698
 * @micro: Library micro version component
699
 *
700
 * Tests the library version against a minimum value,
701
 * as three integer components.
702
 *
703
 * Return value: `true` if the library is equal to or greater than
704
 * the test value, `false` otherwise
705
 *
706
 * Since: 0.9.30
707
 **/
708
hb_bool_t
709
hb_version_atleast (unsigned int major,
710
        unsigned int minor,
711
        unsigned int micro)
712
0
{
713
0
  return HB_VERSION_ATLEAST (major, minor, micro);
714
0
}
715
716
717
718
/* hb_feature_t and hb_variation_t */
719
720
static bool
721
parse_space (const char **pp, const char *end)
722
0
{
723
0
  while (*pp < end && ISSPACE (**pp))
724
0
    (*pp)++;
725
0
  return true;
726
0
}
727
728
static bool
729
parse_char (const char **pp, const char *end, char c)
730
0
{
731
0
  parse_space (pp, end);
732
733
0
  if (*pp == end || **pp != c)
734
0
    return false;
735
736
0
  (*pp)++;
737
0
  return true;
738
0
}
739
740
static bool
741
parse_uint (const char **pp, const char *end, unsigned int *pv)
742
0
{
743
  /* Intentionally use hb_parse_int inside instead of hb_parse_uint,
744
   * such that -1 turns into "big number"... */
745
0
  int v;
746
0
  if (unlikely (!hb_parse_int (pp, end, &v))) return false;
747
748
0
  *pv = v;
749
0
  return true;
750
0
}
751
752
static bool
753
parse_uint32 (const char **pp, const char *end, uint32_t *pv)
754
0
{
755
  /* Intentionally use hb_parse_int inside instead of hb_parse_uint,
756
   * such that -1 turns into "big number"... */
757
0
  int v;
758
0
  if (unlikely (!hb_parse_int (pp, end, &v))) return false;
759
760
0
  *pv = v;
761
0
  return true;
762
0
}
763
764
static bool
765
parse_bool (const char **pp, const char *end, uint32_t *pv)
766
0
{
767
0
  parse_space (pp, end);
768
769
0
  const char *p = *pp;
770
0
  while (*pp < end && ISALPHA(**pp))
771
0
    (*pp)++;
772
773
  /* CSS allows on/off as aliases 1/0. */
774
0
  if (*pp - p == 2
775
0
      && TOLOWER (p[0]) == 'o'
776
0
      && TOLOWER (p[1]) == 'n')
777
0
    *pv = 1;
778
0
  else if (*pp - p == 3
779
0
     && TOLOWER (p[0]) == 'o'
780
0
     && TOLOWER (p[1]) == 'f'
781
0
     && TOLOWER (p[2]) == 'f')
782
0
    *pv = 0;
783
0
  else
784
0
    return false;
785
786
0
  return true;
787
0
}
788
789
/* hb_feature_t */
790
791
static bool
792
parse_feature_value_prefix (const char **pp, const char *end, hb_feature_t *feature)
793
0
{
794
0
  if (parse_char (pp, end, '-'))
795
0
    feature->value = 0;
796
0
  else {
797
0
    parse_char (pp, end, '+');
798
0
    feature->value = 1;
799
0
  }
800
801
0
  return true;
802
0
}
803
804
static bool
805
parse_tag (const char **pp, const char *end, hb_tag_t *tag)
806
0
{
807
0
  parse_space (pp, end);
808
809
0
  char quote = 0;
810
811
0
  if (*pp < end && (**pp == '\'' || **pp == '"'))
812
0
  {
813
0
    quote = **pp;
814
0
    (*pp)++;
815
0
  }
816
817
0
  const char *p = *pp;
818
0
  while (*pp < end && (**pp != ' ' && **pp != '=' && **pp != '[' && **pp != quote))
819
0
    (*pp)++;
820
821
0
  if (p == *pp || *pp - p > 4)
822
0
    return false;
823
824
0
  *tag = hb_tag_from_string (p, *pp - p);
825
826
0
  if (quote)
827
0
  {
828
    /* CSS expects exactly four bytes.  And we only allow quotations for
829
     * CSS compatibility.  So, enforce the length. */
830
0
     if (*pp - p != 4)
831
0
       return false;
832
0
    if (*pp == end || **pp != quote)
833
0
      return false;
834
0
    (*pp)++;
835
0
  }
836
837
0
  return true;
838
0
}
839
840
static bool
841
parse_feature_indices (const char **pp, const char *end, hb_feature_t *feature)
842
0
{
843
0
  parse_space (pp, end);
844
845
0
  bool has_start;
846
847
0
  feature->start = HB_FEATURE_GLOBAL_START;
848
0
  feature->end = HB_FEATURE_GLOBAL_END;
849
850
0
  if (!parse_char (pp, end, '['))
851
0
    return true;
852
853
0
  has_start = parse_uint (pp, end, &feature->start);
854
855
0
  if (parse_char (pp, end, ':') || parse_char (pp, end, ';')) {
856
0
    parse_uint (pp, end, &feature->end);
857
0
  } else {
858
0
    if (has_start)
859
0
      feature->end = feature->start + 1;
860
0
  }
861
862
0
  return parse_char (pp, end, ']');
863
0
}
864
865
static bool
866
parse_feature_value_postfix (const char **pp, const char *end, hb_feature_t *feature)
867
0
{
868
0
  bool had_equal = parse_char (pp, end, '=');
869
0
  bool had_value = parse_uint32 (pp, end, &feature->value) ||
870
0
       parse_bool (pp, end, &feature->value);
871
  /* CSS doesn't use equal-sign between tag and value.
872
   * If there was an equal-sign, then there *must* be a value.
873
   * A value without an equal-sign is ok, but not required. */
874
0
  return !had_equal || had_value;
875
0
}
876
877
static bool
878
parse_one_feature (const char **pp, const char *end, hb_feature_t *feature)
879
0
{
880
0
  return parse_feature_value_prefix (pp, end, feature) &&
881
0
   parse_tag (pp, end, &feature->tag) &&
882
0
   parse_feature_indices (pp, end, feature) &&
883
0
   parse_feature_value_postfix (pp, end, feature) &&
884
0
   parse_space (pp, end) &&
885
0
   *pp == end;
886
0
}
887
888
/**
889
 * hb_feature_from_string:
890
 * @str: (array length=len) (element-type uint8_t): a string to parse
891
 * @len: length of @str, or -1 if string is `NULL` terminated
892
 * @feature: (out): the #hb_feature_t to initialize with the parsed values
893
 *
894
 * Parses a string into a #hb_feature_t.
895
 *
896
 * The format for specifying feature strings follows. All valid CSS
897
 * font-feature-settings values other than 'normal' and the global values are
898
 * also accepted, though not documented below. CSS string escapes are not
899
 * supported.
900
 *
901
 * The range indices refer to the positions between Unicode characters. The
902
 * position before the first character is always 0.
903
 *
904
 * The format is Python-esque.  Here is how it all works:
905
 *
906
 * <informaltable pgwide='1' align='left' frame='none'>
907
 * <tgroup cols='5'>
908
 * <thead>
909
 * <row><entry>Syntax</entry>    <entry>Value</entry> <entry>Start</entry> <entry>End</entry></row>
910
 * </thead>
911
 * <tbody>
912
 * <row><entry>Setting value:</entry></row>
913
 * <row><entry>kern</entry>      <entry>1</entry>     <entry>0</entry>      <entry>∞</entry>   <entry>Turn feature on</entry></row>
914
 * <row><entry>+kern</entry>     <entry>1</entry>     <entry>0</entry>      <entry>∞</entry>   <entry>Turn feature on</entry></row>
915
 * <row><entry>-kern</entry>     <entry>0</entry>     <entry>0</entry>      <entry>∞</entry>   <entry>Turn feature off</entry></row>
916
 * <row><entry>kern=0</entry>    <entry>0</entry>     <entry>0</entry>      <entry>∞</entry>   <entry>Turn feature off</entry></row>
917
 * <row><entry>kern=1</entry>    <entry>1</entry>     <entry>0</entry>      <entry>∞</entry>   <entry>Turn feature on</entry></row>
918
 * <row><entry>aalt=2</entry>    <entry>2</entry>     <entry>0</entry>      <entry>∞</entry>   <entry>Choose 2nd alternate</entry></row>
919
 * <row><entry>Setting index:</entry></row>
920
 * <row><entry>kern[]</entry>    <entry>1</entry>     <entry>0</entry>      <entry>∞</entry>   <entry>Turn feature on</entry></row>
921
 * <row><entry>kern[:]</entry>   <entry>1</entry>     <entry>0</entry>      <entry>∞</entry>   <entry>Turn feature on</entry></row>
922
 * <row><entry>kern[5:]</entry>  <entry>1</entry>     <entry>5</entry>      <entry>∞</entry>   <entry>Turn feature on, partial</entry></row>
923
 * <row><entry>kern[:5]</entry>  <entry>1</entry>     <entry>0</entry>      <entry>5</entry>   <entry>Turn feature on, partial</entry></row>
924
 * <row><entry>kern[3:5]</entry> <entry>1</entry>     <entry>3</entry>      <entry>5</entry>   <entry>Turn feature on, range</entry></row>
925
 * <row><entry>kern[3]</entry>   <entry>1</entry>     <entry>3</entry>      <entry>3+1</entry> <entry>Turn feature on, single char</entry></row>
926
 * <row><entry>Mixing it all:</entry></row>
927
 * <row><entry>aalt[3:5]=2</entry> <entry>2</entry>   <entry>3</entry>      <entry>5</entry>   <entry>Turn 2nd alternate on for range</entry></row>
928
 * </tbody>
929
 * </tgroup>
930
 * </informaltable>
931
 *
932
 * Return value:
933
 * `true` if @str is successfully parsed, `false` otherwise
934
 *
935
 * Since: 0.9.5
936
 **/
937
hb_bool_t
938
hb_feature_from_string (const char *str, int len,
939
      hb_feature_t *feature)
940
0
{
941
0
  hb_feature_t feat;
942
943
0
  if (len < 0)
944
0
    len = strlen (str);
945
946
0
  if (likely (parse_one_feature (&str, str + len, &feat)))
947
0
  {
948
0
    if (feature)
949
0
      *feature = feat;
950
0
    return true;
951
0
  }
952
953
0
  if (feature)
954
0
    hb_memset (feature, 0, sizeof (*feature));
955
0
  return false;
956
0
}
957
958
/**
959
 * hb_feature_to_string:
960
 * @feature: an #hb_feature_t to convert
961
 * @buf: (array length=size) (out): output string
962
 * @size: the allocated size of @buf
963
 *
964
 * Converts a #hb_feature_t into a `NULL`-terminated string in the format
965
 * understood by hb_feature_from_string(). The client in responsible for
966
 * allocating big enough size for @buf, 128 bytes is more than enough.
967
 *
968
 * Since: 0.9.5
969
 **/
970
void
971
hb_feature_to_string (hb_feature_t *feature,
972
          char *buf, unsigned int size)
973
0
{
974
0
  if (unlikely (!size)) return;
975
976
0
  char s[128];
977
0
  unsigned int len = 0;
978
0
  if (feature->value == 0)
979
0
    s[len++] = '-';
980
0
  hb_tag_to_string (feature->tag, s + len);
981
0
  len += 4;
982
0
  while (len && s[len - 1] == ' ')
983
0
    len--;
984
0
  if (feature->start != HB_FEATURE_GLOBAL_START || feature->end != HB_FEATURE_GLOBAL_END)
985
0
  {
986
0
    s[len++] = '[';
987
0
    if (feature->start)
988
0
      len += hb_max (0, snprintf (s + len, ARRAY_LENGTH (s) - len, "%u", feature->start));
989
0
    if (feature->end != feature->start + 1) {
990
0
      s[len++] = ':';
991
0
      if (feature->end != HB_FEATURE_GLOBAL_END)
992
0
  len += hb_max (0, snprintf (s + len, ARRAY_LENGTH (s) - len, "%u", feature->end));
993
0
    }
994
0
    s[len++] = ']';
995
0
  }
996
0
  if (feature->value > 1)
997
0
  {
998
0
    s[len++] = '=';
999
0
    len += hb_max (0, snprintf (s + len, ARRAY_LENGTH (s) - len, "%" PRIu32, feature->value));
1000
0
  }
1001
0
  assert (len < ARRAY_LENGTH (s));
1002
0
  len = hb_min (len, size - 1);
1003
0
  hb_memcpy (buf, s, len);
1004
0
  buf[len] = '\0';
1005
0
}
1006
1007
/* hb_variation_t */
1008
1009
static bool
1010
parse_variation_value (const char **pp, const char *end, hb_variation_t *variation)
1011
0
{
1012
0
  parse_char (pp, end, '='); /* Optional. */
1013
0
  double v;
1014
0
  if (unlikely (!hb_parse_double (pp, end, &v))) return false;
1015
1016
0
  variation->value = v;
1017
0
  return true;
1018
0
}
1019
1020
static bool
1021
parse_one_variation (const char **pp, const char *end, hb_variation_t *variation)
1022
0
{
1023
0
  return parse_tag (pp, end, &variation->tag) &&
1024
0
   parse_variation_value (pp, end, variation) &&
1025
0
   parse_space (pp, end) &&
1026
0
   *pp == end;
1027
0
}
1028
1029
/**
1030
 * hb_variation_from_string:
1031
 * @str: (array length=len) (element-type uint8_t): a string to parse
1032
 * @len: length of @str, or -1 if string is `NULL` terminated
1033
 * @variation: (out): the #hb_variation_t to initialize with the parsed values
1034
 *
1035
 * Parses a string into a #hb_variation_t.
1036
 *
1037
 * The format for specifying variation settings follows. All valid CSS
1038
 * font-variation-settings values other than 'normal' and 'inherited' are also
1039
 * accepted, though, not documented below.
1040
 *
1041
 * The format is a tag, optionally followed by an equals sign, followed by a
1042
 * number. For example `wght=500`, or `slnt=-7.5`.
1043
 *
1044
 * Return value:
1045
 * `true` if @str is successfully parsed, `false` otherwise
1046
 *
1047
 * Since: 1.4.2
1048
 */
1049
hb_bool_t
1050
hb_variation_from_string (const char *str, int len,
1051
        hb_variation_t *variation)
1052
0
{
1053
0
  hb_variation_t var;
1054
1055
0
  if (len < 0)
1056
0
    len = strlen (str);
1057
1058
0
  if (likely (parse_one_variation (&str, str + len, &var)))
1059
0
  {
1060
0
    if (variation)
1061
0
      *variation = var;
1062
0
    return true;
1063
0
  }
1064
1065
0
  if (variation)
1066
0
    hb_memset (variation, 0, sizeof (*variation));
1067
0
  return false;
1068
0
}
1069
1070
#ifndef HB_NO_SETLOCALE
1071
1072
static inline void free_static_C_locale ();
1073
1074
static struct hb_C_locale_lazy_loader_t : hb_lazy_loader_t<hb_remove_pointer<hb_locale_t>,
1075
                 hb_C_locale_lazy_loader_t>
1076
{
1077
  static hb_locale_t create ()
1078
0
  {
1079
0
    hb_locale_t l = newlocale (LC_ALL_MASK, "C", NULL);
1080
0
    if (!l)
1081
0
      return l;
1082
1083
0
    hb_atexit (free_static_C_locale);
1084
1085
0
    return l;
1086
0
  }
1087
  static void destroy (hb_locale_t l)
1088
0
  {
1089
0
    freelocale (l);
1090
0
  }
1091
  static hb_locale_t get_null ()
1092
0
  {
1093
0
    return (hb_locale_t) 0;
1094
0
  }
1095
} static_C_locale;
1096
1097
static inline
1098
void free_static_C_locale ()
1099
0
{
1100
0
  static_C_locale.free_instance ();
1101
0
}
1102
1103
static hb_locale_t
1104
get_C_locale ()
1105
0
{
1106
0
  return static_C_locale.get_unconst ();
1107
0
}
1108
1109
#endif
1110
1111
/**
1112
 * hb_variation_to_string:
1113
 * @variation: an #hb_variation_t to convert
1114
 * @buf: (array length=size) (out caller-allocates): output string
1115
 * @size: the allocated size of @buf
1116
 *
1117
 * Converts an #hb_variation_t into a `NULL`-terminated string in the format
1118
 * understood by hb_variation_from_string(). The client in responsible for
1119
 * allocating big enough size for @buf, 128 bytes is more than enough.
1120
 *
1121
 * Since: 1.4.2
1122
 */
1123
void
1124
hb_variation_to_string (hb_variation_t *variation,
1125
      char *buf, unsigned int size)
1126
0
{
1127
0
  if (unlikely (!size)) return;
1128
1129
0
  char s[128];
1130
0
  unsigned int len = 0;
1131
0
  hb_tag_to_string (variation->tag, s + len);
1132
0
  len += 4;
1133
0
  while (len && s[len - 1] == ' ')
1134
0
    len--;
1135
0
  s[len++] = '=';
1136
1137
0
  hb_locale_t oldlocale HB_UNUSED;
1138
0
  oldlocale = hb_uselocale (get_C_locale ());
1139
0
  len += hb_max (0, snprintf (s + len, ARRAY_LENGTH (s) - len, "%g", (double) variation->value));
1140
0
  (void) hb_uselocale (oldlocale);
1141
1142
0
  assert (len < ARRAY_LENGTH (s));
1143
0
  len = hb_min (len, size - 1);
1144
0
  hb_memcpy (buf, s, len);
1145
0
  buf[len] = '\0';
1146
0
}
1147
1148
/**
1149
 * hb_color_get_alpha:
1150
 * @color: an #hb_color_t we are interested in its channels.
1151
 *
1152
 * Fetches the alpha channel of the given @color.
1153
 *
1154
 * Return value: Alpha channel value
1155
 *
1156
 * Since: 2.1.0
1157
 */
1158
uint8_t
1159
(hb_color_get_alpha) (hb_color_t color)
1160
0
{
1161
0
  return hb_color_get_alpha (color);
1162
0
}
1163
1164
/**
1165
 * hb_color_get_red:
1166
 * @color: an #hb_color_t we are interested in its channels.
1167
 *
1168
 * Fetches the red channel of the given @color.
1169
 *
1170
 * Return value: Red channel value
1171
 *
1172
 * Since: 2.1.0
1173
 */
1174
uint8_t
1175
(hb_color_get_red) (hb_color_t color)
1176
0
{
1177
0
  return hb_color_get_red (color);
1178
0
}
1179
1180
/**
1181
 * hb_color_get_green:
1182
 * @color: an #hb_color_t we are interested in its channels.
1183
 *
1184
 * Fetches the green channel of the given @color.
1185
 *
1186
 * Return value: Green channel value
1187
 *
1188
 * Since: 2.1.0
1189
 */
1190
uint8_t
1191
(hb_color_get_green) (hb_color_t color)
1192
0
{
1193
0
  return hb_color_get_green (color);
1194
0
}
1195
1196
/**
1197
 * hb_color_get_blue:
1198
 * @color: an #hb_color_t we are interested in its channels.
1199
 *
1200
 * Fetches the blue channel of the given @color.
1201
 *
1202
 * Return value: Blue channel value
1203
 *
1204
 * Since: 2.1.0
1205
 */
1206
uint8_t
1207
(hb_color_get_blue) (hb_color_t color)
1208
0
{
1209
0
  return hb_color_get_blue (color);
1210
0
}
1211
1212
1213
/* If there is no visibility control, then hb-static.cc will NOT
1214
 * define anything.  Instead, we get it to define one set in here
1215
 * only, so only libharfbuzz.so defines them, not other libs. */
1216
#ifdef HB_NO_VISIBILITY
1217
#undef HB_NO_VISIBILITY
1218
#include "hb-static.cc"
1219
#define HB_NO_VISIBILITY 1
1220
#endif