CSSPrimitiveValue.h [plain text]
#ifndef CSSPrimitiveValue_h
#define CSSPrimitiveValue_h
#include "CSSValue.h"
#include "Color.h"
#include <wtf/Forward.h>
#include <wtf/MathExtras.h>
#include <wtf/PassRefPtr.h>
namespace WebCore {
class CSSCalcValue;
class Counter;
class DashboardRegion;
class Pair;
class Quad;
class RGBColor;
class Rect;
class RenderStyle;
class CSSWrapShape;
struct Length;
template<typename T> inline T roundForImpreciseConversion(double value)
{
value += (value < 0) ? -0.01 : +0.01;
return ((value > std::numeric_limits<T>::max()) || (value < std::numeric_limits<T>::min())) ? 0 : static_cast<T>(value);
}
class CSSPrimitiveValue : public CSSValue {
public:
enum UnitTypes {
CSS_UNKNOWN = 0,
CSS_NUMBER = 1,
CSS_PERCENTAGE = 2,
CSS_EMS = 3,
CSS_EXS = 4,
CSS_PX = 5,
CSS_CM = 6,
CSS_MM = 7,
CSS_IN = 8,
CSS_PT = 9,
CSS_PC = 10,
CSS_DEG = 11,
CSS_RAD = 12,
CSS_GRAD = 13,
CSS_MS = 14,
CSS_S = 15,
CSS_HZ = 16,
CSS_KHZ = 17,
CSS_DIMENSION = 18,
CSS_STRING = 19,
CSS_URI = 20,
CSS_IDENT = 21,
CSS_ATTR = 22,
CSS_COUNTER = 23,
CSS_RECT = 24,
CSS_RGBCOLOR = 25,
CSS_VW = 26,
CSS_VH = 27,
CSS_VMIN = 28,
CSS_PAIR = 100, CSS_DASHBOARD_REGION = 101, CSS_UNICODE_RANGE = 102,
CSS_PARSER_OPERATOR = 103,
CSS_PARSER_INTEGER = 104,
CSS_PARSER_HEXCOLOR = 105,
CSS_PARSER_IDENTIFIER = 106,
CSS_TURN = 107,
CSS_REMS = 108,
CSS_COUNTER_NAME = 109,
CSS_SHAPE = 110,
CSS_QUAD = 111,
CSS_CALC = 112,
CSS_CALC_PERCENTAGE_WITH_NUMBER = 113,
CSS_CALC_PERCENTAGE_WITH_LENGTH = 114
};
enum UnitCategory {
UNumber,
UPercent,
ULength,
UAngle,
UTime,
UFrequency,
UViewportPercentageLength,
UOther
};
bool isAngle() const
{
return m_primitiveUnitType == CSS_DEG
|| m_primitiveUnitType == CSS_RAD
|| m_primitiveUnitType == CSS_GRAD
|| m_primitiveUnitType == CSS_TURN;
}
bool isAttr() const { return m_primitiveUnitType == CSS_ATTR; }
bool isCounter() const { return m_primitiveUnitType == CSS_COUNTER; }
bool isFontIndependentLength() const { return m_primitiveUnitType >= CSS_PX && m_primitiveUnitType <= CSS_PC; }
bool isFontRelativeLength() const
{
return m_primitiveUnitType == CSS_EMS || m_primitiveUnitType == CSS_EXS || m_primitiveUnitType == CSS_REMS;
}
bool isIdent() const { return m_primitiveUnitType == CSS_IDENT; }
bool isLength() const
{
unsigned short type = primitiveType();
return (type >= CSS_EMS && type <= CSS_PC) || type == CSS_REMS;
}
bool isNumber() const { return primitiveType() == CSS_NUMBER; }
bool isPercentage() const { return primitiveType() == CSS_PERCENTAGE; }
bool isPx() const { return primitiveType() == CSS_PX; }
bool isRect() const { return m_primitiveUnitType == CSS_RECT; }
bool isRGBColor() const { return m_primitiveUnitType == CSS_RGBCOLOR; }
bool isShape() const { return m_primitiveUnitType == CSS_SHAPE; }
bool isString() const { return m_primitiveUnitType == CSS_STRING; }
bool isTime() const { return m_primitiveUnitType == CSS_S || m_primitiveUnitType == CSS_MS; }
bool isURI() const { return m_primitiveUnitType == CSS_URI; }
bool isCalculated() const { return m_primitiveUnitType == CSS_CALC; }
bool isCalculatedPercentageWithNumber() const { return primitiveType() == CSS_CALC_PERCENTAGE_WITH_NUMBER; }
bool isCalculatedPercentageWithLength() const { return primitiveType() == CSS_CALC_PERCENTAGE_WITH_LENGTH; }
bool isViewportPercentageLength() const { return m_primitiveUnitType >= CSS_VW && m_primitiveUnitType <= CSS_VMIN; }
static PassRefPtr<CSSPrimitiveValue> createIdentifier(int identifier) { return adoptRef(new CSSPrimitiveValue(identifier)); }
static PassRefPtr<CSSPrimitiveValue> createColor(unsigned rgbValue) { return adoptRef(new CSSPrimitiveValue(rgbValue)); }
static PassRefPtr<CSSPrimitiveValue> create(double value, UnitTypes type) { return adoptRef(new CSSPrimitiveValue(value, type)); }
static PassRefPtr<CSSPrimitiveValue> create(const String& value, UnitTypes type) { return adoptRef(new CSSPrimitiveValue(value, type)); }
template<typename T> static PassRefPtr<CSSPrimitiveValue> create(T value)
{
return adoptRef(new CSSPrimitiveValue(value));
}
static PassRefPtr<CSSPrimitiveValue> createAllowingMarginQuirk(double value, UnitTypes type)
{
CSSPrimitiveValue* quirkValue = new CSSPrimitiveValue(value, type);
quirkValue->m_isQuirkValue = true;
return adoptRef(quirkValue);
}
~CSSPrimitiveValue();
void cleanup();
unsigned short primitiveType() const;
double computeDegrees();
enum TimeUnit { Seconds, Milliseconds };
template <typename T, TimeUnit timeUnit> T computeTime()
{
if (timeUnit == Seconds && m_primitiveUnitType == CSS_S)
return getValue<T>();
if (timeUnit == Seconds && m_primitiveUnitType == CSS_MS)
return getValue<T>() / 1000;
if (timeUnit == Milliseconds && m_primitiveUnitType == CSS_MS)
return getValue<T>();
if (timeUnit == Milliseconds && m_primitiveUnitType == CSS_S)
return getValue<T>() * 1000;
ASSERT_NOT_REACHED();
return 0;
}
template<typename T> T computeLength(RenderStyle* currStyle, RenderStyle* rootStyle, float multiplier = 1.0f, bool computingFontSize = false);
template<int> Length convertToLength(RenderStyle* currStyle, RenderStyle* rootStyle, double multiplier = 1.0, bool computingFontSize = false);
void setPrimitiveType(unsigned short type) { m_primitiveUnitType = type; }
double getDoubleValue(unsigned short unitType, ExceptionCode&) const;
double getDoubleValue(unsigned short unitType) const;
double getDoubleValue() const;
void setFloatValue(unsigned short unitType, double floatValue, ExceptionCode&);
float getFloatValue(unsigned short unitType, ExceptionCode& ec) const { return getValue<float>(unitType, ec); }
float getFloatValue(unsigned short unitType) const { return getValue<float>(unitType); }
float getFloatValue() const { return getValue<float>(); }
int getIntValue(unsigned short unitType, ExceptionCode& ec) const { return getValue<int>(unitType, ec); }
int getIntValue(unsigned short unitType) const { return getValue<int>(unitType); }
int getIntValue() const { return getValue<int>(); }
template<typename T> inline T getValue(unsigned short unitType, ExceptionCode& ec) const { return clampTo<T>(getDoubleValue(unitType, ec)); }
template<typename T> inline T getValue(unsigned short unitType) const { return clampTo<T>(getDoubleValue(unitType)); }
template<typename T> inline T getValue() const { return clampTo<T>(getDoubleValue()); }
void setStringValue(unsigned short stringType, const String& stringValue, ExceptionCode&);
String getStringValue(ExceptionCode&) const;
String getStringValue() const;
Counter* getCounterValue(ExceptionCode&) const;
Counter* getCounterValue() const { return m_primitiveUnitType != CSS_COUNTER ? 0 : m_value.counter; }
Rect* getRectValue(ExceptionCode&) const;
Rect* getRectValue() const { return m_primitiveUnitType != CSS_RECT ? 0 : m_value.rect; }
Quad* getQuadValue(ExceptionCode&) const;
Quad* getQuadValue() const { return m_primitiveUnitType != CSS_QUAD ? 0 : m_value.quad; }
PassRefPtr<RGBColor> getRGBColorValue(ExceptionCode&) const;
RGBA32 getRGBA32Value() const { return m_primitiveUnitType != CSS_RGBCOLOR ? 0 : m_value.rgbcolor; }
Pair* getPairValue(ExceptionCode&) const;
Pair* getPairValue() const { return m_primitiveUnitType != CSS_PAIR ? 0 : m_value.pair; }
DashboardRegion* getDashboardRegionValue() const { return m_primitiveUnitType != CSS_DASHBOARD_REGION ? 0 : m_value.region; }
CSSWrapShape* getShapeValue() const { return m_primitiveUnitType != CSS_SHAPE ? 0 : m_value.shape; }
CSSCalcValue* cssCalcValue() const { return m_primitiveUnitType != CSS_CALC ? 0 : m_value.calc; }
int getIdent() const { return m_primitiveUnitType == CSS_IDENT ? m_value.ident : 0; }
template<typename T> inline operator T() const;
String customCssText() const;
bool isQuirkValue() { return m_isQuirkValue; }
void addSubresourceStyleURLs(ListHashSet<KURL>&, const StyleSheetInternal*);
Length viewportPercentageLength();
PassRefPtr<CSSPrimitiveValue> cloneForCSSOM() const;
void setCSSOMSafe() { m_isCSSOMSafe = true; }
private:
CSSPrimitiveValue(int ident);
CSSPrimitiveValue(unsigned color); CSSPrimitiveValue(const Length&);
CSSPrimitiveValue(const String&, UnitTypes);
CSSPrimitiveValue(double, UnitTypes);
template<typename T> CSSPrimitiveValue(T); template<typename T> CSSPrimitiveValue(T* val)
: CSSValue(PrimitiveClass)
{
init(PassRefPtr<T>(val));
}
template<typename T> CSSPrimitiveValue(PassRefPtr<T> val)
: CSSValue(PrimitiveClass)
{
init(val);
}
static void create(int); static void create(unsigned); template<typename T> operator T*();
static UnitTypes canonicalUnitTypeForCategory(UnitCategory category);
void init(PassRefPtr<Counter>);
void init(PassRefPtr<Rect>);
void init(PassRefPtr<Pair>);
void init(PassRefPtr<Quad>);
void init(PassRefPtr<DashboardRegion>); void init(PassRefPtr<CSSWrapShape>);
void init(PassRefPtr<CSSCalcValue>);
bool getDoubleValueInternal(UnitTypes targetUnitType, double* result) const;
double computeLengthDouble(RenderStyle* currentStyle, RenderStyle* rootStyle, float multiplier, bool computingFontSize);
union {
int ident;
double num;
StringImpl* string;
Counter* counter;
Rect* rect;
Quad* quad;
unsigned rgbcolor;
Pair* pair;
DashboardRegion* region;
CSSWrapShape* shape;
CSSCalcValue* calc;
} m_value;
};
}
#endif // CSSPrimitiveValue_h