Dealing with ghost spaces

Change-Id: I9cf133e915658f17d00f279ee1fa2662effa2021
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/231646
Commit-Queue: Julia Lavrova <jlavrova@google.com>
Reviewed-by: Ben Wagner <bungeman@google.com>
Reviewed-by: Julia Lavrova <jlavrova@google.com>
diff --git a/modules/skparagraph/src/Run.cpp b/modules/skparagraph/src/Run.cpp
index 9517451..90996fa 100644
--- a/modules/skparagraph/src/Run.cpp
+++ b/modules/skparagraph/src/Run.cpp
@@ -4,6 +4,15 @@
 #include "include/core/SkFontMetrics.h"
 #include "modules/skparagraph/src/ParagraphImpl.h"
 #include <algorithm>
+#include "src/utils/SkUTF.h"
+
+namespace {
+
+SkUnichar utf8_next(const char** ptr, const char* end) {
+    SkUnichar val = SkUTF::NextUTF8(ptr, end);
+    return val < 0 ? 0xFFFD : val;
+}
+}
 
 namespace skia {
 namespace textlayout {
@@ -198,12 +207,14 @@
 }
 
 void Cluster::setIsWhiteSpaces() {
-    auto text = fMaster->text();
-    auto pos = fTextRange.end;
-    while (--pos >= fTextRange.start) {
-        auto ch = text[pos];
-        if (!u_isspace(ch) && u_charType(ch) != U_CONTROL_CHAR &&
-            u_charType(ch) != U_NON_SPACING_MARK) {
+
+    fWhiteSpaces = false;
+
+    auto span = fMaster->text(fTextRange);
+    const char* ch = span.begin();
+    while (ch < span.end()) {
+        auto unichar = utf8_next(&ch, span.end());
+        if (!u_isWhitespace(unichar)) {
             return;
         }
     }