1 /* 2 * (C) 1999-2003 Lars Knoll (knoll@kde.org) 3 * Copyright (C) 2004, 2005, 2006, 2008 Apple Inc. All rights reserved. 4 * Copyright (C) 2007 Alexey Proskuryakov <ap@webkit.org> 5 * 6 * This library is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU Library General Public 8 * License as published by the Free Software Foundation; either 9 * version 2 of the License, or (at your option) any later version. 10 * 11 * This library is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 * Library General Public License for more details. 15 * 16 * You should have received a copy of the GNU Library General Public License 17 * along with this library; see the file COPYING.LIB. If not, write to 18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 19 * Boston, MA 02110-1301, USA. 20 */ 21 22 #ifndef CSSPrimitiveValue_h 23 #define CSSPrimitiveValue_h 24 25 #include "CSSValue.h" 26 #include "Color.h" 27 #include <wtf/PassRefPtr.h> 28 29 namespace WebCore { 30 31 class Counter; 32 class DashboardRegion; 33 class Pair; 34 class RGBColor; 35 class Rect; 36 class RenderStyle; 37 class StringImpl; 38 39 struct Length; 40 41 class CSSPrimitiveValue : public CSSValue { 42 public: 43 enum UnitTypes { 44 CSS_UNKNOWN = 0, 45 CSS_NUMBER = 1, 46 CSS_PERCENTAGE = 2, 47 CSS_EMS = 3, 48 CSS_EXS = 4, 49 CSS_PX = 5, 50 CSS_CM = 6, 51 CSS_MM = 7, 52 CSS_IN = 8, 53 CSS_PT = 9, 54 CSS_PC = 10, 55 CSS_DEG = 11, 56 CSS_RAD = 12, 57 CSS_GRAD = 13, 58 CSS_MS = 14, 59 CSS_S = 15, 60 CSS_HZ = 16, 61 CSS_KHZ = 17, 62 CSS_DIMENSION = 18, 63 CSS_STRING = 19, 64 CSS_URI = 20, 65 CSS_IDENT = 21, 66 CSS_ATTR = 22, 67 CSS_COUNTER = 23, 68 CSS_RECT = 24, 69 CSS_RGBCOLOR = 25, 70 CSS_PAIR = 100, // We envision this being exposed as a means of getting computed style values for pairs (border-spacing/radius, background-position, etc.) 71 CSS_DASHBOARD_REGION = 101, // FIXME: Dashboard region should not be a primitive value. 72 CSS_UNICODE_RANGE = 102, 73 74 // These next types are just used internally to allow us to translate back and forth from CSSPrimitiveValues to CSSParserValues. 75 CSS_PARSER_OPERATOR = 103, 76 CSS_PARSER_INTEGER = 104, 77 CSS_PARSER_VARIABLE_FUNCTION_SYNTAX = 105, 78 CSS_PARSER_HEXCOLOR = 106, 79 80 // This is used internally for unknown identifiers 81 CSS_PARSER_IDENTIFIER = 107, 82 83 // These are from CSS3 Values and Units, but that isn't a finished standard yet 84 CSS_TURN = 108, 85 CSS_REMS = 109 86 }; 87 isUnitTypeLength(int type)88 static bool isUnitTypeLength(int type) { return (type > CSSPrimitiveValue::CSS_PERCENTAGE && type < CSSPrimitiveValue::CSS_DEG) || 89 type == CSSPrimitiveValue::CSS_REMS; } 90 91 static PassRefPtr<CSSPrimitiveValue> createIdentifier(int ident); 92 static PassRefPtr<CSSPrimitiveValue> createColor(unsigned rgbValue); 93 static PassRefPtr<CSSPrimitiveValue> create(double value, UnitTypes type); 94 static PassRefPtr<CSSPrimitiveValue> create(const String& value, UnitTypes type); 95 create(T value)96 template<typename T> static PassRefPtr<CSSPrimitiveValue> create(T value) 97 { 98 return adoptRef(new CSSPrimitiveValue(value)); 99 } 100 101 virtual ~CSSPrimitiveValue(); 102 103 void cleanup(); 104 primitiveType()105 unsigned short primitiveType() const { return m_type; } 106 isVariable()107 bool isVariable() const { return m_type == CSS_PARSER_VARIABLE_FUNCTION_SYNTAX; } 108 109 /* 110 * computes a length in pixels out of the given CSSValue. Need the RenderStyle to get 111 * the fontinfo in case val is defined in em or ex. 112 * 113 * The metrics have to be a bit different for screen and printer output. 114 * For screen output we assume 1 inch == 72 px, for printer we assume 300 dpi 115 * 116 * this is screen/printer dependent, so we probably need a config option for this, 117 * and some tool to calibrate. 118 */ 119 int computeLengthInt(RenderStyle* currStyle, RenderStyle* rootStyle); 120 int computeLengthInt(RenderStyle* currStyle, RenderStyle* rootStyle, double multiplier); 121 int computeLengthIntForLength(RenderStyle* currStyle, RenderStyle* rootStyle); 122 int computeLengthIntForLength(RenderStyle* currStyle, RenderStyle* rootStyle, double multiplier); 123 short computeLengthShort(RenderStyle* currStyle, RenderStyle* rootStyle); 124 short computeLengthShort(RenderStyle* currStyle, RenderStyle* rootStyle, double multiplier); 125 float computeLengthFloat(RenderStyle* currStyle, RenderStyle* rootStyle, bool computingFontSize = false); 126 float computeLengthFloat(RenderStyle* currStyle, RenderStyle* rootStyle, double multiplier, bool computingFontSize = false); 127 double computeLengthDouble(RenderStyle* currentStyle, RenderStyle* rootStyle, double multiplier = 1.0, bool computingFontSize = false); 128 129 // use with care!!! setPrimitiveType(unsigned short type)130 void setPrimitiveType(unsigned short type) { m_type = type; } 131 132 double getDoubleValue(unsigned short unitType, ExceptionCode&); 133 double getDoubleValue(unsigned short unitType); getDoubleValue()134 double getDoubleValue() const { return m_value.num; } 135 136 void setFloatValue(unsigned short unitType, double floatValue, ExceptionCode&); getFloatValue(unsigned short unitType,ExceptionCode & ec)137 float getFloatValue(unsigned short unitType, ExceptionCode& ec) { return static_cast<float>(getDoubleValue(unitType, ec)); } getFloatValue(unsigned short unitType)138 float getFloatValue(unsigned short unitType) { return static_cast<float>(getDoubleValue(unitType)); } getFloatValue()139 float getFloatValue() const { return static_cast<float>(m_value.num); } 140 getIntValue(unsigned short unitType,ExceptionCode & ec)141 int getIntValue(unsigned short unitType, ExceptionCode& ec) { return static_cast<int>(getDoubleValue(unitType, ec)); } getIntValue(unsigned short unitType)142 int getIntValue(unsigned short unitType) { return static_cast<int>(getDoubleValue(unitType)); } getIntValue()143 int getIntValue() const { return static_cast<int>(m_value.num); } 144 145 void setStringValue(unsigned short stringType, const String& stringValue, ExceptionCode&); 146 String getStringValue(ExceptionCode&) const; 147 String getStringValue() const; 148 149 Counter* getCounterValue(ExceptionCode&) const; getCounterValue()150 Counter* getCounterValue() const { return m_type != CSS_COUNTER ? 0 : m_value.counter; } 151 152 Rect* getRectValue(ExceptionCode&) const; getRectValue()153 Rect* getRectValue() const { return m_type != CSS_RECT ? 0 : m_value.rect; } 154 155 RGBColor* getRGBColorValue(ExceptionCode&) const; getRGBA32Value()156 RGBA32 getRGBA32Value() const { return m_type != CSS_RGBCOLOR ? 0 : m_value.rgbcolor; } 157 158 Pair* getPairValue(ExceptionCode&) const; getPairValue()159 Pair* getPairValue() const { return m_type != CSS_PAIR ? 0 : m_value.pair; } 160 getDashboardRegionValue()161 DashboardRegion* getDashboardRegionValue() const { return m_type != CSS_DASHBOARD_REGION ? 0 : m_value.region; } 162 163 int getIdent(); 164 template<typename T> inline operator T() const; // Defined in CSSPrimitiveValueMappings.h 165 166 virtual bool parseString(const String&, bool = false); 167 virtual String cssText() const; 168 isQuirkValue()169 virtual bool isQuirkValue() { return false; } 170 171 virtual CSSParserValue parserValue() const; 172 173 virtual void addSubresourceStyleURLs(ListHashSet<KURL>&, const CSSStyleSheet*); 174 175 protected: 176 // FIXME: int vs. unsigned overloading is too subtle to distinguish the color and identifier cases. 177 CSSPrimitiveValue(int ident); 178 CSSPrimitiveValue(double, UnitTypes); 179 CSSPrimitiveValue(const String&, UnitTypes); 180 181 private: 182 CSSPrimitiveValue(); 183 CSSPrimitiveValue(unsigned color); // RGB value 184 CSSPrimitiveValue(const Length&); 185 186 template<typename T> CSSPrimitiveValue(T); // Defined in CSSPrimitiveValueMappings.h CSSPrimitiveValue(T * val)187 template<typename T> CSSPrimitiveValue(T* val) { init(PassRefPtr<T>(val)); } CSSPrimitiveValue(PassRefPtr<T> val)188 template<typename T> CSSPrimitiveValue(PassRefPtr<T> val) { init(val); } 189 190 static void create(int); // compile-time guard 191 static void create(unsigned); // compile-time guard 192 template<typename T> operator T*(); // compile-time guard 193 194 void init(PassRefPtr<Counter>); 195 void init(PassRefPtr<Rect>); 196 void init(PassRefPtr<Pair>); 197 void init(PassRefPtr<DashboardRegion>); // FIXME: Dashboard region should not be a primitive value. 198 isPrimitiveValue()199 virtual bool isPrimitiveValue() const { return true; } 200 201 virtual unsigned short cssValueType() const; 202 203 int m_type; 204 union { 205 int ident; 206 double num; 207 StringImpl* string; 208 Counter* counter; 209 Rect* rect; 210 unsigned rgbcolor; 211 Pair* pair; 212 DashboardRegion* region; 213 } m_value; 214 }; 215 216 } // namespace WebCore 217 218 #endif // CSSPrimitiveValue_h 219