initial import
[vuplus_webkit] / 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         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,
85         
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,
90         
91         // This is used internally for unknown identifiers 
92         CSS_PARSER_IDENTIFIER = 106,
93         
94         // These are from CSS3 Values and Units, but that isn't a finished standard yet
95         CSS_TURN = 107,
96         CSS_REMS = 108,
97
98         // This is used internally for counter names (as opposed to counter values)
99         CSS_COUNTER_NAME = 109,
100         CSS_FROM_FLOW = 110,
101
102         // This is used by the CSS Exclusions draft
103         CSS_SHAPE = 111,
104         
105         // Used by border images.
106         CSS_QUAD = 112
107     };
108     
109     // This enum follows the CSSParser::Units enum augmented with UNIT_FREQUENCY for frequencies.
110     enum UnitCategory {
111         UNumber,
112         UPercent,
113         ULength,
114         UAngle,
115         UTime,
116         UFrequency,
117         UOther
118     };
119
120     static bool isUnitTypeLength(int type) { return (type > CSSPrimitiveValue::CSS_PERCENTAGE && type < CSSPrimitiveValue::CSS_DEG) ||
121                                                     type == CSSPrimitiveValue::CSS_REMS; }
122
123     bool isLength() const { return isUnitTypeLength(m_type); }
124
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)); }
129     
130     template<typename T> static PassRefPtr<CSSPrimitiveValue> create(T value)
131     {
132         return adoptRef(new CSSPrimitiveValue(value));
133     }
134
135     virtual ~CSSPrimitiveValue();
136
137     void cleanup();
138
139     unsigned short primitiveType() const { return m_type; }
140
141     /*
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.
144      *
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
147      *
148      * this is screen/printer dependent, so we probably need a config option for this,
149      * and some tool to calibrate.
150      */
151     template<typename T> T computeLength(RenderStyle* currStyle, RenderStyle* rootStyle, double multiplier = 1.0, bool computingFontSize = false);
152
153     // use with care!!!
154     void setPrimitiveType(unsigned short type) { m_type = type; }
155     
156     double getDoubleValue(unsigned short unitType, ExceptionCode&) const;
157     double getDoubleValue(unsigned short unitType) const;
158     double getDoubleValue() const { return m_value.num; }
159
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>(); }
164
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>(); }
168
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); }
172
173     void setStringValue(unsigned short stringType, const String& stringValue, ExceptionCode&);
174     String getStringValue(ExceptionCode&) const;
175     String getStringValue() const;
176
177     Counter* getCounterValue(ExceptionCode&) const;
178     Counter* getCounterValue() const { return m_type != CSS_COUNTER ? 0 : m_value.counter; }
179
180     Rect* getRectValue(ExceptionCode&) const;
181     Rect* getRectValue() const { return m_type != CSS_RECT ? 0 : m_value.rect; }
182
183     Quad* getQuadValue(ExceptionCode&) const;
184     Quad* getQuadValue() const { return m_type != CSS_QUAD ? 0 : m_value.quad; }
185     
186     PassRefPtr<RGBColor> getRGBColorValue(ExceptionCode&) const;
187     RGBA32 getRGBA32Value() const { return m_type != CSS_RGBCOLOR ? 0 : m_value.rgbcolor; }
188
189     Pair* getPairValue(ExceptionCode&) const;
190     Pair* getPairValue() const { return m_type != CSS_PAIR ? 0 : m_value.pair; }
191
192     DashboardRegion* getDashboardRegionValue() const { return m_type != CSS_DASHBOARD_REGION ? 0 : m_value.region; }
193
194     CSSWrapShape* getShapeValue() const { return m_type != CSS_SHAPE ? 0 : m_value.shape; }
195
196     int getIdent() const;
197     template<typename T> inline operator T() const; // Defined in CSSPrimitiveValueMappings.h
198
199     virtual bool parseString(const String&, bool = false);
200     virtual String cssText() const;
201
202     virtual bool isQuirkValue() { return false; }
203
204     virtual void addSubresourceStyleURLs(ListHashSet<KURL>&, const CSSStyleSheet*);
205
206 protected:
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);
211
212 private:
213     CSSPrimitiveValue();
214     CSSPrimitiveValue(unsigned color); // RGB value
215     CSSPrimitiveValue(const Length&);
216
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); }
220
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
224
225     static PassRefPtr<CSSPrimitiveValue> createUncachedIdentifier(int identifier);
226     static PassRefPtr<CSSPrimitiveValue> createUncachedColor(unsigned rgbValue);
227     static PassRefPtr<CSSPrimitiveValue> createUncached(double value, UnitTypes type);
228
229     static UnitTypes canonicalUnitTypeForCategory(UnitCategory category);
230
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>);
237
238     bool getDoubleValueInternal(UnitTypes targetUnitType, double* result) const;
239
240     double computeLengthDouble(RenderStyle* currentStyle, RenderStyle* rootStyle, double multiplier, bool computingFontSize);
241
242     virtual bool isPrimitiveValue() const { return true; }
243
244     virtual unsigned short cssValueType() const;
245
246     signed m_type : 31;
247     mutable unsigned m_hasCachedCSSText : 1;
248     union {
249         int ident;
250         double num;
251         StringImpl* string;
252         Counter* counter;
253         Rect* rect;
254         Quad* quad;
255         unsigned rgbcolor;
256         Pair* pair;
257         DashboardRegion* region;
258         CSSWrapShape* shape;
259     } m_value;
260 };
261
262 } // namespace WebCore
263
264 #endif // CSSPrimitiveValue_h