Add new entry point that supports fallback font selection based on language.

BUG= chromium:287995
R=reed@google.com, wangxianzhu@chromium.org

Review URL: https://codereview.chromium.org/23819067

git-svn-id: http://skia.googlecode.com/svn/trunk@11394 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/include/core/SkPaintOptionsAndroid.h b/include/core/SkPaintOptionsAndroid.h
index 78e4386..1bb29f4 100644
--- a/include/core/SkPaintOptionsAndroid.h
+++ b/include/core/SkPaintOptionsAndroid.h
@@ -94,7 +94,7 @@
 
 
     enum FontVariant {
-       kDefault_Variant = 0x01, // Currently setting yourself to Default gives you Compact Variant
+       kDefault_Variant = 0x01,
        kCompact_Variant = 0x02,
        kElegant_Variant = 0x04,
        kLast_Variant = kElegant_Variant,
diff --git a/include/ports/SkTypeface_android.h b/include/ports/SkTypeface_android.h
index 655670f..1ee923c 100644
--- a/include/ports/SkTypeface_android.h
+++ b/include/ports/SkTypeface_android.h
@@ -17,11 +17,33 @@
 
 /**
  *  Get the family name of the font in the fallback font list containing
- *  the specified character. if no font is found, returns false.
+ *  the specified character using the system's default language. This function
+ *  also assumes the only families with the elegant or default variants will be
+ *  returned.
+ *
+ *  @param uni  The unicode character to use for the lookup.
+ *  @param name The family name of the font file containing the unicode character
+ *              in the default language
+ *  @return     true if a font is found and false otherwise
  */
 SK_API bool SkGetFallbackFamilyNameForChar(SkUnichar uni, SkString* name);
 
 /**
+ *  Get the family name of the font in the fallback font list containing
+ *  the specified character taking into account the provided language. This
+ *  function also assumes the only families with the elegant or default variants
+ *  will be returned.
+ *
+ *  @param uni  The unicode character to use for the lookup.
+ *  @param lang The null terminated string representing the BCP 47 language
+ *              identifier for the preferred language
+ *  @param name The family name of the font file containing the unicode character
+ *              in the preferred language
+ *  @return     true if a font is found and false otherwise
+ */
+SK_API bool SkGetFallbackFamilyNameForChar(SkUnichar uni, const char* lang, SkString* name);
+
+/**
  *  For test only.
  *  Load font config from given xml files, instead of those from Android system.
  */
diff --git a/src/ports/SkFontConfigInterface_android.cpp b/src/ports/SkFontConfigInterface_android.cpp
index f56308d..3a4c02e 100644
--- a/src/ports/SkFontConfigInterface_android.cpp
+++ b/src/ports/SkFontConfigInterface_android.cpp
@@ -95,7 +95,7 @@
      *  Get the family name of the font in the default fallback font list that
      *  contains the specified chararacter. if no font is found, returns false.
      */
-    bool getFallbackFamilyNameForChar(SkUnichar uni, SkString* name);
+    bool getFallbackFamilyNameForChar(SkUnichar uni, const char* lang, SkString* name);
     /**
      *
      */
@@ -246,10 +246,10 @@
 
             if (fontRec.fIsValid) {
                 DEBUG_FONT(("---- SystemFonts[%d][%d] fallback=%d file=%s",
-                           i, fFonts.count() - 1, fontRec.fIsFallbackFont, filename.c_str()));
+                           i, fFonts.count() - 1, family->fIsFallbackFont, filename.c_str()));
             } else {
                 DEBUG_FONT(("---- SystemFonts[%d][%d] fallback=%d file=%s (INVALID)",
-                           i, fFonts.count() - 1, fontRec.fIsFallbackFont, filename.c_str()));
+                           i, fFonts.count() - 1, family->fIsFallbackFont, filename.c_str()));
                 continue;
             }
 
@@ -505,10 +505,20 @@
     return face;
 }
 
-bool SkFontConfigInterfaceAndroid::getFallbackFamilyNameForChar(SkUnichar uni, SkString* name) {
-    FallbackFontList* fallbackFontList = this->getCurrentLocaleFallbackFontList();
+bool SkFontConfigInterfaceAndroid::getFallbackFamilyNameForChar(SkUnichar uni,
+                                                                const char* lang,
+                                                                SkString* name) {
+    FallbackFontList* fallbackFontList = this->findFallbackFontList(lang);
     for (int i = 0; i < fallbackFontList->count(); i++) {
         FamilyRecID familyRecID = fallbackFontList->getAt(i);
+
+        // if it is not one of the accepted variants then move to the next family
+        int32_t acceptedVariants = SkPaintOptionsAndroid::kDefault_Variant |
+                                   SkPaintOptionsAndroid::kElegant_Variant;
+        if (!(fFontFamilies[familyRecID].fPaintOptions.getFontVariant() & acceptedVariants)) {
+            continue;
+        }
+
         FontRecID fontRecID = find_best_style(fFontFamilies[familyRecID], SkTypeface::kNormal);
         SkTypeface* face = this->getTypefaceForFontRec(fontRecID);
 
@@ -663,8 +673,14 @@
 ///////////////////////////////////////////////////////////////////////////////
 
 bool SkGetFallbackFamilyNameForChar(SkUnichar uni, SkString* name) {
+    SkString locale = SkFontConfigParser::GetLocale();
     SkFontConfigInterfaceAndroid* fontConfig = getSingletonInterface();
-    return fontConfig->getFallbackFamilyNameForChar(uni, name);
+    return fontConfig->getFallbackFamilyNameForChar(uni, locale.c_str(), name);
+}
+
+bool SkGetFallbackFamilyNameForChar(SkUnichar uni, const char* lang, SkString* name) {
+    SkFontConfigInterfaceAndroid* fontConfig = getSingletonInterface();
+    return fontConfig->getFallbackFamilyNameForChar(uni, lang, name);
 }
 
 void SkUseTestFontConfigFile(const char* mainconf, const char* fallbackconf,