Painting styled text blocks in RTL

Bug: skia:11017
Change-Id: I13bd210a0d38614bab4a5a98c4507150043224cc
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/340317
Reviewed-by: Ben Wagner <bungeman@google.com>
Commit-Queue: Julia Lavrova <jlavrova@google.com>
diff --git a/modules/skparagraph/samples/SampleParagraph.cpp b/modules/skparagraph/samples/SampleParagraph.cpp
index 36c233a..9e7faeb 100644
--- a/modules/skparagraph/samples/SampleParagraph.cpp
+++ b/modules/skparagraph/samples/SampleParagraph.cpp
@@ -3212,6 +3212,62 @@
     using INHERITED = Sample;
 };
 
+class ParagraphView53 : public ParagraphView_Base {
+protected:
+    SkString name() override { return SkString("Paragraph53"); }
+
+    void onDrawContent(SkCanvas* canvas) override {
+        canvas->drawColor(SK_ColorWHITE);
+        const char* text1 = "אאא בבב גגג דדד ההה";
+        const char* text2 = "ששש תתת";
+        //const char* text3 = "אאא בבב גגג דדד הההששש תתת";
+
+        auto fontCollection = sk_make_sp<FontCollection>();
+        fontCollection->setDefaultFontManager(SkFontMgr::RefDefault());
+        fontCollection->enableFontFallback();
+
+        ParagraphStyle paragraph_style;
+        paragraph_style.setTextDirection(TextDirection::kRtl);
+        {
+        ParagraphBuilderImpl builder(paragraph_style, fontCollection);
+        TextStyle text_style;
+        text_style.setFontSize(30);
+        text_style.setColor(SK_ColorBLACK);
+        builder.pushStyle(text_style);
+        builder.addText(text1);
+        builder.addText(text2);
+        builder.pop();
+
+        auto paragraph = builder.Build();
+        paragraph->layout(width());
+        paragraph->paint(canvas, 0, 0);
+        canvas->translate(0, paragraph->getHeight() + 20);
+        }
+
+        {
+        ParagraphBuilderImpl builder(paragraph_style, fontCollection);
+        TextStyle text_style;
+        text_style.setFontSize(30);
+        text_style.setColor(SK_ColorBLACK);
+        builder.pushStyle(text_style);
+        builder.addText(text1);
+        text_style.setColor(SK_ColorRED);
+        builder.pushStyle(text_style);
+        builder.addText(text2);
+        builder.pop();
+
+        auto paragraph = builder.Build();
+        paragraph->layout(width());
+        paragraph->paint(canvas, 0, 0);
+        canvas->translate(0, paragraph->getHeight() + 20);
+        }
+
+    }
+
+private:
+    using INHERITED = Sample;
+};
+
 }  // namespace
 
 //////////////////////////////////////////////////////////////////////////////
@@ -3265,3 +3321,4 @@
 DEF_SAMPLE(return new ParagraphView50();)
 DEF_SAMPLE(return new ParagraphView51();)
 DEF_SAMPLE(return new ParagraphView52();)
+DEF_SAMPLE(return new ParagraphView53();)
diff --git a/modules/skparagraph/src/TextLine.cpp b/modules/skparagraph/src/TextLine.cpp
index d51928b..8354a66 100644
--- a/modules/skparagraph/src/TextLine.cpp
+++ b/modules/skparagraph/src/TextLine.cpp
@@ -723,12 +723,15 @@
     size_t size = 0;
     const TextStyle* prevStyle = nullptr;
     SkScalar textOffsetInRun = 0;
-    for (BlockIndex index = fBlockRange.start; index <= fBlockRange.end; ++index) {
+
+    const BlockIndex blockRangeSize = fBlockRange.end - fBlockRange.start;
+    for (BlockIndex index = 0; index <= blockRangeSize; ++index) {
 
         TextRange intersect;
         TextStyle* style = nullptr;
-        if (index < fBlockRange.end) {
-            auto block = fOwner->styles().begin() + index;
+        if (index < blockRangeSize) {
+            auto block = fOwner->styles().begin() +
+                 (run->leftToRight() ? fBlockRange.start + index : fBlockRange.end - index - 1);
 
             // Get the text
             intersect = intersected(block->fRange, textRange);