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 Counter;
class DashboardRegion;
class Pair;
class RGBColor;
class Rect;
class RenderStyle;
struct Length;
template<typename T, T max, T min> inline T roundForImpreciseConversion(double value)
{
value += (value < 0) ? -0.01 : +0.01;
return ((value > max) || (value < 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_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
};
enum UnitCategory {
UNumber,
UPercent,
ULength,
UAngle,
UTime,
UFrequency,
UOther
};
static bool isUnitTypeLength(int type) { return (type > CSSPrimitiveValue::CSS_PERCENTAGE && type < CSSPrimitiveValue::CSS_DEG) ||
type == CSSPrimitiveValue::CSS_REMS; }
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));
}
virtual ~CSSPrimitiveValue();
void cleanup();
unsigned short primitiveType() const { return m_type; }
int computeLengthInt(RenderStyle* currStyle, RenderStyle* rootStyle);
int computeLengthInt(RenderStyle* currStyle, RenderStyle* rootStyle, double multiplier);
int computeLengthIntForLength(RenderStyle* currStyle, RenderStyle* rootStyle);
int computeLengthIntForLength(RenderStyle* currStyle, RenderStyle* rootStyle, double multiplier);
short computeLengthShort(RenderStyle* currStyle, RenderStyle* rootStyle);
short computeLengthShort(RenderStyle* currStyle, RenderStyle* rootStyle, double multiplier);
float computeLengthFloat(RenderStyle* currStyle, RenderStyle* rootStyle, bool computingFontSize = false);
float computeLengthFloat(RenderStyle* currStyle, RenderStyle* rootStyle, double multiplier, bool computingFontSize = false);
double computeLengthDouble(RenderStyle* currentStyle, RenderStyle* rootStyle, double multiplier = 1.0, bool computingFontSize = false);
void setPrimitiveType(unsigned short type) { m_type = type; }
double getDoubleValue(unsigned short unitType, ExceptionCode&) const;
double getDoubleValue(unsigned short unitType) const;
double getDoubleValue() const { return m_value.num; }
void setFloatValue(unsigned short unitType, double floatValue, ExceptionCode&);
float getFloatValue(unsigned short unitType, ExceptionCode& ec) const { return clampToFloat(getDoubleValue(unitType, ec)); }
float getFloatValue(unsigned short unitType) const { return clampToFloat(getDoubleValue(unitType)); }
float getFloatValue() const { return clampToFloat(m_value.num); }
int getIntValue(unsigned short unitType, ExceptionCode& ec) const { return clampToInteger(getDoubleValue(unitType, ec)); }
int getIntValue(unsigned short unitType) const { return clampToInteger(getDoubleValue(unitType)); }
int getIntValue() const { return clampToInteger(m_value.num); }
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_type != CSS_COUNTER ? 0 : m_value.counter; }
Rect* getRectValue(ExceptionCode&) const;
Rect* getRectValue() const { return m_type != CSS_RECT ? 0 : m_value.rect; }
PassRefPtr<RGBColor> getRGBColorValue(ExceptionCode&) const;
RGBA32 getRGBA32Value() const { return m_type != CSS_RGBCOLOR ? 0 : m_value.rgbcolor; }
Pair* getPairValue(ExceptionCode&) const;
Pair* getPairValue() const { return m_type != CSS_PAIR ? 0 : m_value.pair; }
DashboardRegion* getDashboardRegionValue() const { return m_type != CSS_DASHBOARD_REGION ? 0 : m_value.region; }
int getIdent() const;
template<typename T> inline operator T() const;
virtual bool parseString(const String&, bool = false);
virtual String cssText() const;
virtual bool isQuirkValue() { return false; }
virtual void addSubresourceStyleURLs(ListHashSet<KURL>&, const CSSStyleSheet*);
protected:
CSSPrimitiveValue(int ident);
CSSPrimitiveValue(double, UnitTypes);
CSSPrimitiveValue(const String&, UnitTypes);
private:
CSSPrimitiveValue();
CSSPrimitiveValue(unsigned color); CSSPrimitiveValue(const Length&);
template<typename T> CSSPrimitiveValue(T); template<typename T> CSSPrimitiveValue(T* val) { init(PassRefPtr<T>(val)); }
template<typename T> CSSPrimitiveValue(PassRefPtr<T> val) { init(val); }
static void create(int); static void create(unsigned); template<typename T> operator T*();
static PassRefPtr<CSSPrimitiveValue> createUncachedIdentifier(int identifier);
static PassRefPtr<CSSPrimitiveValue> createUncachedColor(unsigned rgbValue);
static PassRefPtr<CSSPrimitiveValue> createUncached(double value, UnitTypes type);
static UnitTypes canonicalUnitTypeForCategory(UnitCategory category);
void init(PassRefPtr<Counter>);
void init(PassRefPtr<Rect>);
void init(PassRefPtr<Pair>);
void init(PassRefPtr<DashboardRegion>); bool getDoubleValueInternal(UnitTypes targetUnitType, double* result) const;
virtual bool isPrimitiveValue() const { return true; }
virtual unsigned short cssValueType() const;
int m_type : 31;
mutable unsigned m_hasCachedCSSText : 1;
union {
int ident;
double num;
StringImpl* string;
Counter* counter;
Rect* rect;
unsigned rgbcolor;
Pair* pair;
DashboardRegion* region;
} m_value;
};
}
#endif // CSSPrimitiveValue_h