Bug 1491860 - Use utf-8 rather than utf-16 strings to read/store font family names from preferences, as they're almost entirely ASCII. r=lsalzman
authorJonathan Kew <jkew@mozilla.com>
Tue, 18 Sep 2018 09:34:24 +0100
changeset 479331 541a1ba5f55a5119333b9f7de349e5d3e8482bf8
parent 479330 bae2e21a9312dea37815c31e21605527e926dbb9
child 479332 3701bbe68f257a13322bb30f3898230ca2779e54
child 479401 6263695b3cb85d546f5fc46d357c94a057b71cf7
push idunknown
push userunknown
push dateunknown
reviewerslsalzman
bugs1491860
milestone64.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 1491860 - Use utf-8 rather than utf-16 strings to read/store font family names from preferences, as they're almost entirely ASCII. r=lsalzman
gfx/thebes/gfxFT2FontList.cpp
gfx/thebes/gfxFontUtils.cpp
gfx/thebes/gfxFontUtils.h
gfx/thebes/gfxMacPlatformFontList.mm
gfx/thebes/gfxPlatformFontList.cpp
gfx/thebes/gfxPlatformFontList.h
layout/mathml/nsMathMLChar.cpp
--- a/gfx/thebes/gfxFT2FontList.cpp
+++ b/gfx/thebes/gfxFT2FontList.cpp
@@ -1531,24 +1531,24 @@ gfxFT2FontList::GetSystemFontList(Infall
         auto family = static_cast<FT2FontFamily*>(iter.Data().get());
         family->AddFacesToFontList(retValue);
     }
 }
 
 static void
 LoadSkipSpaceLookupCheck(nsTHashtable<nsCStringHashKey>& aSkipSpaceLookupCheck)
 {
-    AutoTArray<nsString, 5> skiplist;
+    AutoTArray<nsCString, 5> skiplist;
     gfxFontUtils::GetPrefsFontList(
         "font.whitelist.skip_default_features_space_check",
         skiplist);
     uint32_t numFonts = skiplist.Length();
     for (uint32_t i = 0; i < numFonts; i++) {
         ToLowerCase(skiplist[i]);
-        aSkipSpaceLookupCheck.PutEntry(NS_ConvertUTF16toUTF8(skiplist[i]));
+        aSkipSpaceLookupCheck.PutEntry(skiplist[i]);
     }
 }
 
 nsresult
 gfxFT2FontList::InitFontListForPlatform()
 {
     LoadSkipSpaceLookupCheck(mSkipSpaceLookupCheckFamilies);
 
--- a/gfx/thebes/gfxFontUtils.cpp
+++ b/gfx/thebes/gfxFontUtils.cpp
@@ -819,59 +819,59 @@ gfxFontUtils::MapCharToGlyph(const uint8
 
         // else the variation sequence was not supported, use default mapping
         // of the character code alone
     }
 
     return gid;
 }
 
-void gfxFontUtils::ParseFontList(const nsAString& aFamilyList,
-                                 nsTArray<nsString>& aFontList)
+void gfxFontUtils::ParseFontList(const nsACString& aFamilyList,
+                                 nsTArray<nsCString>& aFontList)
 {
-    const char16_t kComma = char16_t(',');
+    const char kComma = ',';
 
     // append each font name to the list
-    nsAutoString fontname;
-    const char16_t *p, *p_end;
+    nsAutoCString fontname;
+    const char *p, *p_end;
     aFamilyList.BeginReading(p);
     aFamilyList.EndReading(p_end);
 
      while (p < p_end) {
-        const char16_t *nameStart = p;
+        const char *nameStart = p;
         while (++p != p_end && *p != kComma)
         /* nothing */ ;
 
         // pull out a single name and clean out leading/trailing whitespace
         fontname = Substring(nameStart, p);
         fontname.CompressWhitespace(true, true);
 
         // append it to the list if it's not empty
         if (!fontname.IsEmpty()) {
             aFontList.AppendElement(fontname);
         }
         ++p;
     }
 }
 
 void gfxFontUtils::AppendPrefsFontList(const char *aPrefName,
-                                       nsTArray<nsString>& aFontList)
+                                       nsTArray<nsCString>& aFontList)
 {
     // get the list of single-face font families
-    nsAutoString fontlistValue;
-    nsresult rv = Preferences::GetString(aPrefName, fontlistValue);
+    nsAutoCString fontlistValue;
+    nsresult rv = Preferences::GetCString(aPrefName, fontlistValue);
     if (NS_FAILED(rv)) {
         return;
     }
 
     ParseFontList(fontlistValue, aFontList);
 }
 
 void gfxFontUtils::GetPrefsFontList(const char *aPrefName,
-                                    nsTArray<nsString>& aFontList)
+                                    nsTArray<nsCString>& aFontList)
 {
     aFontList.Clear();
     AppendPrefsFontList(aPrefName, aFontList);
 }
 
 // produce a unique font name that is (1) a valid Postscript name and (2) less
 // than 31 characters in length.  Using AddFontMemResourceEx on Windows fails
 // for names longer than 30 characters in length.
--- a/gfx/thebes/gfxFontUtils.h
+++ b/gfx/thebes/gfxFontUtils.h
@@ -957,26 +957,26 @@ public:
             return true;
 
         // otherwise we know this char cannot trigger bidi reordering
         return false;
     }
 
     // parse a simple list of font family names into
     // an array of strings
-    static void ParseFontList(const nsAString& aFamilyList,
-                              nsTArray<nsString>& aFontList);
+    static void ParseFontList(const nsACString& aFamilyList,
+                              nsTArray<nsCString>& aFontList);
 
     // for a given font list pref name, append list of font names
     static void AppendPrefsFontList(const char *aPrefName,
-                                    nsTArray<nsString>& aFontList);
+                                    nsTArray<nsCString>& aFontList);
 
     // for a given font list pref name, initialize a list of font names
     static void GetPrefsFontList(const char *aPrefName, 
-                                 nsTArray<nsString>& aFontList);
+                                 nsTArray<nsCString>& aFontList);
 
     // generate a unique font name
     static nsresult MakeUniqueUserFontName(nsAString& aName);
 
     // for color layer from glyph using COLR and CPAL tables
     static bool ValidateColorGlyphs(hb_blob_t* aCOLR, hb_blob_t* aCPAL);
     static bool GetColorGlyphLayers(hb_blob_t* aCOLR,
                                     hb_blob_t* aCPAL,
--- a/gfx/thebes/gfxMacPlatformFontList.mm
+++ b/gfx/thebes/gfxMacPlatformFontList.mm
@@ -1239,30 +1239,29 @@ gfxMacPlatformFontList::InitFontListForP
     GetPrefsAndStartLoader();
 
     return NS_OK;
 }
 
 void
 gfxMacPlatformFontList::InitSingleFaceList()
 {
-    AutoTArray<nsString, 10> singleFaceFonts;
+    AutoTArray<nsCString, 10> singleFaceFonts;
     gfxFontUtils::GetPrefsFontList("font.single-face-list", singleFaceFonts);
 
-    for (const auto& singleFaceFamily : singleFaceFonts) {
+    for (auto& familyName : singleFaceFonts) {
         LOG_FONTLIST(("(fontlist-singleface) face name: %s\n",
-                      NS_ConvertUTF16toUTF8(singleFaceFamily).get()));
+                      familyName.get()));
         // Each entry in the "single face families" list is expected to be a
         // colon-separated pair of FaceName:Family,
         // where FaceName is the individual face name (psname) of a font
         // that should be exposed as a separate family name,
         // and Family is the standard family to which that face belongs.
         // The only such face listed by default is
         //    Osaka-Mono:Osaka
-        NS_ConvertUTF16toUTF8 familyName(singleFaceFamily);
         auto colon = familyName.FindChar(':');
         if (colon == kNotFound) {
             continue;
         }
 
         // Look for the parent family in the main font family list,
         // and ensure we have loaded its list of available faces.
         nsAutoCString key(Substring(familyName, colon + 1));
--- a/gfx/thebes/gfxPlatformFontList.cpp
+++ b/gfx/thebes/gfxPlatformFontList.cpp
@@ -270,27 +270,27 @@ gfxPlatformFontList::FontWhitelistPrefCh
 }
 
 // number of CSS generic font families
 const uint32_t kNumGenerics = 5;
 
 void
 gfxPlatformFontList::ApplyWhitelist()
 {
-    nsTArray<nsString> list;
+    nsTArray<nsCString> list;
     gfxFontUtils::GetPrefsFontList(kFontSystemWhitelistPref, list);
     uint32_t numFonts = list.Length();
     mFontFamilyWhitelistActive = (numFonts > 0);
     if (!mFontFamilyWhitelistActive) {
         return;
     }
     nsTHashtable<nsCStringHashKey> familyNamesWhitelist;
     for (uint32_t i = 0; i < numFonts; i++) {
         nsAutoCString key;
-        ToLowerCase(NS_ConvertUTF16toUTF8(list[i]), key);
+        ToLowerCase(list[i], key);
         familyNamesWhitelist.PutEntry(key);
     }
     for (auto iter = mFontFamilies.Iter(); !iter.Done(); iter.Next()) {
         // Don't continue if we only have one font left.
         if (mFontFamilies.Count() == 1) {
             break;
         }
         nsAutoCString fontFamilyName(iter.Key());
@@ -502,42 +502,42 @@ gfxPlatformFontList::LookupInFaceNameLis
     }
 
     return lookup;
 }
 
 void
 gfxPlatformFontList::PreloadNamesList()
 {
-    AutoTArray<nsString, 10> preloadFonts;
+    AutoTArray<nsCString, 10> preloadFonts;
     gfxFontUtils::GetPrefsFontList("font.preload-names-list", preloadFonts);
 
     uint32_t numFonts = preloadFonts.Length();
     for (uint32_t i = 0; i < numFonts; i++) {
         nsAutoCString key;
-        GenerateFontListKey(NS_ConvertUTF16toUTF8(preloadFonts[i]), key);
+        GenerateFontListKey(preloadFonts[i], key);
 
         // only search canonical names!
         gfxFontFamily *familyEntry = mFontFamilies.GetWeak(key);
         if (familyEntry) {
             familyEntry->ReadOtherFamilyNames(this);
         }
     }
 
 }
 
 void
 gfxPlatformFontList::LoadBadUnderlineList()
 {
-    AutoTArray<nsString, 10> blacklist;
+    AutoTArray<nsCString, 10> blacklist;
     gfxFontUtils::GetPrefsFontList("font.blacklist.underline_offset", blacklist);
     uint32_t numFonts = blacklist.Length();
     for (uint32_t i = 0; i < numFonts; i++) {
         nsAutoCString key;
-        GenerateFontListKey(NS_ConvertUTF16toUTF8(blacklist[i]), key);
+        GenerateFontListKey(blacklist[i], key);
         mBadUnderlineFamilyNames.PutEntry(key);
     }
 }
 
 void
 gfxPlatformFontList::UpdateFontList()
 {
     InitFontList();
@@ -906,22 +906,22 @@ 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;
+    AutoTArray<nsCString,4> names;
     gfxFontUtils::AppendPrefsFontList(
         NameListPref(aGenericFamily, aLangGroup).get(), names);
 
-    for (nsString& name : names) {
-        gfxFontFamily* fontFamily = FindFamily(NS_ConvertUTF16toUTF8(name));
+    for (const nsCString& name : names) {
+        gfxFontFamily* fontFamily = FindFamily(name);
         if (fontFamily) {
             return fontFamily;
         }
     }
     return nullptr;
 }
 
 gfxCharacterMap*
@@ -967,17 +967,17 @@ gfxPlatformFontList::ResolveGenericFontN
 {
     const char* langGroupStr = GetPrefLangName(aPrefLang);
     const char* generic = GetGenericName(aGenericType);
 
     if (!generic) {
         return;
     }
 
-    AutoTArray<nsString,4> genericFamilies;
+    AutoTArray<nsCString,4> genericFamilies;
 
     // load family for "font.name.generic.lang"
     gfxFontUtils::AppendPrefsFontList(
         NamePref(generic, langGroupStr).get(), genericFamilies);
 
     // load fonts for "font.name-list.generic.lang"
     gfxFontUtils::AppendPrefsFontList(
         NameListPref(generic, langGroupStr).get(), genericFamilies);
@@ -988,50 +988,50 @@ gfxPlatformFontList::ResolveGenericFontN
     gfxPlatformFontList::GetFontFamiliesFromGenericFamilies(genericFamilies,
                                                            langGroup,
                                                            aGenericFamilies);
 
 #if 0  // dump out generic mappings
     printf("%s ===> ", prefFontName.get());
     for (uint32_t k = 0; k < aGenericFamilies->Length(); k++) {
         if (k > 0) printf(", ");
-        printf("%s", NS_ConvertUTF16toUTF8(aGenericFamilies[k]->Name()).get());
+        printf("%s", aGenericFamilies[k]->Name().get());
     }
     printf("\n");
 #endif
 }
 
 void
 gfxPlatformFontList::ResolveEmojiFontNames(
     nsTArray<RefPtr<gfxFontFamily>>* aGenericFamilies)
 {
     // emoji preference has no lang name
-    AutoTArray<nsString,4> genericFamilies;
+    AutoTArray<nsCString,4> genericFamilies;
 
     nsAutoCString prefFontListName("font.name-list.emoji");
     gfxFontUtils::AppendPrefsFontList(prefFontListName.get(), genericFamilies);
 
     gfxPlatformFontList::GetFontFamiliesFromGenericFamilies(genericFamilies,
                                                             nullptr,
                                                             aGenericFamilies);
 }
 
 void
 gfxPlatformFontList::GetFontFamiliesFromGenericFamilies(
-    nsTArray<nsString>& aGenericNameFamilies,
+    nsTArray<nsCString>& aGenericNameFamilies,
     nsAtom* aLangGroup,
     nsTArray<RefPtr<gfxFontFamily>>* aGenericFamilies)
 {
     // lookup and add platform fonts uniquely
-    for (const nsString& genericFamily : aGenericNameFamilies) {
+    for (const nsCString& genericFamily : aGenericNameFamilies) {
         gfxFontStyle style;
         style.language = aLangGroup;
         style.systemFont = false;
         AutoTArray<FamilyAndGeneric,10> families;
-        FindAndAddFamilies(NS_ConvertUTF16toUTF8(genericFamily),
+        FindAndAddFamilies(genericFamily,
                            &families, FindFamiliesFlags(0),
                            &style);
         for (const FamilyAndGeneric& f : families) {
             if (!aGenericFamilies->Contains(f.mFamily)) {
                 aGenericFamilies->AppendElement(f.mFamily);
             }
         }
     }
--- a/gfx/thebes/gfxPlatformFontList.h
+++ b/gfx/thebes/gfxPlatformFontList.h
@@ -515,17 +515,17 @@ protected:
                             eFontPrefLang aPrefLang,
                             nsTArray<RefPtr<gfxFontFamily>>* aGenericFamilies);
 
     void
     ResolveEmojiFontNames(nsTArray<RefPtr<gfxFontFamily>>* aGenericFamilies);
 
     void
     GetFontFamiliesFromGenericFamilies(
-        nsTArray<nsString>& aGenericFamilies,
+        nsTArray<nsCString>& aGenericFamilies,
         nsAtom* aLangGroup,
         nsTArray<RefPtr<gfxFontFamily>>* aFontFamilies);
 
     virtual nsresult InitFontListForPlatform() = 0;
 
     void ApplyWhitelist();
 
     // Create a new gfxFontFamily of the appropriate subclass for the platform,
--- a/layout/mathml/nsMathMLChar.cpp
+++ b/layout/mathml/nsMathMLChar.cpp
@@ -1474,29 +1474,29 @@ nsMathMLChar::StretchEnumContext::EnumCa
                                               familyList)))
     return false; // no need to continue
 
   return true; // true means continue
 }
 
 static void
 AppendFallbacks(nsTArray<FontFamilyName>& aNames,
-                const nsTArray<nsString>& aFallbacks)
+                const nsTArray<nsCString>& aFallbacks)
 {
-  for (const nsString& fallback : aFallbacks) {
-    aNames.AppendElement(FontFamilyName(NS_ConvertUTF16toUTF8(fallback),
+  for (const nsCString& fallback : aFallbacks) {
+    aNames.AppendElement(FontFamilyName(fallback,
                                         eUnquotedName));
   }
 }
 
 // insert math fallback families just before the first generic or at the end
 // when no generic present
 static void
 InsertMathFallbacks(FontFamilyList& aFamilyList,
-                    nsTArray<nsString>& aFallbacks)
+                    nsTArray<nsCString>& aFallbacks)
 {
   nsTArray<FontFamilyName> mergedList;
 
   bool inserted = false;
   for (const FontFamilyName& name : aFamilyList.GetFontlist()->mNames) {
     if (!inserted && name.IsGeneric()) {
       inserted = true;
       AppendFallbacks(mergedList, aFallbacks);
@@ -1647,17 +1647,17 @@ nsMathMLChar::StretchInternal(nsIFrame* 
 
   if (!done) { // normal case
     // Use the css font-family but add preferred fallback fonts.
     font = mComputedStyle->StyleFont()->mFont;
     NormalizeDefaultFont(font, aFontSizeInflation);
 
     // really shouldn't be doing things this way but for now
     // insert fallbacks into the list
-    AutoTArray<nsString, 16> mathFallbacks;
+    AutoTArray<nsCString, 16> mathFallbacks;
     gfxFontUtils::GetPrefsFontList("font.name.serif.x-math", mathFallbacks);
     gfxFontUtils::AppendPrefsFontList("font.name-list.serif.x-math",
                                       mathFallbacks);
     InsertMathFallbacks(font.fontlist, mathFallbacks);
 
 
 #ifdef NOISY_SEARCH
     nsAutoString fontlistStr;