Fix Harbuzz shaper for being able to do select other scripts than Common or Arabic

- need to take care of diacritics marks that return HB_Script_Inherited for a script

Change-Id: Icbfea46b305e15849b25410fed07d9cd5dfeb818
diff --git a/core/jni/Android.mk b/core/jni/Android.mk
index 6e73889..170957c 100644
--- a/core/jni/Android.mk
+++ b/core/jni/Android.mk
@@ -178,6 +178,7 @@
 	external/icu4c/i18n \
 	external/icu4c/common \
 	external/jpeg \
+	external/harfbuzz/contrib \
 	external/harfbuzz/src \
 	external/zlib \
 	frameworks/opt/emoji \
diff --git a/core/jni/android/graphics/TextLayoutCache.cpp b/core/jni/android/graphics/TextLayoutCache.cpp
index 30fe298..23a4ec7 100644
--- a/core/jni/android/graphics/TextLayoutCache.cpp
+++ b/core/jni/android/graphics/TextLayoutCache.cpp
@@ -17,6 +17,10 @@
 #include "TextLayoutCache.h"
 #include "TextLayout.h"
 
+extern "C" {
+  #include "harfbuzz-unicode.h"
+}
+
 namespace android {
 
 TextLayoutCache::TextLayoutCache() :
@@ -355,7 +359,32 @@
     shaperItem->item.pos = start;
     shaperItem->item.length = count;
     shaperItem->item.bidiLevel = isRTL;
-    shaperItem->item.script = isRTL ? HB_Script_Arabic : HB_Script_Common;
+
+    ssize_t nextCodePoint = 0;
+    uint32_t codePoint = utf16_to_code_point(chars, count, &nextCodePoint);
+
+    HB_Script script = code_point_to_script(codePoint);
+
+    if (script == HB_Script_Inherited) {
+#if DEBUG_GLYPHS
+        LOGD("Cannot find a correct script for code point=%d "
+                " Need to look at the next code points.", codePoint);
+#endif
+        while (script == HB_Script_Inherited && nextCodePoint < count) {
+            codePoint = utf16_to_code_point(chars, count, &nextCodePoint);
+            script = code_point_to_script(codePoint);
+        }
+    }
+
+    if (script == HB_Script_Inherited) {
+#if DEBUG_GLYPHS
+        LOGD("Cannot find a correct script from the text."
+                " Need to select a default script depending on the passed text direction.");
+#endif
+        script = isRTL ? HB_Script_Arabic : HB_Script_Common;
+    }
+
+    shaperItem->item.script = script;
 
     shaperItem->string = chars;
     shaperItem->stringLength = contextCount;