michael@0: /* michael@0: * Copyright © 2009 Red Hat, Inc. michael@0: * Copyright © 2011 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-glib.h" michael@0: michael@0: #include "hb-unicode-private.hh" michael@0: michael@0: michael@0: #if !GLIB_CHECK_VERSION(2,29,14) michael@0: static const hb_script_t michael@0: glib_script_to_script[] = michael@0: { michael@0: HB_SCRIPT_COMMON, michael@0: HB_SCRIPT_INHERITED, michael@0: HB_SCRIPT_ARABIC, michael@0: HB_SCRIPT_ARMENIAN, michael@0: HB_SCRIPT_BENGALI, michael@0: HB_SCRIPT_BOPOMOFO, michael@0: HB_SCRIPT_CHEROKEE, michael@0: HB_SCRIPT_COPTIC, michael@0: HB_SCRIPT_CYRILLIC, michael@0: HB_SCRIPT_DESERET, michael@0: HB_SCRIPT_DEVANAGARI, michael@0: HB_SCRIPT_ETHIOPIC, michael@0: HB_SCRIPT_GEORGIAN, michael@0: HB_SCRIPT_GOTHIC, michael@0: HB_SCRIPT_GREEK, michael@0: HB_SCRIPT_GUJARATI, michael@0: HB_SCRIPT_GURMUKHI, michael@0: HB_SCRIPT_HAN, michael@0: HB_SCRIPT_HANGUL, michael@0: HB_SCRIPT_HEBREW, michael@0: HB_SCRIPT_HIRAGANA, michael@0: HB_SCRIPT_KANNADA, michael@0: HB_SCRIPT_KATAKANA, michael@0: HB_SCRIPT_KHMER, michael@0: HB_SCRIPT_LAO, michael@0: HB_SCRIPT_LATIN, michael@0: HB_SCRIPT_MALAYALAM, michael@0: HB_SCRIPT_MONGOLIAN, michael@0: HB_SCRIPT_MYANMAR, michael@0: HB_SCRIPT_OGHAM, michael@0: HB_SCRIPT_OLD_ITALIC, michael@0: HB_SCRIPT_ORIYA, michael@0: HB_SCRIPT_RUNIC, michael@0: HB_SCRIPT_SINHALA, michael@0: HB_SCRIPT_SYRIAC, michael@0: HB_SCRIPT_TAMIL, michael@0: HB_SCRIPT_TELUGU, michael@0: HB_SCRIPT_THAANA, michael@0: HB_SCRIPT_THAI, michael@0: HB_SCRIPT_TIBETAN, michael@0: HB_SCRIPT_CANADIAN_SYLLABICS, michael@0: HB_SCRIPT_YI, michael@0: HB_SCRIPT_TAGALOG, michael@0: HB_SCRIPT_HANUNOO, michael@0: HB_SCRIPT_BUHID, michael@0: HB_SCRIPT_TAGBANWA, michael@0: michael@0: /* Unicode-4.0 additions */ michael@0: HB_SCRIPT_BRAILLE, michael@0: HB_SCRIPT_CYPRIOT, michael@0: HB_SCRIPT_LIMBU, michael@0: HB_SCRIPT_OSMANYA, michael@0: HB_SCRIPT_SHAVIAN, michael@0: HB_SCRIPT_LINEAR_B, michael@0: HB_SCRIPT_TAI_LE, michael@0: HB_SCRIPT_UGARITIC, michael@0: michael@0: /* Unicode-4.1 additions */ michael@0: HB_SCRIPT_NEW_TAI_LUE, michael@0: HB_SCRIPT_BUGINESE, michael@0: HB_SCRIPT_GLAGOLITIC, michael@0: HB_SCRIPT_TIFINAGH, michael@0: HB_SCRIPT_SYLOTI_NAGRI, michael@0: HB_SCRIPT_OLD_PERSIAN, michael@0: HB_SCRIPT_KHAROSHTHI, michael@0: michael@0: /* Unicode-5.0 additions */ michael@0: HB_SCRIPT_UNKNOWN, michael@0: HB_SCRIPT_BALINESE, michael@0: HB_SCRIPT_CUNEIFORM, michael@0: HB_SCRIPT_PHOENICIAN, michael@0: HB_SCRIPT_PHAGS_PA, michael@0: HB_SCRIPT_NKO, michael@0: michael@0: /* Unicode-5.1 additions */ michael@0: HB_SCRIPT_KAYAH_LI, michael@0: HB_SCRIPT_LEPCHA, michael@0: HB_SCRIPT_REJANG, michael@0: HB_SCRIPT_SUNDANESE, michael@0: HB_SCRIPT_SAURASHTRA, michael@0: HB_SCRIPT_CHAM, michael@0: HB_SCRIPT_OL_CHIKI, michael@0: HB_SCRIPT_VAI, michael@0: HB_SCRIPT_CARIAN, michael@0: HB_SCRIPT_LYCIAN, michael@0: HB_SCRIPT_LYDIAN, michael@0: michael@0: /* Unicode-5.2 additions */ michael@0: HB_SCRIPT_AVESTAN, michael@0: HB_SCRIPT_BAMUM, michael@0: HB_SCRIPT_EGYPTIAN_HIEROGLYPHS, michael@0: HB_SCRIPT_IMPERIAL_ARAMAIC, michael@0: HB_SCRIPT_INSCRIPTIONAL_PAHLAVI, michael@0: HB_SCRIPT_INSCRIPTIONAL_PARTHIAN, michael@0: HB_SCRIPT_JAVANESE, michael@0: HB_SCRIPT_KAITHI, michael@0: HB_SCRIPT_TAI_THAM, michael@0: HB_SCRIPT_LISU, michael@0: HB_SCRIPT_MEETEI_MAYEK, michael@0: HB_SCRIPT_OLD_SOUTH_ARABIAN, michael@0: HB_SCRIPT_OLD_TURKIC, michael@0: HB_SCRIPT_SAMARITAN, michael@0: HB_SCRIPT_TAI_VIET, michael@0: michael@0: /* Unicode-6.0 additions */ michael@0: HB_SCRIPT_BATAK, michael@0: HB_SCRIPT_BRAHMI, michael@0: HB_SCRIPT_MANDAIC, michael@0: michael@0: /* Unicode-6.1 additions */ michael@0: HB_SCRIPT_CHAKMA, michael@0: HB_SCRIPT_MEROITIC_CURSIVE, michael@0: HB_SCRIPT_MEROITIC_HIEROGLYPHS, michael@0: HB_SCRIPT_MIAO, michael@0: HB_SCRIPT_SHARADA, michael@0: HB_SCRIPT_SORA_SOMPENG, michael@0: HB_SCRIPT_TAKRI michael@0: }; michael@0: #endif michael@0: michael@0: hb_script_t michael@0: hb_glib_script_to_script (GUnicodeScript script) michael@0: { michael@0: #if GLIB_CHECK_VERSION(2,29,14) michael@0: return (hb_script_t) g_unicode_script_to_iso15924 (script); michael@0: #else michael@0: if (likely ((unsigned int) script < ARRAY_LENGTH (glib_script_to_script))) michael@0: return glib_script_to_script[script]; michael@0: michael@0: if (unlikely (script == G_UNICODE_SCRIPT_INVALID_CODE)) michael@0: return HB_SCRIPT_INVALID; michael@0: michael@0: return HB_SCRIPT_UNKNOWN; michael@0: #endif michael@0: } michael@0: michael@0: GUnicodeScript michael@0: hb_glib_script_from_script (hb_script_t script) michael@0: { michael@0: #if GLIB_CHECK_VERSION(2,29,14) michael@0: return g_unicode_script_from_iso15924 (script); michael@0: #else michael@0: unsigned int count = ARRAY_LENGTH (glib_script_to_script); michael@0: for (unsigned int i = 0; i < count; i++) michael@0: if (glib_script_to_script[i] == script) michael@0: return (GUnicodeScript) i; michael@0: michael@0: if (unlikely (script == HB_SCRIPT_INVALID)) michael@0: return G_UNICODE_SCRIPT_INVALID_CODE; michael@0: michael@0: return G_UNICODE_SCRIPT_UNKNOWN; michael@0: #endif michael@0: } michael@0: michael@0: michael@0: static hb_unicode_combining_class_t michael@0: hb_glib_unicode_combining_class (hb_unicode_funcs_t *ufuncs HB_UNUSED, michael@0: hb_codepoint_t unicode, michael@0: void *user_data HB_UNUSED) michael@0: michael@0: { michael@0: return (hb_unicode_combining_class_t) g_unichar_combining_class (unicode); michael@0: } michael@0: michael@0: static unsigned int michael@0: hb_glib_unicode_eastasian_width (hb_unicode_funcs_t *ufuncs HB_UNUSED, michael@0: hb_codepoint_t unicode, michael@0: void *user_data HB_UNUSED) michael@0: { michael@0: return g_unichar_iswide (unicode) ? 2 : 1; michael@0: } michael@0: michael@0: static hb_unicode_general_category_t michael@0: hb_glib_unicode_general_category (hb_unicode_funcs_t *ufuncs HB_UNUSED, michael@0: hb_codepoint_t unicode, michael@0: void *user_data HB_UNUSED) michael@0: michael@0: { michael@0: /* hb_unicode_general_category_t and GUnicodeType are identical */ michael@0: return (hb_unicode_general_category_t) g_unichar_type (unicode); michael@0: } michael@0: michael@0: static hb_codepoint_t michael@0: hb_glib_unicode_mirroring (hb_unicode_funcs_t *ufuncs HB_UNUSED, michael@0: hb_codepoint_t unicode, michael@0: void *user_data HB_UNUSED) michael@0: { michael@0: g_unichar_get_mirror_char (unicode, &unicode); michael@0: return unicode; michael@0: } michael@0: michael@0: static hb_script_t michael@0: hb_glib_unicode_script (hb_unicode_funcs_t *ufuncs HB_UNUSED, michael@0: hb_codepoint_t unicode, michael@0: void *user_data HB_UNUSED) michael@0: { michael@0: return hb_glib_script_to_script (g_unichar_get_script (unicode)); michael@0: } michael@0: michael@0: static hb_bool_t michael@0: hb_glib_unicode_compose (hb_unicode_funcs_t *ufuncs HB_UNUSED, michael@0: hb_codepoint_t a, michael@0: hb_codepoint_t b, michael@0: hb_codepoint_t *ab, michael@0: void *user_data HB_UNUSED) michael@0: { michael@0: #if GLIB_CHECK_VERSION(2,29,12) michael@0: return g_unichar_compose (a, b, ab); michael@0: #endif michael@0: michael@0: /* We don't ifdef-out the fallback code such that compiler always michael@0: * sees it and makes sure it's compilable. */ michael@0: michael@0: gchar utf8[12]; michael@0: gchar *normalized; michael@0: int len; michael@0: hb_bool_t ret; michael@0: michael@0: len = g_unichar_to_utf8 (a, utf8); michael@0: len += g_unichar_to_utf8 (b, utf8 + len); michael@0: normalized = g_utf8_normalize (utf8, len, G_NORMALIZE_NFC); michael@0: len = g_utf8_strlen (normalized, -1); michael@0: if (unlikely (!len)) michael@0: return false; michael@0: michael@0: if (len == 1) { michael@0: *ab = g_utf8_get_char (normalized); michael@0: ret = true; michael@0: } else { michael@0: ret = false; michael@0: } michael@0: michael@0: g_free (normalized); michael@0: return ret; michael@0: } michael@0: michael@0: static hb_bool_t michael@0: hb_glib_unicode_decompose (hb_unicode_funcs_t *ufuncs HB_UNUSED, michael@0: hb_codepoint_t ab, michael@0: hb_codepoint_t *a, michael@0: hb_codepoint_t *b, michael@0: void *user_data HB_UNUSED) michael@0: { michael@0: #if GLIB_CHECK_VERSION(2,29,12) michael@0: return g_unichar_decompose (ab, a, b); michael@0: #endif michael@0: michael@0: /* We don't ifdef-out the fallback code such that compiler always michael@0: * sees it and makes sure it's compilable. */ michael@0: michael@0: gchar utf8[6]; michael@0: gchar *normalized; michael@0: int len; michael@0: hb_bool_t ret; michael@0: michael@0: len = g_unichar_to_utf8 (ab, utf8); michael@0: normalized = g_utf8_normalize (utf8, len, G_NORMALIZE_NFD); michael@0: len = g_utf8_strlen (normalized, -1); michael@0: if (unlikely (!len)) michael@0: return false; michael@0: michael@0: if (len == 1) { michael@0: *a = g_utf8_get_char (normalized); michael@0: *b = 0; michael@0: ret = *a != ab; michael@0: } else if (len == 2) { michael@0: *a = g_utf8_get_char (normalized); michael@0: *b = g_utf8_get_char (g_utf8_next_char (normalized)); michael@0: /* Here's the ugly part: if ab decomposes to a single character and michael@0: * that character decomposes again, we have to detect that and undo michael@0: * the second part :-(. */ michael@0: gchar *recomposed = g_utf8_normalize (normalized, -1, G_NORMALIZE_NFC); michael@0: hb_codepoint_t c = g_utf8_get_char (recomposed); michael@0: if (c != ab && c != *a) { michael@0: *a = c; michael@0: *b = 0; michael@0: } michael@0: g_free (recomposed); michael@0: ret = true; michael@0: } else { michael@0: /* If decomposed to more than two characters, take the last one, michael@0: * and recompose the rest to get the first component. */ michael@0: gchar *end = g_utf8_offset_to_pointer (normalized, len - 1); michael@0: gchar *recomposed; michael@0: *b = g_utf8_get_char (end); michael@0: recomposed = g_utf8_normalize (normalized, end - normalized, G_NORMALIZE_NFC); michael@0: /* We expect that recomposed has exactly one character now. */ michael@0: *a = g_utf8_get_char (recomposed); michael@0: g_free (recomposed); michael@0: ret = true; michael@0: } michael@0: michael@0: g_free (normalized); michael@0: return ret; michael@0: } michael@0: michael@0: static unsigned int michael@0: hb_glib_unicode_decompose_compatibility (hb_unicode_funcs_t *ufuncs HB_UNUSED, michael@0: hb_codepoint_t u, michael@0: hb_codepoint_t *decomposed, michael@0: void *user_data HB_UNUSED) michael@0: { michael@0: #if GLIB_CHECK_VERSION(2,29,12) michael@0: return g_unichar_fully_decompose (u, TRUE, decomposed, HB_UNICODE_MAX_DECOMPOSITION_LEN); michael@0: #endif michael@0: michael@0: /* If the user doesn't have GLib >= 2.29.12 we have to perform michael@0: * a round trip to UTF-8 and the associated memory management dance. */ michael@0: gchar utf8[6]; michael@0: gchar *utf8_decomposed, *c; michael@0: gsize utf8_len, utf8_decomposed_len, i; michael@0: michael@0: /* Convert @u to UTF-8 and normalise it in NFKD mode. This performs the compatibility decomposition. */ michael@0: utf8_len = g_unichar_to_utf8 (u, utf8); michael@0: utf8_decomposed = g_utf8_normalize (utf8, utf8_len, G_NORMALIZE_NFKD); michael@0: utf8_decomposed_len = g_utf8_strlen (utf8_decomposed, -1); michael@0: michael@0: assert (utf8_decomposed_len <= HB_UNICODE_MAX_DECOMPOSITION_LEN); michael@0: michael@0: for (i = 0, c = utf8_decomposed; i < utf8_decomposed_len; i++, c = g_utf8_next_char (c)) michael@0: *decomposed++ = g_utf8_get_char (c); michael@0: michael@0: g_free (utf8_decomposed); michael@0: michael@0: return utf8_decomposed_len; michael@0: } michael@0: michael@0: hb_unicode_funcs_t * michael@0: hb_glib_get_unicode_funcs (void) michael@0: { michael@0: static const hb_unicode_funcs_t _hb_glib_unicode_funcs = { michael@0: HB_OBJECT_HEADER_STATIC, michael@0: michael@0: NULL, /* parent */ michael@0: true, /* immutable */ michael@0: { michael@0: #define HB_UNICODE_FUNC_IMPLEMENT(name) hb_glib_unicode_##name, michael@0: HB_UNICODE_FUNCS_IMPLEMENT_CALLBACKS michael@0: #undef HB_UNICODE_FUNC_IMPLEMENT michael@0: } michael@0: }; michael@0: michael@0: return const_cast (&_hb_glib_unicode_funcs); michael@0: } michael@0: