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 #if ENABLE(TIZEN_MEDIA_QUERY)
86 CSS_PAIR = 100, // We envision this being exposed as a means of getting computed style values for pairs (border-spacing/radius, background-position, etc.)
87 CSS_DASHBOARD_REGION = 101, // FIXME: Dashboard region should not be a primitive value.
88 CSS_UNICODE_RANGE = 102,
90 // These next types are just used internally to allow us to translate back and forth from CSSPrimitiveValues to CSSParserValues.
91 CSS_PARSER_OPERATOR = 103,
92 CSS_PARSER_INTEGER = 104,
93 CSS_PARSER_HEXCOLOR = 105,
95 // This is used internally for unknown identifiers
96 CSS_PARSER_IDENTIFIER = 106,
98 // These are from CSS3 Values and Units, but that isn't a finished standard yet
102 // This is used internally for counter names (as opposed to counter values)
103 CSS_COUNTER_NAME = 109,
105 // This is used by the CSS Exclusions draft
108 // Used by border images.
112 // This enum follows the CSSParser::Units enum augmented with UNIT_FREQUENCY for frequencies.
123 static bool isUnitTypeLength(int type) { return (type > CSSPrimitiveValue::CSS_PERCENTAGE && type < CSSPrimitiveValue::CSS_DEG) ||
124 type == CSSPrimitiveValue::CSS_REMS; }
126 bool isLength() const { return isUnitTypeLength(m_primitiveUnitType); }
127 bool isPercentage() const { return m_primitiveUnitType == CSSPrimitiveValue::CSS_PERCENTAGE; }
129 static PassRefPtr<CSSPrimitiveValue> createIdentifier(int identifier) { return adoptRef(new CSSPrimitiveValue(identifier)); }
130 static PassRefPtr<CSSPrimitiveValue> createColor(unsigned rgbValue) { return adoptRef(new CSSPrimitiveValue(rgbValue)); }
131 static PassRefPtr<CSSPrimitiveValue> create(double value, UnitTypes type) { return adoptRef(new CSSPrimitiveValue(value, type)); }
132 static PassRefPtr<CSSPrimitiveValue> create(const String& value, UnitTypes type) { return adoptRef(new CSSPrimitiveValue(value, type)); }
134 template<typename T> static PassRefPtr<CSSPrimitiveValue> create(T value)
136 return adoptRef(new CSSPrimitiveValue(value));
139 // This value is used to handle quirky margins in reflow roots (body, td, and th) like WinIE.
140 // The basic idea is that a stylesheet can use the value __qem (for quirky em) instead of em.
141 // When the quirky value is used, if you're in quirks mode, the margin will collapse away
142 // inside a table cell.
143 static PassRefPtr<CSSPrimitiveValue> createAllowingMarginQuirk(double value, UnitTypes type)
145 CSSPrimitiveValue* quirkValue = new CSSPrimitiveValue(value, type);
146 quirkValue->m_isQuirkValue = true;
147 return adoptRef(quirkValue);
150 ~CSSPrimitiveValue();
154 unsigned short primitiveType() const { return m_primitiveUnitType; }
157 * computes a length in pixels out of the given CSSValue. Need the RenderStyle to get
158 * the fontinfo in case val is defined in em or ex.
160 * The metrics have to be a bit different for screen and printer output.
161 * For screen output we assume 1 inch == 72 px, for printer we assume 300 dpi
163 * this is screen/printer dependent, so we probably need a config option for this,
164 * and some tool to calibrate.
166 template<typename T> T computeLength(RenderStyle* currStyle, RenderStyle* rootStyle, double multiplier = 1.0, bool computingFontSize = false);
169 void setPrimitiveType(unsigned short type) { m_primitiveUnitType = type; }
171 double getDoubleValue(unsigned short unitType, ExceptionCode&) const;
172 double getDoubleValue(unsigned short unitType) const;
173 double getDoubleValue() const { return m_value.num; }
175 void setFloatValue(unsigned short unitType, double floatValue, ExceptionCode&);
176 float getFloatValue(unsigned short unitType, ExceptionCode& ec) const { return getValue<float>(unitType, ec); }
177 float getFloatValue(unsigned short unitType) const { return getValue<float>(unitType); }
178 float getFloatValue() const { return getValue<float>(); }
180 int getIntValue(unsigned short unitType, ExceptionCode& ec) const { return getValue<int>(unitType, ec); }
181 int getIntValue(unsigned short unitType) const { return getValue<int>(unitType); }
182 int getIntValue() const { return getValue<int>(); }
184 template<typename T> inline T getValue(unsigned short unitType, ExceptionCode& ec) const { return clampTo<T>(getDoubleValue(unitType, ec)); }
185 template<typename T> inline T getValue(unsigned short unitType) const { return clampTo<T>(getDoubleValue(unitType)); }
186 template<typename T> inline T getValue() const { return clampTo<T>(m_value.num); }
188 void setStringValue(unsigned short stringType, const String& stringValue, ExceptionCode&);
189 String getStringValue(ExceptionCode&) const;
190 String getStringValue() const;
192 Counter* getCounterValue(ExceptionCode&) const;
193 Counter* getCounterValue() const { return m_primitiveUnitType != CSS_COUNTER ? 0 : m_value.counter; }
195 Rect* getRectValue(ExceptionCode&) const;
196 Rect* getRectValue() const { return m_primitiveUnitType != CSS_RECT ? 0 : m_value.rect; }
198 Quad* getQuadValue(ExceptionCode&) const;
199 Quad* getQuadValue() const { return m_primitiveUnitType != CSS_QUAD ? 0 : m_value.quad; }
201 PassRefPtr<RGBColor> getRGBColorValue(ExceptionCode&) const;
202 RGBA32 getRGBA32Value() const { return m_primitiveUnitType != CSS_RGBCOLOR ? 0 : m_value.rgbcolor; }
204 Pair* getPairValue(ExceptionCode&) const;
205 Pair* getPairValue() const { return m_primitiveUnitType != CSS_PAIR ? 0 : m_value.pair; }
207 DashboardRegion* getDashboardRegionValue() const { return m_primitiveUnitType != CSS_DASHBOARD_REGION ? 0 : m_value.region; }
209 CSSWrapShape* getShapeValue() const { return m_primitiveUnitType != CSS_SHAPE ? 0 : m_value.shape; }
211 int getIdent() const;
212 template<typename T> inline operator T() const; // Defined in CSSPrimitiveValueMappings.h
214 String customCssText() const;
216 bool isQuirkValue() { return m_isQuirkValue; }
218 void addSubresourceStyleURLs(ListHashSet<KURL>&, const CSSStyleSheet*);
221 CSSPrimitiveValue(ClassType, int ident);
222 CSSPrimitiveValue(ClassType, const String&, UnitTypes);
226 // FIXME: int vs. unsigned overloading is too subtle to distinguish the color and identifier cases.
227 CSSPrimitiveValue(int ident);
228 CSSPrimitiveValue(unsigned color); // RGB value
229 CSSPrimitiveValue(const Length&);
230 CSSPrimitiveValue(const String&, UnitTypes);
231 CSSPrimitiveValue(double, UnitTypes);
233 template<typename T> CSSPrimitiveValue(T); // Defined in CSSPrimitiveValueMappings.h
234 template<typename T> CSSPrimitiveValue(T* val)
235 : CSSValue(PrimitiveClass)
237 init(PassRefPtr<T>(val));
240 template<typename T> CSSPrimitiveValue(PassRefPtr<T> val)
241 : CSSValue(PrimitiveClass)
246 static void create(int); // compile-time guard
247 static void create(unsigned); // compile-time guard
248 template<typename T> operator T*(); // compile-time guard
250 static PassRefPtr<CSSPrimitiveValue> createUncachedIdentifier(int identifier);
251 static PassRefPtr<CSSPrimitiveValue> createUncachedColor(unsigned rgbValue);
252 static PassRefPtr<CSSPrimitiveValue> createUncached(double value, UnitTypes type);
254 static UnitTypes canonicalUnitTypeForCategory(UnitCategory category);
256 void init(PassRefPtr<Counter>);
257 void init(PassRefPtr<Rect>);
258 void init(PassRefPtr<Pair>);
259 void init(PassRefPtr<Quad>);
260 void init(PassRefPtr<DashboardRegion>); // FIXME: Dashboard region should not be a primitive value.
261 void init(PassRefPtr<CSSWrapShape>);
263 bool getDoubleValueInternal(UnitTypes targetUnitType, double* result) const;
265 double computeLengthDouble(RenderStyle* currentStyle, RenderStyle* rootStyle, double multiplier, bool computingFontSize);
276 DashboardRegion* region;
281 } // namespace WebCore
283 #endif // CSSPrimitiveValue_h