Bug 1351200 - Part 2: stylo: Separate out caching code from font pref initialization code in GetFontPrefsForLang; r?emilio draft
authorManish Goregaokar <manishearth@gmail.com>
Tue, 04 Apr 2017 11:11:27 -0700
changeset 559268 c6689e36485b1ee6e0ce4ff344cde815369db04f
parent 559267 ebf16abe6d6f1ff27888029a221350a29b737e1d
child 559269 af0804ad4ddf910e530c78d396e1b6c4bc28f415
push id53032
push userbmo:manishearth@gmail.com
push dateSun, 09 Apr 2017 10:30:28 +0000
reviewersemilio
bugs1351200
milestone55.0a1
Bug 1351200 - Part 2: stylo: Separate out caching code from font pref initialization code in GetFontPrefsForLang; r?emilio MozReview-Commit-ID: JW0CqMz0GfW
layout/base/StaticPresData.cpp
layout/base/StaticPresData.h
--- a/layout/base/StaticPresData.cpp
+++ b/layout/base/StaticPresData.cpp
@@ -65,51 +65,20 @@ enum {
   eDefaultFont_Serif,
   eDefaultFont_SansSerif,
   eDefaultFont_Monospace,
   eDefaultFont_Cursive,
   eDefaultFont_Fantasy,
   eDefaultFont_COUNT
 };
 
-const LangGroupFontPrefs*
-StaticPresData::GetFontPrefsForLangHelper(nsIAtom *aLanguage,
-                                          const LangGroupFontPrefs* aPrefs) const
+void
+LangGroupFontPrefs::Initialize(nsIAtom* aLangGroupAtom)
 {
-  // Get language group for aLanguage:
-  MOZ_ASSERT(aLanguage);
-  MOZ_ASSERT(mLangService);
-  MOZ_ASSERT(aPrefs);
-
-  nsresult rv = NS_OK;
-  nsIAtom *langGroupAtom = nullptr;
-  langGroupAtom = mLangService->GetLanguageGroup(aLanguage, &rv);
-  if (NS_FAILED(rv) || !langGroupAtom) {
-    langGroupAtom = nsGkAtoms::x_western; // Assume x-western is safe...
-  }
-
-  LangGroupFontPrefs *prefs = const_cast<LangGroupFontPrefs*>(aPrefs);
-  if (prefs->mLangGroup) { // if initialized
-    DebugOnly<uint32_t> count = 0;
-    for (;;) {
-      NS_ASSERTION(++count < 35, "Lang group count exceeded!!!");
-      if (prefs->mLangGroup == langGroupAtom) {
-        return prefs;
-      }
-      if (!prefs->mNext) {
-        break;
-      }
-      prefs = prefs->mNext;
-    }
-
-    // nothing cached, so go on and fetch the prefs for this lang group:
-    prefs = prefs->mNext = new LangGroupFontPrefs;
-  }
-
-  prefs->mLangGroup = langGroupAtom;
+  mLangGroup = aLangGroupAtom;
 
   /* Fetch the font prefs to be used -- see bug 61883 for details.
      Not all prefs are needed upfront. Some are fallback prefs intended
      for the GFX font sub-system...
 
   1) unit : assumed to be the same for all language groups -------------
   font.size.unit = px | pt    XXX could be folded in the size... bug 90440
 
@@ -118,20 +87,20 @@ StaticPresData::GetFontPrefsForLangHelpe
   font.name.[generic].[langGroup] = current user' selected font on the pref dialog
   font.name-list.[generic].[langGroup] = fontname1, fontname2, ... [factory pre-built list]
   font.size.[generic].[langGroup] = integer - settable by the user
   font.size-adjust.[generic].[langGroup] = "float" - settable by the user
   font.minimum-size.[langGroup] = integer - settable by the user
   */
 
   nsAutoCString langGroup;
-  langGroupAtom->ToUTF8String(langGroup);
+  aLangGroupAtom->ToUTF8String(langGroup);
 
-  prefs->mDefaultVariableFont.size = nsPresContext::CSSPixelsToAppUnits(16);
-  prefs->mDefaultFixedFont.size = nsPresContext::CSSPixelsToAppUnits(13);
+  mDefaultVariableFont.size = nsPresContext::CSSPixelsToAppUnits(16);
+  mDefaultFixedFont.size = nsPresContext::CSSPixelsToAppUnits(13);
 
   nsAutoCString pref;
 
   // get the current applicable font-size unit
   enum {eUnit_unknown = -1, eUnit_px, eUnit_pt};
   int32_t unit = eUnit_px;
 
   nsAdoptingCString cvalue =
@@ -153,30 +122,30 @@ StaticPresData::GetFontPrefsForLangHelpe
   }
 
   // get font.minimum-size.[langGroup]
 
   MAKE_FONT_PREF_KEY(pref, "font.minimum-size.", langGroup);
 
   int32_t size = Preferences::GetInt(pref.get());
   if (unit == eUnit_px) {
-    prefs->mMinimumFontSize = nsPresContext::CSSPixelsToAppUnits(size);
+    mMinimumFontSize = nsPresContext::CSSPixelsToAppUnits(size);
   }
   else if (unit == eUnit_pt) {
-    prefs->mMinimumFontSize = nsPresContext::CSSPointsToAppUnits(size);
+    mMinimumFontSize = nsPresContext::CSSPointsToAppUnits(size);
   }
 
   nsFont* fontTypes[] = {
-    &prefs->mDefaultVariableFont,
-    &prefs->mDefaultFixedFont,
-    &prefs->mDefaultSerifFont,
-    &prefs->mDefaultSansSerifFont,
-    &prefs->mDefaultMonospaceFont,
-    &prefs->mDefaultCursiveFont,
-    &prefs->mDefaultFantasyFont
+    &mDefaultVariableFont,
+    &mDefaultFixedFont,
+    &mDefaultSerifFont,
+    &mDefaultSansSerifFont,
+    &mDefaultMonospaceFont,
+    &mDefaultCursiveFont,
+    &mDefaultFantasyFont
   };
   static_assert(MOZ_ARRAY_LENGTH(fontTypes) == eDefaultFont_COUNT,
                 "FontTypes array count is not correct");
 
   // Get attributes specific to each generic font. We do not get the user's
   // generic-font-name-to-specific-family-name preferences because its the
   // generic name that should be fed into the cascade. It is up to the GFX
   // code to look up the font prefs to convert generic names to specific
@@ -196,43 +165,43 @@ StaticPresData::GetFontPrefsForLangHelpe
 
       nsAdoptingString value = Preferences::GetString(pref.get());
       if (!value.IsEmpty()) {
         FontFamilyName defaultVariableName = FontFamilyName::Convert(value);
         FontFamilyType defaultType = defaultVariableName.mType;
         NS_ASSERTION(defaultType == eFamily_serif ||
                      defaultType == eFamily_sans_serif,
                      "default type must be serif or sans-serif");
-        prefs->mDefaultVariableFont.fontlist = FontFamilyList(defaultType);
+        mDefaultVariableFont.fontlist = FontFamilyList(defaultType);
       }
       else {
         MAKE_FONT_PREF_KEY(pref, "font.default.", langGroup);
         value = Preferences::GetString(pref.get());
         if (!value.IsEmpty()) {
           FontFamilyName defaultVariableName = FontFamilyName::Convert(value);
           FontFamilyType defaultType = defaultVariableName.mType;
           NS_ASSERTION(defaultType == eFamily_serif ||
                        defaultType == eFamily_sans_serif,
                        "default type must be serif or sans-serif");
-          prefs->mDefaultVariableFont.fontlist = FontFamilyList(defaultType);
+          mDefaultVariableFont.fontlist = FontFamilyList(defaultType);
         }
       }
     }
     else {
       if (eType == eDefaultFont_Monospace) {
         // This takes care of the confusion whereby people often expect "monospace"
         // to have the same default font-size as "-moz-fixed" (this tentative
         // size may be overwritten with the specific value for "monospace" when
         // "font.size.monospace.[langGroup]" is read -- see below)
-        prefs->mDefaultMonospaceFont.size = prefs->mDefaultFixedFont.size;
+        mDefaultMonospaceFont.size = mDefaultFixedFont.size;
       }
       else if (eType != eDefaultFont_Fixed) {
         // all the other generic fonts are initialized with the size of the
         // variable font, but their specific size can supersede later -- see below
-        font->size = prefs->mDefaultVariableFont.size;
+        font->size = mDefaultVariableFont.size;
       }
     }
 
     // Bug 84398: for spec purists, a different font-size only applies to the
     // .variable. and .fixed. fonts and the other fonts should get |font-size-adjust|.
     // The problem is that only GfxWin has the support for |font-size-adjust|. So for
     // parity, we enable the ability to set a different font-size on all platforms.
 
@@ -259,16 +228,60 @@ StaticPresData::GetFontPrefsForLangHelpe
 
 #ifdef DEBUG_rbs
     printf("%s Family-list:%s size:%d sizeAdjust:%.2f\n",
            generic_dot_langGroup.get(),
            NS_ConvertUTF16toUTF8(font->name).get(), font->size,
            font->sizeAdjust);
 #endif
   }
+}
+
+nsIAtom*
+StaticPresData::GetLangGroup(nsIAtom* aLanguage) const
+{
+  nsresult rv = NS_OK;
+  nsIAtom* langGroupAtom = nullptr;
+  langGroupAtom = mLangService->GetLanguageGroup(aLanguage, &rv);
+  if (NS_FAILED(rv) || !langGroupAtom) {
+    langGroupAtom = nsGkAtoms::x_western; // Assume x-western is safe...
+  }
+  return langGroupAtom;
+}
+
+const LangGroupFontPrefs*
+StaticPresData::GetFontPrefsForLangHelper(nsIAtom* aLanguage,
+                                          const LangGroupFontPrefs* aPrefs) const
+{
+  // Get language group for aLanguage:
+  MOZ_ASSERT(aLanguage);
+  MOZ_ASSERT(mLangService);
+  MOZ_ASSERT(aPrefs);
+
+  nsIAtom* langGroupAtom = GetLangGroup(aLanguage);
+
+  LangGroupFontPrefs* prefs = const_cast<LangGroupFontPrefs*>(aPrefs);
+  if (prefs->mLangGroup) { // if initialized
+    DebugOnly<uint32_t> count = 0;
+    for (;;) {
+      NS_ASSERTION(++count < 35, "Lang group count exceeded!!!");
+      if (prefs->mLangGroup == langGroupAtom) {
+        return prefs;
+      }
+      if (!prefs->mNext) {
+        break;
+      }
+      prefs = prefs->mNext;
+    }
+
+    // nothing cached, so go on and fetch the prefs for this lang group:
+    prefs = prefs->mNext = new LangGroupFontPrefs;
+  }
+
+  prefs->Initialize(langGroupAtom);
 
   return prefs;
 }
 
 const nsFont*
 StaticPresData::GetDefaultFontHelper(uint8_t aFontID, nsIAtom *aLanguage,
                                      const LangGroupFontPrefs* aPrefs) const
 {
--- a/layout/base/StaticPresData.h
+++ b/layout/base/StaticPresData.h
@@ -50,16 +50,19 @@ struct LangGroupFontPrefs {
       // - mLangGroup
       // - mDefault*Font
 
       curr = curr->mNext;
     }
     return n;
   }
 
+  // Initialize this with the data for a given language
+  void Initialize(nsIAtom* aLangGroupAtom);
+
   nsCOMPtr<nsIAtom> mLangGroup;
   nscoord mMinimumFontSize;
   nsFont mDefaultVariableFont;
   nsFont mDefaultFixedFont;
   nsFont mDefaultSerifFont;
   nsFont mDefaultSansSerifFont;
   nsFont mDefaultMonospaceFont;
   nsFont mDefaultCursiveFont;
@@ -86,16 +89,23 @@ public:
 
   /**
    * This table maps border-width enums 'thin', 'medium', 'thick'
    * to actual nscoord values.
    */
   const nscoord* GetBorderWidthTable() { return mBorderWidthTable; }
 
   /**
+   * Given a language, get the language group name, which can
+   * be used as an argument to LangGroupFontPrefs::Initialize()
+   *
+   */
+  nsIAtom* GetLangGroup(nsIAtom* aLanguage) const;
+
+  /**
    * Fetch the user's font preferences for the given aLanguage's
    * langugage group.
    *
    * The original code here is pretty old, and includes an optimization
    * whereby language-specific prefs are read per-document, and the
    * results are stored in a linked list, which is assumed to be very short
    * since most documents only ever use one language.
    *