Small changes

Change-Id: Ic9c41a2dd11b4df8ab24037df0109e36536ec6c3
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/257892
Reviewed-by: Ben Wagner <bungeman@google.com>
Commit-Queue: Julia Lavrova <jlavrova@google.com>
diff --git a/modules/skparagraph/src/TextWrapper.cpp b/modules/skparagraph/src/TextWrapper.cpp
index 98fc5a4..4f69c46 100644
--- a/modules/skparagraph/src/TextWrapper.cpp
+++ b/modules/skparagraph/src/TextWrapper.cpp
@@ -35,9 +35,7 @@
                 break;
             }
             if (cluster->width() > maxWidth) {
-                // Break the cluster into parts by glyph position
-                auto delta = maxWidth - (fWords.width() + fClusters.width());
-                fClip.extend(cluster, cluster->roundPos(delta));
+                fClusters.extend(cluster);
                 fTooLongCluster = true;
                 fTooLongWord = true;
                 break;
@@ -53,11 +51,9 @@
             }
             if (nextWordLength > maxWidth) {
                 // If the word is too long we can break it right now and hope it's enough
+                fMinIntrinsicWidth = SkTMax(fMinIntrinsicWidth, nextWordLength);
                 fTooLongWord = true;
             }
-
-            // TODO: this is the place when we use hyphenation
-            fMinIntrinsicWidth = SkTMax(fMinIntrinsicWidth, fTooLongWord ? maxWidth : nextWordLength);
             break;
         }
 
@@ -83,8 +79,10 @@
         } else if (fClusters.width() > 0) {
             fEndLine.extend(fClusters);
             fTooLongWord = false;
+            fTooLongCluster = false;
         } else if (fClip.width() > 0 || (fTooLongWord && fTooLongCluster)) {
-            fEndLine.extend(fClip);
+            // Flutter: forget the clipped cluster but keep the metrics
+            fEndLine.metrics().add(fClip.metrics());
             fTooLongWord = false;
             fTooLongCluster = false;
         } else {
@@ -159,8 +157,8 @@
                                      SkScalar maxWidth,
                                      const AddLineToParagraph& addLine) {
     fHeight = 0;
-    fMinIntrinsicWidth = 0;
-    fMaxIntrinsicWidth = 0;
+    fMinIntrinsicWidth = std::numeric_limits<SkScalar>::min();
+    fMaxIntrinsicWidth = std::numeric_limits<SkScalar>::min();
 
     auto span = parent->clusters();
     if (span.size() == 0) {
@@ -268,16 +266,35 @@
     }
 
     // We finished formatting the text but we need to scan the rest for some numbers
-    auto cluster = fEndLine.endCluster();
-    while (cluster != end) {
-        fExceededMaxLines = true;
-        if (cluster->isHardBreak()) {
-            softLineMaxIntrinsicWidth = 0;
-        } else {
-            softLineMaxIntrinsicWidth += cluster->width();
-            fMaxIntrinsicWidth = SkTMax(fMaxIntrinsicWidth, softLineMaxIntrinsicWidth);
+    if (fEndLine.breakCluster() != nullptr) {
+        auto lastWordLength = 0.0f;
+        auto cluster = fEndLine.breakCluster();
+        if (cluster != end) {
+            ++cluster;
         }
-        ++cluster;
+        while (cluster != end || cluster->endPos() < end->endPos()) {
+            fExceededMaxLines = true;
+            if (cluster->isHardBreak()) {
+                fMaxIntrinsicWidth = SkTMax(fMaxIntrinsicWidth, softLineMaxIntrinsicWidth);
+                softLineMaxIntrinsicWidth = 0;
+
+                fMinIntrinsicWidth = SkTMax(fMinIntrinsicWidth, lastWordLength);
+                lastWordLength = 0;
+            } else if (cluster->isWhitespaces()) {
+                SkASSERT(cluster->isWhitespaces());
+                softLineMaxIntrinsicWidth += cluster->width();
+                fMinIntrinsicWidth = SkTMax(fMinIntrinsicWidth, lastWordLength);
+                lastWordLength = 0;
+            } else {
+                softLineMaxIntrinsicWidth += cluster->width();
+                lastWordLength += cluster->width();
+            }
+            ++cluster;
+        }
+        fMinIntrinsicWidth = SkTMax(fMinIntrinsicWidth, lastWordLength);
+        fMaxIntrinsicWidth = SkTMax(fMaxIntrinsicWidth, softLineMaxIntrinsicWidth);
+        // In case we could not place a single cluster on the line
+        fHeight = SkTMax(fHeight, fEndLine.metrics().height());
     }
 
     if (fHardLineBreak) {