Upgrade to latest harfbuzz
[framework/uifw/harfbuzz.git] / src / hb-glib.cc
index 7a177e4..6b655dd 100644 (file)
 
 #include "hb-unicode-private.hh"
 
-#include <glib.h>
-
-HB_BEGIN_DECLS
 
+#if !GLIB_CHECK_VERSION(2,29,14)
 static const hb_script_t
 glib_script_to_script[] =
 {
@@ -146,12 +144,25 @@ glib_script_to_script[] =
   /* Unicode-6.0 additions */
   HB_SCRIPT_BATAK,
   HB_SCRIPT_BRAHMI,
-  HB_SCRIPT_MANDAIC
+  HB_SCRIPT_MANDAIC,
+
+  /* Unicode-6.1 additions */
+  HB_SCRIPT_CHAKMA,
+  HB_SCRIPT_MEROITIC_CURSIVE,
+  HB_SCRIPT_MEROITIC_HIEROGLYPHS,
+  HB_SCRIPT_MIAO,
+  HB_SCRIPT_SHARADA,
+  HB_SCRIPT_SORA_SOMPENG,
+  HB_SCRIPT_TAKRI
 };
+#endif
 
 hb_script_t
 hb_glib_script_to_script (GUnicodeScript script)
 {
+#if GLIB_CHECK_VERSION(2,29,14)
+  return (hb_script_t) g_unicode_script_to_iso15924 (script);
+#else
   if (likely ((unsigned int) script < ARRAY_LENGTH (glib_script_to_script)))
     return glib_script_to_script[script];
 
@@ -159,11 +170,15 @@ hb_glib_script_to_script (GUnicodeScript script)
     return HB_SCRIPT_INVALID;
 
   return HB_SCRIPT_UNKNOWN;
+#endif
 }
 
 GUnicodeScript
 hb_glib_script_from_script (hb_script_t script)
 {
+#if GLIB_CHECK_VERSION(2,29,14)
+  return g_unicode_script_from_iso15924 (script);
+#else
   unsigned int count = ARRAY_LENGTH (glib_script_to_script);
   for (unsigned int i = 0; i < count; i++)
     if (glib_script_to_script[i] == script)
@@ -173,6 +188,7 @@ hb_glib_script_from_script (hb_script_t script)
     return G_UNICODE_SCRIPT_INVALID_CODE;
 
   return G_UNICODE_SCRIPT_UNKNOWN;
+#endif
 }
 
 
@@ -234,6 +250,9 @@ hb_glib_unicode_compose (hb_unicode_funcs_t *ufuncs HB_UNUSED,
   /* We don't ifdef-out the fallback code such that compiler always
    * sees it and makes sure it's compilable. */
 
+  if (!a || !b)
+    return false;
+
   gchar utf8[12];
   gchar *normalized;
   gint len;
@@ -242,13 +261,15 @@ hb_glib_unicode_compose (hb_unicode_funcs_t *ufuncs HB_UNUSED,
   len = g_unichar_to_utf8 (a, utf8);
   len += g_unichar_to_utf8 (b, utf8 + len);
   normalized = g_utf8_normalize (utf8, len, G_NORMALIZE_NFC);
-
   len = g_utf8_strlen (normalized, -1);
+  if (unlikely (!len))
+    return false;
+
   if (len == 1) {
     *ab = g_utf8_get_char (normalized);
-    ret = TRUE;
+    ret = true;
   } else {
-    ret = FALSE;
+    ret = false;
   }
 
   g_free (normalized);
@@ -276,8 +297,10 @@ hb_glib_unicode_decompose (hb_unicode_funcs_t *ufuncs HB_UNUSED,
 
   len = g_unichar_to_utf8 (ab, utf8);
   normalized = g_utf8_normalize (utf8, len, G_NORMALIZE_NFD);
-
   len = g_utf8_strlen (normalized, -1);
+  if (unlikely (!len))
+    return false;
+
   if (len == 1) {
     *a = g_utf8_get_char (normalized);
     *b = 0;
@@ -285,7 +308,17 @@ hb_glib_unicode_decompose (hb_unicode_funcs_t *ufuncs HB_UNUSED,
   } else if (len == 2) {
     *a = g_utf8_get_char (normalized);
     *b = g_utf8_get_char (g_utf8_next_char (normalized));
-    ret = TRUE;
+    /* Here's the ugly part: if ab decomposes to a single character and
+     * that character decomposes again, we have to detect that and undo
+     * the second part :-(. */
+    gchar *recomposed = g_utf8_normalize (normalized, -1, G_NORMALIZE_NFC);
+    hb_codepoint_t c = g_utf8_get_char (recomposed);
+    if (c != ab && c != *a) {
+      *a = c;
+      *b = 0;
+    }
+    g_free (recomposed);
+    ret = true;
   } else {
     /* If decomposed to more than two characters, take the last one,
      * and recompose the rest to get the first component. */
@@ -296,7 +329,7 @@ hb_glib_unicode_decompose (hb_unicode_funcs_t *ufuncs HB_UNUSED,
     /* We expect that recomposed has exactly one character now. */
     *a = g_utf8_get_char (recomposed);
     g_free (recomposed);
-    ret = TRUE;
+    ret = true;
   }
 
   g_free (normalized);
@@ -304,12 +337,12 @@ hb_glib_unicode_decompose (hb_unicode_funcs_t *ufuncs HB_UNUSED,
 }
 
 
-extern HB_INTERNAL hb_unicode_funcs_t _hb_unicode_funcs_glib;
-hb_unicode_funcs_t _hb_glib_unicode_funcs = {
+extern HB_INTERNAL const hb_unicode_funcs_t _hb_glib_unicode_funcs;
+const hb_unicode_funcs_t _hb_glib_unicode_funcs = {
   HB_OBJECT_HEADER_STATIC,
 
   NULL, /* parent */
-  TRUE, /* immutable */
+  true, /* immutable */
   {
 #define HB_UNICODE_FUNC_IMPLEMENT(name) hb_glib_unicode_##name,
     HB_UNICODE_FUNCS_IMPLEMENT_CALLBACKS
@@ -320,8 +353,6 @@ hb_unicode_funcs_t _hb_glib_unicode_funcs = {
 hb_unicode_funcs_t *
 hb_glib_get_unicode_funcs (void)
 {
-  return &_hb_glib_unicode_funcs;
+  return const_cast<hb_unicode_funcs_t *> (&_hb_glib_unicode_funcs);
 }
 
-
-HB_END_DECLS