tizen beta release
[profile/ivi/webkit-efl.git] / Source / WebCore / css / CSSPrimitiveValue.h
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/Forward.h>
28 #include <wtf/MathExtras.h>
29 #include <wtf/PassRefPtr.h>
30
31 namespace WebCore {
32
33 class Counter;
34 class DashboardRegion;
35 class Pair;
36 class Quad;
37 class RGBColor;
38 class Rect;
39 class RenderStyle;
40 class CSSWrapShape;
41
42 struct Length;
43
44 template<typename T, T max, T min> inline T roundForImpreciseConversion(double value)
45 {
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);
51 }
52
53 class CSSPrimitiveValue : public CSSValue {
54 public:
55     enum UnitTypes {
56         CSS_UNKNOWN = 0,
57         CSS_NUMBER = 1,
58         CSS_PERCENTAGE = 2,
59         CSS_EMS = 3,
60         CSS_EXS = 4,
61         CSS_PX = 5,
62         CSS_CM = 6,
63         CSS_MM = 7,
64         CSS_IN = 8,
65         CSS_PT = 9,
66         CSS_PC = 10,
67         CSS_DEG = 11,
68         CSS_RAD = 12,
69         CSS_GRAD = 13,
70         CSS_MS = 14,
71         CSS_S = 15,
72         CSS_HZ = 16,
73         CSS_KHZ = 17,
74         CSS_DIMENSION = 18,
75         CSS_STRING = 19,
76         CSS_URI = 20,
77         CSS_IDENT = 21,
78         CSS_ATTR = 22,
79         CSS_COUNTER = 23,
80         CSS_RECT = 24,
81         CSS_RGBCOLOR = 25,
82 #if ENABLE(TIZEN_MEDIA_QUERY)
83         CSS_DPI = 26,
84         CSS_DPCM = 27,
85 #endif
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,
89
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,
94
95         // This is used internally for unknown identifiers
96         CSS_PARSER_IDENTIFIER = 106,
97
98         // These are from CSS3 Values and Units, but that isn't a finished standard yet
99         CSS_TURN = 107,
100         CSS_REMS = 108,
101
102         // This is used internally for counter names (as opposed to counter values)
103         CSS_COUNTER_NAME = 109,
104
105         // This is used by the CSS Exclusions draft
106         CSS_SHAPE = 110,
107
108         // Used by border images.
109         CSS_QUAD = 111
110     };
111
112     // This enum follows the CSSParser::Units enum augmented with UNIT_FREQUENCY for frequencies.
113     enum UnitCategory {
114         UNumber,
115         UPercent,
116         ULength,
117         UAngle,
118         UTime,
119         UFrequency,
120         UOther
121     };
122
123     static bool isUnitTypeLength(int type) { return (type > CSSPrimitiveValue::CSS_PERCENTAGE && type < CSSPrimitiveValue::CSS_DEG) ||
124                                                     type == CSSPrimitiveValue::CSS_REMS; }
125
126     bool isLength() const { return isUnitTypeLength(m_primitiveUnitType); }
127     bool isPercentage() const { return m_primitiveUnitType == CSSPrimitiveValue::CSS_PERCENTAGE; }
128
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)); }
133
134     template<typename T> static PassRefPtr<CSSPrimitiveValue> create(T value)
135     {
136         return adoptRef(new CSSPrimitiveValue(value));
137     }
138
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)
144     {
145         CSSPrimitiveValue* quirkValue = new CSSPrimitiveValue(value, type);
146         quirkValue->m_isQuirkValue = true;
147         return adoptRef(quirkValue);
148     }
149
150     ~CSSPrimitiveValue();
151
152     void cleanup();
153
154     unsigned short primitiveType() const { return m_primitiveUnitType; }
155
156     /*
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.
159      *
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
162      *
163      * this is screen/printer dependent, so we probably need a config option for this,
164      * and some tool to calibrate.
165      */
166     template<typename T> T computeLength(RenderStyle* currStyle, RenderStyle* rootStyle, double multiplier = 1.0, bool computingFontSize = false);
167
168     // use with care!!!
169     void setPrimitiveType(unsigned short type) { m_primitiveUnitType = type; }
170
171     double getDoubleValue(unsigned short unitType, ExceptionCode&) const;
172     double getDoubleValue(unsigned short unitType) const;
173     double getDoubleValue() const { return m_value.num; }
174
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>(); }
179
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>(); }
183
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); }
187
188     void setStringValue(unsigned short stringType, const String& stringValue, ExceptionCode&);
189     String getStringValue(ExceptionCode&) const;
190     String getStringValue() const;
191
192     Counter* getCounterValue(ExceptionCode&) const;
193     Counter* getCounterValue() const { return m_primitiveUnitType != CSS_COUNTER ? 0 : m_value.counter; }
194
195     Rect* getRectValue(ExceptionCode&) const;
196     Rect* getRectValue() const { return m_primitiveUnitType != CSS_RECT ? 0 : m_value.rect; }
197
198     Quad* getQuadValue(ExceptionCode&) const;
199     Quad* getQuadValue() const { return m_primitiveUnitType != CSS_QUAD ? 0 : m_value.quad; }
200
201     PassRefPtr<RGBColor> getRGBColorValue(ExceptionCode&) const;
202     RGBA32 getRGBA32Value() const { return m_primitiveUnitType != CSS_RGBCOLOR ? 0 : m_value.rgbcolor; }
203
204     Pair* getPairValue(ExceptionCode&) const;
205     Pair* getPairValue() const { return m_primitiveUnitType != CSS_PAIR ? 0 : m_value.pair; }
206
207     DashboardRegion* getDashboardRegionValue() const { return m_primitiveUnitType != CSS_DASHBOARD_REGION ? 0 : m_value.region; }
208
209     CSSWrapShape* getShapeValue() const { return m_primitiveUnitType != CSS_SHAPE ? 0 : m_value.shape; }
210
211     int getIdent() const;
212     template<typename T> inline operator T() const; // Defined in CSSPrimitiveValueMappings.h
213
214     String customCssText() const;
215
216     bool isQuirkValue() { return m_isQuirkValue; }
217
218     void addSubresourceStyleURLs(ListHashSet<KURL>&, const CSSStyleSheet*);
219
220 protected:
221     CSSPrimitiveValue(ClassType, int ident);
222     CSSPrimitiveValue(ClassType, const String&, UnitTypes);
223
224 private:
225     CSSPrimitiveValue();
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);
232
233     template<typename T> CSSPrimitiveValue(T); // Defined in CSSPrimitiveValueMappings.h
234     template<typename T> CSSPrimitiveValue(T* val)
235         : CSSValue(PrimitiveClass)
236     {
237         init(PassRefPtr<T>(val));
238     }
239
240     template<typename T> CSSPrimitiveValue(PassRefPtr<T> val)
241         : CSSValue(PrimitiveClass)
242     {
243         init(val);
244     }
245
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
249
250     static PassRefPtr<CSSPrimitiveValue> createUncachedIdentifier(int identifier);
251     static PassRefPtr<CSSPrimitiveValue> createUncachedColor(unsigned rgbValue);
252     static PassRefPtr<CSSPrimitiveValue> createUncached(double value, UnitTypes type);
253
254     static UnitTypes canonicalUnitTypeForCategory(UnitCategory category);
255
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>);
262
263     bool getDoubleValueInternal(UnitTypes targetUnitType, double* result) const;
264
265     double computeLengthDouble(RenderStyle* currentStyle, RenderStyle* rootStyle, double multiplier, bool computingFontSize);
266
267     union {
268         int ident;
269         double num;
270         StringImpl* string;
271         Counter* counter;
272         Rect* rect;
273         Quad* quad;
274         unsigned rgbcolor;
275         Pair* pair;
276         DashboardRegion* region;
277         CSSWrapShape* shape;
278     } m_value;
279 };
280
281 } // namespace WebCore
282
283 #endif // CSSPrimitiveValue_h