Bug 1344990 part.3 Implement nsIFontEnumerator::GetDefaultFont() as returning first available font in font.name-list.* r=jfkthame
authorMasayuki Nakano <masayuki@d-toybox.com>
Mon, 03 Apr 2017 21:00:14 +0900
changeset 399063 74fa1a2c624020caa50d8c13028268550defc537
parent 399062 2b99ea907d3d434e698a551b21ba65e0c4546b11
child 399064 0b45ef5f7702377bded5a67c6af0a713eccfc41e
push id7391
push usermtabara@mozilla.com
push dateMon, 12 Jun 2017 13:08:53 +0000
treeherdermozilla-beta@2191d7f87e2e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjfkthame
bugs1344990
milestone55.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1344990 part.3 Implement nsIFontEnumerator::GetDefaultFont() as returning first available font in font.name-list.* r=jfkthame nsIFontEnumerator::GetDefaultFont() returns always nullptr. However, it's used in font setting UI at creating drop down list of available fonts. So, if we implement this as returning first available font of "font.name-list.*", it's what is the necessary UI for "default" font when "font.name.*" is empty string. So, with this patch, the top item of font list becomes "Default (%s)" if there is available font. MozReview-Commit-ID: cRU8gixgdF
gfx/src/nsThebesFontEnumerator.cpp
gfx/thebes/gfxPlatform.cpp
gfx/thebes/gfxPlatform.h
gfx/thebes/gfxPlatformFontList.cpp
gfx/thebes/gfxPlatformFontList.h
--- a/gfx/src/nsThebesFontEnumerator.cpp
+++ b/gfx/src/nsThebesFontEnumerator.cpp
@@ -86,18 +86,28 @@ nsThebesFontEnumerator::HaveFontFor(cons
     return NS_OK;
 }
 
 NS_IMETHODIMP
 nsThebesFontEnumerator::GetDefaultFont(const char *aLangGroup,
                                        const char *aGeneric,
                                        char16_t **aResult)
 {
-    NS_ENSURE_ARG_POINTER(aResult);
+    if (NS_WARN_IF(!aResult) || NS_WARN_IF(!aLangGroup) ||
+        NS_WARN_IF(!aGeneric)) {
+        return NS_ERROR_INVALID_ARG;
+    }
+
     *aResult = nullptr;
+    nsAutoString defaultFontName(gfxPlatform::GetPlatform()->
+        GetDefaultFontName(nsDependentCString(aLangGroup),
+                           nsDependentCString(aGeneric)));
+    if (!defaultFontName.IsEmpty()) {
+        *aResult = ToNewUnicode(defaultFontName);
+    }
     return NS_OK;
 }
 
 NS_IMETHODIMP
 nsThebesFontEnumerator::UpdateFontList(bool *_retval)
 {
     gfxPlatform::GetPlatform()->UpdateFontList();
     *_retval = false; // always return false for now
--- a/gfx/thebes/gfxPlatform.cpp
+++ b/gfx/thebes/gfxPlatform.cpp
@@ -1545,16 +1545,30 @@ nsresult
 gfxPlatform::GetStandardFamilyName(const nsAString& aFontName,
                                    nsAString& aFamilyName)
 {
     gfxPlatformFontList::PlatformFontList()->GetStandardFamilyName(aFontName,
                                                                    aFamilyName);
     return NS_OK;
 }
 
+nsString
+gfxPlatform::GetDefaultFontName(const nsACString& aLangGroup,
+                                const nsACString& aGenericFamily)
+{
+    gfxFontFamily* fontFamily = gfxPlatformFontList::PlatformFontList()->
+        GetDefaultFontFamily(aLangGroup, aGenericFamily);
+    if (!fontFamily) {
+      return EmptyString();
+    }
+    nsAutoString result;
+    fontFamily->LocalizedName(result);
+    return result;
+}
+
 bool
 gfxPlatform::DownloadableFontsEnabled()
 {
     if (mAllowDownloadableFonts == UNINITIALIZED_VALUE) {
         mAllowDownloadableFonts =
             Preferences::GetBool(GFX_DOWNLOADABLE_FONTS_ENABLED, false);
     }
 
--- a/gfx/thebes/gfxPlatform.h
+++ b/gfx/thebes/gfxPlatform.h
@@ -351,16 +351,26 @@ public:
 
     /**
      * Resolving a font name to family name. The result MUST be in the result of GetFontList().
      * If the name doesn't in the system, aFamilyName will be empty string, but not failed.
      */
     virtual nsresult GetStandardFamilyName(const nsAString& aFontName, nsAString& aFamilyName);
 
     /**
+     * Returns default font name (localized family name) for aLangGroup and
+     * aGenericFamily.  The result is typically the first font in
+     * font.name-list.<aGenericFamily>.<aLangGroup>.  However, if it's not
+     * available in the system, this may return second or later font in the
+     * pref.  If there are no available fonts in the pref, returns empty string.
+     */
+    nsString GetDefaultFontName(const nsACString& aLangGroup,
+                                const nsACString& aGenericFamily);
+
+    /**
      * Create the appropriate platform font group
      */
     virtual gfxFontGroup*
     CreateFontGroup(const mozilla::FontFamilyList& aFontFamilyList,
                     const gfxFontStyle *aStyle,
                     gfxTextPerfMetrics* aTextPerf,
                     gfxUserFontSet *aUserFontSet,
                     gfxFloat aDevToCssSize) = 0;
--- a/gfx/thebes/gfxPlatformFontList.cpp
+++ b/gfx/thebes/gfxPlatformFontList.cpp
@@ -788,16 +788,41 @@ gfxPlatformFontList::GetStandardFamilyNa
     gfxFontFamily *ff = FindFamily(aFontName);
     if (!ff) {
         return false;
     }
     aFamilyName.Assign(ff->Name());
     return true;
 }
 
+gfxFontFamily*
+gfxPlatformFontList::GetDefaultFontFamily(const nsACString& aLangGroup,
+                                          const nsACString& aGenericFamily)
+{
+    if (NS_WARN_IF(aLangGroup.IsEmpty()) ||
+        NS_WARN_IF(aGenericFamily.IsEmpty())) {
+        return nullptr;
+    }
+
+    AutoTArray<nsString,4> names;
+    nsAutoCString prefName("font.name-list.");
+    prefName.Append(aGenericFamily);
+    prefName.Append('.');
+    prefName.Append(aLangGroup);
+    gfxFontUtils::AppendPrefsFontList(prefName.get(), names);
+
+    for (nsString& name : names) {
+        gfxFontFamily* fontFamily = FindFamily(name);
+        if (fontFamily) {
+            return fontFamily;
+        }
+    }
+    return nullptr;
+}
+
 gfxCharacterMap*
 gfxPlatformFontList::FindCharMap(gfxCharacterMap *aCmap)
 {
     aCmap->CalcHash();
     gfxCharacterMap *cmap = AddCmap(aCmap);
     cmap->mShared = true;
     return cmap;
 }
--- a/gfx/thebes/gfxPlatformFontList.h
+++ b/gfx/thebes/gfxPlatformFontList.h
@@ -171,16 +171,22 @@ public:
                                            uint8_t aStyle,
                                            const uint8_t* aFontData,
                                            uint32_t aLength) = 0;
 
     // get the standard family name on the platform for a given font name
     // (platforms may override, eg Mac)
     virtual bool GetStandardFamilyName(const nsAString& aFontName, nsAString& aFamilyName);
 
+    // get the default font name which is available on the system from
+    // font.name-list.*.  if there are no available fonts in the pref,
+    // returns nullptr.
+    gfxFontFamily* GetDefaultFontFamily(const nsACString& aLangGroup,
+                                        const nsACString& aGenericFamily);
+
     virtual void AddSizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf,
                                         FontListSizes* aSizes) const;
     virtual void AddSizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf,
                                         FontListSizes* aSizes) const;
 
     // search for existing cmap that matches the input
     // return the input if no match is found
     gfxCharacterMap* FindCharMap(gfxCharacterMap *aCmap);