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>
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.
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.
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.
22 #ifndef CSSPrimitiveValue_h
23 #define CSSPrimitiveValue_h
27 #include <wtf/Forward.h>
28 #include <wtf/MathExtras.h>
29 #include <wtf/PassRefPtr.h>
34 class DashboardRegion;
44 template<typename T, T max, T min> inline T roundForImpreciseConversion(double value)
46 // Dimension calculations are imprecise, often resulting in values of e.g.
47 // 44.99998. We need to go ahead and round if we're really close to the
48 // next integer value.
49 value += (value < 0) ? -0.01 : +0.01;
50 return ((value > max) || (value < min)) ? 0 : static_cast<T>(value);
53 class CSSPrimitiveValue : public CSSValue {
82 CSS_PAIR = 100, // We envision this being exposed as a means of getting computed style values for pairs (border-spacing/radius, background-position, etc.)
83 CSS_DASHBOARD_REGION = 101, // FIXME: Dashboard region should not be a primitive value.
84 CSS_UNICODE_RANGE = 102,
86 // These next types are just used internally to allow us to translate back and forth from CSSPrimitiveValues to CSSParserValues.
87 CSS_PARSER_OPERATOR = 103,
88 CSS_PARSER_INTEGER = 104,
89 CSS_PARSER_HEXCOLOR = 105,
91 // This is used internally for unknown identifiers
92 CSS_PARSER_IDENTIFIER = 106,
94 // These are from CSS3 Values and Units, but that isn't a finished standard yet
98 // This is used internally for counter names (as opposed to counter values)
99 CSS_COUNTER_NAME = 109,
102 // This is used by the CSS Exclusions draft
105 // Used by border images.
109 // This enum follows the CSSParser::Units enum augmented with UNIT_FREQUENCY for frequencies.
120 static bool isUnitTypeLength(int type) { return (type > CSSPrimitiveValue::CSS_PERCENTAGE && type < CSSPrimitiveValue::CSS_DEG) ||
121 type == CSSPrimitiveValue::CSS_REMS; }
123 bool isLength() const { return isUnitTypeLength(m_type); }
125 static PassRefPtr<CSSPrimitiveValue> createIdentifier(int identifier) { return adoptRef(new CSSPrimitiveValue(identifier)); }
126 static PassRefPtr<CSSPrimitiveValue> createColor(unsigned rgbValue) { return adoptRef(new CSSPrimitiveValue(rgbValue)); }
127 static PassRefPtr<CSSPrimitiveValue> create(double value, UnitTypes type) { return adoptRef(new CSSPrimitiveValue(value, type)); }
128 static PassRefPtr<CSSPrimitiveValue> create(const String& value, UnitTypes type) { return adoptRef(new CSSPrimitiveValue(value, type)); }
130 template<typename T> static PassRefPtr<CSSPrimitiveValue> create(T value)
132 return adoptRef(new CSSPrimitiveValue(value));
135 virtual ~CSSPrimitiveValue();
139 unsigned short primitiveType() const { return m_type; }
142 * computes a length in pixels out of the given CSSValue. Need the RenderStyle to get
143 * the fontinfo in case val is defined in em or ex.
145 * The metrics have to be a bit different for screen and printer output.
146 * For screen output we assume 1 inch == 72 px, for printer we assume 300 dpi
148 * this is screen/printer dependent, so we probably need a config option for this,
149 * and some tool to calibrate.
151 template<typename T> T computeLength(RenderStyle* currStyle, RenderStyle* rootStyle, double multiplier = 1.0, bool computingFontSize = false);
154 void setPrimitiveType(unsigned short type) { m_type = type; }
156 double getDoubleValue(unsigned short unitType, ExceptionCode&) const;
157 double getDoubleValue(unsigned short unitType) const;
158 double getDoubleValue() const { return m_value.num; }
160 void setFloatValue(unsigned short unitType, double floatValue, ExceptionCode&);
161 float getFloatValue(unsigned short unitType, ExceptionCode& ec) const { return getValue<float>(unitType, ec); }
162 float getFloatValue(unsigned short unitType) const { return getValue<float>(unitType); }
163 float getFloatValue() const { return getValue<float>(); }
165 int getIntValue(unsigned short unitType, ExceptionCode& ec) const { return getValue<int>(unitType, ec); }
166 int getIntValue(unsigned short unitType) const { return getValue<int>(unitType); }
167 int getIntValue() const { return getValue<int>(); }
169 template<typename T> inline T getValue(unsigned short unitType, ExceptionCode& ec) const { return clampTo<T>(getDoubleValue(unitType, ec)); }
170 template<typename T> inline T getValue(unsigned short unitType) const { return clampTo<T>(getDoubleValue(unitType)); }
171 template<typename T> inline T getValue() const { return clampTo<T>(m_value.num); }
173 void setStringValue(unsigned short stringType, const String& stringValue, ExceptionCode&);
174 String getStringValue(ExceptionCode&) const;
175 String getStringValue() const;
177 Counter* getCounterValue(ExceptionCode&) const;
178 Counter* getCounterValue() const { return m_type != CSS_COUNTER ? 0 : m_value.counter; }
180 Rect* getRectValue(ExceptionCode&) const;
181 Rect* getRectValue() const { return m_type != CSS_RECT ? 0 : m_value.rect; }
183 Quad* getQuadValue(ExceptionCode&) const;
184 Quad* getQuadValue() const { return m_type != CSS_QUAD ? 0 : m_value.quad; }
186 PassRefPtr<RGBColor> getRGBColorValue(ExceptionCode&) const;
187 RGBA32 getRGBA32Value() const { return m_type != CSS_RGBCOLOR ? 0 : m_value.rgbcolor; }
189 Pair* getPairValue(ExceptionCode&) const;
190 Pair* getPairValue() const { return m_type != CSS_PAIR ? 0 : m_value.pair; }
192 DashboardRegion* getDashboardRegionValue() const { return m_type != CSS_DASHBOARD_REGION ? 0 : m_value.region; }
194 CSSWrapShape* getShapeValue() const { return m_type != CSS_SHAPE ? 0 : m_value.shape; }
196 int getIdent() const;
197 template<typename T> inline operator T() const; // Defined in CSSPrimitiveValueMappings.h
199 virtual bool parseString(const String&, bool = false);
200 virtual String cssText() const;
202 virtual bool isQuirkValue() { return false; }
204 virtual void addSubresourceStyleURLs(ListHashSet<KURL>&, const CSSStyleSheet*);
207 // FIXME: int vs. unsigned overloading is too subtle to distinguish the color and identifier cases.
208 CSSPrimitiveValue(int ident);
209 CSSPrimitiveValue(double, UnitTypes);
210 CSSPrimitiveValue(const String&, UnitTypes);
214 CSSPrimitiveValue(unsigned color); // RGB value
215 CSSPrimitiveValue(const Length&);
217 template<typename T> CSSPrimitiveValue(T); // Defined in CSSPrimitiveValueMappings.h
218 template<typename T> CSSPrimitiveValue(T* val) { init(PassRefPtr<T>(val)); }
219 template<typename T> CSSPrimitiveValue(PassRefPtr<T> val) { init(val); }
221 static void create(int); // compile-time guard
222 static void create(unsigned); // compile-time guard
223 template<typename T> operator T*(); // compile-time guard
225 static PassRefPtr<CSSPrimitiveValue> createUncachedIdentifier(int identifier);
226 static PassRefPtr<CSSPrimitiveValue> createUncachedColor(unsigned rgbValue);
227 static PassRefPtr<CSSPrimitiveValue> createUncached(double value, UnitTypes type);
229 static UnitTypes canonicalUnitTypeForCategory(UnitCategory category);
231 void init(PassRefPtr<Counter>);
232 void init(PassRefPtr<Rect>);
233 void init(PassRefPtr<Pair>);
234 void init(PassRefPtr<Quad>);
235 void init(PassRefPtr<DashboardRegion>); // FIXME: Dashboard region should not be a primitive value.
236 void init(PassRefPtr<CSSWrapShape>);
238 bool getDoubleValueInternal(UnitTypes targetUnitType, double* result) const;
240 double computeLengthDouble(RenderStyle* currentStyle, RenderStyle* rootStyle, double multiplier, bool computingFontSize);
242 virtual bool isPrimitiveValue() const { return true; }
244 virtual unsigned short cssValueType() const;
247 mutable unsigned m_hasCachedCSSText : 1;
257 DashboardRegion* region;
262 } // namespace WebCore
264 #endif // CSSPrimitiveValue_h