Bug 1257121 part 2 - Merge nsFontMetrics::Init to the constructor and remove unused failure handling code.
MozReview-Commit-ID: 15ZzTGHStLd
--- 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