michael@0: /* michael@0: * Copyright © 2009,2010 Red Hat, Inc. michael@0: * Copyright © 2011,2012 Google, Inc. michael@0: * michael@0: * This is part of HarfBuzz, a text shaping library. michael@0: * michael@0: * Permission is hereby granted, without written agreement and without michael@0: * license or royalty fees, to use, copy, modify, and distribute this michael@0: * software and its documentation for any purpose, provided that the michael@0: * above copyright notice and the following two paragraphs appear in michael@0: * all copies of this software. michael@0: * michael@0: * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR michael@0: * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES michael@0: * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN michael@0: * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH michael@0: * DAMAGE. michael@0: * michael@0: * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, michael@0: * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND michael@0: * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS michael@0: * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO michael@0: * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. michael@0: * michael@0: * Red Hat Author(s): Behdad Esfahbod michael@0: * Google Author(s): Behdad Esfahbod michael@0: */ michael@0: michael@0: #include "hb-private.hh" michael@0: michael@0: #include "hb-mutex-private.hh" michael@0: #include "hb-object-private.hh" michael@0: michael@0: #include michael@0: michael@0: michael@0: /* hb_options_t */ michael@0: michael@0: hb_options_union_t _hb_options; michael@0: michael@0: void michael@0: _hb_options_init (void) michael@0: { michael@0: hb_options_union_t u; michael@0: u.i = 0; michael@0: u.opts.initialized = 1; michael@0: michael@0: char *c = getenv ("HB_OPTIONS"); michael@0: u.opts.uniscribe_bug_compatible = c && strstr (c, "uniscribe-bug-compatible"); michael@0: michael@0: /* This is idempotent and threadsafe. */ michael@0: _hb_options = u; michael@0: } michael@0: michael@0: michael@0: /* hb_tag_t */ michael@0: michael@0: /** michael@0: * hb_tag_from_string: michael@0: * @str: (array length=len): michael@0: * @len: michael@0: * michael@0: * michael@0: * michael@0: * Return value: michael@0: * michael@0: * Since: 1.0 michael@0: **/ michael@0: hb_tag_t michael@0: hb_tag_from_string (const char *str, int len) michael@0: { michael@0: char tag[4]; michael@0: unsigned int i; michael@0: michael@0: if (!str || !len || !*str) michael@0: return HB_TAG_NONE; michael@0: michael@0: if (len < 0 || len > 4) michael@0: len = 4; michael@0: for (i = 0; i < (unsigned) len && str[i]; i++) michael@0: tag[i] = str[i]; michael@0: for (; i < 4; i++) michael@0: tag[i] = ' '; michael@0: michael@0: return HB_TAG_CHAR4 (tag); michael@0: } michael@0: michael@0: /** michael@0: * hb_tag_to_string: michael@0: * @tag: michael@0: * @buf: (array fixed-size=4): michael@0: * michael@0: * michael@0: * michael@0: * Since: 1.0 michael@0: **/ michael@0: void michael@0: hb_tag_to_string (hb_tag_t tag, char *buf) michael@0: { michael@0: buf[0] = (char) (uint8_t) (tag >> 24); michael@0: buf[1] = (char) (uint8_t) (tag >> 16); michael@0: buf[2] = (char) (uint8_t) (tag >> 8); michael@0: buf[3] = (char) (uint8_t) (tag >> 0); michael@0: } michael@0: michael@0: michael@0: /* hb_direction_t */ michael@0: michael@0: const char direction_strings[][4] = { michael@0: "ltr", michael@0: "rtl", michael@0: "ttb", michael@0: "btt" michael@0: }; michael@0: michael@0: /** michael@0: * hb_direction_from_string: michael@0: * @str: (array length=len): michael@0: * @len: michael@0: * michael@0: * michael@0: * michael@0: * Return value: michael@0: * michael@0: * Since: 1.0 michael@0: **/ michael@0: hb_direction_t michael@0: hb_direction_from_string (const char *str, int len) michael@0: { michael@0: if (unlikely (!str || !len || !*str)) michael@0: return HB_DIRECTION_INVALID; michael@0: michael@0: /* Lets match loosely: just match the first letter, such that michael@0: * all of "ltr", "left-to-right", etc work! michael@0: */ michael@0: char c = TOLOWER (str[0]); michael@0: for (unsigned int i = 0; i < ARRAY_LENGTH (direction_strings); i++) michael@0: if (c == direction_strings[i][0]) michael@0: return (hb_direction_t) (HB_DIRECTION_LTR + i); michael@0: michael@0: return HB_DIRECTION_INVALID; michael@0: } michael@0: michael@0: /** michael@0: * hb_direction_to_string: michael@0: * @direction: michael@0: * michael@0: * michael@0: * michael@0: * Return value: (transfer none): michael@0: * michael@0: * Since: 1.0 michael@0: **/ michael@0: const char * michael@0: hb_direction_to_string (hb_direction_t direction) michael@0: { michael@0: if (likely ((unsigned int) (direction - HB_DIRECTION_LTR) michael@0: < ARRAY_LENGTH (direction_strings))) michael@0: return direction_strings[direction - HB_DIRECTION_LTR]; michael@0: michael@0: return "invalid"; michael@0: } michael@0: michael@0: michael@0: /* hb_language_t */ michael@0: michael@0: struct hb_language_impl_t { michael@0: const char s[1]; michael@0: }; michael@0: michael@0: static const char canon_map[256] = { michael@0: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, michael@0: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, michael@0: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '-', 0, 0, michael@0: '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 0, 0, 0, 0, 0, 0, michael@0: '-', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', michael@0: 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 0, 0, 0, 0, '-', michael@0: 0, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', michael@0: 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 0, 0, 0, 0, 0 michael@0: }; michael@0: michael@0: static hb_bool_t michael@0: lang_equal (hb_language_t v1, michael@0: const void *v2) michael@0: { michael@0: const unsigned char *p1 = (const unsigned char *) v1; michael@0: const unsigned char *p2 = (const unsigned char *) v2; michael@0: michael@0: while (*p1 && *p1 == canon_map[*p2]) michael@0: p1++, p2++; michael@0: michael@0: return *p1 == canon_map[*p2]; michael@0: } michael@0: michael@0: #if 0 michael@0: static unsigned int michael@0: lang_hash (const void *key) michael@0: { michael@0: const unsigned char *p = key; michael@0: unsigned int h = 0; michael@0: while (canon_map[*p]) michael@0: { michael@0: h = (h << 5) - h + canon_map[*p]; michael@0: p++; michael@0: } michael@0: michael@0: return h; michael@0: } michael@0: #endif michael@0: michael@0: michael@0: struct hb_language_item_t { michael@0: michael@0: struct hb_language_item_t *next; michael@0: hb_language_t lang; michael@0: michael@0: inline bool operator == (const char *s) const { michael@0: return lang_equal (lang, s); michael@0: } michael@0: michael@0: inline hb_language_item_t & operator = (const char *s) { michael@0: lang = (hb_language_t) strdup (s); michael@0: for (unsigned char *p = (unsigned char *) lang; *p; p++) michael@0: *p = canon_map[*p]; michael@0: michael@0: return *this; michael@0: } michael@0: michael@0: void finish (void) { free ((void *) lang); } michael@0: }; michael@0: michael@0: michael@0: /* Thread-safe lock-free language list */ michael@0: michael@0: static hb_language_item_t *langs; michael@0: michael@0: static inline michael@0: void free_langs (void) michael@0: { michael@0: while (langs) { michael@0: hb_language_item_t *next = langs->next; michael@0: langs->finish (); michael@0: free (langs); michael@0: langs = next; michael@0: } michael@0: } michael@0: michael@0: static hb_language_item_t * michael@0: lang_find_or_insert (const char *key) michael@0: { michael@0: retry: michael@0: hb_language_item_t *first_lang = (hb_language_item_t *) hb_atomic_ptr_get (&langs); michael@0: michael@0: for (hb_language_item_t *lang = first_lang; lang; lang = lang->next) michael@0: if (*lang == key) michael@0: return lang; michael@0: michael@0: /* Not found; allocate one. */ michael@0: hb_language_item_t *lang = (hb_language_item_t *) calloc (1, sizeof (hb_language_item_t)); michael@0: if (unlikely (!lang)) michael@0: return NULL; michael@0: lang->next = first_lang; michael@0: *lang = key; michael@0: michael@0: if (!hb_atomic_ptr_cmpexch (&langs, first_lang, lang)) { michael@0: free (lang); michael@0: goto retry; michael@0: } michael@0: michael@0: #ifdef HAVE_ATEXIT michael@0: if (!first_lang) michael@0: atexit (free_langs); /* First person registers atexit() callback. */ michael@0: #endif michael@0: michael@0: return lang; michael@0: } michael@0: michael@0: michael@0: /** michael@0: * hb_language_from_string: michael@0: * @str: (array length=len): michael@0: * @len: michael@0: * michael@0: * michael@0: * michael@0: * Return value: michael@0: * michael@0: * Since: 1.0 michael@0: **/ michael@0: hb_language_t michael@0: hb_language_from_string (const char *str, int len) michael@0: { michael@0: char strbuf[64]; michael@0: michael@0: if (!str || !len || !*str) michael@0: return HB_LANGUAGE_INVALID; michael@0: michael@0: if (len >= 0) michael@0: { michael@0: len = MIN (len, (int) sizeof (strbuf) - 1); michael@0: str = (char *) memcpy (strbuf, str, len); michael@0: strbuf[len] = '\0'; michael@0: } michael@0: michael@0: hb_language_item_t *item = lang_find_or_insert (str); michael@0: michael@0: return likely (item) ? item->lang : HB_LANGUAGE_INVALID; michael@0: } michael@0: michael@0: /** michael@0: * hb_language_to_string: michael@0: * @language: michael@0: * michael@0: * michael@0: * michael@0: * Return value: (transfer none): michael@0: * michael@0: * Since: 1.0 michael@0: **/ michael@0: const char * michael@0: hb_language_to_string (hb_language_t language) michael@0: { michael@0: /* This is actually NULL-safe! */ michael@0: return language->s; michael@0: } michael@0: michael@0: /** michael@0: * hb_language_get_default: michael@0: * michael@0: * michael@0: * michael@0: * Return value: michael@0: * michael@0: * Since: 1.0 michael@0: **/ michael@0: hb_language_t michael@0: hb_language_get_default (void) michael@0: { michael@0: static hb_language_t default_language = HB_LANGUAGE_INVALID; michael@0: michael@0: hb_language_t language = (hb_language_t) hb_atomic_ptr_get (&default_language); michael@0: if (unlikely (language == HB_LANGUAGE_INVALID)) { michael@0: language = hb_language_from_string (setlocale (LC_CTYPE, NULL), -1); michael@0: hb_atomic_ptr_cmpexch (&default_language, HB_LANGUAGE_INVALID, language); michael@0: } michael@0: michael@0: return default_language; michael@0: } michael@0: michael@0: michael@0: /* hb_script_t */ michael@0: michael@0: /** michael@0: * hb_script_from_iso15924_tag: michael@0: * @tag: michael@0: * michael@0: * michael@0: * michael@0: * Return value: michael@0: * michael@0: * Since: 1.0 michael@0: **/ michael@0: hb_script_t michael@0: hb_script_from_iso15924_tag (hb_tag_t tag) michael@0: { michael@0: if (unlikely (tag == HB_TAG_NONE)) michael@0: return HB_SCRIPT_INVALID; michael@0: michael@0: /* Be lenient, adjust case (one capital letter followed by three small letters) */ michael@0: tag = (tag & 0xDFDFDFDF) | 0x00202020; michael@0: michael@0: switch (tag) { michael@0: michael@0: /* These graduated from the 'Q' private-area codes, but michael@0: * the old code is still aliased by Unicode, and the Qaai michael@0: * one in use by ICU. */ michael@0: case HB_TAG('Q','a','a','i'): return HB_SCRIPT_INHERITED; michael@0: case HB_TAG('Q','a','a','c'): return HB_SCRIPT_COPTIC; michael@0: michael@0: /* Script variants from http://unicode.org/iso15924/ */ michael@0: case HB_TAG('C','y','r','s'): return HB_SCRIPT_CYRILLIC; michael@0: case HB_TAG('L','a','t','f'): return HB_SCRIPT_LATIN; michael@0: case HB_TAG('L','a','t','g'): return HB_SCRIPT_LATIN; michael@0: case HB_TAG('S','y','r','e'): return HB_SCRIPT_SYRIAC; michael@0: case HB_TAG('S','y','r','j'): return HB_SCRIPT_SYRIAC; michael@0: case HB_TAG('S','y','r','n'): return HB_SCRIPT_SYRIAC; michael@0: } michael@0: michael@0: /* If it looks right, just use the tag as a script */ michael@0: if (((uint32_t) tag & 0xE0E0E0E0) == 0x40606060) michael@0: return (hb_script_t) tag; michael@0: michael@0: /* Otherwise, return unknown */ michael@0: return HB_SCRIPT_UNKNOWN; michael@0: } michael@0: michael@0: /** michael@0: * hb_script_from_string: michael@0: * @s: (array length=len): michael@0: * @len: michael@0: * michael@0: * michael@0: * michael@0: * Return value: michael@0: * michael@0: * Since: 1.0 michael@0: **/ michael@0: hb_script_t michael@0: hb_script_from_string (const char *s, int len) michael@0: { michael@0: return hb_script_from_iso15924_tag (hb_tag_from_string (s, len)); michael@0: } michael@0: michael@0: /** michael@0: * hb_script_to_iso15924_tag: michael@0: * @script: michael@0: * michael@0: * michael@0: * michael@0: * Return value: michael@0: * michael@0: * Since: 1.0 michael@0: **/ michael@0: hb_tag_t michael@0: hb_script_to_iso15924_tag (hb_script_t script) michael@0: { michael@0: return (hb_tag_t) script; michael@0: } michael@0: michael@0: /** michael@0: * hb_script_get_horizontal_direction: michael@0: * @script: michael@0: * michael@0: * michael@0: * michael@0: * Return value: michael@0: * michael@0: * Since: 1.0 michael@0: **/ michael@0: hb_direction_t michael@0: hb_script_get_horizontal_direction (hb_script_t script) michael@0: { michael@0: /* http://goo.gl/x9ilM */ michael@0: switch ((hb_tag_t) script) michael@0: { michael@0: /* Unicode-1.1 additions */ michael@0: case HB_SCRIPT_ARABIC: michael@0: case HB_SCRIPT_HEBREW: michael@0: michael@0: /* Unicode-3.0 additions */ michael@0: case HB_SCRIPT_SYRIAC: michael@0: case HB_SCRIPT_THAANA: michael@0: michael@0: /* Unicode-4.0 additions */ michael@0: case HB_SCRIPT_CYPRIOT: michael@0: michael@0: /* Unicode-4.1 additions */ michael@0: case HB_SCRIPT_KHAROSHTHI: michael@0: michael@0: /* Unicode-5.0 additions */ michael@0: case HB_SCRIPT_PHOENICIAN: michael@0: case HB_SCRIPT_NKO: michael@0: michael@0: /* Unicode-5.1 additions */ michael@0: case HB_SCRIPT_LYDIAN: michael@0: michael@0: /* Unicode-5.2 additions */ michael@0: case HB_SCRIPT_AVESTAN: michael@0: case HB_SCRIPT_IMPERIAL_ARAMAIC: michael@0: case HB_SCRIPT_INSCRIPTIONAL_PAHLAVI: michael@0: case HB_SCRIPT_INSCRIPTIONAL_PARTHIAN: michael@0: case HB_SCRIPT_OLD_SOUTH_ARABIAN: michael@0: case HB_SCRIPT_OLD_TURKIC: michael@0: case HB_SCRIPT_SAMARITAN: michael@0: michael@0: /* Unicode-6.0 additions */ michael@0: case HB_SCRIPT_MANDAIC: michael@0: michael@0: /* Unicode-6.1 additions */ michael@0: case HB_SCRIPT_MEROITIC_CURSIVE: michael@0: case HB_SCRIPT_MEROITIC_HIEROGLYPHS: michael@0: michael@0: return HB_DIRECTION_RTL; michael@0: } michael@0: michael@0: return HB_DIRECTION_LTR; michael@0: } michael@0: michael@0: michael@0: /* hb_user_data_array_t */ michael@0: michael@0: bool michael@0: hb_user_data_array_t::set (hb_user_data_key_t *key, michael@0: void * data, michael@0: hb_destroy_func_t destroy, michael@0: hb_bool_t replace) michael@0: { michael@0: if (!key) michael@0: return false; michael@0: michael@0: if (replace) { michael@0: if (!data && !destroy) { michael@0: items.remove (key, lock); michael@0: return true; michael@0: } michael@0: } michael@0: hb_user_data_item_t item = {key, data, destroy}; michael@0: bool ret = !!items.replace_or_insert (item, lock, replace); michael@0: michael@0: return ret; michael@0: } michael@0: michael@0: void * michael@0: hb_user_data_array_t::get (hb_user_data_key_t *key) michael@0: { michael@0: hb_user_data_item_t item = {NULL }; michael@0: michael@0: return items.find (key, &item, lock) ? item.data : NULL; michael@0: } michael@0: michael@0: michael@0: /* hb_version */ michael@0: michael@0: /** michael@0: * hb_version: michael@0: * @major: (out): Library major version component. michael@0: * @minor: (out): Library minor version component. michael@0: * @micro: (out): Library micro version component. michael@0: * michael@0: * Returns library version as three integer components. michael@0: * michael@0: * Since: 1.0 michael@0: **/ michael@0: void michael@0: hb_version (unsigned int *major, michael@0: unsigned int *minor, michael@0: unsigned int *micro) michael@0: { michael@0: *major = HB_VERSION_MAJOR; michael@0: *minor = HB_VERSION_MINOR; michael@0: *micro = HB_VERSION_MICRO; michael@0: } michael@0: michael@0: /** michael@0: * hb_version_string: michael@0: * michael@0: * Returns library version as a string with three components. michael@0: * michael@0: * Return value: library version string. michael@0: * michael@0: * Since: 1.0 michael@0: **/ michael@0: const char * michael@0: hb_version_string (void) michael@0: { michael@0: return HB_VERSION_STRING; michael@0: } michael@0: michael@0: /** michael@0: * hb_version_check: michael@0: * @major: michael@0: * @minor: michael@0: * @micro: michael@0: * michael@0: * michael@0: * michael@0: * Return value: michael@0: * michael@0: * Since: 1.0 michael@0: **/ michael@0: hb_bool_t michael@0: hb_version_check (unsigned int major, michael@0: unsigned int minor, michael@0: unsigned int micro) michael@0: { michael@0: return HB_VERSION_CHECK (major, minor, micro); michael@0: }