Bug 898175 - Refactor filter parsing to use a keyword lookup table for filter function names. r=dbaron

This commit is contained in:
Max Vujovic 2013-08-06 17:53:30 +10:00
parent 7fe33ccd60
commit bcb8fc561d
7 changed files with 76 additions and 122 deletions

View File

@ -1647,6 +1647,20 @@ const int32_t nsCSSProps::kFillRuleKTable[] = {
eCSSKeyword_UNKNOWN, -1
};
const int32_t nsCSSProps::kFilterFunctionKTable[] = {
eCSSKeyword_blur, NS_STYLE_FILTER_BLUR,
eCSSKeyword_brightness, NS_STYLE_FILTER_BRIGHTNESS,
eCSSKeyword_contrast, NS_STYLE_FILTER_CONTRAST,
eCSSKeyword_grayscale, NS_STYLE_FILTER_GRAYSCALE,
eCSSKeyword_invert, NS_STYLE_FILTER_INVERT,
eCSSKeyword_opacity, NS_STYLE_FILTER_OPACITY,
eCSSKeyword_saturate, NS_STYLE_FILTER_SATURATE,
eCSSKeyword_sepia, NS_STYLE_FILTER_SEPIA,
eCSSKeyword_hue_rotate, NS_STYLE_FILTER_HUE_ROTATE,
eCSSKeyword_drop_shadow, NS_STYLE_FILTER_DROP_SHADOW,
eCSSKeyword_UNKNOWN, -1
};
const int32_t nsCSSProps::kImageRenderingKTable[] = {
eCSSKeyword_auto, NS_STYLE_IMAGE_RENDERING_AUTO,
eCSSKeyword_optimizespeed, NS_STYLE_IMAGE_RENDERING_OPTIMIZESPEED,

View File

@ -449,6 +449,7 @@ public:
static const int32_t kBoxPackKTable[];
static const int32_t kDominantBaselineKTable[];
static const int32_t kFillRuleKTable[];
static const int32_t kFilterFunctionKTable[];
static const int32_t kImageRenderingKTable[];
static const int32_t kShapeRenderingKTable[];
static const int32_t kStrokeLinecapKTable[];

View File

@ -4497,62 +4497,27 @@ nsComputedDOMStyle::SetCssTextToCoord(nsAString& aCssText,
delete value;
}
static void
GetFilterFunctionName(nsAString& aString, nsStyleFilter::Type aType)
{
switch (aType) {
case nsStyleFilter::Type::eBlur:
aString.AssignLiteral("blur(");
break;
case nsStyleFilter::Type::eBrightness:
aString.AssignLiteral("brightness(");
break;
case nsStyleFilter::Type::eContrast:
aString.AssignLiteral("contrast(");
break;
case nsStyleFilter::Type::eDropShadow:
aString.AssignLiteral("drop-shadow(");
break;
case nsStyleFilter::Type::eGrayscale:
aString.AssignLiteral("grayscale(");
break;
case nsStyleFilter::Type::eHueRotate:
aString.AssignLiteral("hue-rotate(");
break;
case nsStyleFilter::Type::eInvert:
aString.AssignLiteral("invert(");
break;
case nsStyleFilter::Type::eOpacity:
aString.AssignLiteral("opacity(");
break;
case nsStyleFilter::Type::eSaturate:
aString.AssignLiteral("saturate(");
break;
case nsStyleFilter::Type::eSepia:
aString.AssignLiteral("sepia(");
break;
default:
NS_NOTREACHED("unrecognized filter type");
}
}
CSSValue*
nsComputedDOMStyle::CreatePrimitiveValueForStyleFilter(
const nsStyleFilter& aStyleFilter)
{
nsROCSSPrimitiveValue* value = new nsROCSSPrimitiveValue;
// Handle url().
if (nsStyleFilter::Type::eURL == aStyleFilter.GetType()) {
if (aStyleFilter.GetType() == NS_STYLE_FILTER_URL) {
value->SetURI(aStyleFilter.GetURL());
return value;
}
// Filter function name and opening parenthesis.
nsAutoString filterFunctionString;
GetFilterFunctionName(filterFunctionString, aStyleFilter.GetType());
AppendASCIItoUTF16(
nsCSSProps::ValueToKeyword(aStyleFilter.GetType(),
nsCSSProps::kFilterFunctionKTable),
filterFunctionString);
filterFunctionString.AppendLiteral("(");
nsAutoString argumentString;
if (nsStyleFilter::Type::eDropShadow == aStyleFilter.GetType()) {
if (aStyleFilter.GetType() == NS_STYLE_FILTER_DROP_SHADOW) {
// Handle drop-shadow()
nsRefPtr<CSSValue> shadowValue =
GetCSSShadowArray(aStyleFilter.GetDropShadow(),

View File

@ -7719,36 +7719,6 @@ nsRuleNode::ComputeSVGData(void* aStartStruct,
COMPUTE_END_INHERITED(SVG, svg)
}
static nsStyleFilter::Type
StyleFilterTypeForFunctionName(nsCSSKeyword aFunctionName)
{
switch (aFunctionName) {
case eCSSKeyword_blur:
return nsStyleFilter::Type::eBlur;
case eCSSKeyword_brightness:
return nsStyleFilter::Type::eBrightness;
case eCSSKeyword_contrast:
return nsStyleFilter::Type::eContrast;
case eCSSKeyword_drop_shadow:
return nsStyleFilter::Type::eDropShadow;
case eCSSKeyword_grayscale:
return nsStyleFilter::Type::eGrayscale;
case eCSSKeyword_hue_rotate:
return nsStyleFilter::Type::eHueRotate;
case eCSSKeyword_invert:
return nsStyleFilter::Type::eInvert;
case eCSSKeyword_opacity:
return nsStyleFilter::Type::eOpacity;
case eCSSKeyword_saturate:
return nsStyleFilter::Type::eSaturate;
case eCSSKeyword_sepia:
return nsStyleFilter::Type::eSepia;
default:
NS_NOTREACHED("Unknown filter type.");
return nsStyleFilter::Type::eNull;
}
}
void
nsRuleNode::SetStyleFilterToCSSValue(nsStyleFilter* aStyleFilter,
const nsCSSValue& aValue,
@ -7768,8 +7738,13 @@ nsRuleNode::SetStyleFilterToCSSValue(nsStyleFilter* aStyleFilter,
nsCSSKeyword functionName =
(nsCSSKeyword)filterFunction->Item(0).GetIntValue();
nsStyleFilter::Type type = StyleFilterTypeForFunctionName(functionName);
if (type == nsStyleFilter::Type::eDropShadow) {
int32_t type;
DebugOnly<bool> foundKeyword =
nsCSSProps::FindKeyword(functionName,
nsCSSProps::kFilterFunctionKTable,
type);
NS_ABORT_IF_FALSE(foundKeyword, "unknown filter type");
if (type == NS_STYLE_FILTER_DROP_SHADOW) {
nsRefPtr<nsCSSShadowArray> shadowArray = GetShadowData(
filterFunction->Item(1).GetListValue(),
aStyleContext,
@ -7780,9 +7755,9 @@ nsRuleNode::SetStyleFilterToCSSValue(nsStyleFilter* aStyleFilter,
}
int32_t mask = SETCOORD_PERCENT | SETCOORD_FACTOR;
if (type == nsStyleFilter::Type::eBlur) {
if (type == NS_STYLE_FILTER_BLUR) {
mask = SETCOORD_LENGTH | SETCOORD_STORE_CALC;
} else if (type == nsStyleFilter::Type::eHueRotate) {
} else if (type == NS_STYLE_FILTER_HUE_ROTATE) {
mask = SETCOORD_ANGLE;
}
@ -7792,12 +7767,12 @@ nsRuleNode::SetStyleFilterToCSSValue(nsStyleFilter* aStyleFilter,
nsCSSValue& arg = filterFunction->Item(1);
nsStyleCoord filterParameter;
DebugOnly<bool> success = SetCoord(arg, filterParameter,
nsStyleCoord(), mask,
aStyleContext, aPresContext,
aCanStoreInRuleTree);
DebugOnly<bool> didSetCoord = SetCoord(arg, filterParameter,
nsStyleCoord(), mask,
aStyleContext, aPresContext,
aCanStoreInRuleTree);
aStyleFilter->SetFilterParameter(filterParameter, type);
NS_ABORT_IF_FALSE(success, "unexpected unit");
NS_ABORT_IF_FALSE(didSetCoord, "unexpected unit");
}
const void*
@ -7895,7 +7870,7 @@ nsRuleNode::ComputeSVGResetData(void* aStartStruct,
nsStyleFilter styleFilter;
SetStyleFilterToCSSValue(&styleFilter, cur->mValue, aContext,
mPresContext, canStoreInRuleTree);
NS_ABORT_IF_FALSE(styleFilter.GetType() != nsStyleFilter::Type::eNull,
NS_ABORT_IF_FALSE(styleFilter.GetType() != NS_STYLE_FILTER_NONE,
"filter should be set");
svgReset->mFilters.AppendElement(styleFilter);
cur = cur->mNext;

View File

@ -441,6 +441,20 @@ static inline mozilla::css::Side operator++(mozilla::css::Side& side, int) {
#define NS_STYLE_FLOAT_LEFT 1
#define NS_STYLE_FLOAT_RIGHT 2
// See nsStyleFilter
#define NS_STYLE_FILTER_NONE 0
#define NS_STYLE_FILTER_URL 1
#define NS_STYLE_FILTER_BLUR 2
#define NS_STYLE_FILTER_BRIGHTNESS 3
#define NS_STYLE_FILTER_CONTRAST 4
#define NS_STYLE_FILTER_GRAYSCALE 5
#define NS_STYLE_FILTER_INVERT 6
#define NS_STYLE_FILTER_OPACITY 7
#define NS_STYLE_FILTER_SATURATE 8
#define NS_STYLE_FILTER_SEPIA 9
#define NS_STYLE_FILTER_HUE_ROTATE 10
#define NS_STYLE_FILTER_DROP_SHADOW 11
// See nsStyleFont
// We should eventually stop using the NS_STYLE_* variants here.
#define NS_STYLE_FONT_STYLE_NORMAL NS_FONT_STYLE_NORMAL

View File

@ -1005,22 +1005,22 @@ nsChangeHint nsStyleSVG::CalcDifference(const nsStyleSVG& aOther) const
// nsStyleFilter
//
nsStyleFilter::nsStyleFilter()
: mType(eNull)
: mType(NS_STYLE_FILTER_NONE)
, mDropShadow(nullptr)
{
MOZ_COUNT_CTOR(nsStyleFilter);
}
nsStyleFilter::nsStyleFilter(const nsStyleFilter& aSource)
: mType(eNull)
: mType(NS_STYLE_FILTER_NONE)
, mDropShadow(nullptr)
{
MOZ_COUNT_CTOR(nsStyleFilter);
if (aSource.mType == eURL) {
if (aSource.mType == NS_STYLE_FILTER_URL) {
SetURL(aSource.mURL);
} else if (aSource.mType == eDropShadow) {
} else if (aSource.mType == NS_STYLE_FILTER_DROP_SHADOW) {
SetDropShadow(aSource.mDropShadow);
} else if (aSource.mType != eNull) {
} else if (aSource.mType != NS_STYLE_FILTER_NONE) {
SetFilterParameter(aSource.mFilterParameter, aSource.mType);
}
}
@ -1037,11 +1037,11 @@ nsStyleFilter::operator=(const nsStyleFilter& aOther)
if (this == &aOther)
return *this;
if (aOther.mType == eURL) {
if (aOther.mType == NS_STYLE_FILTER_URL) {
SetURL(aOther.mURL);
} else if (aOther.mType == eDropShadow) {
} else if (aOther.mType == NS_STYLE_FILTER_DROP_SHADOW) {
SetDropShadow(aOther.mDropShadow);
} else if (aOther.mType != eNull) {
} else if (aOther.mType != NS_STYLE_FILTER_NONE) {
SetFilterParameter(aOther.mFilterParameter, aOther.mType);
}
return *this;
@ -1055,11 +1055,11 @@ nsStyleFilter::operator==(const nsStyleFilter& aOther) const
return false;
}
if (mType == eURL) {
if (mType == NS_STYLE_FILTER_URL) {
return EqualURIs(mURL, aOther.mURL);
} else if (mType == eDropShadow) {
} else if (mType == NS_STYLE_FILTER_DROP_SHADOW) {
return *mDropShadow == *aOther.mDropShadow;
} else if (mType != eNull) {
} else if (mType != NS_STYLE_FILTER_NONE) {
return mFilterParameter == aOther.mFilterParameter;
}
@ -1069,10 +1069,10 @@ nsStyleFilter::operator==(const nsStyleFilter& aOther) const
void
nsStyleFilter::ReleaseRef()
{
if (mType == eDropShadow) {
if (mType == NS_STYLE_FILTER_DROP_SHADOW) {
NS_ASSERTION(mDropShadow, "expected pointer");
mDropShadow->Release();
} else if (mType == eURL) {
} else if (mType == NS_STYLE_FILTER_URL) {
NS_ASSERTION(mURL, "expected pointer");
mURL->Release();
}
@ -1080,7 +1080,7 @@ nsStyleFilter::ReleaseRef()
void
nsStyleFilter::SetFilterParameter(const nsStyleCoord& aFilterParameter,
Type aType)
int32_t aType)
{
ReleaseRef();
mFilterParameter = aFilterParameter;
@ -1094,7 +1094,7 @@ nsStyleFilter::SetURL(nsIURI* aURL)
ReleaseRef();
mURL = aURL;
mURL->AddRef();
mType = eURL;
mType = NS_STYLE_FILTER_URL;
}
void
@ -1104,7 +1104,7 @@ nsStyleFilter::SetDropShadow(nsCSSShadowArray* aDropShadow)
ReleaseRef();
mDropShadow = aDropShadow;
mDropShadow->AddRef();
mType = eDropShadow;
mType = NS_STYLE_FILTER_DROP_SHADOW;
}
// --------------------

View File

@ -2289,42 +2289,27 @@ struct nsStyleFilter {
bool operator==(const nsStyleFilter& aOther) const;
enum Type {
eNull,
eURL,
eBlur,
eBrightness,
eContrast,
eDropShadow,
eGrayscale,
eHueRotate,
eInvert,
eOpacity,
eSaturate,
eSepia,
};
Type GetType() const {
int32_t GetType() const {
return mType;
}
const nsStyleCoord& GetFilterParameter() const {
NS_ASSERTION(mType != eDropShadow &&
mType != eURL &&
mType != eNull, "wrong filter type");
NS_ASSERTION(mType != NS_STYLE_FILTER_DROP_SHADOW &&
mType != NS_STYLE_FILTER_URL &&
mType != NS_STYLE_FILTER_NONE, "wrong filter type");
return mFilterParameter;
}
void SetFilterParameter(const nsStyleCoord& aFilterParameter,
Type aType);
int32_t aType);
nsIURI* GetURL() const {
NS_ASSERTION(mType == eURL, "wrong filter type");
NS_ASSERTION(mType == NS_STYLE_FILTER_URL, "wrong filter type");
return mURL;
}
void SetURL(nsIURI* aURL);
nsCSSShadowArray* GetDropShadow() const {
NS_ASSERTION(mType == eDropShadow, "wrong filter type");
NS_ASSERTION(mType == NS_STYLE_FILTER_DROP_SHADOW, "wrong filter type");
return mDropShadow;
}
void SetDropShadow(nsCSSShadowArray* aDropShadow);
@ -2332,7 +2317,7 @@ struct nsStyleFilter {
private:
void ReleaseRef();
Type mType;
int32_t mType; // see NS_STYLE_FILTER_* constants in nsStyleConsts.h
nsStyleCoord mFilterParameter; // coord, percent, factor, angle
union {
nsIURI* mURL;
@ -2367,7 +2352,7 @@ struct nsStyleSVGReset {
// filter functions.
nsIURI* SingleFilter() const {
return (mFilters.Length() == 1 &&
mFilters[0].GetType() == nsStyleFilter::Type::eURL) ?
mFilters[0].GetType() == NS_STYLE_FILTER_URL) ?
mFilters[0].GetURL() : nullptr;
}