Bug 1257121 part 2 - Merge nsFontMetrics::Init to the constructor and remove unused failure handling code. r=jfkthame
authorXidorn Quan <quanxunzhen@gmail.com>
Thu, 17 Mar 2016 10:12:27 +0800
changeset 289636 c3a24f3d0e6e22361e2d65777b85e2676b13808b
parent 289635 4af78cb017495e521731f0c60895289d051bc536
child 289637 d3761d63e160b38cdd7ad02f2e852d9a4c1c9cc8
push id30108
push usercbook@mozilla.com
push dateTue, 22 Mar 2016 11:14:31 +0000
treeherdermozilla-central@ea6298e1b4f7 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjfkthame
bugs1257121
milestone48.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 1257121 part 2 - Merge nsFontMetrics::Init to the constructor and remove unused failure handling code. r=jfkthame MozReview-Commit-ID: 15ZzTGHStLd
gfx/src/nsDeviceContext.cpp
gfx/src/nsFontMetrics.cpp
gfx/src/nsFontMetrics.h
--- a/gfx/src/nsDeviceContext.cpp
+++ b/gfx/src/nsDeviceContext.cpp
@@ -151,59 +151,24 @@ nsFontCache::GetMetricsFor(const nsFont&
             return NS_OK;
         }
     }
 
     // It's not in the cache. Get font metrics and then cache them.
 
     nsFontMetrics::Params params = aParams;
     params.language = language;
-    fm = new nsFontMetrics();
-    NS_ADDREF(fm);
-    nsresult rv = fm->Init(aFont, params, mContext);
-    if (NS_SUCCEEDED(rv)) {
-        // the mFontMetrics list has the "head" at the end, because append
-        // is cheaper than insert
-        mFontMetrics.AppendElement(fm);
-        aMetrics = fm;
-        NS_ADDREF(aMetrics);
-        return NS_OK;
-    }
-    fm->Destroy();
-    NS_RELEASE(fm);
-
-    // One reason why Init() fails is because the system is running out of
-    // resources. e.g., on Win95/98 only a very limited number of GDI
-    // objects are available. Compact the cache and try again.
-
-    Compact();
-    fm = new nsFontMetrics();
+    fm = new nsFontMetrics(aFont, params, mContext);
     NS_ADDREF(fm);
-    rv = fm->Init(aFont, params, mContext);
-    if (NS_SUCCEEDED(rv)) {
-        mFontMetrics.AppendElement(fm);
-        aMetrics = fm;
-        return NS_OK;
-    }
-    fm->Destroy();
-    NS_RELEASE(fm);
-
-    // could not setup a new one, send an old one (XXX search a "best
-    // match"?)
-
-    n = mFontMetrics.Length() - 1; // could have changed in Compact()
-    if (n >= 0) {
-        aMetrics = mFontMetrics[n];
-        NS_ADDREF(aMetrics);
-        return NS_OK;
-    }
-
-    NS_POSTCONDITION(NS_SUCCEEDED(rv),
-                     "font metrics should not be null - bug 136248");
-    return rv;
+    // the mFontMetrics list has the "head" at the end, because append
+    // is cheaper than insert
+    mFontMetrics.AppendElement(fm);
+    aMetrics = fm;
+    NS_ADDREF(aMetrics);
+    return NS_OK;
 }
 
 void
 nsFontCache::FontMetricsDeleted(const nsFontMetrics* aFontMetrics)
 {
     mFontMetrics.RemoveElement(aFontMetrics);
 }
 
--- a/gfx/src/nsFontMetrics.cpp
+++ b/gfx/src/nsFontMetrics.cpp
@@ -103,40 +103,27 @@ public:
     }
     virtual void GetSpacing(gfxTextRun::Range aRange, Spacing* aSpacing) {
         NS_ERROR("This shouldn't be called because we never enable spacing");
     }
 };
 
 } // namespace
 
-nsFontMetrics::nsFontMetrics()
-    : mDeviceContext(nullptr), mP2A(0), mTextRunRTL(false)
-    , mVertical(false), mTextOrientation(0)
-{
-}
-
-nsFontMetrics::~nsFontMetrics()
+nsFontMetrics::nsFontMetrics(const nsFont& aFont, const Params& aParams,
+                             nsDeviceContext *aContext)
+    : mFont(aFont)
+    , mLanguage(aParams.language)
+    , mDeviceContext(aContext)
+    , mP2A(aContext->AppUnitsPerDevPixel())
+    , mOrientation(aParams.orientation)
+    , mTextRunRTL(false)
+    , mVertical(false)
+    , mTextOrientation(0)
 {
-    if (mDeviceContext)
-        mDeviceContext->FontMetricsDeleted(this);
-}
-
-nsresult
-nsFontMetrics::Init(const nsFont& aFont, const Params& aParams,
-                    nsDeviceContext *aContext)
-{
-    MOZ_ASSERT(mP2A == 0, "already initialized");
-
-    mFont = aFont;
-    mLanguage = aParams.language;
-    mOrientation = aParams.orientation;
-    mDeviceContext = aContext;
-    mP2A = mDeviceContext->AppUnitsPerDevPixel();
-
     gfxFontStyle style(aFont.style,
                        aFont.weight,
                        aFont.stretch,
                        gfxFloat(aFont.size) / mP2A,
                        aParams.language,
                        aParams.explicitLanguage,
                        aFont.sizeAdjust,
                        aFont.systemFont,
@@ -147,17 +134,23 @@ nsFontMetrics::Init(const nsFont& aFont,
 
     aFont.AddFontFeaturesToStyle(&style);
 
     gfxFloat devToCssSize = gfxFloat(mP2A) /
         gfxFloat(mDeviceContext->AppUnitsPerCSSPixel());
     mFontGroup = gfxPlatform::GetPlatform()->
         CreateFontGroup(aFont.fontlist, &style, aParams.textPerf,
                         aParams.userFontSet, devToCssSize);
-    return NS_OK;
+}
+
+nsFontMetrics::~nsFontMetrics()
+{
+    if (mDeviceContext) {
+        mDeviceContext->FontMetricsDeleted(this);
+    }
 }
 
 void
 nsFontMetrics::Destroy()
 {
     mDeviceContext = nullptr;
 }
 
--- a/gfx/src/nsFontMetrics.h
+++ b/gfx/src/nsFontMetrics.h
@@ -53,28 +53,22 @@ public:
     {
       nsIAtom* language = nullptr;
       bool explicitLanguage = false;
       gfxFont::Orientation orientation = gfxFont::eHorizontal;
       gfxUserFontSet* userFontSet = nullptr;
       gfxTextPerfMetrics* textPerf = nullptr;
     };
 
-    nsFontMetrics();
+    nsFontMetrics(const nsFont& aFont, const Params& aParams,
+                  nsDeviceContext *aContext);
 
     NS_INLINE_DECL_REFCOUNTING(nsFontMetrics)
 
     /**
-     * Initialize the font metrics. Call this after creating the font metrics.
-     * Font metrics you get from the font cache do NOT need to be initialized
-     */
-    nsresult Init(const nsFont& aFont, const Params& aParams,
-                  nsDeviceContext *aContext);
-
-    /**
      * Destroy this font metrics. This breaks the association between
      * the font metrics and the device context.
      */
     void Destroy();
 
     /**
      * Return the font's x-height.
      */
@@ -245,17 +239,17 @@ private:
     }
 
     const gfxFont::Metrics&
     GetMetrics(const gfxFont::Orientation aFontOrientation) const;
 
     nsFont mFont;
     RefPtr<gfxFontGroup> mFontGroup;
     nsCOMPtr<nsIAtom> mLanguage;
-    nsDeviceContext *mDeviceContext;
+    nsDeviceContext* mDeviceContext;
     int32_t mP2A;
 
     // The font orientation (horizontal or vertical) for which these metrics
     // have been initialized. This determines which line metrics (ascent and
     // descent) they will return.
     gfxFont::Orientation mOrientation;
 
     // These fields may be set by clients to control the behavior of methods