hb-common.cc 22.7 KB
Newer Older
B
Behdad Esfahbod 已提交
1
/*
B
Behdad Esfahbod 已提交
2
 * Copyright © 2009,2010  Red Hat, Inc.
3
 * Copyright © 2011,2012  Google, Inc.
B
Behdad Esfahbod 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 *  This is part of HarfBuzz, a text shaping library.
 *
 * Permission is hereby granted, without written agreement and without
 * license or royalty fees, to use, copy, modify, and distribute this
 * software and its documentation for any purpose, provided that the
 * above copyright notice and the following two paragraphs appear in
 * all copies of this software.
 *
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 *
 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 *
 * Red Hat Author(s): Behdad Esfahbod
B
Behdad Esfahbod 已提交
26
 * Google Author(s): Behdad Esfahbod
B
Behdad Esfahbod 已提交
27 28
 */

29
#include "hb-private.hh"
B
Behdad Esfahbod 已提交
30

31
#include "hb-mutex-private.hh"
B
Behdad Esfahbod 已提交
32
#include "hb-object-private.hh"
33

34
#include <locale.h>
35 36 37
#ifdef HAVE_XLOCALE_H
#include <xlocale.h>
#endif
38

B
Behdad Esfahbod 已提交
39

40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
/* hb_options_t */

hb_options_union_t _hb_options;

void
_hb_options_init (void)
{
  hb_options_union_t u;
  u.i = 0;
  u.opts.initialized = 1;

  char *c = getenv ("HB_OPTIONS");
  u.opts.uniscribe_bug_compatible = c && strstr (c, "uniscribe-bug-compatible");

  /* This is idempotent and threadsafe. */
  _hb_options = u;
}

B
Behdad Esfahbod 已提交
58

59 60
/* hb_tag_t */

61 62
/**
 * hb_tag_from_string:
63
 * @str: (array length=len) (element-type uint8_t): 
64 65 66 67 68 69
 * @len: 
 *
 * 
 *
 * Return value: 
 *
B
Behdad Esfahbod 已提交
70
 * Since: 0.9.2
71
 **/
B
Behdad Esfahbod 已提交
72
hb_tag_t
73
hb_tag_from_string (const char *str, int len)
B
Behdad Esfahbod 已提交
74 75 76 77
{
  char tag[4];
  unsigned int i;

78
  if (!str || !len || !*str)
79 80
    return HB_TAG_NONE;

81 82
  if (len < 0 || len > 4)
    len = 4;
83 84
  for (i = 0; i < (unsigned) len && str[i]; i++)
    tag[i] = str[i];
B
Behdad Esfahbod 已提交
85 86 87
  for (; i < 4; i++)
    tag[i] = ' ';

B
Behdad Esfahbod 已提交
88
  return HB_TAG (tag[0], tag[1], tag[2], tag[3]);
B
Behdad Esfahbod 已提交
89
}
B
Behdad Esfahbod 已提交
90

91 92 93
/**
 * hb_tag_to_string:
 * @tag: 
94
 * @buf: (out caller-allocates) (array fixed-size=4) (element-type uint8_t): 
95 96 97
 *
 * 
 *
S
Sascha Brawer 已提交
98
 * Since: 0.9.5
99
 **/
100 101 102 103 104 105 106 107 108
void
hb_tag_to_string (hb_tag_t tag, char *buf)
{
  buf[0] = (char) (uint8_t) (tag >> 24);
  buf[1] = (char) (uint8_t) (tag >> 16);
  buf[2] = (char) (uint8_t) (tag >>  8);
  buf[3] = (char) (uint8_t) (tag >>  0);
}

B
Behdad Esfahbod 已提交
109

110 111 112 113 114 115 116 117 118
/* hb_direction_t */

const char direction_strings[][4] = {
  "ltr",
  "rtl",
  "ttb",
  "btt"
};

119 120
/**
 * hb_direction_from_string:
121
 * @str: (array length=len) (element-type uint8_t): 
122 123 124 125 126 127
 * @len: 
 *
 * 
 *
 * Return value: 
 *
B
Behdad Esfahbod 已提交
128
 * Since: 0.9.2
129
 **/
130
hb_direction_t
131
hb_direction_from_string (const char *str, int len)
132
{
133
  if (unlikely (!str || !len || !*str))
134 135 136 137 138 139 140 141
    return HB_DIRECTION_INVALID;

  /* Lets match loosely: just match the first letter, such that
   * all of "ltr", "left-to-right", etc work!
   */
  char c = TOLOWER (str[0]);
  for (unsigned int i = 0; i < ARRAY_LENGTH (direction_strings); i++)
    if (c == direction_strings[i][0])
142
      return (hb_direction_t) (HB_DIRECTION_LTR + i);
143 144 145 146

  return HB_DIRECTION_INVALID;
}

147 148 149 150 151 152 153 154
/**
 * hb_direction_to_string:
 * @direction: 
 *
 * 
 *
 * Return value: (transfer none): 
 *
B
Behdad Esfahbod 已提交
155
 * Since: 0.9.2
156
 **/
157 158 159
const char *
hb_direction_to_string (hb_direction_t direction)
{
160 161 162
  if (likely ((unsigned int) (direction - HB_DIRECTION_LTR)
	      < ARRAY_LENGTH (direction_strings)))
    return direction_strings[direction - HB_DIRECTION_LTR];
163 164 165 166 167

  return "invalid";
}


168 169
/* hb_language_t */

170
struct hb_language_impl_t {
B
Behdad Esfahbod 已提交
171 172 173
  const char s[1];
};

174 175 176 177 178 179 180 181 182 183 184
static const char canon_map[256] = {
   0,   0,   0,   0,   0,   0,   0,   0,    0,   0,   0,   0,   0,   0,   0,   0,
   0,   0,   0,   0,   0,   0,   0,   0,    0,   0,   0,   0,   0,   0,   0,   0,
   0,   0,   0,   0,   0,   0,   0,   0,    0,   0,   0,   0,   0,  '-',  0,   0,
  '0', '1', '2', '3', '4', '5', '6', '7',  '8', '9',  0,   0,   0,   0,   0,   0,
  '-', 'a', 'b', 'c', 'd', 'e', 'f', 'g',  'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
  'p', 'q', 'r', 's', 't', 'u', 'v', 'w',  'x', 'y', 'z',  0,   0,   0,   0,  '-',
   0,  'a', 'b', 'c', 'd', 'e', 'f', 'g',  'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
  'p', 'q', 'r', 's', 't', 'u', 'v', 'w',  'x', 'y', 'z',  0,   0,   0,   0,   0
};

B
Minor  
Behdad Esfahbod 已提交
185
static bool
186 187
lang_equal (hb_language_t  v1,
	    const void    *v2)
188
{
189 190
  const unsigned char *p1 = (const unsigned char *) v1;
  const unsigned char *p2 = (const unsigned char *) v2;
191

192 193 194 195
  while (*p1 && *p1 == canon_map[*p2]) {
    p1++;
    p2++;
  }
196

197
  return *p1 == canon_map[*p2];
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
}

#if 0
static unsigned int
lang_hash (const void *key)
{
  const unsigned char *p = key;
  unsigned int h = 0;
  while (canon_map[*p])
    {
      h = (h << 5) - h + canon_map[*p];
      p++;
    }

  return h;
}
#endif


217 218
struct hb_language_item_t {

219
  struct hb_language_item_t *next;
220 221 222 223 224 225 226
  hb_language_t lang;

  inline bool operator == (const char *s) const {
    return lang_equal (lang, s);
  }

  inline hb_language_item_t & operator = (const char *s) {
227 228 229 230 231 232
    /* If a custom allocated is used calling strdup() pairs
    badly with a call to the custom free() in finish() below.
    Therefore don't call strdup(), implement its behavior.
    */
    size_t len = strlen(s) + 1;
    lang = (hb_language_t) malloc(len);
233 234 235 236 237 238
    if (likely (lang))
    {
      memcpy((unsigned char *) lang, s, len);
      for (unsigned char *p = (unsigned char *) lang; *p; p++)
	*p = canon_map[*p];
    }
239 240 241 242

    return *this;
  }

B
Behdad Esfahbod 已提交
243
  void finish (void) { free ((void *) lang); }
244 245
};

246 247 248 249 250

/* Thread-safe lock-free language list */

static hb_language_item_t *langs;

251
#ifdef HB_USE_ATEXIT
252 253
static void
free_langs (void)
254 255 256 257 258 259 260 261
{
  while (langs) {
    hb_language_item_t *next = langs->next;
    langs->finish ();
    free (langs);
    langs = next;
  }
}
262
#endif
263 264 265 266 267 268 269 270 271 272 273 274 275 276

static hb_language_item_t *
lang_find_or_insert (const char *key)
{
retry:
  hb_language_item_t *first_lang = (hb_language_item_t *) hb_atomic_ptr_get (&langs);

  for (hb_language_item_t *lang = first_lang; lang; lang = lang->next)
    if (*lang == key)
      return lang;

  /* Not found; allocate one. */
  hb_language_item_t *lang = (hb_language_item_t *) calloc (1, sizeof (hb_language_item_t));
  if (unlikely (!lang))
B
Behdad Esfahbod 已提交
277
    return nullptr;
278 279
  lang->next = first_lang;
  *lang = key;
280 281 282
  if (unlikely (!lang->lang))
  {
    free (lang);
B
Behdad Esfahbod 已提交
283
    return nullptr;
284
  }
285 286

  if (!hb_atomic_ptr_cmpexch (&langs, first_lang, lang)) {
287
    lang->finish ();
288 289 290 291
    free (lang);
    goto retry;
  }

292
#ifdef HB_USE_ATEXIT
293 294
  if (!first_lang)
    atexit (free_langs); /* First person registers atexit() callback. */
295 296 297 298 299
#endif

  return lang;
}

300

301 302
/**
 * hb_language_from_string:
303 304 305
 * @str: (array length=len) (element-type uint8_t): a string representing
 *       ISO 639 language code
 * @len: length of the @str, or -1 if it is %NULL-terminated.
306
 *
307 308
 * Converts @str representing an ISO 639 language code to the corresponding
 * #hb_language_t.
309
 *
310
 * Return value: (transfer none):
311
 * The #hb_language_t corresponding to the ISO 639 language code.
312
 *
B
Behdad Esfahbod 已提交
313
 * Since: 0.9.2
314
 **/
315
hb_language_t
316
hb_language_from_string (const char *str, int len)
317
{
318
  if (!str || !len || !*str)
B
Behdad Esfahbod 已提交
319
    return HB_LANGUAGE_INVALID;
320

B
Behdad Esfahbod 已提交
321
  hb_language_item_t *item = nullptr;
322 323
  if (len >= 0)
  {
B
Behdad Esfahbod 已提交
324
    /* NUL-terminate it. */
325
    char strbuf[64];
326
    len = MIN (len, (int) sizeof (strbuf) - 1);
B
Behdad Esfahbod 已提交
327
    memcpy (strbuf, str, len);
328
    strbuf[len] = '\0';
329
    item = lang_find_or_insert (strbuf);
330
  }
331 332
  else
    item = lang_find_or_insert (str);
333

B
Behdad Esfahbod 已提交
334
  return likely (item) ? item->lang : HB_LANGUAGE_INVALID;
335 336
}

337 338
/**
 * hb_language_to_string:
339
 * @language: an #hb_language_t to convert.
340
 *
341
 * See hb_language_from_string().
342
 *
343 344 345
 * Return value: (transfer none):
 * A %NULL-terminated string representing the @language. Must not be freed by
 * the caller.
346
 *
B
Behdad Esfahbod 已提交
347
 * Since: 0.9.2
348
 **/
349 350 351
const char *
hb_language_to_string (hb_language_t language)
{
B
Behdad Esfahbod 已提交
352
  /* This is actually nullptr-safe! */
B
Behdad Esfahbod 已提交
353
  return language->s;
354 355
}

356 357 358 359 360
/**
 * hb_language_get_default:
 *
 * 
 *
361
 * Return value: (transfer none):
362
 *
B
Behdad Esfahbod 已提交
363
 * Since: 0.9.2
364
 **/
365 366 367
hb_language_t
hb_language_get_default (void)
{
368
  static hb_language_t default_language = HB_LANGUAGE_INVALID;
369

370 371
  hb_language_t language = (hb_language_t) hb_atomic_ptr_get (&default_language);
  if (unlikely (language == HB_LANGUAGE_INVALID)) {
B
Behdad Esfahbod 已提交
372
    language = hb_language_from_string (setlocale (LC_CTYPE, nullptr), -1);
373
    (void) hb_atomic_ptr_cmpexch (&default_language, HB_LANGUAGE_INVALID, language);
374 375 376 377 378
  }

  return default_language;
}

379

B
Behdad Esfahbod 已提交
380 381
/* hb_script_t */

382 383
/**
 * hb_script_from_iso15924_tag:
384
 * @tag: an #hb_tag_t representing an ISO 15924 tag.
385
 *
386
 * Converts an ISO 15924 script tag to a corresponding #hb_script_t.
387 388
 *
 * Return value: 
389
 * An #hb_script_t corresponding to the ISO 15924 tag.
390
 *
B
Behdad Esfahbod 已提交
391
 * Since: 0.9.2
392
 **/
B
Behdad Esfahbod 已提交
393 394 395
hb_script_t
hb_script_from_iso15924_tag (hb_tag_t tag)
{
396 397 398
  if (unlikely (tag == HB_TAG_NONE))
    return HB_SCRIPT_INVALID;

B
Behdad Esfahbod 已提交
399
  /* Be lenient, adjust case (one capital letter followed by three small letters) */
400
  tag = (tag & 0xDFDFDFDFu) | 0x00202020u;
B
Behdad Esfahbod 已提交
401

402
  switch (tag) {
B
Behdad Esfahbod 已提交
403 404 405 406 407 408 409 410

    /* These graduated from the 'Q' private-area codes, but
     * the old code is still aliased by Unicode, and the Qaai
     * one in use by ICU. */
    case HB_TAG('Q','a','a','i'): return HB_SCRIPT_INHERITED;
    case HB_TAG('Q','a','a','c'): return HB_SCRIPT_COPTIC;

    /* Script variants from http://unicode.org/iso15924/ */
411 412 413 414 415 416 417
    case HB_TAG('C','y','r','s'): return HB_SCRIPT_CYRILLIC;
    case HB_TAG('L','a','t','f'): return HB_SCRIPT_LATIN;
    case HB_TAG('L','a','t','g'): return HB_SCRIPT_LATIN;
    case HB_TAG('S','y','r','e'): return HB_SCRIPT_SYRIAC;
    case HB_TAG('S','y','r','j'): return HB_SCRIPT_SYRIAC;
    case HB_TAG('S','y','r','n'): return HB_SCRIPT_SYRIAC;
  }
B
Behdad Esfahbod 已提交
418 419

  /* If it looks right, just use the tag as a script */
420
  if (((uint32_t) tag & 0xE0E0E0E0u) == 0x40606060u)
B
Behdad Esfahbod 已提交
421 422 423 424 425 426
    return (hb_script_t) tag;

  /* Otherwise, return unknown */
  return HB_SCRIPT_UNKNOWN;
}

427 428
/**
 * hb_script_from_string:
429 430 431
 * @str: (array length=len) (element-type uint8_t): a string representing an
 *       ISO 15924 tag.
 * @len: length of the @str, or -1 if it is %NULL-terminated.
432
 *
433 434 435
 * Converts a string @str representing an ISO 15924 script tag to a
 * corresponding #hb_script_t. Shorthand for hb_tag_from_string() then
 * hb_script_from_iso15924_tag().
436 437
 *
 * Return value: 
438
 * An #hb_script_t corresponding to the ISO 15924 tag.
439
 *
B
Behdad Esfahbod 已提交
440
 * Since: 0.9.2
441
 **/
B
Behdad Esfahbod 已提交
442
hb_script_t
443
hb_script_from_string (const char *str, int len)
B
Behdad Esfahbod 已提交
444
{
445
  return hb_script_from_iso15924_tag (hb_tag_from_string (str, len));
B
Behdad Esfahbod 已提交
446 447
}

448 449
/**
 * hb_script_to_iso15924_tag:
450
 * @script: an #hb_script_ to convert.
451
 *
452
 * See hb_script_from_iso15924_tag().
453
 *
454 455
 * Return value:
 * An #hb_tag_t representing an ISO 15924 script tag.
456
 *
B
Behdad Esfahbod 已提交
457
 * Since: 0.9.2
458
 **/
B
Behdad Esfahbod 已提交
459 460 461
hb_tag_t
hb_script_to_iso15924_tag (hb_script_t script)
{
462
  return (hb_tag_t) script;
B
Behdad Esfahbod 已提交
463 464
}

465 466 467 468 469 470 471 472
/**
 * hb_script_get_horizontal_direction:
 * @script: 
 *
 * 
 *
 * Return value: 
 *
B
Behdad Esfahbod 已提交
473
 * Since: 0.9.2
474
 **/
B
Behdad Esfahbod 已提交
475 476 477
hb_direction_t
hb_script_get_horizontal_direction (hb_script_t script)
{
478
  /* http://goo.gl/x9ilM */
479 480
  switch ((hb_tag_t) script)
  {
481
    /* Unicode-1.1 additions */
482 483
    case HB_SCRIPT_ARABIC:
    case HB_SCRIPT_HEBREW:
484 485

    /* Unicode-3.0 additions */
486 487 488 489 490 491
    case HB_SCRIPT_SYRIAC:
    case HB_SCRIPT_THAANA:

    /* Unicode-4.0 additions */
    case HB_SCRIPT_CYPRIOT:

492 493 494
    /* Unicode-4.1 additions */
    case HB_SCRIPT_KHAROSHTHI:

495 496 497 498
    /* Unicode-5.0 additions */
    case HB_SCRIPT_PHOENICIAN:
    case HB_SCRIPT_NKO:

499 500 501
    /* Unicode-5.1 additions */
    case HB_SCRIPT_LYDIAN:

502 503 504 505 506 507 508 509 510 511 512 513
    /* Unicode-5.2 additions */
    case HB_SCRIPT_AVESTAN:
    case HB_SCRIPT_IMPERIAL_ARAMAIC:
    case HB_SCRIPT_INSCRIPTIONAL_PAHLAVI:
    case HB_SCRIPT_INSCRIPTIONAL_PARTHIAN:
    case HB_SCRIPT_OLD_SOUTH_ARABIAN:
    case HB_SCRIPT_OLD_TURKIC:
    case HB_SCRIPT_SAMARITAN:

    /* Unicode-6.0 additions */
    case HB_SCRIPT_MANDAIC:

514 515 516 517
    /* Unicode-6.1 additions */
    case HB_SCRIPT_MEROITIC_CURSIVE:
    case HB_SCRIPT_MEROITIC_HIEROGLYPHS:

518 519 520 521 522 523 524 525
    /* Unicode-7.0 additions */
    case HB_SCRIPT_MANICHAEAN:
    case HB_SCRIPT_MENDE_KIKAKUI:
    case HB_SCRIPT_NABATAEAN:
    case HB_SCRIPT_OLD_NORTH_ARABIAN:
    case HB_SCRIPT_PALMYRENE:
    case HB_SCRIPT_PSALTER_PAHLAVI:

B
Behdad Esfahbod 已提交
526
    /* Unicode-8.0 additions */
527
    case HB_SCRIPT_HATRAN:
B
Behdad Esfahbod 已提交
528 529
    case HB_SCRIPT_OLD_HUNGARIAN:

B
Behdad Esfahbod 已提交
530 531 532
    /* Unicode-9.0 additions */
    case HB_SCRIPT_ADLAM:

533 534
      return HB_DIRECTION_RTL;
  }
B
Behdad Esfahbod 已提交
535

536
  return HB_DIRECTION_LTR;
B
Behdad Esfahbod 已提交
537 538 539
}


B
Behdad Esfahbod 已提交
540 541 542 543 544
/* hb_user_data_array_t */

bool
hb_user_data_array_t::set (hb_user_data_key_t *key,
			   void *              data,
545
			   hb_destroy_func_t   destroy,
546
			   hb_bool_t           replace)
B
Behdad Esfahbod 已提交
547 548 549
{
  if (!key)
    return false;
550

551 552
  if (replace) {
    if (!data && !destroy) {
B
Behdad Esfahbod 已提交
553
      items.remove (key, lock);
554 555
      return true;
    }
B
Behdad Esfahbod 已提交
556 557
  }
  hb_user_data_item_t item = {key, data, destroy};
558
  bool ret = !!items.replace_or_insert (item, lock, (bool) replace);
559 560

  return ret;
B
Behdad Esfahbod 已提交
561 562 563
}

void *
564
hb_user_data_array_t::get (hb_user_data_key_t *key)
B
Behdad Esfahbod 已提交
565
{
B
Behdad Esfahbod 已提交
566
  hb_user_data_item_t item = {nullptr, nullptr, nullptr};
567

B
Behdad Esfahbod 已提交
568
  return items.find (key, &item, lock) ? item.data : nullptr;
569
}
570

B
Behdad Esfahbod 已提交
571

572 573
/* hb_version */

574 575 576 577 578 579 580 581
/**
 * hb_version:
 * @major: (out): Library major version component.
 * @minor: (out): Library minor version component.
 * @micro: (out): Library micro version component.
 *
 * Returns library version as three integer components.
 *
B
Behdad Esfahbod 已提交
582
 * Since: 0.9.2
583
 **/
584 585 586 587 588 589 590 591 592 593
void
hb_version (unsigned int *major,
	    unsigned int *minor,
	    unsigned int *micro)
{
  *major = HB_VERSION_MAJOR;
  *minor = HB_VERSION_MINOR;
  *micro = HB_VERSION_MICRO;
}

594 595 596 597 598 599 600
/**
 * hb_version_string:
 *
 * Returns library version as a string with three components.
 *
 * Return value: library version string.
 *
B
Behdad Esfahbod 已提交
601
 * Since: 0.9.2
602
 **/
603 604 605 606 607 608
const char *
hb_version_string (void)
{
  return HB_VERSION_STRING;
}

609
/**
610
 * hb_version_atleast:
611 612 613 614 615 616 617 618
 * @major: 
 * @minor: 
 * @micro: 
 *
 * 
 *
 * Return value: 
 *
S
Sascha Brawer 已提交
619
 * Since: 0.9.30
620
 **/
621
hb_bool_t
622 623 624
hb_version_atleast (unsigned int major,
		    unsigned int minor,
		    unsigned int micro)
625
{
626
  return HB_VERSION_ATLEAST (major, minor, micro);
627
}
B
Behdad Esfahbod 已提交
628 629


630

631
/* hb_feature_t and hb_variation_t */
B
Behdad Esfahbod 已提交
632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676

static bool
parse_space (const char **pp, const char *end)
{
  while (*pp < end && ISSPACE (**pp))
    (*pp)++;
  return true;
}

static bool
parse_char (const char **pp, const char *end, char c)
{
  parse_space (pp, end);

  if (*pp == end || **pp != c)
    return false;

  (*pp)++;
  return true;
}

static bool
parse_uint (const char **pp, const char *end, unsigned int *pv)
{
  char buf[32];
  unsigned int len = MIN (ARRAY_LENGTH (buf) - 1, (unsigned int) (end - *pp));
  strncpy (buf, *pp, len);
  buf[len] = '\0';

  char *p = buf;
  char *pend = p;
  unsigned int v;

  /* Intentionally use strtol instead of strtoul, such that
   * -1 turns into "big number"... */
  errno = 0;
  v = strtol (p, &pend, 0);
  if (errno || p == pend)
    return false;

  *pv = v;
  *pp += pend - p;
  return true;
}

677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700
static bool
parse_uint32 (const char **pp, const char *end, uint32_t *pv)
{
  char buf[32];
  unsigned int len = MIN (ARRAY_LENGTH (buf) - 1, (unsigned int) (end - *pp));
  strncpy (buf, *pp, len);
  buf[len] = '\0';

  char *p = buf;
  char *pend = p;
  unsigned int v;

  /* Intentionally use strtol instead of strtoul, such that
   * -1 turns into "big number"... */
  errno = 0;
  v = strtol (p, &pend, 0);
  if (errno || p == pend)
    return false;

  *pv = v;
  *pp += pend - p;
  return true;
}

701 702
#if defined (HAVE_NEWLOCALE) && defined (HAVE_STRTOD_L)
#define USE_XLOCALE 1
B
Behdad Esfahbod 已提交
703 704 705 706 707 708 709 710 711
#define HB_LOCALE_T locale_t
#define HB_CREATE_LOCALE(locName) newlocale (LC_ALL_MASK, locName, nullptr)
#define HB_FREE_LOCALE(loc) freelocale (loc)
#elif defined(_MSC_VER)
#define USE_XLOCALE 1
#define HB_LOCALE_T _locale_t
#define HB_CREATE_LOCALE(locName) _create_locale (LC_ALL, locName)
#define HB_FREE_LOCALE(loc) _free_locale (loc)
#define strtod_l(a, b, c) _strtod_l ((a), (b), (c))
712 713 714 715
#endif

#ifdef USE_XLOCALE

B
Behdad Esfahbod 已提交
716
static HB_LOCALE_T C_locale;
717 718 719 720 721 722

#ifdef HB_USE_ATEXIT
static void
free_C_locale (void)
{
  if (C_locale)
B
Behdad Esfahbod 已提交
723
    HB_FREE_LOCALE (C_locale);
724 725 726
}
#endif

B
Behdad Esfahbod 已提交
727
static HB_LOCALE_T
728 729 730
get_C_locale (void)
{
retry:
B
Behdad Esfahbod 已提交
731
  HB_LOCALE_T C = (HB_LOCALE_T) hb_atomic_ptr_get (&C_locale);
732 733 734

  if (unlikely (!C))
  {
B
Behdad Esfahbod 已提交
735
    C = HB_CREATE_LOCALE ("C");
736

B
Behdad Esfahbod 已提交
737
    if (!hb_atomic_ptr_cmpexch (&C_locale, nullptr, C))
738
    {
B
Behdad Esfahbod 已提交
739
      HB_FREE_LOCALE (C_locale);
740 741 742 743 744 745 746 747 748 749 750 751
      goto retry;
    }

#ifdef HB_USE_ATEXIT
    atexit (free_C_locale); /* First person registers atexit() callback. */
#endif
  }

  return C;
}
#endif

752 753 754 755 756 757 758 759 760 761 762 763 764
static bool
parse_float (const char **pp, const char *end, float *pv)
{
  char buf[32];
  unsigned int len = MIN (ARRAY_LENGTH (buf) - 1, (unsigned int) (end - *pp));
  strncpy (buf, *pp, len);
  buf[len] = '\0';

  char *p = buf;
  char *pend = p;
  float v;

  errno = 0;
765 766 767
#ifdef USE_XLOCALE
  v = strtod_l (p, &pend, get_C_locale ());
#else
768
  v = strtod (p, &pend);
769
#endif
770 771 772 773 774 775 776 777
  if (errno || p == pend)
    return false;

  *pv = v;
  *pp += pend - p;
  return true;
}

B
Behdad Esfahbod 已提交
778
static bool
779
parse_bool (const char **pp, const char *end, uint32_t *pv)
B
Behdad Esfahbod 已提交
780 781 782 783 784 785 786 787
{
  parse_space (pp, end);

  const char *p = *pp;
  while (*pp < end && ISALPHA(**pp))
    (*pp)++;

  /* CSS allows on/off as aliases 1/0. */
D
David Corbett 已提交
788
  if (*pp - p == 2 && 0 == strncmp (p, "on", 2))
B
Behdad Esfahbod 已提交
789
    *pv = 1;
D
David Corbett 已提交
790
  else if (*pp - p == 3 && 0 == strncmp (p, "off", 3))
B
Behdad Esfahbod 已提交
791 792 793 794 795 796 797
    *pv = 0;
  else
    return false;

  return true;
}

798 799
/* hb_feature_t */

B
Behdad Esfahbod 已提交
800 801 802 803 804 805 806 807 808 809 810 811 812 813
static bool
parse_feature_value_prefix (const char **pp, const char *end, hb_feature_t *feature)
{
  if (parse_char (pp, end, '-'))
    feature->value = 0;
  else {
    parse_char (pp, end, '+');
    feature->value = 1;
  }

  return true;
}

static bool
814
parse_tag (const char **pp, const char *end, hb_tag_t *tag)
B
Behdad Esfahbod 已提交
815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832
{
  parse_space (pp, end);

  char quote = 0;

  if (*pp < end && (**pp == '\'' || **pp == '"'))
  {
    quote = **pp;
    (*pp)++;
  }

  const char *p = *pp;
  while (*pp < end && ISALNUM(**pp))
    (*pp)++;

  if (p == *pp || *pp - p > 4)
    return false;

833
  *tag = hb_tag_from_string (p, *pp - p);
B
Behdad Esfahbod 已提交
834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877

  if (quote)
  {
    /* CSS expects exactly four bytes.  And we only allow quotations for
     * CSS compatibility.  So, enforce the length. */
     if (*pp - p != 4)
       return false;
    if (*pp == end || **pp != quote)
      return false;
    (*pp)++;
  }

  return true;
}

static bool
parse_feature_indices (const char **pp, const char *end, hb_feature_t *feature)
{
  parse_space (pp, end);

  bool has_start;

  feature->start = 0;
  feature->end = (unsigned int) -1;

  if (!parse_char (pp, end, '['))
    return true;

  has_start = parse_uint (pp, end, &feature->start);

  if (parse_char (pp, end, ':')) {
    parse_uint (pp, end, &feature->end);
  } else {
    if (has_start)
      feature->end = feature->start + 1;
  }

  return parse_char (pp, end, ']');
}

static bool
parse_feature_value_postfix (const char **pp, const char *end, hb_feature_t *feature)
{
  bool had_equal = parse_char (pp, end, '=');
878
  bool had_value = parse_uint32 (pp, end, &feature->value) ||
B
Behdad Esfahbod 已提交
879 880 881 882 883 884 885 886 887 888 889
                   parse_bool (pp, end, &feature->value);
  /* CSS doesn't use equal-sign between tag and value.
   * If there was an equal-sign, then there *must* be a value.
   * A value without an eqaul-sign is ok, but not required. */
  return !had_equal || had_value;
}

static bool
parse_one_feature (const char **pp, const char *end, hb_feature_t *feature)
{
  return parse_feature_value_prefix (pp, end, feature) &&
890
	 parse_tag (pp, end, &feature->tag) &&
B
Behdad Esfahbod 已提交
891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980
	 parse_feature_indices (pp, end, feature) &&
	 parse_feature_value_postfix (pp, end, feature) &&
	 parse_space (pp, end) &&
	 *pp == end;
}

/**
 * hb_feature_from_string:
 * @str: (array length=len) (element-type uint8_t): a string to parse
 * @len: length of @str, or -1 if string is %NULL terminated
 * @feature: (out): the #hb_feature_t to initialize with the parsed values
 *
 * Parses a string into a #hb_feature_t.
 *
 * TODO: document the syntax here.
 *
 * Return value:
 * %true if @str is successfully parsed, %false otherwise.
 *
 * Since: 0.9.5
 **/
hb_bool_t
hb_feature_from_string (const char *str, int len,
			hb_feature_t *feature)
{
  hb_feature_t feat;

  if (len < 0)
    len = strlen (str);

  if (likely (parse_one_feature (&str, str + len, &feat)))
  {
    if (feature)
      *feature = feat;
    return true;
  }

  if (feature)
    memset (feature, 0, sizeof (*feature));
  return false;
}

/**
 * hb_feature_to_string:
 * @feature: an #hb_feature_t to convert
 * @buf: (array length=size) (out): output string
 * @size: the allocated size of @buf
 *
 * Converts a #hb_feature_t into a %NULL-terminated string in the format
 * understood by hb_feature_from_string(). The client in responsible for
 * allocating big enough size for @buf, 128 bytes is more than enough.
 *
 * Since: 0.9.5
 **/
void
hb_feature_to_string (hb_feature_t *feature,
		      char *buf, unsigned int size)
{
  if (unlikely (!size)) return;

  char s[128];
  unsigned int len = 0;
  if (feature->value == 0)
    s[len++] = '-';
  hb_tag_to_string (feature->tag, s + len);
  len += 4;
  while (len && s[len - 1] == ' ')
    len--;
  if (feature->start != 0 || feature->end != (unsigned int) -1)
  {
    s[len++] = '[';
    if (feature->start)
      len += MAX (0, snprintf (s + len, ARRAY_LENGTH (s) - len, "%u", feature->start));
    if (feature->end != feature->start + 1) {
      s[len++] = ':';
      if (feature->end != (unsigned int) -1)
	len += MAX (0, snprintf (s + len, ARRAY_LENGTH (s) - len, "%u", feature->end));
    }
    s[len++] = ']';
  }
  if (feature->value > 1)
  {
    s[len++] = '=';
    len += MAX (0, snprintf (s + len, ARRAY_LENGTH (s) - len, "%u", feature->value));
  }
  assert (len < ARRAY_LENGTH (s));
  len = MIN (len, size - 1);
  memcpy (buf, s, len);
  buf[len] = '\0';
}
981

982
/* hb_variation_t */
983 984

static bool
985
parse_variation_value (const char **pp, const char *end, hb_variation_t *variation)
986 987
{
  parse_char (pp, end, '='); /* Optional. */
988
  return parse_float (pp, end, &variation->value);
989 990 991
}

static bool
992
parse_one_variation (const char **pp, const char *end, hb_variation_t *variation)
993
{
994 995
  return parse_tag (pp, end, &variation->tag) &&
	 parse_variation_value (pp, end, variation) &&
996 997 998 999
	 parse_space (pp, end) &&
	 *pp == end;
}

1000 1001 1002 1003 1004
/**
 * hb_variation_from_string:
 *
 * Since: 1.4.2
 */
1005
hb_bool_t
1006 1007
hb_variation_from_string (const char *str, int len,
			  hb_variation_t *variation)
1008
{
1009
  hb_variation_t var;
1010 1011 1012 1013

  if (len < 0)
    len = strlen (str);

1014
  if (likely (parse_one_variation (&str, str + len, &var)))
1015
  {
1016 1017
    if (variation)
      *variation = var;
1018 1019 1020
    return true;
  }

1021 1022
  if (variation)
    memset (variation, 0, sizeof (*variation));
1023 1024 1025
  return false;
}

1026 1027 1028 1029 1030
/**
 * hb_variation_to_string:
 *
 * Since: 1.4.2
 */
1031
void
1032
hb_variation_to_string (hb_variation_t *variation,
1033 1034 1035 1036 1037 1038
			char *buf, unsigned int size)
{
  if (unlikely (!size)) return;

  char s[128];
  unsigned int len = 0;
1039
  hb_tag_to_string (variation->tag, s + len);
1040 1041 1042 1043
  len += 4;
  while (len && s[len - 1] == ' ')
    len--;
  s[len++] = '=';
1044
  len += MAX (0, snprintf (s + len, ARRAY_LENGTH (s) - len, "%g", variation->value));
1045 1046 1047 1048 1049 1050

  assert (len < ARRAY_LENGTH (s));
  len = MIN (len, size - 1);
  memcpy (buf, s, len);
  buf[len] = '\0';
}