Select a position on an empty line

Bugs: skia:10821
Change-Id: I26746e7baa143a6be7afdc91c994e4b8731d07f7
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/324879
Commit-Queue: Julia Lavrova <jlavrova@google.com>
Reviewed-by: Ben Wagner <bungeman@google.com>
diff --git a/modules/skparagraph/src/TextLine.cpp b/modules/skparagraph/src/TextLine.cpp
index b755959..e53c785 100644
--- a/modules/skparagraph/src/TextLine.cpp
+++ b/modules/skparagraph/src/TextLine.cpp
@@ -1085,6 +1085,13 @@
 
 PositionWithAffinity TextLine::getGlyphPositionAtCoordinate(SkScalar dx) {
 
+    if (SkScalarNearlyZero(this->width())) {
+        // TODO: this is one of the flutter changes that have to go away eventually
+        //  Empty line is a special case in txtlib
+        auto utf16Index = fOwner->getUTF16Index(this->fClusterRange.start);
+        return { SkToS32(utf16Index) , kDownstream };
+    }
+
     PositionWithAffinity result(0, Affinity::kDownstream);
     this->iterateThroughVisualRuns(true,
         [this, dx, &result]
diff --git a/modules/skparagraph/tests/SkParagraphTest.cpp b/modules/skparagraph/tests/SkParagraphTest.cpp
index 18f84e1..8c19970 100644
--- a/modules/skparagraph/tests/SkParagraphTest.cpp
+++ b/modules/skparagraph/tests/SkParagraphTest.cpp
@@ -4826,6 +4826,8 @@
     sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
     if (!fontCollection->fontsFound()) return;
     fontCollection->setDefaultFontManager(SkFontMgr::RefDefault());
+    fontCollection->enableFontFallback();
+
     TestCanvas canvas("SkParagraph_EmptyParagraphWithLineBreak.png");
 
     ParagraphStyle paragraph_style;
@@ -4833,12 +4835,17 @@
     text_style.setFontSize(16);
     text_style.setFontFamilies({SkString("Roboto")});
     ParagraphBuilderImpl builder(paragraph_style, fontCollection);
-    builder.addText("\n", 1);
+    builder.addText("abc\n\ndef");
 
     auto paragraph = builder.Build();
     paragraph->layout(TestCanvasWidth);
     paragraph->paint(canvas.get(), 0, 0);
-    auto result = paragraph->getRectsForPlaceholders();
+
+    // Select a position at the second (empty) line
+    auto pos = paragraph->getGlyphPositionAtCoordinate(0, 21);
+    REPORTER_ASSERT(reporter, pos.affinity == Affinity::kDownstream && pos.position == 4);
+    auto rect = paragraph->getRectsForRange(4, 5, RectHeightStyle::kTight, RectWidthStyle::kTight);
+    REPORTER_ASSERT(reporter, rect.size() == 1 && rect[0].rect.width() == 0);
 }
 
 DEF_TEST(SkParagraph_NullInMiddleOfText, reporter) {