2 * Copyright © 2009,2010 Red Hat, Inc.
3 * Copyright © 2011,2012 Google, Inc.
5 * This is part of HarfBuzz, a text shaping library.
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.
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
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.
25 * Red Hat Author(s): Behdad Esfahbod
26 * Google Author(s): Behdad Esfahbod
31 #include "hb-machinery.hh"
42 * @short_description: Common data types
45 * Common data types used across HarfBuzz are defined here.
51 hb_atomic_int_t _hb_options;
58 u.opts.initialized = true;
60 const char *c = getenv ("HB_OPTIONS");
65 const char *p = strchr (c, ':');
69 #define OPTION(name, symbol) \
70 if (0 == strncmp (c, name, p - c) && strlen (name) == p - c) u.opts.symbol = true;
72 OPTION ("uniscribe-bug-compatible", uniscribe_bug_compatible);
82 /* This is idempotent and threadsafe. */
83 _hb_options.set_relaxed (u.i);
91 * @str: (array length=len) (element-type uint8_t):
101 hb_tag_from_string (const char *str, int len)
106 if (!str || !len || !*str)
109 if (len < 0 || len > 4)
111 for (i = 0; i < (unsigned) len && str[i]; i++)
116 return HB_TAG (tag[0], tag[1], tag[2], tag[3]);
122 * @buf: (out caller-allocates) (array fixed-size=4) (element-type uint8_t):
129 hb_tag_to_string (hb_tag_t tag, char *buf)
131 buf[0] = (char) (uint8_t) (tag >> 24);
132 buf[1] = (char) (uint8_t) (tag >> 16);
133 buf[2] = (char) (uint8_t) (tag >> 8);
134 buf[3] = (char) (uint8_t) (tag >> 0);
140 const char direction_strings[][4] = {
148 * hb_direction_from_string:
149 * @str: (array length=len) (element-type uint8_t):
159 hb_direction_from_string (const char *str, int len)
161 if (unlikely (!str || !len || !*str))
162 return HB_DIRECTION_INVALID;
164 /* Lets match loosely: just match the first letter, such that
165 * all of "ltr", "left-to-right", etc work!
167 char c = TOLOWER (str[0]);
168 for (unsigned int i = 0; i < ARRAY_LENGTH (direction_strings); i++)
169 if (c == direction_strings[i][0])
170 return (hb_direction_t) (HB_DIRECTION_LTR + i);
172 return HB_DIRECTION_INVALID;
176 * hb_direction_to_string:
181 * Return value: (transfer none):
186 hb_direction_to_string (hb_direction_t direction)
188 if (likely ((unsigned int) (direction - HB_DIRECTION_LTR)
189 < ARRAY_LENGTH (direction_strings)))
190 return direction_strings[direction - HB_DIRECTION_LTR];
198 struct hb_language_impl_t {
202 static const char canon_map[256] = {
203 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
204 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
205 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '-', 0, 0,
206 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 0, 0, 0, 0, 0, 0,
207 0, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
208 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 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, 0
214 lang_equal (hb_language_t v1,
217 const unsigned char *p1 = (const unsigned char *) v1;
218 const unsigned char *p2 = (const unsigned char *) v2;
220 while (*p1 && *p1 == canon_map[*p2]) {
225 return *p1 == canon_map[*p2];
230 lang_hash (const void *key)
232 const unsigned char *p = key;
234 while (canon_map[*p])
236 h = (h << 5) - h + canon_map[*p];
245 struct hb_language_item_t {
247 struct hb_language_item_t *next;
250 bool operator == (const char *s) const
251 { return lang_equal (lang, s); }
253 hb_language_item_t & operator = (const char *s) {
254 /* If a custom allocated is used calling strdup() pairs
255 badly with a call to the custom free() in fini() below.
256 Therefore don't call strdup(), implement its behavior.
258 size_t len = strlen(s) + 1;
259 lang = (hb_language_t) malloc(len);
262 memcpy((unsigned char *) lang, s, len);
263 for (unsigned char *p = (unsigned char *) lang; *p; p++)
270 void fini () { free ((void *) lang); }
274 /* Thread-safe lock-free language list */
276 static hb_atomic_ptr_t <hb_language_item_t> langs;
283 hb_language_item_t *first_lang = langs;
284 if (unlikely (!langs.cmpexch (first_lang, nullptr)))
288 hb_language_item_t *next = first_lang->next;
296 static hb_language_item_t *
297 lang_find_or_insert (const char *key)
300 hb_language_item_t *first_lang = langs;
302 for (hb_language_item_t *lang = first_lang; lang; lang = lang->next)
306 /* Not found; allocate one. */
307 hb_language_item_t *lang = (hb_language_item_t *) calloc (1, sizeof (hb_language_item_t));
308 if (unlikely (!lang))
310 lang->next = first_lang;
312 if (unlikely (!lang->lang))
318 if (unlikely (!langs.cmpexch (first_lang, lang)))
327 atexit (free_langs); /* First person registers atexit() callback. */
335 * hb_language_from_string:
336 * @str: (array length=len) (element-type uint8_t): a string representing
337 * a BCP 47 language tag
338 * @len: length of the @str, or -1 if it is %NULL-terminated.
340 * Converts @str representing a BCP 47 language tag to the corresponding
343 * Return value: (transfer none):
344 * The #hb_language_t corresponding to the BCP 47 language tag.
349 hb_language_from_string (const char *str, int len)
351 if (!str || !len || !*str)
352 return HB_LANGUAGE_INVALID;
354 hb_language_item_t *item = nullptr;
357 /* NUL-terminate it. */
359 len = MIN (len, (int) sizeof (strbuf) - 1);
360 memcpy (strbuf, str, len);
362 item = lang_find_or_insert (strbuf);
365 item = lang_find_or_insert (str);
367 return likely (item) ? item->lang : HB_LANGUAGE_INVALID;
371 * hb_language_to_string:
372 * @language: an #hb_language_t to convert.
374 * See hb_language_from_string().
376 * Return value: (transfer none):
377 * A %NULL-terminated string representing the @language. Must not be freed by
383 hb_language_to_string (hb_language_t language)
385 /* This is actually nullptr-safe! */
390 * hb_language_get_default:
392 * Get default language from current locale.
394 * Note that the first time this function is called, it calls
395 * "setlocale (LC_CTYPE, nullptr)" to fetch current locale. The underlying
396 * setlocale function is, in many implementations, NOT threadsafe. To avoid
397 * problems, call this function once before multiple threads can call it.
398 * This function is only used from hb_buffer_guess_segment_properties() by
401 * Return value: (transfer none):
406 hb_language_get_default ()
408 static hb_atomic_ptr_t <hb_language_t> default_language;
410 hb_language_t language = default_language;
411 if (unlikely (language == HB_LANGUAGE_INVALID))
413 language = hb_language_from_string (setlocale (LC_CTYPE, nullptr), -1);
414 (void) default_language.cmpexch (HB_LANGUAGE_INVALID, language);
424 * hb_script_from_iso15924_tag:
425 * @tag: an #hb_tag_t representing an ISO 15924 tag.
427 * Converts an ISO 15924 script tag to a corresponding #hb_script_t.
430 * An #hb_script_t corresponding to the ISO 15924 tag.
435 hb_script_from_iso15924_tag (hb_tag_t tag)
437 if (unlikely (tag == HB_TAG_NONE))
438 return HB_SCRIPT_INVALID;
440 /* Be lenient, adjust case (one capital letter followed by three small letters) */
441 tag = (tag & 0xDFDFDFDFu) | 0x00202020u;
445 /* These graduated from the 'Q' private-area codes, but
446 * the old code is still aliased by Unicode, and the Qaai
447 * one in use by ICU. */
448 case HB_TAG('Q','a','a','i'): return HB_SCRIPT_INHERITED;
449 case HB_TAG('Q','a','a','c'): return HB_SCRIPT_COPTIC;
451 /* Script variants from https://unicode.org/iso15924/ */
452 case HB_TAG('C','y','r','s'): return HB_SCRIPT_CYRILLIC;
453 case HB_TAG('L','a','t','f'): return HB_SCRIPT_LATIN;
454 case HB_TAG('L','a','t','g'): return HB_SCRIPT_LATIN;
455 case HB_TAG('S','y','r','e'): return HB_SCRIPT_SYRIAC;
456 case HB_TAG('S','y','r','j'): return HB_SCRIPT_SYRIAC;
457 case HB_TAG('S','y','r','n'): return HB_SCRIPT_SYRIAC;
460 /* If it looks right, just use the tag as a script */
461 if (((uint32_t) tag & 0xE0E0E0E0u) == 0x40606060u)
462 return (hb_script_t) tag;
464 /* Otherwise, return unknown */
465 return HB_SCRIPT_UNKNOWN;
469 * hb_script_from_string:
470 * @str: (array length=len) (element-type uint8_t): a string representing an
472 * @len: length of the @str, or -1 if it is %NULL-terminated.
474 * Converts a string @str representing an ISO 15924 script tag to a
475 * corresponding #hb_script_t. Shorthand for hb_tag_from_string() then
476 * hb_script_from_iso15924_tag().
479 * An #hb_script_t corresponding to the ISO 15924 tag.
484 hb_script_from_string (const char *str, int len)
486 return hb_script_from_iso15924_tag (hb_tag_from_string (str, len));
490 * hb_script_to_iso15924_tag:
491 * @script: an #hb_script_ to convert.
493 * See hb_script_from_iso15924_tag().
496 * An #hb_tag_t representing an ISO 15924 script tag.
501 hb_script_to_iso15924_tag (hb_script_t script)
503 return (hb_tag_t) script;
507 * hb_script_get_horizontal_direction:
517 hb_script_get_horizontal_direction (hb_script_t script)
519 /* https://docs.google.com/spreadsheets/d/1Y90M0Ie3MUJ6UVCRDOypOtijlMDLNNyyLk36T6iMu0o */
520 switch ((hb_tag_t) script)
522 /* Unicode-1.1 additions */
523 case HB_SCRIPT_ARABIC:
524 case HB_SCRIPT_HEBREW:
526 /* Unicode-3.0 additions */
527 case HB_SCRIPT_SYRIAC:
528 case HB_SCRIPT_THAANA:
530 /* Unicode-4.0 additions */
531 case HB_SCRIPT_CYPRIOT:
533 /* Unicode-4.1 additions */
534 case HB_SCRIPT_KHAROSHTHI:
536 /* Unicode-5.0 additions */
537 case HB_SCRIPT_PHOENICIAN:
540 /* Unicode-5.1 additions */
541 case HB_SCRIPT_LYDIAN:
543 /* Unicode-5.2 additions */
544 case HB_SCRIPT_AVESTAN:
545 case HB_SCRIPT_IMPERIAL_ARAMAIC:
546 case HB_SCRIPT_INSCRIPTIONAL_PAHLAVI:
547 case HB_SCRIPT_INSCRIPTIONAL_PARTHIAN:
548 case HB_SCRIPT_OLD_SOUTH_ARABIAN:
549 case HB_SCRIPT_OLD_TURKIC:
550 case HB_SCRIPT_SAMARITAN:
552 /* Unicode-6.0 additions */
553 case HB_SCRIPT_MANDAIC:
555 /* Unicode-6.1 additions */
556 case HB_SCRIPT_MEROITIC_CURSIVE:
557 case HB_SCRIPT_MEROITIC_HIEROGLYPHS:
559 /* Unicode-7.0 additions */
560 case HB_SCRIPT_MANICHAEAN:
561 case HB_SCRIPT_MENDE_KIKAKUI:
562 case HB_SCRIPT_NABATAEAN:
563 case HB_SCRIPT_OLD_NORTH_ARABIAN:
564 case HB_SCRIPT_PALMYRENE:
565 case HB_SCRIPT_PSALTER_PAHLAVI:
567 /* Unicode-8.0 additions */
568 case HB_SCRIPT_HATRAN:
570 /* Unicode-9.0 additions */
571 case HB_SCRIPT_ADLAM:
573 /* Unicode-11.0 additions */
574 case HB_SCRIPT_HANIFI_ROHINGYA:
575 case HB_SCRIPT_OLD_SOGDIAN:
576 case HB_SCRIPT_SOGDIAN:
578 return HB_DIRECTION_RTL;
581 /* https://github.com/harfbuzz/harfbuzz/issues/1000 */
582 case HB_SCRIPT_OLD_HUNGARIAN:
583 case HB_SCRIPT_OLD_ITALIC:
584 case HB_SCRIPT_RUNIC:
586 return HB_DIRECTION_INVALID;
589 return HB_DIRECTION_LTR;
593 /* hb_user_data_array_t */
596 hb_user_data_array_t::set (hb_user_data_key_t *key,
598 hb_destroy_func_t destroy,
605 if (!data && !destroy) {
606 items.remove (key, lock);
610 hb_user_data_item_t item = {key, data, destroy};
611 bool ret = !!items.replace_or_insert (item, lock, (bool) replace);
617 hb_user_data_array_t::get (hb_user_data_key_t *key)
619 hb_user_data_item_t item = {nullptr, nullptr, nullptr};
621 return items.find (key, &item, lock) ? item.data : nullptr;
631 * @short_description: Information about the version of HarfBuzz in use
634 * These functions and macros allow accessing version of the HarfBuzz
635 * library used at compile- as well as run-time, and to direct code
636 * conditionally based on those versions, again, at compile- or run-time.
642 * @major: (out): Library major version component.
643 * @minor: (out): Library minor version component.
644 * @micro: (out): Library micro version component.
646 * Returns library version as three integer components.
651 hb_version (unsigned int *major,
655 *major = HB_VERSION_MAJOR;
656 *minor = HB_VERSION_MINOR;
657 *micro = HB_VERSION_MICRO;
663 * Returns library version as a string with three components.
665 * Return value: library version string.
672 return HB_VERSION_STRING;
676 * hb_version_atleast:
688 hb_version_atleast (unsigned int major,
692 return HB_VERSION_ATLEAST (major, minor, micro);
697 /* hb_feature_t and hb_variation_t */
700 parse_space (const char **pp, const char *end)
702 while (*pp < end && ISSPACE (**pp))
708 parse_char (const char **pp, const char *end, char c)
710 parse_space (pp, end);
712 if (*pp == end || **pp != c)
720 parse_uint (const char **pp, const char *end, unsigned int *pv)
723 unsigned int len = MIN (ARRAY_LENGTH (buf) - 1, (unsigned int) (end - *pp));
724 strncpy (buf, *pp, len);
731 /* Intentionally use strtol instead of strtoul, such that
732 * -1 turns into "big number"... */
734 v = strtol (p, &pend, 10);
735 if (errno || p == pend)
744 parse_uint32 (const char **pp, const char *end, uint32_t *pv)
747 unsigned int len = MIN (ARRAY_LENGTH (buf) - 1, (unsigned int) (end - *pp));
748 strncpy (buf, *pp, len);
755 /* Intentionally use strtol instead of strtoul, such that
756 * -1 turns into "big number"... */
758 v = strtol (p, &pend, 10);
759 if (errno || p == pend)
767 #if defined (HAVE_NEWLOCALE) && defined (HAVE_STRTOD_L)
768 #define USE_XLOCALE 1
769 #define HB_LOCALE_T locale_t
770 #define HB_CREATE_LOCALE(locName) newlocale (LC_ALL_MASK, locName, nullptr)
771 #define HB_FREE_LOCALE(loc) freelocale (loc)
772 #elif defined(_MSC_VER)
773 #define USE_XLOCALE 1
774 #define HB_LOCALE_T _locale_t
775 #define HB_CREATE_LOCALE(locName) _create_locale (LC_ALL, locName)
776 #define HB_FREE_LOCALE(loc) _free_locale (loc)
777 #define strtod_l(a, b, c) _strtod_l ((a), (b), (c))
783 static void free_static_C_locale ();
786 static struct hb_C_locale_lazy_loader_t : hb_lazy_loader_t<hb_remove_pointer (HB_LOCALE_T),
787 hb_C_locale_lazy_loader_t>
789 static HB_LOCALE_T create ()
791 HB_LOCALE_T C_locale = HB_CREATE_LOCALE ("C");
794 atexit (free_static_C_locale);
799 static void destroy (HB_LOCALE_T p)
803 static HB_LOCALE_T get_null ()
811 void free_static_C_locale ()
813 static_C_locale.free_instance ();
820 return static_C_locale.get_unconst ();
822 #endif /* USE_XLOCALE */
825 parse_float (const char **pp, const char *end, float *pv)
828 unsigned int len = MIN (ARRAY_LENGTH (buf) - 1, (unsigned int) (end - *pp));
829 strncpy (buf, *pp, len);
838 v = strtod_l (p, &pend, get_C_locale ());
840 v = strtod (p, &pend);
842 if (errno || p == pend)
851 parse_bool (const char **pp, const char *end, uint32_t *pv)
853 parse_space (pp, end);
856 while (*pp < end && ISALPHA(**pp))
859 /* CSS allows on/off as aliases 1/0. */
861 && TOLOWER (p[0]) == 'o'
862 && TOLOWER (p[1]) == 'n')
864 else if (*pp - p == 3
865 && TOLOWER (p[0]) == 'o'
866 && TOLOWER (p[1]) == 'f'
867 && TOLOWER (p[2]) == 'f')
878 parse_feature_value_prefix (const char **pp, const char *end, hb_feature_t *feature)
880 if (parse_char (pp, end, '-'))
883 parse_char (pp, end, '+');
891 parse_tag (const char **pp, const char *end, hb_tag_t *tag)
893 parse_space (pp, end);
897 if (*pp < end && (**pp == '\'' || **pp == '"'))
904 while (*pp < end && (ISALNUM(**pp) || **pp == '_'))
907 if (p == *pp || *pp - p > 4)
910 *tag = hb_tag_from_string (p, *pp - p);
914 /* CSS expects exactly four bytes. And we only allow quotations for
915 * CSS compatibility. So, enforce the length. */
918 if (*pp == end || **pp != quote)
927 parse_feature_indices (const char **pp, const char *end, hb_feature_t *feature)
929 parse_space (pp, end);
933 feature->start = HB_FEATURE_GLOBAL_START;
934 feature->end = HB_FEATURE_GLOBAL_END;
936 if (!parse_char (pp, end, '['))
939 has_start = parse_uint (pp, end, &feature->start);
941 if (parse_char (pp, end, ':') || parse_char (pp, end, ';')) {
942 parse_uint (pp, end, &feature->end);
945 feature->end = feature->start + 1;
948 return parse_char (pp, end, ']');
952 parse_feature_value_postfix (const char **pp, const char *end, hb_feature_t *feature)
954 bool had_equal = parse_char (pp, end, '=');
955 bool had_value = parse_uint32 (pp, end, &feature->value) ||
956 parse_bool (pp, end, &feature->value);
957 /* CSS doesn't use equal-sign between tag and value.
958 * If there was an equal-sign, then there *must* be a value.
959 * A value without an equal-sign is ok, but not required. */
960 return !had_equal || had_value;
964 parse_one_feature (const char **pp, const char *end, hb_feature_t *feature)
966 return parse_feature_value_prefix (pp, end, feature) &&
967 parse_tag (pp, end, &feature->tag) &&
968 parse_feature_indices (pp, end, feature) &&
969 parse_feature_value_postfix (pp, end, feature) &&
970 parse_space (pp, end) &&
975 * hb_feature_from_string:
976 * @str: (array length=len) (element-type uint8_t): a string to parse
977 * @len: length of @str, or -1 if string is %NULL terminated
978 * @feature: (out): the #hb_feature_t to initialize with the parsed values
980 * Parses a string into a #hb_feature_t.
982 * The format for specifying feature strings follows. All valid CSS
983 * font-feature-settings values other than 'normal' and the global values are
984 * also accepted, though not documented below. CSS string escapes are not
987 * The range indices refer to the positions between Unicode characters. The
988 * position before the first character is always 0.
990 * The format is Python-esque. Here is how it all works:
992 * <informaltable pgwide='1' align='left' frame='none'>
995 * <row><entry>Syntax</entry> <entry>Value</entry> <entry>Start</entry> <entry>End</entry></row>
998 * <row><entry>Setting value:</entry></row>
999 * <row><entry>kern</entry> <entry>1</entry> <entry>0</entry> <entry>∞</entry> <entry>Turn feature on</entry></row>
1000 * <row><entry>+kern</entry> <entry>1</entry> <entry>0</entry> <entry>∞</entry> <entry>Turn feature on</entry></row>
1001 * <row><entry>-kern</entry> <entry>0</entry> <entry>0</entry> <entry>∞</entry> <entry>Turn feature off</entry></row>
1002 * <row><entry>kern=0</entry> <entry>0</entry> <entry>0</entry> <entry>∞</entry> <entry>Turn feature off</entry></row>
1003 * <row><entry>kern=1</entry> <entry>1</entry> <entry>0</entry> <entry>∞</entry> <entry>Turn feature on</entry></row>
1004 * <row><entry>aalt=2</entry> <entry>2</entry> <entry>0</entry> <entry>∞</entry> <entry>Choose 2nd alternate</entry></row>
1005 * <row><entry>Setting index:</entry></row>
1006 * <row><entry>kern[]</entry> <entry>1</entry> <entry>0</entry> <entry>∞</entry> <entry>Turn feature on</entry></row>
1007 * <row><entry>kern[:]</entry> <entry>1</entry> <entry>0</entry> <entry>∞</entry> <entry>Turn feature on</entry></row>
1008 * <row><entry>kern[5:]</entry> <entry>1</entry> <entry>5</entry> <entry>∞</entry> <entry>Turn feature on, partial</entry></row>
1009 * <row><entry>kern[:5]</entry> <entry>1</entry> <entry>0</entry> <entry>5</entry> <entry>Turn feature on, partial</entry></row>
1010 * <row><entry>kern[3:5]</entry> <entry>1</entry> <entry>3</entry> <entry>5</entry> <entry>Turn feature on, range</entry></row>
1011 * <row><entry>kern[3]</entry> <entry>1</entry> <entry>3</entry> <entry>3+1</entry> <entry>Turn feature on, single char</entry></row>
1012 * <row><entry>Mixing it all:</entry></row>
1013 * <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>
1019 * %true if @str is successfully parsed, %false otherwise.
1024 hb_feature_from_string (const char *str, int len,
1025 hb_feature_t *feature)
1032 if (likely (parse_one_feature (&str, str + len, &feat)))
1040 memset (feature, 0, sizeof (*feature));
1045 * hb_feature_to_string:
1046 * @feature: an #hb_feature_t to convert
1047 * @buf: (array length=size) (out): output string
1048 * @size: the allocated size of @buf
1050 * Converts a #hb_feature_t into a %NULL-terminated string in the format
1051 * understood by hb_feature_from_string(). The client in responsible for
1052 * allocating big enough size for @buf, 128 bytes is more than enough.
1057 hb_feature_to_string (hb_feature_t *feature,
1058 char *buf, unsigned int size)
1060 if (unlikely (!size)) return;
1063 unsigned int len = 0;
1064 if (feature->value == 0)
1066 hb_tag_to_string (feature->tag, s + len);
1068 while (len && s[len - 1] == ' ')
1070 if (feature->start != 0 || feature->end != (unsigned int) -1)
1074 len += MAX (0, snprintf (s + len, ARRAY_LENGTH (s) - len, "%u", feature->start));
1075 if (feature->end != feature->start + 1) {
1077 if (feature->end != (unsigned int) -1)
1078 len += MAX (0, snprintf (s + len, ARRAY_LENGTH (s) - len, "%u", feature->end));
1082 if (feature->value > 1)
1085 len += MAX (0, snprintf (s + len, ARRAY_LENGTH (s) - len, "%u", feature->value));
1087 assert (len < ARRAY_LENGTH (s));
1088 len = MIN (len, size - 1);
1089 memcpy (buf, s, len);
1093 /* hb_variation_t */
1096 parse_variation_value (const char **pp, const char *end, hb_variation_t *variation)
1098 parse_char (pp, end, '='); /* Optional. */
1099 return parse_float (pp, end, &variation->value);
1103 parse_one_variation (const char **pp, const char *end, hb_variation_t *variation)
1105 return parse_tag (pp, end, &variation->tag) &&
1106 parse_variation_value (pp, end, variation) &&
1107 parse_space (pp, end) &&
1112 * hb_variation_from_string:
1117 hb_variation_from_string (const char *str, int len,
1118 hb_variation_t *variation)
1125 if (likely (parse_one_variation (&str, str + len, &var)))
1133 memset (variation, 0, sizeof (*variation));
1138 * hb_variation_to_string:
1143 hb_variation_to_string (hb_variation_t *variation,
1144 char *buf, unsigned int size)
1146 if (unlikely (!size)) return;
1149 unsigned int len = 0;
1150 hb_tag_to_string (variation->tag, s + len);
1152 while (len && s[len - 1] == ' ')
1155 len += MAX (0, snprintf (s + len, ARRAY_LENGTH (s) - len, "%g", (double) variation->value));
1157 assert (len < ARRAY_LENGTH (s));
1158 len = MIN (len, size - 1);
1159 memcpy (buf, s, len);
1163 /* If there is no visibility control, then hb-static.cc will NOT
1164 * define anything. Instead, we get it to define one set in here
1165 * only, so only libharfbuzz.so defines them, not other libs. */
1166 #ifdef HB_NO_VISIBILITY
1167 #undef HB_NO_VISIBILITY
1168 #include "hb-static.cc"
1169 #define HB_NO_VISIBILITY 1