Bug 1257121 part 1 - Use struct for passing some params of font metrics. r=jfkthame

MozReview-Commit-ID: FTJlYS3bbTa

--HG--
extra : rebase_source : f3df5f8ebe3f223abc62dc1ae1bf5f7830d63ab8
This commit is contained in:
Xidorn Quan 2016-03-21 11:59:36 +08:00
parent 509c7d11f9
commit f4ef02b391
11 changed files with 93 additions and 113 deletions

View File

@ -2468,12 +2468,13 @@ public:
virtual float GetExLength() const override
{
gfxTextPerfMetrics* tp = mPresContext->GetTextPerfMetrics();
RefPtr<nsFontMetrics> fontMetrics;
nsDeviceContext* dc = mPresContext->DeviceContext();
dc->GetMetricsFor(mFont, mFontLanguage, mExplicitLanguage,
gfxFont::eHorizontal, nullptr, tp,
*getter_AddRefs(fontMetrics));
nsFontMetrics::Params params;
params.language = mFontLanguage;
params.explicitLanguage = mExplicitLanguage;
params.textPerf = mPresContext->GetTextPerfMetrics();
dc->GetMetricsFor(mFont, params, *getter_AddRefs(fontMetrics));
return NSAppUnitsToFloatPixels(fontMetrics->XHeight(),
nsPresContext::AppUnitsPerCSSPixel());
}
@ -3230,13 +3231,13 @@ CanvasRenderingContext2D::SetFontInternal(const nsAString& aFont,
resizedFont.size =
(fontStyle->mSize * c->AppUnitsPerDevPixel()) / c->AppUnitsPerCSSPixel();
nsFontMetrics::Params params;
params.language = fontStyle->mLanguage;
params.explicitLanguage = fontStyle->mExplicitLanguage;
params.userFontSet = c->GetUserFontSet();
params.textPerf = c->GetTextPerfMetrics();
RefPtr<nsFontMetrics> metrics;
c->DeviceContext()->GetMetricsFor(resizedFont,
fontStyle->mLanguage,
fontStyle->mExplicitLanguage,
gfxFont::eHorizontal,
c->GetUserFontSet(),
c->GetTextPerfMetrics(),
c->DeviceContext()->GetMetricsFor(resizedFont, params,
*getter_AddRefs(metrics));
gfxFontGroup* newFontGroup = metrics->GetThebesFontGroup();

View File

@ -65,10 +65,7 @@ public:
void Destroy();
nsresult GetMetricsFor(const nsFont& aFont,
nsIAtom* aLanguage, bool aExplicitLanguage,
gfxFont::Orientation aOrientation,
gfxUserFontSet* aUserFontSet,
gfxTextPerfMetrics* aTextPerf,
const nsFontMetrics::Params& aParams,
nsFontMetrics*& aMetrics);
void FontMetricsDeleted(const nsFontMetrics* aFontMetrics);
@ -127,14 +124,11 @@ nsFontCache::Observe(nsISupports*, const char* aTopic, const char16_t*)
nsresult
nsFontCache::GetMetricsFor(const nsFont& aFont,
nsIAtom* aLanguage, bool aExplicitLanguage,
gfxFont::Orientation aOrientation,
gfxUserFontSet* aUserFontSet,
gfxTextPerfMetrics* aTextPerf,
const nsFontMetrics::Params& aParams,
nsFontMetrics*& aMetrics)
{
if (!aLanguage)
aLanguage = mLocaleLanguage;
nsIAtom* language = aParams.language ? aParams.language
: mLocaleLanguage.get();
// First check our cache
// start from the end, which is where we put the most-recent-used element
@ -143,8 +137,10 @@ nsFontCache::GetMetricsFor(const nsFont& aFont,
int32_t n = mFontMetrics.Length() - 1;
for (int32_t i = n; i >= 0; --i) {
fm = mFontMetrics[i];
if (fm->Font().Equals(aFont) && fm->GetUserFontSet() == aUserFontSet &&
fm->Language() == aLanguage && fm->Orientation() == aOrientation) {
if (fm->Font().Equals(aFont) &&
fm->GetUserFontSet() == aParams.userFontSet &&
fm->Language() == language &&
fm->Orientation() == aParams.orientation) {
if (i != n) {
// promote it to the end of the cache
mFontMetrics.RemoveElementAt(i);
@ -158,10 +154,11 @@ nsFontCache::GetMetricsFor(const nsFont& aFont,
// 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, aLanguage, aExplicitLanguage, aOrientation,
mContext, aUserFontSet, aTextPerf);
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
@ -180,8 +177,7 @@ nsFontCache::GetMetricsFor(const nsFont& aFont,
Compact();
fm = new nsFontMetrics();
NS_ADDREF(fm);
rv = fm->Init(aFont, aLanguage, aExplicitLanguage, aOrientation, mContext,
aUserFontSet, aTextPerf);
rv = fm->Init(aFont, params, mContext);
if (NS_SUCCEEDED(rv)) {
mFontMetrics.AppendElement(fm);
aMetrics = fm;
@ -268,11 +264,7 @@ nsDeviceContext::~nsDeviceContext()
nsresult
nsDeviceContext::GetMetricsFor(const nsFont& aFont,
nsIAtom* aLanguage,
bool aExplicitLanguage,
gfxFont::Orientation aOrientation,
gfxUserFontSet* aUserFontSet,
gfxTextPerfMetrics* aTextPerf,
const nsFontMetrics::Params& aParams,
nsFontMetrics*& aMetrics)
{
if (!mFontCache) {
@ -281,9 +273,7 @@ nsDeviceContext::GetMetricsFor(const nsFont& aFont,
mFontCache->Init(this);
}
return mFontCache->GetMetricsFor(aFont, aLanguage, aExplicitLanguage,
aOrientation, aUserFontSet, aTextPerf,
aMetrics);
return mFontCache->GetMetricsFor(aFont, aParams, aMetrics);
}
nsresult

View File

@ -19,6 +19,7 @@
#include "nsMathUtils.h" // for NS_round
#include "nscore.h" // for char16_t, nsAString
#include "mozilla/AppUnits.h" // for AppUnits
#include "nsFontMetrics.h" // for nsFontMetrics::Params
class gfxASurface;
class gfxContext;
@ -26,7 +27,6 @@ class gfxTextPerfMetrics;
class gfxUserFontSet;
struct nsFont;
class nsFontCache;
class nsFontMetrics;
class nsIAtom;
class nsIDeviceContextSpec;
class nsIScreen;
@ -113,16 +113,11 @@ public:
* Get the nsFontMetrics that describe the properties of
* an nsFont.
* @param aFont font description to obtain metrics for
* @param aLanguage the language of the document
* @param aMetrics out parameter for font metrics
* @param aUserFontSet user font set
* @return error status
*/
nsresult GetMetricsFor(const nsFont& aFont,
nsIAtom* aLanguage, bool aExplicitLanguage,
gfxFont::Orientation aOrientation,
gfxUserFontSet* aUserFontSet,
gfxTextPerfMetrics* aTextPerf,
const nsFontMetrics::Params& aParams,
nsFontMetrics*& aMetrics);
/**

View File

@ -121,18 +121,14 @@ nsFontMetrics::~nsFontMetrics()
}
nsresult
nsFontMetrics::Init(const nsFont& aFont,
nsIAtom* aLanguage, bool aExplicitLanguage,
gfxFont::Orientation aOrientation,
nsDeviceContext *aContext,
gfxUserFontSet *aUserFontSet,
gfxTextPerfMetrics *aTextPerf)
nsFontMetrics::Init(const nsFont& aFont, const Params& aParams,
nsDeviceContext *aContext)
{
MOZ_ASSERT(mP2A == 0, "already initialized");
mFont = aFont;
mLanguage = aLanguage;
mOrientation = aOrientation;
mLanguage = aParams.language;
mOrientation = aParams.orientation;
mDeviceContext = aContext;
mP2A = mDeviceContext->AppUnitsPerDevPixel();
@ -140,8 +136,8 @@ nsFontMetrics::Init(const nsFont& aFont,
aFont.weight,
aFont.stretch,
gfxFloat(aFont.size) / mP2A,
aLanguage,
aExplicitLanguage,
aParams.language,
aParams.explicitLanguage,
aFont.sizeAdjust,
aFont.systemFont,
mDeviceContext->IsPrinterSurface(),
@ -154,8 +150,8 @@ nsFontMetrics::Init(const nsFont& aFont,
gfxFloat devToCssSize = gfxFloat(mP2A) /
gfxFloat(mDeviceContext->AppUnitsPerCSSPixel());
mFontGroup = gfxPlatform::GetPlatform()->
CreateFontGroup(aFont.fontlist, &style, aTextPerf,
aUserFontSet, devToCssSize);
CreateFontGroup(aFont.fontlist, &style, aParams.textPerf,
aParams.userFontSet, devToCssSize);
return NS_OK;
}

View File

@ -49,6 +49,15 @@ public:
typedef gfxTextRun::Range Range;
typedef mozilla::gfx::DrawTarget DrawTarget;
struct Params
{
nsIAtom* language = nullptr;
bool explicitLanguage = false;
gfxFont::Orientation orientation = gfxFont::eHorizontal;
gfxUserFontSet* userFontSet = nullptr;
gfxTextPerfMetrics* textPerf = nullptr;
};
nsFontMetrics();
NS_INLINE_DECL_REFCOUNTING(nsFontMetrics)
@ -56,15 +65,9 @@ public:
/**
* 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
*
* @see nsDeviceContext#GetMetricsFor()
*/
nsresult Init(const nsFont& aFont,
nsIAtom* aLanguage, bool aExplicitLanguage,
gfxFont::Orientation aOrientation,
nsDeviceContext *aContext,
gfxUserFontSet *aUserFontSet,
gfxTextPerfMetrics *aTextPerf);
nsresult Init(const nsFont& aFont, const Params& aParams,
nsDeviceContext *aContext);
/**
* Destroy this font metrics. This breaks the association between

View File

@ -4039,36 +4039,33 @@ nsLayoutUtils::GetFontMetricsForStyleContext(nsStyleContext* aStyleContext,
nsFontMetrics** aFontMetrics,
float aInflation)
{
// pass the user font set object into the device context to pass along to CreateFontGroup
nsPresContext* pc = aStyleContext->PresContext();
gfxUserFontSet* fs = pc->GetUserFontSet();
gfxTextPerfMetrics* tp = pc->GetTextPerfMetrics();
WritingMode wm(aStyleContext);
gfxFont::Orientation orientation =
const nsStyleFont* styleFont = aStyleContext->StyleFont();
nsFontMetrics::Params params;
params.language = styleFont->mLanguage;
params.explicitLanguage = styleFont->mExplicitLanguage;
params.orientation =
wm.IsVertical() && !wm.IsSideways() ? gfxFont::eVertical
: gfxFont::eHorizontal;
const nsStyleFont* styleFont = aStyleContext->StyleFont();
// pass the user font set object into the device context to
// pass along to CreateFontGroup
params.userFontSet = pc->GetUserFontSet();
params.textPerf = pc->GetTextPerfMetrics();
// When aInflation is 1.0, avoid making a local copy of the nsFont.
// This also avoids running font.size through floats when it is large,
// which would be lossy. Fortunately, in such cases, aInflation is
// guaranteed to be 1.0f.
if (aInflation == 1.0f) {
return pc->DeviceContext()->GetMetricsFor(styleFont->mFont,
styleFont->mLanguage,
styleFont->mExplicitLanguage,
orientation, fs, tp,
return pc->DeviceContext()->GetMetricsFor(styleFont->mFont, params,
*aFontMetrics);
}
nsFont font = styleFont->mFont;
font.size = NSToCoordRound(font.size * aInflation);
return pc->DeviceContext()->GetMetricsFor(font, styleFont->mLanguage,
styleFont->mExplicitLanguage,
orientation, fs, tp,
*aFontMetrics);
return pc->DeviceContext()->GetMetricsFor(font, params, *aFontMetrics);
}
nsIFrame*

View File

@ -10341,10 +10341,12 @@ void ReflowCountMgr::PaintCount(const char* aName,
// We don't care about the document language or user fonts here;
// just get a default Latin font.
nsFont font(eFamily_serif, nsPresContext::CSSPixelsToAppUnits(11));
nsFontMetrics::Params params;
params.language = nsGkAtoms::x_western;
params.textPerf = aPresContext->GetTextPerfMetrics();
RefPtr<nsFontMetrics> fm;
aPresContext->DeviceContext()->GetMetricsFor(font,
nsGkAtoms::x_western, false, gfxFont::eHorizontal, nullptr,
aPresContext->GetTextPerfMetrics(), *getter_AddRefs(fm));
aPresContext->DeviceContext()->
GetMetricsFor(font, params, *getter_AddRefs(fm));
char buf[16];
int len = snprintf_literal(buf, "%d", counter->mCount);

View File

@ -741,14 +741,13 @@ MathMLTextRunFactory::RebuildTextRun(nsTransformedTextRun* aTextRun,
if (length) {
font.size = NSToCoordRound(font.size * mFontInflation);
nsPresContext* pc = styles[0]->mPresContext;
nsFontMetrics::Params params;
params.language = styles[0]->mLanguage;
params.explicitLanguage = styles[0]->mExplicitLanguage;
params.userFontSet = pc->GetUserFontSet();
params.textPerf = pc->GetTextPerfMetrics();
RefPtr<nsFontMetrics> metrics;
pc->DeviceContext()->GetMetricsFor(font,
styles[0]->mLanguage,
styles[0]->mExplicitLanguage,
gfxFont::eHorizontal,
pc->GetUserFontSet(),
pc->GetTextPerfMetrics(),
*getter_AddRefs(metrics));
pc->DeviceContext()->GetMetricsFor(font, params, *getter_AddRefs(metrics));
if (metrics) {
newFontGroup = metrics->GetThebesFontGroup();
}

View File

@ -630,11 +630,11 @@ nsPageFrame::PaintHeaderFooter(nsRenderingContext& aRenderingContext,
disable(aRenderingContext.GetDrawTarget(), aDisableSubpixelAA);
// Get the FontMetrics to determine width.height of strings
nsFontMetrics::Params params;
params.userFontSet = pc->GetUserFontSet();
params.textPerf = pc->GetTextPerfMetrics();
RefPtr<nsFontMetrics> fontMet;
pc->DeviceContext()->GetMetricsFor(mPD->mHeadFootFont, nullptr, false,
gfxFont::eHorizontal,
pc->GetUserFontSet(),
pc->GetTextPerfMetrics(),
pc->DeviceContext()->GetMetricsFor(mPD->mHeadFootFont, params,
*getter_AddRefs(fontMet));
nscoord ascent = 0;

View File

@ -991,15 +991,14 @@ nsMathMLChar::SetFontFamily(nsPresContext* aPresContext,
nsFont font = aFont;
font.fontlist = familyList;
const nsStyleFont* styleFont = mStyleContext->StyleFont();
nsFontMetrics::Params params;
params.language = styleFont->mLanguage;
params.explicitLanguage = styleFont->mExplicitLanguage;
params.userFontSet = aPresContext->GetUserFontSet();
params.textPerf = aPresContext->GetTextPerfMetrics();
RefPtr<nsFontMetrics> fm;
aPresContext->DeviceContext()->
GetMetricsFor(font,
styleFont->mLanguage,
styleFont->mExplicitLanguage,
gfxFont::eHorizontal,
aPresContext->GetUserFontSet(),
aPresContext->GetTextPerfMetrics(),
*getter_AddRefs(fm));
GetMetricsFor(font, params, *getter_AddRefs(fm));
// Set the font if it is an unicode table
// or if the same family name has been found
gfxFont *firstFont = fm->GetThebesFontGroup()->GetFirstValidFont();
@ -1533,15 +1532,14 @@ nsMathMLChar::StretchInternal(nsPresContext* aPresContext,
NormalizeDefaultFont(font, aFontSizeInflation);
const nsStyleFont* styleFont = mStyleContext->StyleFont();
nsFontMetrics::Params params;
params.language = styleFont->mLanguage;
params.explicitLanguage = styleFont->mExplicitLanguage;
params.userFontSet = aPresContext->GetUserFontSet();
params.textPerf = aPresContext->GetTextPerfMetrics();
RefPtr<nsFontMetrics> fm;
aPresContext->DeviceContext()->
GetMetricsFor(font,
styleFont->mLanguage,
styleFont->mExplicitLanguage,
gfxFont::eHorizontal,
aPresContext->GetUserFontSet(),
aPresContext->GetTextPerfMetrics(),
*getter_AddRefs(fm));
GetMetricsFor(font, params, *getter_AddRefs(fm));
uint32_t len = uint32_t(mData.Length());
nsAutoPtr<gfxTextRun> textRun;
textRun = fm->GetThebesFontGroup()->

View File

@ -321,11 +321,6 @@ GetMetricsFor(nsPresContext* aPresContext,
{
nsFont font = aStyleFont->mFont;
font.size = aFontSize;
gfxUserFontSet *fs = nullptr;
if (aUseUserFontSet) {
fs = aPresContext->GetUserFontSet();
}
gfxTextPerfMetrics *tp = aPresContext->GetTextPerfMetrics();
gfxFont::Orientation orientation = gfxFont::eHorizontal;
if (aStyleContext) {
WritingMode wm(aStyleContext);
@ -333,12 +328,16 @@ GetMetricsFor(nsPresContext* aPresContext,
orientation = gfxFont::eVertical;
}
}
nsFontMetrics::Params params;
params.language = aStyleFont->mLanguage;
params.explicitLanguage = aStyleFont->mExplicitLanguage;
params.orientation = orientation;
params.userFontSet =
aUseUserFontSet ? aPresContext->GetUserFontSet() : nullptr;
params.textPerf = aPresContext->GetTextPerfMetrics();
RefPtr<nsFontMetrics> fm;
aPresContext->DeviceContext()->GetMetricsFor(font,
aStyleFont->mLanguage,
aStyleFont->mExplicitLanguage,
orientation,
fs, tp, *getter_AddRefs(fm));
aPresContext->DeviceContext()->
GetMetricsFor(font, params, *getter_AddRefs(fm));
return fm.forget();
}