jdk
1220 строк · 30.8 Кб
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
45hb_atomic_int_t _hb_options;46
47void
48_hb_options_init ()49{
50hb_options_union_t u;51u.i = 0;52u.opts.initialized = true;53
54const char *c = getenv ("HB_OPTIONS");55if (c)56{57while (*c)58{59const char *p = strchr (c, ':');60if (!p)61p = c + strlen (c);62
63#define OPTION(name, symbol) \64if (0 == strncmp (c, name, p - c) && strlen (name) == static_cast<size_t>(p - c)) do { u.opts.symbol = true; } while (0)65
66OPTION ("uniscribe-bug-compatible", uniscribe_bug_compatible);67
68#undef OPTION69
70c = *p ? p + 1 : p;71}72
73}74
75/* This is idempotent and threadsafe. */76_hb_options = u.i;77}
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**/
96hb_tag_t
97hb_tag_from_string (const char *str, int len)98{
99char tag[4];100unsigned int i;101
102if (!str || !len || !*str)103return HB_TAG_NONE;104
105if (len < 0 || len > 4)106len = 4;107for (i = 0; i < (unsigned) len && str[i]; i++)108tag[i] = str[i];109for (; i < 4; i++)110tag[i] = ' ';111
112return HB_TAG (tag[0], tag[1], tag[2], tag[3]);113}
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**/
125void
126hb_tag_to_string (hb_tag_t tag, char *buf)127{
128buf[0] = (char) (uint8_t) (tag >> 24);129buf[1] = (char) (uint8_t) (tag >> 16);130buf[2] = (char) (uint8_t) (tag >> 8);131buf[3] = (char) (uint8_t) (tag >> 0);132}
133
134
135/* hb_direction_t */
136
137static 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**/
160hb_direction_t
161hb_direction_from_string (const char *str, int len)162{
163if (unlikely (!str || !len || !*str))164return HB_DIRECTION_INVALID;165
166/* Lets match loosely: just match the first letter, such that167* all of "ltr", "left-to-right", etc work!
168*/
169char c = TOLOWER (str[0]);170for (unsigned int i = 0; i < ARRAY_LENGTH (direction_strings); i++)171if (c == direction_strings[i][0])172return (hb_direction_t) (HB_DIRECTION_LTR + i);173
174return HB_DIRECTION_INVALID;175}
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**/
187const char *188hb_direction_to_string (hb_direction_t direction)189{
190if (likely ((unsigned int) (direction - HB_DIRECTION_LTR)191< ARRAY_LENGTH (direction_strings)))192return direction_strings[direction - HB_DIRECTION_LTR];193
194return "invalid";195}
196
197
198/* hb_language_t */
199
200struct hb_language_impl_t {201const char s[1];202};203
204static const char canon_map[256] = {2050, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,2060, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,2070, 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,2090, '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, '-',2110, '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, 0213};214
215static bool216lang_equal (hb_language_t v1,217const void *v2)218{
219const unsigned char *p1 = (const unsigned char *) v1;220const unsigned char *p2 = (const unsigned char *) v2;221
222while (*p1 && *p1 == canon_map[*p2]) {223p1++;224p2++;225}226
227return *p1 == canon_map[*p2];228}
229
230#if 0231static unsigned int232lang_hash (const void *key)233{
234const unsigned char *p = key;235unsigned int h = 0;236while (canon_map[*p])237{238h = (h << 5) - h + canon_map[*p];239p++;240}241
242return h;243}
244#endif245
246
247struct hb_language_item_t {248
249struct hb_language_item_t *next;250hb_language_t lang;251
252bool operator == (const char *s) const253{ return lang_equal (lang, s); }254
255hb_language_item_t & operator = (const char *s)256{257/* We can't call strdup(), because we allow custom allocators. */258size_t len = strlen(s) + 1;259lang = (hb_language_t) hb_malloc(len);260if (likely (lang))261{262hb_memcpy((unsigned char *) lang, s, len);263for (unsigned char *p = (unsigned char *) lang; *p; p++)264*p = canon_map[*p];265}266
267return *this;268}269
270void fini () { hb_free ((void *) lang); }271};272
273
274/* Thread-safe lockfree language list */
275
276static hb_atomic_ptr_t <hb_language_item_t> langs;277
278static inline void279free_langs ()280{
281retry:282hb_language_item_t *first_lang = langs;283if (unlikely (!langs.cmpexch (first_lang, nullptr)))284goto retry;285
286while (first_lang) {287hb_language_item_t *next = first_lang->next;288first_lang->fini ();289hb_free (first_lang);290first_lang = next;291}292}
293
294static hb_language_item_t *295lang_find_or_insert (const char *key)296{
297retry:298hb_language_item_t *first_lang = langs;299
300for (hb_language_item_t *lang = first_lang; lang; lang = lang->next)301if (*lang == key)302return lang;303
304/* Not found; allocate one. */305hb_language_item_t *lang = (hb_language_item_t *) hb_calloc (1, sizeof (hb_language_item_t));306if (unlikely (!lang))307return nullptr;308lang->next = first_lang;309*lang = key;310if (unlikely (!lang->lang))311{312hb_free (lang);313return nullptr;314}315
316if (unlikely (!langs.cmpexch (first_lang, lang)))317{318lang->fini ();319hb_free (lang);320goto retry;321}322
323if (!first_lang)324hb_atexit (free_langs); /* First person registers atexit() callback. */325
326return lang;327}
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**/
344hb_language_t
345hb_language_from_string (const char *str, int len)346{
347if (!str || !len || !*str)348return HB_LANGUAGE_INVALID;349
350hb_language_item_t *item = nullptr;351if (len >= 0)352{353/* NUL-terminate it. */354char strbuf[64];355len = hb_min (len, (int) sizeof (strbuf) - 1);356hb_memcpy (strbuf, str, len);357strbuf[len] = '\0';358item = lang_find_or_insert (strbuf);359}360else361item = lang_find_or_insert (str);362
363return likely (item) ? item->lang : HB_LANGUAGE_INVALID;364}
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**/
378const char *379hb_language_to_string (hb_language_t language)380{
381if (unlikely (!language)) return nullptr;382
383return language->s;384}
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**/
403hb_language_t
404hb_language_get_default ()405{
406static hb_atomic_ptr_t <hb_language_t> default_language;407
408hb_language_t language = default_language;409if (unlikely (language == HB_LANGUAGE_INVALID))410{411language = hb_language_from_string (hb_setlocale (LC_CTYPE, nullptr), -1);412(void) default_language.cmpexch (HB_LANGUAGE_INVALID, language);413}414
415return language;416}
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**/
431hb_bool_t
432hb_language_matches (hb_language_t language,433hb_language_t specific)434{
435if (language == specific) return true;436if (!language || !specific) return false;437
438const char *l = language->s;439const char *s = specific->s;440unsigned ll = strlen (l);441unsigned sl = strlen (s);442
443if (ll > sl)444return false;445
446return strncmp (l, s, ll) == 0 &&447(s[ll] == '\0' || s[ll] == '-');448}
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**/
464hb_script_t
465hb_script_from_iso15924_tag (hb_tag_t tag)466{
467if (unlikely (tag == HB_TAG_NONE))468return HB_SCRIPT_INVALID;469
470/* Be lenient, adjust case (one capital letter followed by three small letters) */471tag = (tag & 0xDFDFDFDFu) | 0x00202020u;472
473switch (tag) {474
475/* These graduated from the 'Q' private-area codes, but476* the old code is still aliased by Unicode, and the Qaai
477* one in use by ICU. */
478case HB_TAG('Q','a','a','i'): return HB_SCRIPT_INHERITED;479case HB_TAG('Q','a','a','c'): return HB_SCRIPT_COPTIC;480
481/* Script variants from https://unicode.org/iso15924/ */482case HB_TAG('A','r','a','n'): return HB_SCRIPT_ARABIC;483case HB_TAG('C','y','r','s'): return HB_SCRIPT_CYRILLIC;484case HB_TAG('G','e','o','k'): return HB_SCRIPT_GEORGIAN;485case HB_TAG('H','a','n','s'): return HB_SCRIPT_HAN;486case HB_TAG('H','a','n','t'): return HB_SCRIPT_HAN;487case HB_TAG('J','a','m','o'): return HB_SCRIPT_HANGUL;488case HB_TAG('L','a','t','f'): return HB_SCRIPT_LATIN;489case HB_TAG('L','a','t','g'): return HB_SCRIPT_LATIN;490case HB_TAG('S','y','r','e'): return HB_SCRIPT_SYRIAC;491case HB_TAG('S','y','r','j'): return HB_SCRIPT_SYRIAC;492case HB_TAG('S','y','r','n'): return HB_SCRIPT_SYRIAC;493}494
495/* If it looks right, just use the tag as a script */496if (((uint32_t) tag & 0xE0E0E0E0u) == 0x40606060u)497return (hb_script_t) tag;498
499/* Otherwise, return unknown */500return HB_SCRIPT_UNKNOWN;501}
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**/
518hb_script_t
519hb_script_from_string (const char *str, int len)520{
521return hb_script_from_iso15924_tag (hb_tag_from_string (str, len));522}
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**/
535hb_tag_t
536hb_script_to_iso15924_tag (hb_script_t script)537{
538return (hb_tag_t) script;539}
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**/
556hb_direction_t
557hb_script_get_horizontal_direction (hb_script_t script)558{
559/* https://docs.google.com/spreadsheets/d/1Y90M0Ie3MUJ6UVCRDOypOtijlMDLNNyyLk36T6iMu0o */560switch ((hb_tag_t) script)561{562/* Unicode-1.1 additions */563case HB_SCRIPT_ARABIC:564case HB_SCRIPT_HEBREW:565
566/* Unicode-3.0 additions */567case HB_SCRIPT_SYRIAC:568case HB_SCRIPT_THAANA:569
570/* Unicode-4.0 additions */571case HB_SCRIPT_CYPRIOT:572
573/* Unicode-4.1 additions */574case HB_SCRIPT_KHAROSHTHI:575
576/* Unicode-5.0 additions */577case HB_SCRIPT_PHOENICIAN:578case HB_SCRIPT_NKO:579
580/* Unicode-5.1 additions */581case HB_SCRIPT_LYDIAN:582
583/* Unicode-5.2 additions */584case HB_SCRIPT_AVESTAN:585case HB_SCRIPT_IMPERIAL_ARAMAIC:586case HB_SCRIPT_INSCRIPTIONAL_PAHLAVI:587case HB_SCRIPT_INSCRIPTIONAL_PARTHIAN:588case HB_SCRIPT_OLD_SOUTH_ARABIAN:589case HB_SCRIPT_OLD_TURKIC:590case HB_SCRIPT_SAMARITAN:591
592/* Unicode-6.0 additions */593case HB_SCRIPT_MANDAIC:594
595/* Unicode-6.1 additions */596case HB_SCRIPT_MEROITIC_CURSIVE:597case HB_SCRIPT_MEROITIC_HIEROGLYPHS:598
599/* Unicode-7.0 additions */600case HB_SCRIPT_MANICHAEAN:601case HB_SCRIPT_MENDE_KIKAKUI:602case HB_SCRIPT_NABATAEAN:603case HB_SCRIPT_OLD_NORTH_ARABIAN:604case HB_SCRIPT_PALMYRENE:605case HB_SCRIPT_PSALTER_PAHLAVI:606
607/* Unicode-8.0 additions */608case HB_SCRIPT_HATRAN:609
610/* Unicode-9.0 additions */611case HB_SCRIPT_ADLAM:612
613/* Unicode-11.0 additions */614case HB_SCRIPT_HANIFI_ROHINGYA:615case HB_SCRIPT_OLD_SOGDIAN:616case HB_SCRIPT_SOGDIAN:617
618/* Unicode-12.0 additions */619case HB_SCRIPT_ELYMAIC:620
621/* Unicode-13.0 additions */622case HB_SCRIPT_CHORASMIAN:623case HB_SCRIPT_YEZIDI:624
625/* Unicode-14.0 additions */626case HB_SCRIPT_OLD_UYGHUR:627
628return HB_DIRECTION_RTL;629
630
631/* https://github.com/harfbuzz/harfbuzz/issues/1000 */632case HB_SCRIPT_OLD_HUNGARIAN:633case HB_SCRIPT_OLD_ITALIC:634case HB_SCRIPT_RUNIC:635case HB_SCRIPT_TIFINAGH:636
637return HB_DIRECTION_INVALID;638}639
640return HB_DIRECTION_LTR;641}
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**/
669void
670hb_version (unsigned int *major,671unsigned int *minor,672unsigned int *micro)673{
674*major = HB_VERSION_MAJOR;675*minor = HB_VERSION_MINOR;676*micro = HB_VERSION_MICRO;677}
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**/
688const char *689hb_version_string ()690{
691return HB_VERSION_STRING;692}
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**/
708hb_bool_t
709hb_version_atleast (unsigned int major,710unsigned int minor,711unsigned int micro)712{
713return HB_VERSION_ATLEAST (major, minor, micro);714}
715
716
717
718/* hb_feature_t and hb_variation_t */
719
720static bool721parse_space (const char **pp, const char *end)722{
723while (*pp < end && ISSPACE (**pp))724(*pp)++;725return true;726}
727
728static bool729parse_char (const char **pp, const char *end, char c)730{
731parse_space (pp, end);732
733if (*pp == end || **pp != c)734return false;735
736(*pp)++;737return true;738}
739
740static bool741parse_uint (const char **pp, const char *end, unsigned int *pv)742{
743/* Intentionally use hb_parse_int inside instead of hb_parse_uint,744* such that -1 turns into "big number"... */
745int v;746if (unlikely (!hb_parse_int (pp, end, &v))) return false;747
748*pv = v;749return true;750}
751
752static bool753parse_uint32 (const char **pp, const char *end, uint32_t *pv)754{
755/* Intentionally use hb_parse_int inside instead of hb_parse_uint,756* such that -1 turns into "big number"... */
757int v;758if (unlikely (!hb_parse_int (pp, end, &v))) return false;759
760*pv = v;761return true;762}
763
764static bool765parse_bool (const char **pp, const char *end, uint32_t *pv)766{
767parse_space (pp, end);768
769const char *p = *pp;770while (*pp < end && ISALPHA(**pp))771(*pp)++;772
773/* CSS allows on/off as aliases 1/0. */774if (*pp - p == 2775&& TOLOWER (p[0]) == 'o'776&& TOLOWER (p[1]) == 'n')777*pv = 1;778else if (*pp - p == 3779&& TOLOWER (p[0]) == 'o'780&& TOLOWER (p[1]) == 'f'781&& TOLOWER (p[2]) == 'f')782*pv = 0;783else784return false;785
786return true;787}
788
789/* hb_feature_t */
790
791static bool792parse_feature_value_prefix (const char **pp, const char *end, hb_feature_t *feature)793{
794if (parse_char (pp, end, '-'))795feature->value = 0;796else {797parse_char (pp, end, '+');798feature->value = 1;799}800
801return true;802}
803
804static bool805parse_tag (const char **pp, const char *end, hb_tag_t *tag)806{
807parse_space (pp, end);808
809char quote = 0;810
811if (*pp < end && (**pp == '\'' || **pp == '"'))812{813quote = **pp;814(*pp)++;815}816
817const char *p = *pp;818while (*pp < end && (**pp != ' ' && **pp != '=' && **pp != '[' && **pp != quote))819(*pp)++;820
821if (p == *pp || *pp - p > 4)822return false;823
824*tag = hb_tag_from_string (p, *pp - p);825
826if (quote)827{828/* CSS expects exactly four bytes. And we only allow quotations for829* CSS compatibility. So, enforce the length. */
830if (*pp - p != 4)831return false;832if (*pp == end || **pp != quote)833return false;834(*pp)++;835}836
837return true;838}
839
840static bool841parse_feature_indices (const char **pp, const char *end, hb_feature_t *feature)842{
843parse_space (pp, end);844
845bool has_start;846
847feature->start = HB_FEATURE_GLOBAL_START;848feature->end = HB_FEATURE_GLOBAL_END;849
850if (!parse_char (pp, end, '['))851return true;852
853has_start = parse_uint (pp, end, &feature->start);854
855if (parse_char (pp, end, ':') || parse_char (pp, end, ';')) {856parse_uint (pp, end, &feature->end);857} else {858if (has_start)859feature->end = feature->start + 1;860}861
862return parse_char (pp, end, ']');863}
864
865static bool866parse_feature_value_postfix (const char **pp, const char *end, hb_feature_t *feature)867{
868bool had_equal = parse_char (pp, end, '=');869bool had_value = parse_uint32 (pp, end, &feature->value) ||870parse_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. */
874return !had_equal || had_value;875}
876
877static bool878parse_one_feature (const char **pp, const char *end, hb_feature_t *feature)879{
880return parse_feature_value_prefix (pp, end, feature) &&881parse_tag (pp, end, &feature->tag) &&882parse_feature_indices (pp, end, feature) &&883parse_feature_value_postfix (pp, end, feature) &&884parse_space (pp, end) &&885*pp == end;886}
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**/
937hb_bool_t
938hb_feature_from_string (const char *str, int len,939hb_feature_t *feature)940{
941hb_feature_t feat;942
943if (len < 0)944len = strlen (str);945
946if (likely (parse_one_feature (&str, str + len, &feat)))947{948if (feature)949*feature = feat;950return true;951}952
953if (feature)954hb_memset (feature, 0, sizeof (*feature));955return false;956}
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**/
970void
971hb_feature_to_string (hb_feature_t *feature,972char *buf, unsigned int size)973{
974if (unlikely (!size)) return;975
976char s[128];977unsigned int len = 0;978if (feature->value == 0)979s[len++] = '-';980hb_tag_to_string (feature->tag, s + len);981len += 4;982while (len && s[len - 1] == ' ')983len--;984if (feature->start != HB_FEATURE_GLOBAL_START || feature->end != HB_FEATURE_GLOBAL_END)985{986s[len++] = '[';987if (feature->start)988len += hb_max (0, snprintf (s + len, ARRAY_LENGTH (s) - len, "%u", feature->start));989if (feature->end != feature->start + 1) {990s[len++] = ':';991if (feature->end != HB_FEATURE_GLOBAL_END)992len += hb_max (0, snprintf (s + len, ARRAY_LENGTH (s) - len, "%u", feature->end));993}994s[len++] = ']';995}996if (feature->value > 1)997{998s[len++] = '=';999len += hb_max (0, snprintf (s + len, ARRAY_LENGTH (s) - len, "%u", feature->value));1000}1001assert (len < ARRAY_LENGTH (s));1002len = hb_min (len, size - 1);1003hb_memcpy (buf, s, len);1004buf[len] = '\0';1005}
1006
1007/* hb_variation_t */
1008
1009static bool1010parse_variation_value (const char **pp, const char *end, hb_variation_t *variation)1011{
1012parse_char (pp, end, '='); /* Optional. */1013double v;1014if (unlikely (!hb_parse_double (pp, end, &v))) return false;1015
1016variation->value = v;1017return true;1018}
1019
1020static bool1021parse_one_variation (const char **pp, const char *end, hb_variation_t *variation)1022{
1023return parse_tag (pp, end, &variation->tag) &&1024parse_variation_value (pp, end, variation) &&1025parse_space (pp, end) &&1026*pp == end;1027}
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*/
1049hb_bool_t
1050hb_variation_from_string (const char *str, int len,1051hb_variation_t *variation)1052{
1053hb_variation_t var;1054
1055if (len < 0)1056len = strlen (str);1057
1058if (likely (parse_one_variation (&str, str + len, &var)))1059{1060if (variation)1061*variation = var;1062return true;1063}1064
1065if (variation)1066hb_memset (variation, 0, sizeof (*variation));1067return false;1068}
1069
1070#ifndef HB_NO_SETLOCALE1071
1072static inline void free_static_C_locale ();1073
1074static struct hb_C_locale_lazy_loader_t : hb_lazy_loader_t<hb_remove_pointer<hb_locale_t>,1075hb_C_locale_lazy_loader_t>1076{
1077static hb_locale_t create ()1078{1079hb_locale_t l = newlocale (LC_ALL_MASK, "C", NULL);1080if (!l)1081return l;1082
1083hb_atexit (free_static_C_locale);1084
1085return l;1086}1087static void destroy (hb_locale_t l)1088{1089freelocale (l);1090}1091static hb_locale_t get_null ()1092{1093return (hb_locale_t) 0;1094}1095} static_C_locale;1096
1097static inline1098void free_static_C_locale ()1099{
1100static_C_locale.free_instance ();1101}
1102
1103static hb_locale_t1104get_C_locale ()1105{
1106return static_C_locale.get_unconst ();1107}
1108
1109#endif1110
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*/
1123void
1124hb_variation_to_string (hb_variation_t *variation,1125char *buf, unsigned int size)1126{
1127if (unlikely (!size)) return;1128
1129char s[128];1130unsigned int len = 0;1131hb_tag_to_string (variation->tag, s + len);1132len += 4;1133while (len && s[len - 1] == ' ')1134len--;1135s[len++] = '=';1136
1137hb_locale_t oldlocale HB_UNUSED;1138oldlocale = hb_uselocale (get_C_locale ());1139len += hb_max (0, snprintf (s + len, ARRAY_LENGTH (s) - len, "%g", (double) variation->value));1140(void) hb_uselocale (oldlocale);1141
1142assert (len < ARRAY_LENGTH (s));1143len = hb_min (len, size - 1);1144hb_memcpy (buf, s, len);1145buf[len] = '\0';1146}
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*/
1158uint8_t
1159(hb_color_get_alpha) (hb_color_t color)1160{
1161return hb_color_get_alpha (color);1162}
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*/
1174uint8_t
1175(hb_color_get_red) (hb_color_t color)1176{
1177return hb_color_get_red (color);1178}
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*/
1190uint8_t
1191(hb_color_get_green) (hb_color_t color)1192{
1193return hb_color_get_green (color);1194}
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*/
1206uint8_t
1207(hb_color_get_blue) (hb_color_t color)1208{
1209return hb_color_get_blue (color);1210}
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_VISIBILITY1217#undef HB_NO_VISIBILITY1218#include "hb-static.cc"1219#define HB_NO_VISIBILITY 11220#endif1221