Make SkFont a bit more useable

Split out of https://codereview.chromium.org/2163483002/ (Use SkFont in GrStencilAndCoverTextContext)

GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2163633002

Review-Url: https://codereview.chromium.org/2163633002
diff --git a/include/core/SkFont.h b/include/core/SkFont.h
index 6c231c9..6e8a715 100644
--- a/include/core/SkFont.h
+++ b/include/core/SkFont.h
@@ -126,6 +126,10 @@
      *  If size is not supported (e.g. <= 0 or non-finite) NULL will be returned.
      */
     sk_sp<SkFont> makeWithSize(SkScalar size) const;
+    /**
+     *  Return a font with the same attributes of this font, but with the flags.
+     */
+    sk_sp<SkFont> makeWithFlags(uint32_t newFlags) const;
 
     SkTypeface* getTypeface() const { return fTypeface.get(); }
     SkScalar    getSize() const { return fSize; }
@@ -139,10 +143,15 @@
     bool isEnableAutoHints() const { return SkToBool(fFlags & kEnableAutoHints_Flag); }
     bool isEnableByteCodeHints() const { return SkToBool(fFlags & kEnableByteCodeHints_Flag); }
     bool isUseNonLinearMetrics() const { return SkToBool(fFlags & kUseNonlinearMetrics_Flag); }
+    bool isDevKern() const { return SkToBool(fFlags & kDevKern_Flag); }
 
     int textToGlyphs(const void* text, size_t byteLength, SkTextEncoding,
                      uint16_t glyphs[], int maxGlyphCount) const;
 
+    int countText(const void* text, size_t byteLength, SkTextEncoding encoding) {
+        return this->textToGlyphs(text, byteLength, encoding, nullptr, 0);
+    }
+
     SkScalar measureText(const void* text, size_t byteLength, SkTextEncoding) const;
 
     static sk_sp<SkFont> Testing_CreateFromPaint(const SkPaint&);
diff --git a/include/core/SkPaint.h b/include/core/SkPaint.h
index c0f1f3e..caa1856 100644
--- a/include/core/SkPaint.h
+++ b/include/core/SkPaint.h
@@ -1085,7 +1085,9 @@
         uint32_t fBitfieldsUInt;
     };
 
-    GlyphCacheProc getGlyphCacheProc(bool needFullMetrics) const;
+    static GlyphCacheProc GetGlyphCacheProc(TextEncoding encoding,
+                                            bool isDevKern,
+                                            bool needFullMetrics);
 
     SkScalar measure_text(SkGlyphCache*, const char* text, size_t length,
                           int* count, SkRect* bounds) const;
diff --git a/src/core/SkDraw.cpp b/src/core/SkDraw.cpp
index 32fe4bb..7787a28 100644
--- a/src/core/SkDraw.cpp
+++ b/src/core/SkDraw.cpp
@@ -1638,7 +1638,9 @@
     paint.setStyle(SkPaint::kFill_Style);
     paint.setPathEffect(nullptr);
 
-    SkPaint::GlyphCacheProc glyphCacheProc = paint.getGlyphCacheProc(true);
+    SkPaint::GlyphCacheProc glyphCacheProc = SkPaint::GetGlyphCacheProc(paint.getTextEncoding(),
+                                                                        paint.isDevKernText(),
+                                                                        true);
     SkAutoGlyphCache cache(paint, &fDevice->surfaceProps(), this->scalerContextFlags(), nullptr);
 
     const char*        stop = text + byteLength;
diff --git a/src/core/SkFont.cpp b/src/core/SkFont.cpp
index 1300011..aa7fe36 100644
--- a/src/core/SkFont.cpp
+++ b/src/core/SkFont.cpp
@@ -48,6 +48,10 @@
                         this->getSkewX(), this->getMaskType(), this->getFlags());
 }
 
+sk_sp<SkFont> SkFont::makeWithFlags(uint32_t newFlags) const {
+    return SkFont::Make(sk_ref_sp(this->getTypeface()), this->getSize(), this->getScaleX(),
+                        this->getSkewX(), this->getMaskType(), newFlags);
+}
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
 int SkFont::textToGlyphs(const void* text, size_t byteLength, SkTextEncoding encoding,
@@ -74,21 +78,21 @@
             count = SkToInt(byteLength >> 1);
             break;
     }
-    if (nullptr == glyphs) {
+    if (!glyphs) {
         return count;
     }
 
     // TODO: unify/eliminate SkTypeface::Encoding with SkTextEncoding
-    SkTypeface::Encoding typeface_encoding;
+    SkTypeface::Encoding typefaceEncoding;
     switch (encoding) {
         case kUTF8_SkTextEncoding:
-            typeface_encoding = SkTypeface::kUTF8_Encoding;
+            typefaceEncoding = SkTypeface::kUTF8_Encoding;
             break;
         case kUTF16_SkTextEncoding:
-            typeface_encoding = SkTypeface::kUTF16_Encoding;
+            typefaceEncoding = SkTypeface::kUTF16_Encoding;
             break;
         case kUTF32_SkTextEncoding:
-            typeface_encoding = SkTypeface::kUTF32_Encoding;
+            typefaceEncoding = SkTypeface::kUTF32_Encoding;
             break;
         default:
             SkASSERT(kGlyphID_SkTextEncoding == encoding);
@@ -97,7 +101,7 @@
             return count;
     }
 
-    (void)fTypeface->charsToGlyphs(text, typeface_encoding, glyphs, count);
+    (void)fTypeface->charsToGlyphs(text, typefaceEncoding, glyphs, count);
     return count;
 }
 
diff --git a/src/core/SkPaint.cpp b/src/core/SkPaint.cpp
index 25b6aec..528b1d0 100644
--- a/src/core/SkPaint.cpp
+++ b/src/core/SkPaint.cpp
@@ -661,7 +661,9 @@
     return cache->getGlyphIDAdvance(glyphID);
 }
 
-SkPaint::GlyphCacheProc SkPaint::getGlyphCacheProc(bool needFullMetrics) const {
+SkPaint::GlyphCacheProc SkPaint::GetGlyphCacheProc(TextEncoding encoding,
+                                                   bool isDevKern,
+                                                   bool needFullMetrics) {
     static const GlyphCacheProc gGlyphCacheProcs[] = {
         sk_getMetrics_utf8_next,
         sk_getMetrics_utf16_next,
@@ -674,9 +676,9 @@
         sk_getAdvance_glyph_next,
     };
 
-    unsigned index = this->getTextEncoding();
+    unsigned index = encoding;
 
-    if (!needFullMetrics && !this->isDevKernText()) {
+    if (!needFullMetrics && !isDevKern) {
         index += 4;
     }
 
@@ -774,7 +776,9 @@
         return 0;
     }
 
-    GlyphCacheProc glyphCacheProc = this->getGlyphCacheProc(nullptr != bounds);
+    GlyphCacheProc glyphCacheProc = SkPaint::GetGlyphCacheProc(this->getTextEncoding(),
+                                                               this->isDevKernText(),
+                                                               nullptr != bounds);
 
     int xyIndex;
     JoinBoundsProc joinBoundsProc;
@@ -892,7 +896,9 @@
     SkAutoGlyphCache    autoCache(paint, nullptr, nullptr);
     SkGlyphCache*       cache = autoCache.getCache();
 
-    GlyphCacheProc   glyphCacheProc = paint.getGlyphCacheProc(false);
+    GlyphCacheProc   glyphCacheProc = SkPaint::GetGlyphCacheProc(paint.getTextEncoding(),
+                                                                 paint.isDevKernText(),
+                                                                 false);
     const int        xyIndex = paint.isVerticalText() ? 1 : 0;
     SkScalar         width = 0;
 
@@ -1005,7 +1011,9 @@
 
     SkAutoGlyphCache    autoCache(paint, nullptr, nullptr);
     SkGlyphCache*       cache = autoCache.getCache();
-    GlyphCacheProc      glyphCacheProc = paint.getGlyphCacheProc(nullptr != bounds);
+    GlyphCacheProc      glyphCacheProc = SkPaint::GetGlyphCacheProc(paint.getTextEncoding(),
+                                                                    paint.isDevKernText(),
+                                                                    nullptr != bounds);
 
     const char* text = (const char*)textData;
     const char* stop = text + byteLength;
@@ -2182,7 +2190,9 @@
                                    const SkPaint& paint,
                                    bool applyStrokeAndPathEffects)
     : fPaint(paint) {
-    fGlyphCacheProc = paint.getGlyphCacheProc(true);
+    fGlyphCacheProc = SkPaint::GetGlyphCacheProc(paint.getTextEncoding(),
+                                                 paint.isDevKernText(),
+                                                 true);
 
     fPaint.setLinearText(true);
     fPaint.setMaskFilter(nullptr);   // don't want this affecting our path-cache lookup
diff --git a/src/gpu/text/GrStencilAndCoverTextContext.cpp b/src/gpu/text/GrStencilAndCoverTextContext.cpp
index dd93025..632bdcd 100644
--- a/src/gpu/text/GrStencilAndCoverTextContext.cpp
+++ b/src/gpu/text/GrStencilAndCoverTextContext.cpp
@@ -469,7 +469,9 @@
     SkASSERT(byteLength == 0 || text != nullptr);
 
     SkGlyphCache* glyphCache = this->getGlyphCache();
-    SkPaint::GlyphCacheProc glyphCacheProc = fFont.getGlyphCacheProc(true);
+    SkPaint::GlyphCacheProc glyphCacheProc = SkPaint::GetGlyphCacheProc(fFont.getTextEncoding(),
+                                                                        fFont.isDevKernText(),
+                                                                        true);
 
     fTotalGlyphCount = fFont.countText(text, byteLength);
     fInstanceData.reset(InstanceData::Alloc(GrPathRendering::kTranslate_PathTransformType,
@@ -529,7 +531,9 @@
     SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
 
     SkGlyphCache* glyphCache = this->getGlyphCache();
-    SkPaint::GlyphCacheProc glyphCacheProc = fFont.getGlyphCacheProc(true);
+    SkPaint::GlyphCacheProc glyphCacheProc = SkPaint::GetGlyphCacheProc(fFont.getTextEncoding(),
+                                                                        fFont.isDevKernText(),
+                                                                        true);
 
     fTotalGlyphCount = fFont.countText(text, byteLength);
     fInstanceData.reset(InstanceData::Alloc(GrPathRendering::kTranslate_PathTransformType,
diff --git a/src/gpu/text/GrTextUtils.cpp b/src/gpu/text/GrTextUtils.cpp
index 9205d2f..56d4ec1 100644
--- a/src/gpu/text/GrTextUtils.cpp
+++ b/src/gpu/text/GrTextUtils.cpp
@@ -253,7 +253,9 @@
         return;
     }
 
-    SkPaint::GlyphCacheProc glyphCacheProc = skPaint.getGlyphCacheProc(true);
+    SkPaint::GlyphCacheProc glyphCacheProc = SkPaint::GetGlyphCacheProc(skPaint.getTextEncoding(),
+                                                                        skPaint.isDevKernText(),
+                                                                        true);
     SkAutoDescriptor desc;
     SkScalerContextEffects effects;
     // We apply the fake-gamma by altering the distance in the shader, so we ignore the
@@ -345,7 +347,9 @@
     // passed-in scaler context flags. (It's only used when we fall-back to bitmap text).
     SkGlyphCache* cache = blob->setupCache(runIndex, props, SkPaint::kNone_ScalerContextFlags,
                                            dfPaint, nullptr);
-    SkPaint::GlyphCacheProc glyphCacheProc = dfPaint.getGlyphCacheProc(true);
+    SkPaint::GlyphCacheProc glyphCacheProc = SkPaint::GetGlyphCacheProc(dfPaint.getTextEncoding(),
+                                                                        dfPaint.isDevKernText(),
+                                                                        true);
 
     const char* stop = text + byteLength;
 
@@ -508,7 +512,9 @@
     paint.setStyle(SkPaint::kFill_Style);
     paint.setPathEffect(nullptr);
 
-    SkPaint::GlyphCacheProc    glyphCacheProc = paint.getGlyphCacheProc(true);
+    SkPaint::GlyphCacheProc    glyphCacheProc = SkPaint::GetGlyphCacheProc(paint.getTextEncoding(),
+                                                                           paint.isDevKernText(),
+                                                                           true);
     SkAutoGlyphCache           autoCache(paint, &props, nullptr);
     SkGlyphCache*              cache = autoCache.getCache();
 
diff --git a/src/pdf/SkPDFDevice.cpp b/src/pdf/SkPDFDevice.cpp
index 33ae8a4..01e8c82 100644
--- a/src/pdf/SkPDFDevice.cpp
+++ b/src/pdf/SkPDFDevice.cpp
@@ -1216,7 +1216,9 @@
     int numGlyphs = force_glyph_encoding(paint, text, len, &storage, &glyphIDs);
     textPaint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
 
-    SkPaint::GlyphCacheProc glyphCacheProc = textPaint.getGlyphCacheProc(true);
+    SkPaint::GlyphCacheProc glyphCacheProc = SkPaint::GetGlyphCacheProc(textPaint.getTextEncoding(),
+                                                                        textPaint.isDevKernText(),
+                                                                        true);
     align_text(glyphCacheProc, textPaint, glyphIDs, numGlyphs, &x, &y);
     content.entry()->fContent.writeText("BT\n");
     set_text_transform(x, y, textPaint.getTextSkewX(),
@@ -1293,7 +1295,9 @@
     size_t numGlyphs = force_glyph_encoding(paint, text, len, &storage, &glyphIDs);
     textPaint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
 
-    SkPaint::GlyphCacheProc glyphCacheProc = textPaint.getGlyphCacheProc(true);
+    SkPaint::GlyphCacheProc glyphCacheProc = SkPaint::GetGlyphCacheProc(textPaint.getTextEncoding(),
+                                                                        textPaint.isDevKernText(),
+                                                                        true);
     content.entry()->fContent.writeText("BT\n");
     this->updateFont(textPaint, glyphIDs[0], content.entry());
     GlyphPositioner glyphPositioner(&content.entry()->fContent,