Move all glyphsToUnichars to a single impl (rely on typeface)

- make it private in SkFont ('cause we hate this idea)
- remove from SkPaint as soon as we update callers

Bug: skia:8600
Change-Id: I8382b34b50b549d76ae4e849287994c4a8e1be87
Reviewed-on: https://skia-review.googlesource.com/c/175427
Reviewed-by: Herb Derby <herb@google.com>
Commit-Queue: Mike Reed <reed@google.com>
diff --git a/include/core/SkFont.h b/include/core/SkFont.h
index 781508a..08928ae 100644
--- a/include/core/SkFont.h
+++ b/include/core/SkFont.h
@@ -492,7 +492,11 @@
 
     SkScalar setupForAsPaths(SkPaint*);
 
+    void glyphsToUnichars(const SkGlyphID glyphs[], int count, SkUnichar text[]) const;
+
     friend class SkCanonicalizeFont;
+    friend class SkPaint;
+    friend class SVGTextBuilder;
 };
 
 #endif
diff --git a/include/core/SkTypeface.h b/include/core/SkTypeface.h
index 3d52e7a..ac6914d 100644
--- a/include/core/SkTypeface.h
+++ b/include/core/SkTypeface.h
@@ -437,6 +437,7 @@
 
     friend class SkFontPriv;       // GetDefaultTypeface
     friend class SkPaintPriv;      // GetDefaultTypeface
+    friend class SkFont;           // getGlyphToUnicodeMap
 
 private:
     SkFontID            fUniqueID;
diff --git a/src/core/SkFont.cpp b/src/core/SkFont.cpp
index 8f3fd9c..db07281 100644
--- a/src/core/SkFont.cpp
+++ b/src/core/SkFont.cpp
@@ -179,6 +179,22 @@
     return count;
 }
 
+void SkFont::glyphsToUnichars(const SkGlyphID glyphs[], int count, SkUnichar text[]) const {
+    if (count <= 0) {
+        return;
+    }
+
+    auto typeface = SkFontPriv::GetTypefaceOrDefault(*this);
+    const unsigned numGlyphsInTypeface = typeface->countGlyphs();
+    SkAutoTArray<SkUnichar> unichars(count);
+    typeface->getGlyphToUnicodeMap(unichars.get());
+
+    for (int i = 0; i < count; ++i) {
+        unsigned id = glyphs[i];
+        text[i] = (id < numGlyphsInTypeface) ? unichars[id] : 0xFFFD;
+    }
+}
+
 static SkTypeface::Encoding to_encoding(SkTextEncoding e) {
     static_assert((int)SkTypeface::kUTF8_Encoding  == (int)kUTF8_SkTextEncoding,  "");
     static_assert((int)SkTypeface::kUTF16_Encoding == (int)kUTF16_SkTextEncoding, "");
diff --git a/src/core/SkGlyphCache.cpp b/src/core/SkGlyphCache.cpp
index 7b97f09..266d207 100644
--- a/src/core/SkGlyphCache.cpp
+++ b/src/core/SkGlyphCache.cpp
@@ -71,10 +71,6 @@
     }
 }
 
-SkUnichar SkGlyphCache::glyphToUnichar(SkGlyphID glyphID) {
-    return fScalerContext->glyphIDToChar(glyphID);
-}
-
 unsigned SkGlyphCache::getGlyphCount() const {
     return fScalerContext->getGlyphCount();
 }
diff --git a/src/core/SkGlyphCache.h b/src/core/SkGlyphCache.h
index a4c3e85..9a24b33 100644
--- a/src/core/SkGlyphCache.h
+++ b/src/core/SkGlyphCache.h
@@ -74,10 +74,6 @@
     */
     SkGlyphID unicharToGlyph(SkUnichar);
 
-    /** Map the glyph to its Unicode equivalent. Unmappable glyphs map to a character code of zero.
-    */
-    SkUnichar glyphToUnichar(SkGlyphID);
-
     /** Returns the number of glyphs for this strike.
     */
     unsigned getGlyphCount() const;
diff --git a/src/core/SkPaint_text.cpp b/src/core/SkPaint_text.cpp
index 3b3b796..530fc93 100644
--- a/src/core/SkPaint_text.cpp
+++ b/src/core/SkPaint_text.cpp
@@ -77,21 +77,7 @@
 }
 
 void SkPaint::glyphsToUnichars(const uint16_t glyphs[], int count, SkUnichar textData[]) const {
-    if (count <= 0) {
-        return;
-    }
-
-    SkASSERT(glyphs != nullptr);
-    SkASSERT(textData != nullptr);
-
-    SkFont font = SkFont::LEGACY_ExtractFromPaint(*this);
-    SkSurfaceProps props(0, kUnknown_SkPixelGeometry);
-    auto cache = SkStrikeCache::FindOrCreateStrikeExclusive(
-            font, *this, props, SkScalerContextFlags::kFakeGammaAndBoostContrast, SkMatrix::I());
-
-    for (int index = 0; index < count; index++) {
-        textData[index] = cache->glyphToUnichar(glyphs[index]);
-    }
+    SkFont::LEGACY_ExtractFromPaint(*this).glyphsToUnichars(glyphs, count, textData);
 }
 
 ///////////////////////////////////////////////////////////////////////////////
diff --git a/src/core/SkScalerContext.cpp b/src/core/SkScalerContext.cpp
index 5927995..ffa6c38 100644
--- a/src/core/SkScalerContext.cpp
+++ b/src/core/SkScalerContext.cpp
@@ -555,10 +555,6 @@
     this->generateFontMetrics(fm);
 }
 
-SkUnichar SkScalerContext::generateGlyphToChar(uint16_t glyph) {
-    return 0;
-}
-
 ///////////////////////////////////////////////////////////////////////////////
 
 bool SkScalerContext::internalGetPath(SkPackedGlyphID glyphID, SkPath* devPath) {
diff --git a/src/core/SkScalerContext.h b/src/core/SkScalerContext.h
index 1217192..06ccce2 100644
--- a/src/core/SkScalerContext.h
+++ b/src/core/SkScalerContext.h
@@ -285,13 +285,6 @@
         return generateCharToGlyph(uni);
     }
 
-    /** Map the glyphID to its glyph index, and then to its char code. Unmapped
-        glyphs return zero.
-    */
-    SkUnichar glyphIDToChar(uint16_t glyphID) {
-        return (glyphID < getGlyphCount()) ? generateGlyphToChar(glyphID) : 0;
-    }
-
     unsigned    getGlyphCount() { return this->generateGlyphCount(); }
     void        getAdvance(SkGlyph*);
     void        getMetrics(SkGlyph*);
@@ -411,12 +404,6 @@
      */
     virtual uint16_t generateCharToGlyph(SkUnichar unichar) = 0;
 
-    /** Returns the unichar for the given glyph id.
-     *  If there is no 1:1 mapping from the glyph id to a unichar, returns 0.
-     *  The default implementation always returns 0, indicating failure.
-     */
-    virtual SkUnichar generateGlyphToChar(uint16_t glyphId);
-
     void forceGenerateImageFromPath() { fGenerateImageFromPath = true; }
     void forceOffGenerateImageFromPath() { fGenerateImageFromPath = false; }
 
diff --git a/src/ports/SkFontHost_FreeType.cpp b/src/ports/SkFontHost_FreeType.cpp
index c871e94..9f79ed4 100644
--- a/src/ports/SkFontHost_FreeType.cpp
+++ b/src/ports/SkFontHost_FreeType.cpp
@@ -503,7 +503,6 @@
     void generateImage(const SkGlyph& glyph) override;
     bool generatePath(SkGlyphID glyphID, SkPath* path) override;
     void generateFontMetrics(SkFontMetrics*) override;
-    SkUnichar generateGlyphToChar(uint16_t glyph) override;
 
 private:
     using UnrefFTFace = SkFunctionWrapper<void, SkFaceRec, unref_ft_face>;
@@ -1034,22 +1033,6 @@
     return SkToU16(FT_Get_Char_Index( fFace, uni ));
 }
 
-SkUnichar SkScalerContext_FreeType::generateGlyphToChar(uint16_t glyph) {
-    SkAutoMutexAcquire  ac(gFTMutex);
-    // iterate through each cmap entry, looking for matching glyph indices
-    FT_UInt glyphIndex;
-    SkUnichar charCode = FT_Get_First_Char( fFace, &glyphIndex );
-
-    while (glyphIndex != 0) {
-        if (glyphIndex == glyph) {
-            return charCode;
-        }
-        charCode = FT_Get_Next_Char( fFace, charCode, &glyphIndex );
-    }
-
-    return 0;
-}
-
 bool SkScalerContext_FreeType::generateAdvance(SkGlyph* glyph) {
    /* unhinted and light hinted text have linearly scaled advances
     * which are very cheap to compute with some font formats...
diff --git a/src/svg/SkSVGDevice.cpp b/src/svg/SkSVGDevice.cpp
index 4ad42bc..f594b67 100644
--- a/src/svg/SkSVGDevice.cpp
+++ b/src/svg/SkSVGDevice.cpp
@@ -852,10 +852,10 @@
     SVGTextBuilder(SkPoint origin, const SkGlyphRun& glyphRun)
             : fOrigin(origin)
             , fLastCharWasWhitespace(true) { // start off in whitespace mode to strip all leadingspace
-        const SkPaint& paint = glyphRun.paint();
+        const SkFont font = SkFont::LEGACY_ExtractFromPaint(glyphRun.paint());
         auto runSize = glyphRun.runSize();
         SkAutoSTArray<64, SkUnichar> unichars(runSize);
-        paint.glyphsToUnichars(glyphRun.glyphsIDs().data(), runSize, unichars.get());
+        font.glyphsToUnichars(glyphRun.glyphsIDs().data(), runSize, unichars.get());
         auto positions = glyphRun.positions();
         for (size_t i = 0; i < runSize; ++i) {
             this->appendUnichar(unichars[i], positions[i]);