make fontscalar gammatext lcdtext typeface verttext2 gm portable

Pass generic font names to tool util function to generate
platform specific fonts and gm test output by unique name.

R=bungeman@google.com

Review URL: https://codereview.chromium.org/1256903002
diff --git a/gm/fontscaler.cpp b/gm/fontscaler.cpp
index c3b2c87..6087478 100644
--- a/gm/fontscaler.cpp
+++ b/gm/fontscaler.cpp
@@ -21,7 +21,9 @@
 protected:
 
     SkString onShortName() override {
-        return SkString("fontscaler");
+        SkString name("fontscaler");
+        name.append(sk_tool_utils::major_platform_os_name());
+        return name;
     }
 
     SkISize onISize() override {
@@ -44,7 +46,6 @@
         //With freetype the default (normal hinting) can be really ugly.
         //Most distros now set slight (vertical hinting only) in any event.
         paint.setHinting(SkPaint::kSlight_Hinting);
-        sk_tool_utils::set_portable_typeface(&paint, "Times Roman", SkTypeface::kNormal);
 
         const char* text = "Hamburgefons ooo mmm";
         const size_t textLen = strlen(text);
diff --git a/gm/gammatext.cpp b/gm/gammatext.cpp
index ed8c829..b6d4ea2 100644
--- a/gm/gammatext.cpp
+++ b/gm/gammatext.cpp
@@ -40,7 +40,9 @@
 class GammaTextGM : public skiagm::GM {
 protected:
     SkString onShortName() override {
-        return SkString("gammatext");
+        SkString name("gammatext");
+        name.append(sk_tool_utils::major_platform_os_name());
+        return name;
     }
 
     SkISize onISize() override {
@@ -72,7 +74,7 @@
         size_t len = strlen(text);
 
         SkPaint paint;
-        setFont(&paint, "Times");
+        setFont(&paint, sk_tool_utils::platform_font_name("serif"));
         paint.setTextSize(SkIntToScalar(16));
         paint.setAntiAlias(true);
         paint.setLCDRenderText(true);
diff --git a/gm/lcdtext.cpp b/gm/lcdtext.cpp
index 5ea9396..6daec82 100644
--- a/gm/lcdtext.cpp
+++ b/gm/lcdtext.cpp
@@ -27,7 +27,9 @@
 protected:
     
     SkString onShortName() {
-        return SkString("lcdtext");
+        SkString name("lcdtext");
+        name.append(sk_tool_utils::major_platform_os_name());
+        return name;
     }
     
     SkISize onISize() { return SkISize::Make(640, 480); }
@@ -51,7 +53,6 @@
         paint.setColor(SK_ColorBLACK);
         paint.setDither(true);
         paint.setAntiAlias(true);
-        sk_tool_utils::set_portable_typeface(&paint);
         paint.setSubpixelText(subpixelTextEnabled);
         paint.setLCDRenderText(lcdRenderTextEnabled);
         paint.setTextSize(textHeight);
diff --git a/gm/typeface.cpp b/gm/typeface.cpp
index 5ddc338..b8f54ab 100644
--- a/gm/typeface.cpp
+++ b/gm/typeface.cpp
@@ -11,75 +11,6 @@
 #include "SkTypeface.h"
 #include "SkTypes.h"
 
-static const char* gFaces[] = {
-    "Times Roman",
-    "Hiragino Maru Gothic Pro",
-    "Papyrus",
-    "Helvetica",
-    "Courier New"
-};
-
-class TypefaceGM : public skiagm::GM {
-public:
-    TypefaceGM()
-        : fFaces(NULL) {
-    }
-
-    virtual ~TypefaceGM() {
-        if (fFaces) {
-            for (size_t i = 0; i < SK_ARRAY_COUNT(gFaces); i++) {
-                SkSafeUnref(fFaces[i]);
-            }
-            delete [] fFaces;
-        }
-    }
-
-protected:
-    void onOnceBeforeDraw() override {
-        fFaces = new SkTypeface*[SK_ARRAY_COUNT(gFaces)];
-        for (size_t i = 0; i < SK_ARRAY_COUNT(gFaces); i++) {
-            fFaces[i] = sk_tool_utils::create_portable_typeface(gFaces[i], SkTypeface::kNormal);
-        }
-    }
-
-    SkString onShortName() override {
-        return SkString("typeface");
-    }
-
-    SkISize onISize() override {
-        return SkISize::Make(640, 480);
-    }
-
-    void onDraw(SkCanvas* canvas) override {
-        SkString text("Typefaces are fun!");
-        SkScalar y = 0;
-
-        SkPaint paint;
-        paint.setAntiAlias(true);
-        for (int i = 0; i < (int)SK_ARRAY_COUNT(gFaces); i++) {
-            this->drawWithFace(text, i, y, paint, canvas);
-        }
-        // Now go backwards
-        for (int i = SK_ARRAY_COUNT(gFaces) - 1; i >= 0; i--) {
-            this->drawWithFace(text, i, y, paint, canvas);
-        }
-    }
-
-private:
-    void drawWithFace(const SkString& text, int i, SkScalar& y, SkPaint& paint,
-                      SkCanvas* canvas) {
-        paint.setTypeface(fFaces[i]);
-        y += paint.getFontMetrics(NULL);
-        canvas->drawText(text.c_str(), text.size(), 0, y, paint);
-    }
-
-    SkTypeface** fFaces;
-
-    typedef skiagm::GM INHERITED;
-};
-
-///////////////////////////////////////////////////////////////////////////////
-
 static void getGlyphPositions(const SkPaint& paint, const uint16_t glyphs[],
                              int count, SkScalar x, SkScalar y, SkPoint pos[]) {
     SkASSERT(SkPaint::kGlyphID_TextEncoding == paint.getTextEncoding());
@@ -177,8 +108,8 @@
 protected:
     void onOnceBeforeDraw() override {
         for (int i = 0; i < gFaceStylesCount; i++) {
-            fFaces[i] = sk_tool_utils::create_portable_typeface(gFaceStyles[i].fName,
-                                                         gFaceStyles[i].fStyle);
+            fFaces[i] = SkTypeface::CreateFromName(
+                    sk_tool_utils::platform_font_name(gFaceStyles[i].fName), gFaceStyles[i].fStyle);
         }
     }
 
@@ -187,6 +118,7 @@
         if (fApplyKerning) {
             name.append("_kerning");
         }
+        name.append(sk_tool_utils::major_platform_os_name());
         return name;
     }
 
@@ -227,6 +159,5 @@
 
 ///////////////////////////////////////////////////////////////////////////////
 
-DEF_GM( return new TypefaceGM; )
 DEF_GM( return new TypefaceStylesGM(false); )
 DEF_GM( return new TypefaceStylesGM(true); )
diff --git a/gm/verttext2.cpp b/gm/verttext2.cpp
index 9918049..3a3955e 100644
--- a/gm/verttext2.cpp
+++ b/gm/verttext2.cpp
@@ -32,12 +32,16 @@
     void onOnceBeforeDraw() override {
         const int pointSize = 24;
         textHeight = SkIntToScalar(pointSize);
-        fProp = sk_tool_utils::create_portable_typeface("Helvetica", SkTypeface::kNormal);
-        fMono = sk_tool_utils::create_portable_typeface("Courier New", SkTypeface::kNormal);
+        fProp = SkTypeface::CreateFromName(sk_tool_utils::platform_font_name("sans-serif"),
+                SkTypeface::kNormal);
+        fMono = SkTypeface::CreateFromName(sk_tool_utils::platform_font_name("monospace"),
+                SkTypeface::kNormal);
     }
 
     SkString onShortName() override {
-        return SkString("verttext2");
+        SkString name("verttext2");
+        name.append(sk_tool_utils::major_platform_os_name());
+        return name;
     }
 
     SkISize onISize() override { return SkISize::Make(640, 480); }
diff --git a/tools/sk_tool_utils.cpp b/tools/sk_tool_utils.cpp
index 9ab86ed..4897938 100644
--- a/tools/sk_tool_utils.cpp
+++ b/tools/sk_tool_utils.cpp
@@ -17,10 +17,52 @@
 #include "SkTextBlob.h"
 
 DEFINE_bool(portableFonts, false, "Use portable fonts");
-DEFINE_bool(resourceFonts, false, "Use resource fonts");
 
 namespace sk_tool_utils {
 
+/* these are the default fonts chosen by Chrome for serif, sans-serif, and monospace */
+static const char* gStandardFontNames[][3] = {
+    { "Times", "Helvetica", "Courier" }, // Mac
+    { "Times New Roman", "Helvetica", "Courier" }, // iOS
+    { "Times New Roman", "Arial", "Courier New" }, // Win
+    { "Times New Roman", "Arial", "Monospace" }, // Ubuntu
+    { "Droid Serif", "Droid Sans", "Droid Sans Mono" }, // Android
+    { "Tinos", "Arimo", "Cousine" } // ChromeOS
+};
+
+const char* platform_font_name(const char* name) {
+    SkString platform = major_platform_os_name();
+    int index;
+    if (!strcmp(name, "serif")) {
+        index = 0;
+    } else if (!strcmp(name, "san-serif")) {
+        index = 1;
+    } else if (!strcmp(name, "monospace")) {
+        index = 2;
+    } else {
+        return name;
+    }
+    if (platform.equals("Mac")) {
+        return gStandardFontNames[0][index];
+    }
+    if (platform.equals("iOS")) {
+        return gStandardFontNames[1][index];
+    }
+    if (platform.equals("Win")) {
+        return gStandardFontNames[2][index];
+    }
+    if (platform.equals("Ubuntu")) {
+        return gStandardFontNames[3][index];
+    }
+    if (platform.equals("Android")) {
+        return gStandardFontNames[4][index];
+    }
+    if (platform.equals("ChromeOS")) {
+        return gStandardFontNames[5][index];
+    }
+    return name;
+}
+
 const char* platform_os_emoji() {
     const char* osName = platform_os_name();
     if (!strcmp(osName, "Android") || !strcmp(osName, "Ubuntu")) {
@@ -69,6 +111,23 @@
     return "";
 }
 
+// omit version number in returned value
+SkString major_platform_os_name() {
+    SkString name;
+    for (int index = 0; index < FLAGS_key.count(); index += 2) {
+        if (!strcmp("os", FLAGS_key[index])) {
+            const char* platform = FLAGS_key[index + 1];
+            const char* end = platform;
+            while (*end && (*end < '0' || *end > '9')) {
+                ++end;
+            }
+            name.append(platform, end - platform);
+            break;
+        }
+    }
+    return name;
+}
+
 const char* platform_extra_config(const char* config) {
     for (int index = 0; index < FLAGS_key.count(); index += 2) {
         if (!strcmp("extra_config", FLAGS_key[index]) && !strcmp(config, FLAGS_key[index + 1])) {
@@ -103,8 +162,6 @@
     SkTypeface* face;
     if (FLAGS_portableFonts) {
         face = create_font(name, style);
-    } else if (FLAGS_resourceFonts) {
-        face = resource_font(name, style);
     } else {
         face = SkTypeface::CreateFromName(name, style);
     }
diff --git a/tools/sk_tool_utils.h b/tools/sk_tool_utils.h
index 2bef19d..59ae66a 100644
--- a/tools/sk_tool_utils.h
+++ b/tools/sk_tool_utils.h
@@ -24,26 +24,73 @@
 namespace sk_tool_utils {
 
     const char* colortype_name(SkColorType);
+
+    /**
+     * Map opaque colors from 8888 to 565.
+     */
     SkColor color_to_565(SkColor color);
+
+    /**
+     * Return a color emoji typeface if available.
+     */
     void emoji_typeface(SkAutoTUnref<SkTypeface>* );
+
+    /**
+     * If the platform supports color emoji, return sample text the emoji can render.
+     */
     const char* emoji_sample_text();
+
+    /**
+     * If the platform supports color emoji, return the type (i.e. "CBDT", "SBIX", "").
+     */
     const char* platform_os_emoji();
+
+    /**
+     * Return the platform name with the version number ("Mac10.9", "Win8", etc.) if available.
+     */
     const char* platform_os_name();
+
+    /**
+     * Return the platform name without the version number ("Mac", "Win", etc.) if available.
+     */
+    SkString major_platform_os_name();
+
+    /**
+     * Return the platform extra config (e.g. "GDI") if available.
+     */
     const char* platform_extra_config(const char* config);
 
     /**
+     * Map serif, san-serif, and monospace to the platform-specific font name.
+     */
+    const char* platform_font_name(const char* name);
+
+    /**
      * Sets the paint to use a platform-independent text renderer if FLAGS_portableFonts is set.
      * FIXME: will become obsolete as GMs migrate to set portable typeface always.
      */
     void set_portable_typeface(SkPaint* paint, const char* name = NULL,
                                SkTypeface::Style style = SkTypeface::kNormal);
+
+    /**
+     * Sets the paint to use a platform-independent text renderer
+     */
     void set_portable_typeface_always(SkPaint* paint, const char* name = NULL,
                                SkTypeface::Style style = SkTypeface::kNormal);
+
+    /** 
+     * Returns a platform-independent text renderer if FLAGS_portableFonts is set.
+     * FIXME: will become obsolete as GMs migrate to set portable typeface always.
+     */
     SkTypeface* create_portable_typeface(const char* name, SkTypeface::Style style);
+
+    /**
+     * Returns a platform-independent text renderer.
+     */
     SkTypeface* create_portable_typeface_always(const char* name, SkTypeface::Style style);
+
     /** Call to clean up portable font references. */
     void release_portable_typefaces();
-    void report_used_chars();
 
     /**
      *  Call canvas->writePixels() by using the pixels from bitmap, but with an info that claims
@@ -53,7 +100,6 @@
 
     // private to sk_tool_utils
     SkTypeface* create_font(const char* name, SkTypeface::Style );
-    SkTypeface* resource_font(const char* name, SkTypeface::Style );
 
     /** Returns a newly created CheckerboardShader. */
     SkShader* create_checkerboard_shader(SkColor c1, SkColor c2, int size);
diff --git a/tools/sk_tool_utils_font.cpp b/tools/sk_tool_utils_font.cpp
index 7b95fb0..06008c6 100644
--- a/tools/sk_tool_utils_font.cpp
+++ b/tools/sk_tool_utils_font.cpp
@@ -68,113 +68,4 @@
     return SkNEW_ARGS(SkTestTypeface, (font, SkFontStyle(style)));
 }
 
-
-SkTypeface* resource_font(const char* name, SkTypeface::Style style) {
-    const char* file = NULL;
-    if (name) {
-        for (int index = 0; index < gSubFontsCount; ++index) {
-            const SubFont& sub = gSubFonts[index];
-            if (!strcmp(name, sub.fName) && sub.fStyle == style) {
-                file = sub.fFile;
-                break;
-            }
-        }
-        if (!file) {
-            return SkTypeface::CreateFromName(name, style);
-        }
-    } else {
-        file = gSubFonts[gDefaultFontIndex].fFile;
-    }
-    SkString filepath(GetResourcePath(file));
-    if (sk_exists(filepath.c_str())) {
-        return SkTypeface::CreateFromFile(filepath.c_str());
-    }
-    return SkTypeface::CreateFromName(name, style);
-}
-
-#ifdef SK_DEBUG
-#include <stdio.h>
-
-char const * const gStyleName[] = {
-    "",
-    "_Bold",
-    "_Italic",
-    "_BoldItalic",
-};
-
-static SkString strip_spaces(const char* str) {
-    SkString result;
-    int count = (int) strlen(str);
-    for (int index = 0; index < count; ++index) {
-        char c = str[index];
-        if (c != ' ' && c != '-') {
-            result += c;
-        }
-    }
-    return result;
-}
-
-const char gHeader[] =
-"/*\n"
-" * Copyright 2014 Google Inc.\n"
-" *\n"
-" * Use of this source code is governed by a BSD-style license that can be\n"
-" * found in the LICENSE file.\n"
-" */\n"
-"\n"
-"// Auto-generated by ";
-
-static FILE* font_header() {
-    SkString pathStr(GetResourcePath());
-    pathStr = SkOSPath::Join(pathStr.c_str(), "..");
-    pathStr = SkOSPath::Join(pathStr.c_str(), "tools");
-    pathStr = SkOSPath::Join(pathStr.c_str(), "test_font_data_chars.cpp");
-    FILE* out = fopen(pathStr.c_str(), "w");
-    fprintf(out, "%s%s\n\n", gHeader, SkOSPath::Basename(__FILE__).c_str());
-    return out;
-}
-
-void report_used_chars() {
-    FILE* out = font_header();
-    for (int index = 0; index < gTestFontsCount; ++index) {
-        SkTestFontData& fontData = gTestFonts[index];
-        SkTestFont* font = fontData.fFontCache;
-        if (!font) {
-            continue;
-        }
-        SkString name(strip_spaces(font->debugFontName()));
-        fprintf(out, "const char g%s%s[] =\n", name.c_str(), gStyleName[font->fDebugStyle]);
-        SkString used("    \"");
-        for (int c = ' '; c <= '~'; ++c) {
-            int bitOffset = c - ' ';
-            if (font->fDebugBits[bitOffset >> 3] & (1 << (bitOffset & 7))) {
-                if (c == '"' || c == '\\') {
-                    used += '\\';
-                }
-                used += c;
-            }
-        }
-        if (used.size() > 1) {
-            fprintf(out, "%s\"", used.c_str());
-        }
-        int oIndex = 0;
-        while (font->fDebugOverage[oIndex]) {
-            uint16_t uni = font->fDebugOverage[oIndex];
-            size_t byteCount = SkUTF16_ToUTF8(&uni, 1, NULL);
-            SkAutoSTMalloc<10, char> utf8(byteCount);
-            SkUTF16_ToUTF8(&uni, 1, utf8);
-            for (unsigned byteIndex = 0; byteIndex < byteCount; ++byteIndex) {
-                char unibyte = utf8[byteIndex];
-                fprintf(out, " \"\\x%02X\"", (unsigned char) unibyte);
-            }
-            if (++oIndex >= (int) sizeof(font->fDebugOverage)) {
-                break;
-            }
-        }
-       fprintf(out, ";\n");
-    }
-    fclose(out);
-}
-#endif
-
 }