Merge "Use Harfbuzz for Paint drawText / measureText / breakText APIs"
diff --git a/core/jni/android/graphics/Paint.cpp b/core/jni/android/graphics/Paint.cpp
index 04c7fb9..0a54e17 100644
--- a/core/jni/android/graphics/Paint.cpp
+++ b/core/jni/android/graphics/Paint.cpp
@@ -331,14 +331,19 @@
             return 0;
         }
 
-        const SkPaint* paint = GraphicsJNI::getNativePaint(env, jpaint);
+        SkPaint* paint = GraphicsJNI::getNativePaint(env, jpaint);
         const jchar* textArray = env->GetCharArrayElements(text, NULL);
+        jfloat result = 0;
+#if RTL_USE_HARFBUZZ
+        TextLayout::getTextRunAdvances(paint, textArray, index, count, count, paint->getFlags(),
+                NULL /* dont need all advances */, result);
+#else
         // we double count, since measureText wants a byteLength
         SkScalar width = paint->measureText(textArray + index, count << 1);
-        env->ReleaseCharArrayElements(text, const_cast<jchar*>(textArray),
-                                      JNI_ABORT);
-
-        return SkScalarToFloat(width);
+        result = SkScalarToFloat(width);
+#endif
+        env->ReleaseCharArrayElements(text, const_cast<jchar*>(textArray), JNI_ABORT);
+        return result;
     }
 
     static jfloat measureText_StringII(JNIEnv* env, jobject jpaint, jstring text, int start, int end) {
@@ -347,15 +352,22 @@
 
         SkPaint* paint = GraphicsJNI::getNativePaint(env, jpaint);
         const jchar* textArray = env->GetStringChars(text, NULL);
-        size_t textLength = env->GetStringLength(text);
 
         int count = end - start;
+        size_t textLength = env->GetStringLength(text);
         if ((start | count) < 0 || (size_t)count > textLength) {
             doThrowAIOOBE(env);
             return 0;
         }
+        jfloat width = 0;
 
-        jfloat width = SkScalarToFloat(paint->measureText(textArray + start, count << 1));
+#if RTL_USE_HARFBUZZ
+        TextLayout::getTextRunAdvances(paint, textArray, 0, count, count, paint->getFlags(),
+                NULL /* dont need all advances */, width);
+#else
+
+        width = SkScalarToFloat(paint->measureText(textArray + start, count << 1));
+#endif
         env->ReleaseStringChars(text, textArray);
         return width;
     }
@@ -367,8 +379,13 @@
         SkPaint* paint = GraphicsJNI::getNativePaint(env, jpaint);
         const jchar* textArray = env->GetStringChars(text, NULL);
         size_t textLength = env->GetStringLength(text);
-
-        jfloat width = SkScalarToFloat(paint->measureText(textArray, textLength << 1));
+        jfloat width = 0;
+#if RTL_USE_HARFBUZZ
+        TextLayout::getTextRunAdvances(paint, textArray, 0, textLength, textLength, paint->getFlags(),
+                NULL /* dont need all advances */, width);
+#else
+        width = SkScalarToFloat(paint->measureText(textArray, textLength << 1));
+#endif
         env->ReleaseStringChars(text, textArray);
         return width;
     }
@@ -376,12 +393,19 @@
     static int dotextwidths(JNIEnv* env, SkPaint* paint, const jchar text[], int count, jfloatArray widths) {
         AutoJavaFloatArray autoWidths(env, widths, count);
         jfloat* widthsArray = autoWidths.ptr();
+#if RTL_USE_HARFBUZZ
+        jfloat totalAdvance;
+
+        TextLayout::getTextRunAdvances(paint, text, 0, count, count, paint->getFlags(),
+                widthsArray, totalAdvance);
+#else
         SkScalar* scalarArray = (SkScalar*)widthsArray;
 
         count = paint->getTextWidths(text, count << 1, scalarArray);
         for (int i = 0; i < count; i++) {
             widthsArray[i] = SkScalarToFloat(scalarArray[i]);
         }
+#endif
         return count;
     }
 
@@ -502,6 +526,13 @@
 
     static jint doTextRunCursor(JNIEnv *env, SkPaint* paint, const jchar *text, jint start,
             jint count, jint flags, jint offset, jint opt) {
+#if RTL_USE_HARFBUZZ
+        jfloat scalarArray[count];
+        jfloat totalAdvance;
+
+        TextLayout::getTextRunAdvances(paint, text, start, count, count, flags,
+                scalarArray, totalAdvance);
+#else
         SkScalar scalarArray[count];
         jchar buffer[count];
 
@@ -543,7 +574,7 @@
                 }
             }
         }
-
+#endif
         jint pos = offset - start;
         switch (opt) {
         case AFTER:
diff --git a/core/jni/android/graphics/TextLayout.cpp b/core/jni/android/graphics/TextLayout.cpp
index 2890662..46e6c2b 100644
--- a/core/jni/android/graphics/TextLayout.cpp
+++ b/core/jni/android/graphics/TextLayout.cpp
@@ -259,7 +259,9 @@
     sp<TextLayoutCacheValue> layout = gTextLayoutCache.getValue(
             paint, chars, start, count, contextCount, dirFlags);
     if (layout != NULL) {
-        memcpy(resultAdvances, layout->getAdvances(), layout->getAdvancesCount() * sizeof(jfloat));
+        if (resultAdvances != NULL) {
+            memcpy(resultAdvances, layout->getAdvances(), layout->getAdvancesCount() * sizeof(jfloat));
+        }
         resultTotalAdvance = layout->getTotalAdvance();
     }
 #else