fix SkCanvas::drawGlyphs's bounds calculation

Change-Id: I9aea1f516ccb362cf28684de571bdd6971dd6f6c
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/397478
Reviewed-by: Mike Reed <reed@google.com>
Commit-Queue: Herb Derby <herb@google.com>
diff --git a/gm/drawglyphs.cpp b/gm/drawglyphs.cpp
new file mode 100644
index 0000000..895dd03
--- /dev/null
+++ b/gm/drawglyphs.cpp
@@ -0,0 +1,67 @@
+/*
+ * Copyright 2021 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "gm/gm.h"
+#include "include/core/SkCanvas.h"
+#include "include/core/SkFont.h"
+#include "include/core/SkPaint.h"
+#include "include/private/SkTDArray.h"
+#include "tools/ToolUtils.h"
+
+static const char gText[] = "Call me Ishmael. Some years ago—never mind how long precisely";
+
+class DrawGlyphsGM : public skiagm::GM {
+public:
+    void onOnceBeforeDraw() override {
+        fTypeface = ToolUtils::create_portable_typeface("serif", SkFontStyle());
+        fFont = SkFont(fTypeface);
+        fFont.setSubpixel(true);
+        fFont.setSize(18);
+        size_t txtLen = strlen(gText);
+        fGlyphCount = fFont.countText(gText, txtLen, SkTextEncoding::kUTF8);
+
+        fGlyphs.append(fGlyphCount);
+        fFont.textToGlyphs(gText, txtLen, SkTextEncoding::kUTF8, fGlyphs.begin(), fGlyphCount);
+
+        fPositions.append(fGlyphCount);
+        fFont.getPos(fGlyphs.begin(), fGlyphCount, fPositions.begin());
+    }
+
+    SkString onShortName() override {
+        return SkString("drawglyphs");
+    }
+
+    SkISize onISize() override {
+        return SkISize::Make(640, 480);
+    }
+
+    void onDraw(SkCanvas* canvas) override {
+        canvas->drawGlyphs(fGlyphCount, fGlyphs.begin(), fPositions.begin(), {50, 100}, fFont,
+                           SkPaint{});
+
+        canvas->drawGlyphs(fGlyphCount, fGlyphs.begin(), fPositions.begin(), {50, 120}, fFont,
+                           SkPaint{});
+
+        // Check bounding box calculation.
+        for (auto& pos : fPositions) {
+            pos += {0, -500};
+        }
+        canvas->drawGlyphs(fGlyphCount, fGlyphs.begin(), fPositions.begin(), {50, 640}, fFont,
+                           SkPaint{});
+
+        // TODO: add tests for cluster versions of drawGlyphs.
+    }
+
+private:
+    sk_sp<SkTypeface>   fTypeface;
+    SkFont fFont;
+    SkTDArray<SkGlyphID> fGlyphs;
+    SkTDArray<SkPoint>  fPositions;
+    int fGlyphCount;
+};
+
+DEF_GM(return new DrawGlyphsGM{};)
diff --git a/gn/gm.gni b/gn/gm.gni
index 33718a2..59438aa 100644
--- a/gn/gm.gni
+++ b/gn/gm.gni
@@ -154,6 +154,7 @@
   "$_gm/drawatlas.cpp",
   "$_gm/drawatlascolor.cpp",
   "$_gm/drawbitmaprect.cpp",
+  "$_gm/drawglyphs.cpp",
   "$_gm/drawimageset.cpp",
   "$_gm/drawminibitmaprect.cpp",
   "$_gm/drawquadset.cpp",
diff --git a/src/core/SkCanvas.cpp b/src/core/SkCanvas.cpp
index 1ea7921..a3e9dad 100644
--- a/src/core/SkCanvas.cpp
+++ b/src/core/SkCanvas.cpp
@@ -2285,7 +2285,7 @@
 void SkCanvas::onDrawGlyphRunList(const SkGlyphRunList& glyphRunList, const SkPaint& paint) {
     SkRect bounds = glyphRunList.sourceBounds();
     if (this->internalQuickReject(bounds, paint)) {
-//        return;
+        return;
     }
     AutoLayerForImageFilter layer(this, paint, &bounds);
     this->topDevice()->drawGlyphRunList(glyphRunList, layer.paint());
@@ -2318,7 +2318,7 @@
     };
     SkGlyphRunList glyphRunList {
         glyphRun,
-        glyphRun.sourceBounds(paint),
+        glyphRun.sourceBounds(paint).makeOffset(origin),
         origin
     };
     this->onDrawGlyphRunList(glyphRunList, paint);
@@ -2339,7 +2339,7 @@
     };
     SkGlyphRunList glyphRunList {
         glyphRun,
-        glyphRun.sourceBounds(paint),
+        glyphRun.sourceBounds(paint).makeOffset(origin),
         origin
     };
     this->onDrawGlyphRunList(glyphRunList, paint);