Bug 1028497 - Part 10: Implement FontFace descriptor getters. r=jdaggett

We add an mDescriptors that will be used to store the descriptor values
of an unconnected FontFace object.  For CSS-connected FontFace objects,
the descriptors are stored in the nsCSSFontFaceRule::mDecl.mDescriptors.
(Both of these use the same type, though, CSSFontFaceDescriptors.)

Ideally we could have the descriptors always stored on the FontFace,
and for the nsCSSFontFaceStyleDecl go and fetch them from the FontFace,
but it turned out to be impossible to ensure that a FontFace could be
created whenever a nsCSSFontFaceStyleDecl was cloned, since it needs
access to the window object to create the FontFace's promise objects!
This commit is contained in:
Cameron McCormack 2014-10-02 12:32:07 +10:00
parent 98a3173e51
commit 18b7324bdf
2 changed files with 82 additions and 0 deletions

View File

@ -10,6 +10,7 @@
#include "mozilla/dom/Promise.h"
#include "nsCSSRules.h"
#include "nsIDocument.h"
#include "nsStyleUtil.h"
using namespace mozilla::dom;
@ -109,6 +110,15 @@ FontFace::Constructor(const GlobalObject& aGlobal,
void
FontFace::GetFamily(nsString& aResult)
{
mPresContext->FlushUserFontSet();
// Serialize the same way as in nsCSSFontFaceStyleDecl::GetPropertyValue.
nsCSSValue value;
GetDesc(eCSSFontDesc_Family, value);
aResult.Truncate();
nsDependentString family(value.GetStringBufferValue());
nsStyleUtil::AppendEscapedCSSString(family, aResult);
}
void
@ -119,6 +129,8 @@ FontFace::SetFamily(const nsAString& aValue, ErrorResult& aRv)
void
FontFace::GetStyle(nsString& aResult)
{
mPresContext->FlushUserFontSet();
GetDesc(eCSSFontDesc_Style, eCSSProperty_font_style, aResult);
}
void
@ -129,6 +141,8 @@ FontFace::SetStyle(const nsAString& aValue, ErrorResult& aRv)
void
FontFace::GetWeight(nsString& aResult)
{
mPresContext->FlushUserFontSet();
GetDesc(eCSSFontDesc_Weight, eCSSProperty_font_weight, aResult);
}
void
@ -139,6 +153,8 @@ FontFace::SetWeight(const nsAString& aValue, ErrorResult& aRv)
void
FontFace::GetStretch(nsString& aResult)
{
mPresContext->FlushUserFontSet();
GetDesc(eCSSFontDesc_Stretch, eCSSProperty_font_stretch, aResult);
}
void
@ -149,6 +165,13 @@ FontFace::SetStretch(const nsAString& aValue, ErrorResult& aRv)
void
FontFace::GetUnicodeRange(nsString& aResult)
{
mPresContext->FlushUserFontSet();
nsCSSValue value;
GetDesc(eCSSFontDesc_UnicodeRange, value);
aResult.Truncate();
nsStyleUtil::AppendUnicodeRange(value, aResult);
}
void
@ -159,6 +182,11 @@ FontFace::SetUnicodeRange(const nsAString& aValue, ErrorResult& aRv)
void
FontFace::GetVariant(nsString& aResult)
{
mPresContext->FlushUserFontSet();
// XXX Just expose the font-variant descriptor as "normal" until we
// support it properly (bug 1055385).
aResult.AssignLiteral("normal");
}
void
@ -169,6 +197,13 @@ FontFace::SetVariant(const nsAString& aValue, ErrorResult& aRv)
void
FontFace::GetFeatureSettings(nsString& aResult)
{
mPresContext->FlushUserFontSet();
nsCSSValue value;
GetDesc(eCSSFontDesc_FontFeatureSettings, value);
aResult.Truncate();
nsStyleUtil::AppendFontFeatureSettings(value, aResult);
}
void
@ -200,6 +235,40 @@ FontFace::SetStatus(FontFaceLoadStatus aStatus)
mStatus = aStatus;
}
void
FontFace::GetDesc(nsCSSFontDesc aDescID, nsCSSValue& aResult) const
{
if (mRule) {
MOZ_ASSERT(!mDescriptors);
mRule->GetDesc(aDescID, aResult);
} else {
aResult = mDescriptors->Get(aDescID);
}
}
void
FontFace::GetDesc(nsCSSFontDesc aDescID,
nsCSSProperty aPropID,
nsString& aResult) const
{
nsCSSValue value;
GetDesc(aDescID, value);
aResult.Truncate();
// Fill in a default value for missing descriptors.
if (value.GetUnit() == eCSSUnit_Null) {
if (aDescID == eCSSFontDesc_UnicodeRange) {
aResult.AssignLiteral("U+0-10FFFF");
} else if (aDescID != eCSSFontDesc_Family &&
aDescID != eCSSFontDesc_Src) {
aResult.AssignLiteral("normal");
}
} else {
value.AppendToString(aPropID, aResult, nsCSSValue::eNormalized);
}
}
// -- FontFace::Entry --------------------------------------------------------
/* virtual */ void

View File

@ -8,12 +8,15 @@
#include "mozilla/dom/FontFaceBinding.h"
#include "gfxUserFontSet.h"
#include "nsCSSProperty.h"
#include "nsCSSValue.h"
#include "nsWrapperCache.h"
class nsCSSFontFaceRule;
class nsPresContext;
namespace mozilla {
struct CSSFontFaceDescriptors;
namespace dom {
struct FontFaceDescriptors;
class Promise;
@ -100,6 +103,11 @@ private:
*/
void SetStatus(mozilla::dom::FontFaceLoadStatus aStatus);
void GetDesc(nsCSSFontDesc aDescID, nsCSSValue& aResult) const;
void GetDesc(nsCSSFontDesc aDescID,
nsCSSProperty aPropID,
nsString& aResult) const;
nsCOMPtr<nsISupports> mParent;
nsPresContext* mPresContext;
@ -114,6 +122,11 @@ private:
// status, since the spec sometimes requires us to go through the event
// loop before updating the status, rather than doing it immediately.
mozilla::dom::FontFaceLoadStatus mStatus;
// The values corresponding to the font face descriptors, if we are not
// a CSS-connected FontFace object. For CSS-connected objects, we use
// the descriptors stored in mRule.
nsAutoPtr<mozilla::CSSFontFaceDescriptors> mDescriptors;
};
} // namespace dom