add virtual SkTypeface::onOpenStream and override that for fontconfig
(other ports to follow)

When this is complete, we will be able to remove from SkFontHost
- OpenStream
- GetFileName
Review URL: https://codereview.chromium.org/12988002

git-svn-id: http://skia.googlecode.com/svn/trunk@8299 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/include/core/SkTypeface.h b/include/core/SkTypeface.h
index edf9306..a89c31f 100644
--- a/include/core/SkTypeface.h
+++ b/include/core/SkTypeface.h
@@ -193,6 +193,12 @@
      */
     int getUnitsPerEm() const;
 
+    /**
+     *  Return a stream for the contents of the font data, or NULL on failure.
+     *  If ttcIndex is not null, it is set to the TrueTypeCollection index
+     *  of this typeface within the stream, or 0 if the stream is not a
+     *  collection.
+     */
     SkStream* openStream(int* ttcIndex) const;
     SkScalerContext* createScalerContext(const SkDescriptor*) const;
 
@@ -212,8 +218,11 @@
                         SkAdvancedTypefaceMetrics::PerGlyphInfo perGlyphInfo,
                         const uint32_t* glyphIDs,
                         uint32_t glyphIDsCount) const = 0;
+    // TODO: remove SkFontHost::OpenStream and make this guy pure-virtual
+    virtual SkStream* onOpenStream(int* ttcIndex) const;
 
     virtual int onGetUPEM() const;
+
     virtual int onGetTableTags(SkFontTableTag tags[]) const;
     virtual size_t onGetTableData(SkFontTableTag, size_t offset,
                                   size_t length, void* data) const;
diff --git a/src/core/SkTypeface.cpp b/src/core/SkTypeface.cpp
index 9f72faa..4a3dd4f 100644
--- a/src/core/SkTypeface.cpp
+++ b/src/core/SkTypeface.cpp
@@ -123,12 +123,12 @@
 }
 
 SkStream* SkTypeface::openStream(int* ttcIndex) const {
-    if (ttcIndex) {
-        int32_t ndx = 0;
-        (void)SkFontHost::GetFileName(fUniqueID, NULL, 0, &ndx);
-        *ttcIndex = (int)ndx;
+    int ttcIndexStorage;
+    if (NULL == ttcIndex) {
+        // So our subclasses don't need to check for null param
+        ttcIndex = &ttcIndexStorage;
     }
-    return SkFontHost::OpenStream(fUniqueID);
+    return this->onOpenStream(ttcIndex);
 }
 
 int SkTypeface::getUnitsPerEm() const {
@@ -155,6 +155,15 @@
     return upem;
 }
 
+SkStream* SkTypeface::onOpenStream(int* ttcIndex) const {
+    if (ttcIndex) {
+        int32_t ndx = 0;
+        (void)SkFontHost::GetFileName(fUniqueID, NULL, 0, &ndx);
+        *ttcIndex = (int)ndx;
+    }
+    return SkFontHost::OpenStream(fUniqueID);
+}
+
 int SkTypeface::onGetTableTags(SkFontTableTag tags[]) const { return 0; }
 size_t SkTypeface::onGetTableData(SkFontTableTag, size_t offset,
                                   size_t length, void* data) const { return 0; }
diff --git a/src/ports/SkFontHost_FreeType.cpp b/src/ports/SkFontHost_FreeType.cpp
index 4aaf31b..e858759 100644
--- a/src/ports/SkFontHost_FreeType.cpp
+++ b/src/ports/SkFontHost_FreeType.cpp
@@ -260,7 +260,8 @@
 
 // Will return 0 on failure
 // Caller must lock gFTMutex before calling this function.
-static SkFaceRec* ref_ft_face(uint32_t fontID) {
+static SkFaceRec* ref_ft_face(const SkTypeface* typeface) {
+    const SkFontID fontID = typeface->uniqueID();
     SkFaceRec* rec = gFaceRecHead;
     while (rec) {
         if (rec->fFontID == fontID) {
@@ -271,10 +272,10 @@
         rec = rec->fNext;
     }
 
-    SkStream* strm = SkFontHost::OpenStream(fontID);
+    int face_index;
+    SkStream* strm = typeface->openStream(&face_index);
     if (NULL == strm) {
-        SkDEBUGF(("SkFontHost::OpenStream failed opening %x\n", fontID));
-        return 0;
+        return NULL;
     }
 
     // this passes ownership of strm to the rec
@@ -295,15 +296,11 @@
         args.stream = &rec->fFTStream;
     }
 
-    int face_index;
-    int length = SkFontHost::GetFileName(fontID, NULL, 0, &face_index);
-    FT_Error err = FT_Open_Face(gFTLibrary, &args, length ? face_index : 0,
-                                &rec->fFace);
-
+    FT_Error err = FT_Open_Face(gFTLibrary, &args, face_index, &rec->fFace);
     if (err) {    // bad filename, try the default font
         fprintf(stderr, "ERROR: unable to open font '%x'\n", fontID);
         SkDELETE(rec);
-        return 0;
+        return NULL;
     } else {
         SkASSERT(rec->fFace);
         //fprintf(stderr, "Opened font '%s'\n", filename.c_str());
@@ -462,7 +459,7 @@
         libInit = gFTLibrary;
     }
     SkAutoTCallIProc<struct FT_LibraryRec_, FT_Done_FreeType> ftLib(libInit);
-    SkFaceRec* rec = ref_ft_face(this->uniqueID());
+    SkFaceRec* rec = ref_ft_face(this);
     if (NULL == rec)
         return NULL;
     FT_Face face = rec->fFace;
@@ -707,7 +704,7 @@
         libInit = gFTLibrary;
     }
     SkAutoTCallIProc<struct FT_LibraryRec_, FT_Done_FreeType> ftLib(libInit);
-    SkFaceRec *rec = ref_ft_face(this->uniqueID());
+    SkFaceRec *rec = ref_ft_face(this);
     int unitsPerEm = 0;
 
     if (rec != NULL && rec->fFace != NULL) {
@@ -733,7 +730,7 @@
     // load the font file
     fFTSize = NULL;
     fFace = NULL;
-    fFaceRec = ref_ft_face(fRec.fFontID);
+    fFaceRec = ref_ft_face(typeface);
     if (NULL == fFaceRec) {
         return;
     }
diff --git a/src/ports/SkFontHost_fontconfig.cpp b/src/ports/SkFontHost_fontconfig.cpp
index 52ecb31..ea09710 100644
--- a/src/ports/SkFontHost_fontconfig.cpp
+++ b/src/ports/SkFontHost_fontconfig.cpp
@@ -90,6 +90,7 @@
     virtual size_t onGetTableData(SkFontTableTag, size_t offset,
                                   size_t length, void* data) const SK_OVERRIDE;
     virtual void onGetFontDescriptor(SkFontDescriptor*) const SK_OVERRIDE;
+    virtual SkStream* onOpenStream(int* ttcIndex) const SK_OVERRIDE;
 
 private:
     typedef SkTypeface_FreeType INHERITED;
@@ -244,8 +245,8 @@
 
 ///////////////////////////////////////////////////////////////////////////////
 
-static SkStream* open_stream(const FontConfigTypeface* face, int* ttcIndex) {
-    SkStream* stream = face->getLocalStream();
+SkStream* FontConfigTypeface::onOpenStream(int* ttcIndex) const {
+    SkStream* stream = this->getLocalStream();
     if (stream) {
         // TODO: fix issue 1176.
         // As of now open_stream will return a stream and unwind it, but the
@@ -262,48 +263,28 @@
         if (NULL == fci.get()) {
             return NULL;
         }
-        stream = fci->openStream(face->getIdentity());
-        *ttcIndex = face->getIdentity().fTTCIndex;
+        stream = fci->openStream(this->getIdentity());
+        *ttcIndex = this->getIdentity().fTTCIndex;
     }
     return stream;
 }
 
-SkStream* SkFontHost::OpenStream(uint32_t id) {
-    FontConfigTypeface* face = (FontConfigTypeface*)SkTypefaceCache::FindByID(id);
-    if (NULL == face) {
-        return NULL;
-    }
-
-    int ttcIndex;
-    // We should return ttcIndex from this call.
-    return open_stream(face, &ttcIndex);
+SkStream* SkFontHost::OpenStream(uint32_t) {
+    SkASSERT(!"SkFontHost::OpenStream is DEPRECATED: call SkTypeface::openStream\n");
+    return NULL;
 }
 
 size_t SkFontHost::GetFileName(SkFontID fontID, char path[], size_t length,
                                int32_t* index) {
-    FontConfigTypeface* face = (FontConfigTypeface*)SkTypefaceCache::FindByID(fontID);
-    if (NULL == face || face->getLocalStream()) {
-        return 0;
-    }
-
-    // Here we cheat, and "know" what is in the identity fields.
-
-    const SkString& filename = face->getIdentity().fString;
-    if (index) {
-        *index = face->getIdentity().fTTCIndex;
-    }
-    if (path) {
-        size_t len = SkMin32(length, filename.size());
-        memcpy(path, filename.c_str(), len);
-    }
-    return filename.size();
+    SkASSERT(!"SkFontHost::GetFileName is DEPRECATED: call SkTypeface::openStream\n");
+    return 0;
 }
 
 ///////////////////////////////////////////////////////////////////////////////
 
 int FontConfigTypeface::onGetTableTags(SkFontTableTag tags[]) const {
     int ttcIndex;
-    SkAutoTUnref<SkStream> stream(open_stream(this, &ttcIndex));
+    SkAutoTUnref<SkStream> stream(this->openStream(&ttcIndex));
     return stream.get()
                 ? SkFontStream::GetTableTags(stream, ttcIndex, tags)
                 : 0;
@@ -312,7 +293,7 @@
 size_t FontConfigTypeface::onGetTableData(SkFontTableTag tag, size_t offset,
                                   size_t length, void* data) const {
     int ttcIndex;
-    SkAutoTUnref<SkStream> stream(open_stream(this, &ttcIndex));
+    SkAutoTUnref<SkStream> stream(this->openStream(&ttcIndex));
     return stream.get()
                 ? SkFontStream::GetTableData(stream, ttcIndex,
                                              tag, offset, length, data)