Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / 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 "CSSPropertyNames.h"
26 #include "CSSValueKeywords.h"
27 #include "core/css/CSSValue.h"
28 #include "platform/graphics/Color.h"
29 #include "wtf/Forward.h"
30 #include "wtf/MathExtras.h"
31 #include "wtf/PassRefPtr.h"
32
33 namespace WebCore {
34
35 class CSSBasicShape;
36 class CSSCalcValue;
37 class CSSToLengthConversionData;
38 class Counter;
39 class ExceptionState;
40 class Length;
41 class LengthSize;
42 class Pair;
43 class Quad;
44 class RGBColor;
45 class Rect;
46 class RenderStyle;
47
48 // Dimension calculations are imprecise, often resulting in values of e.g.
49 // 44.99998. We need to go ahead and round if we're really close to the next
50 // integer value.
51 template<typename T> inline T roundForImpreciseConversion(double value)
52 {
53     value += (value < 0) ? -0.01 : +0.01;
54     return ((value > std::numeric_limits<T>::max()) || (value < std::numeric_limits<T>::min())) ? 0 : static_cast<T>(value);
55 }
56
57 template<> inline float roundForImpreciseConversion(double value)
58 {
59     double ceiledValue = ceil(value);
60     double proximityToNextInt = ceiledValue - value;
61     if (proximityToNextInt <= 0.01 && value > 0)
62         return static_cast<float>(ceiledValue);
63     if (proximityToNextInt >= 0.99 && value < 0)
64         return static_cast<float>(floor(value));
65     return static_cast<float>(value);
66 }
67
68 class CSSPrimitiveValue : public CSSValue {
69 public:
70     enum UnitTypes {
71         CSS_UNKNOWN = 0,
72         CSS_NUMBER = 1,
73         CSS_PERCENTAGE = 2,
74         CSS_EMS = 3,
75         CSS_EXS = 4,
76         CSS_PX = 5,
77         CSS_CM = 6,
78         CSS_MM = 7,
79         CSS_IN = 8,
80         CSS_PT = 9,
81         CSS_PC = 10,
82         CSS_DEG = 11,
83         CSS_RAD = 12,
84         CSS_GRAD = 13,
85         CSS_MS = 14,
86         CSS_S = 15,
87         CSS_HZ = 16,
88         CSS_KHZ = 17,
89         CSS_DIMENSION = 18,
90         CSS_STRING = 19,
91         CSS_URI = 20,
92         CSS_IDENT = 21,
93         CSS_ATTR = 22,
94         CSS_COUNTER = 23,
95         CSS_RECT = 24,
96         CSS_RGBCOLOR = 25,
97         // From CSS Values and Units. Viewport-percentage Lengths (vw/vh/vmin/vmax).
98         CSS_VW = 26,
99         CSS_VH = 27,
100         CSS_VMIN = 28,
101         CSS_VMAX = 29,
102         CSS_DPPX = 30,
103         CSS_DPI = 31,
104         CSS_DPCM = 32,
105         CSS_FR = 33,
106         CSS_PAIR = 100, // We envision this being exposed as a means of getting computed style values for pairs (border-spacing/radius, background-position, etc.)
107         CSS_UNICODE_RANGE = 102,
108
109         // These next types are just used internally to allow us to translate back and forth from CSSPrimitiveValues to CSSParserValues.
110         CSS_PARSER_OPERATOR = 103,
111         CSS_PARSER_INTEGER = 104,
112         CSS_PARSER_HEXCOLOR = 105,
113
114         // This is used internally for unknown identifiers
115         CSS_PARSER_IDENTIFIER = 106,
116
117         // These are from CSS3 Values and Units, but that isn't a finished standard yet
118         CSS_TURN = 107,
119         CSS_REMS = 108,
120         CSS_CHS = 109,
121
122         // This is used internally for counter names (as opposed to counter values)
123         CSS_COUNTER_NAME = 110,
124
125         // This is used by the CSS Shapes draft
126         CSS_SHAPE = 111,
127
128         // Used by border images.
129         CSS_QUAD = 112,
130
131         CSS_CALC = 113,
132         CSS_CALC_PERCENTAGE_WITH_NUMBER = 114,
133         CSS_CALC_PERCENTAGE_WITH_LENGTH = 115,
134
135         CSS_PROPERTY_ID = 117,
136         CSS_VALUE_ID = 118
137     };
138
139     // This enum follows the BisonCSSParser::Units enum augmented with UNIT_FREQUENCY for frequencies.
140     enum UnitCategory {
141         UNumber,
142         UPercent,
143         ULength,
144         UAngle,
145         UTime,
146         UFrequency,
147         UResolution,
148         UOther
149     };
150     static UnitCategory unitCategory(CSSPrimitiveValue::UnitTypes);
151
152     bool isAngle() const
153     {
154         return m_primitiveUnitType == CSS_DEG
155                || m_primitiveUnitType == CSS_RAD
156                || m_primitiveUnitType == CSS_GRAD
157                || m_primitiveUnitType == CSS_TURN;
158     }
159     bool isAttr() const { return m_primitiveUnitType == CSS_ATTR; }
160     bool isCounter() const { return m_primitiveUnitType == CSS_COUNTER; }
161     bool isFontIndependentLength() const { return m_primitiveUnitType >= CSS_PX && m_primitiveUnitType <= CSS_PC; }
162     bool isFontRelativeLength() const
163     {
164         return m_primitiveUnitType == CSS_EMS
165             || m_primitiveUnitType == CSS_EXS
166             || m_primitiveUnitType == CSS_REMS
167             || m_primitiveUnitType == CSS_CHS;
168     }
169     bool isLength() const
170     {
171         unsigned short type = primitiveType();
172         return (type >= CSS_EMS && type <= CSS_PC) || type == CSS_REMS || type == CSS_CHS || isViewportPercentageLength();
173     }
174     bool isNumber() const { return primitiveType() == CSS_NUMBER; }
175     bool isPercentage() const { return primitiveType() == CSS_PERCENTAGE; }
176     bool isPx() const { return primitiveType() == CSS_PX; }
177     bool isRect() const { return m_primitiveUnitType == CSS_RECT; }
178     bool isRGBColor() const { return m_primitiveUnitType == CSS_RGBCOLOR; }
179     bool isShape() const { return m_primitiveUnitType == CSS_SHAPE; }
180     bool isString() const { return m_primitiveUnitType == CSS_STRING; }
181     bool isTime() const { return m_primitiveUnitType == CSS_S || m_primitiveUnitType == CSS_MS; }
182     bool isURI() const { return m_primitiveUnitType == CSS_URI; }
183     bool isCalculated() const { return m_primitiveUnitType == CSS_CALC; }
184     bool isCalculatedPercentageWithNumber() const { return primitiveType() == CSS_CALC_PERCENTAGE_WITH_NUMBER; }
185     bool isCalculatedPercentageWithLength() const { return primitiveType() == CSS_CALC_PERCENTAGE_WITH_LENGTH; }
186     bool isDotsPerInch() const { return primitiveType() == CSS_DPI; }
187     bool isDotsPerPixel() const { return primitiveType() == CSS_DPPX; }
188     bool isDotsPerCentimeter() const { return primitiveType() == CSS_DPCM; }
189     bool isResolution() const
190     {
191         unsigned short type = primitiveType();
192         return type >= CSS_DPPX && type <= CSS_DPCM;
193     }
194     bool isViewportPercentageLength() const { return m_primitiveUnitType >= CSS_VW && m_primitiveUnitType <= CSS_VMAX; }
195     bool isFlex() const { return primitiveType() == CSS_FR; }
196     bool isValueID() const { return m_primitiveUnitType == CSS_VALUE_ID; }
197     bool colorIsDerivedFromElement() const;
198
199     static PassRefPtrWillBeRawPtr<CSSPrimitiveValue> createIdentifier(CSSValueID valueID)
200     {
201         return adoptRefCountedWillBeRefCountedGarbageCollected(new CSSPrimitiveValue(valueID));
202     }
203     static PassRefPtrWillBeRawPtr<CSSPrimitiveValue> createIdentifier(CSSPropertyID propertyID)
204     {
205         return adoptRefCountedWillBeRefCountedGarbageCollected(new CSSPrimitiveValue(propertyID));
206     }
207     static PassRefPtrWillBeRawPtr<CSSPrimitiveValue> createParserOperator(int parserOperator)
208     {
209         return adoptRefCountedWillBeRefCountedGarbageCollected(new CSSPrimitiveValue(parserOperator));
210     }
211     static PassRefPtrWillBeRawPtr<CSSPrimitiveValue> createColor(unsigned rgbValue)
212     {
213         return adoptRefCountedWillBeRefCountedGarbageCollected(new CSSPrimitiveValue(rgbValue));
214     }
215     static PassRefPtrWillBeRawPtr<CSSPrimitiveValue> create(double value, UnitTypes type)
216     {
217         return adoptRefCountedWillBeRefCountedGarbageCollected(new CSSPrimitiveValue(value, type));
218     }
219     static PassRefPtrWillBeRawPtr<CSSPrimitiveValue> create(const String& value, UnitTypes type)
220     {
221         return adoptRefCountedWillBeRefCountedGarbageCollected(new CSSPrimitiveValue(value, type));
222     }
223     static PassRefPtrWillBeRawPtr<CSSPrimitiveValue> create(const Length& value, float zoom)
224     {
225         return adoptRefCountedWillBeRefCountedGarbageCollected(new CSSPrimitiveValue(value, zoom));
226     }
227     static PassRefPtrWillBeRawPtr<CSSPrimitiveValue> create(const LengthSize& value)
228     {
229         return adoptRefCountedWillBeRefCountedGarbageCollected(new CSSPrimitiveValue(value));
230     }
231     template<typename T> static PassRefPtrWillBeRawPtr<CSSPrimitiveValue> create(T value)
232     {
233         return adoptRefCountedWillBeRefCountedGarbageCollected(new CSSPrimitiveValue(value));
234     }
235
236     // This value is used to handle quirky margins in reflow roots (body, td, and th) like WinIE.
237     // The basic idea is that a stylesheet can use the value __qem (for quirky em) instead of em.
238     // When the quirky value is used, if you're in quirks mode, the margin will collapse away
239     // inside a table cell.
240     static PassRefPtrWillBeRawPtr<CSSPrimitiveValue> createAllowingMarginQuirk(double value, UnitTypes type)
241     {
242         CSSPrimitiveValue* quirkValue = new CSSPrimitiveValue(value, type);
243         quirkValue->m_isQuirkValue = true;
244         return adoptRefCountedWillBeRefCountedGarbageCollected(quirkValue);
245     }
246
247     ~CSSPrimitiveValue();
248
249     void cleanup();
250
251     unsigned short primitiveType() const;
252
253     double computeDegrees();
254
255     enum TimeUnit { Seconds, Milliseconds };
256     template <typename T, TimeUnit timeUnit> T computeTime()
257     {
258         if (timeUnit == Seconds && m_primitiveUnitType == CSS_S)
259             return getValue<T>();
260         if (timeUnit == Seconds && m_primitiveUnitType == CSS_MS)
261             return getValue<T>() / 1000;
262         if (timeUnit == Milliseconds && m_primitiveUnitType == CSS_MS)
263             return getValue<T>();
264         if (timeUnit == Milliseconds && m_primitiveUnitType == CSS_S)
265             return getValue<T>() * 1000;
266         ASSERT_NOT_REACHED();
267         return 0;
268     }
269
270     /*
271      * Computes a length in pixels out of the given CSSValue
272      *
273      * The metrics have to be a bit different for screen and printer output.
274      * For screen output we assume 1 inch == 72 px, for printer we assume 300 dpi
275      *
276      * this is screen/printer dependent, so we probably need a config option for this,
277      * and some tool to calibrate.
278      */
279     template<typename T> T computeLength(const CSSToLengthConversionData&);
280
281     // Converts to a Length, mapping various unit types appropriately.
282     template<int> Length convertToLength(const CSSToLengthConversionData&);
283
284     // use with care!!!
285     void setPrimitiveType(unsigned short type) { m_primitiveUnitType = type; }
286
287     double getDoubleValue(unsigned short unitType, ExceptionState&) const;
288     double getDoubleValue(unsigned short unitType) const;
289     double getDoubleValue() const;
290
291     void setFloatValue(unsigned short unitType, double floatValue, ExceptionState&);
292     float getFloatValue(unsigned short unitType, ExceptionState& exceptionState) const { return getValue<float>(unitType, exceptionState); }
293     float getFloatValue(unsigned short unitType) const { return getValue<float>(unitType); }
294     float getFloatValue() const { return getValue<float>(); }
295
296     int getIntValue(unsigned short unitType, ExceptionState& exceptionState) const { return getValue<int>(unitType, exceptionState); }
297     int getIntValue(unsigned short unitType) const { return getValue<int>(unitType); }
298     int getIntValue() const { return getValue<int>(); }
299
300     template<typename T> inline T getValue(unsigned short unitType, ExceptionState& exceptionState) const { return clampTo<T>(getDoubleValue(unitType, exceptionState)); }
301     template<typename T> inline T getValue(unsigned short unitType) const { return clampTo<T>(getDoubleValue(unitType)); }
302     template<typename T> inline T getValue() const { return clampTo<T>(getDoubleValue()); }
303
304     void setStringValue(unsigned short stringType, const String& stringValue, ExceptionState&);
305     String getStringValue(ExceptionState&) const;
306     String getStringValue() const;
307
308     Counter* getCounterValue(ExceptionState&) const;
309     Counter* getCounterValue() const { return m_primitiveUnitType != CSS_COUNTER ? 0 : m_value.counter; }
310
311     Rect* getRectValue(ExceptionState&) const;
312     Rect* getRectValue() const { return m_primitiveUnitType != CSS_RECT ? 0 : m_value.rect; }
313
314     Quad* getQuadValue(ExceptionState&) const;
315     Quad* getQuadValue() const { return m_primitiveUnitType != CSS_QUAD ? 0 : m_value.quad; }
316
317     PassRefPtr<RGBColor> getRGBColorValue(ExceptionState&) const;
318     RGBA32 getRGBA32Value() const { return m_primitiveUnitType != CSS_RGBCOLOR ? 0 : m_value.rgbcolor; }
319
320     Pair* getPairValue(ExceptionState&) const;
321     Pair* getPairValue() const { return m_primitiveUnitType != CSS_PAIR ? 0 : m_value.pair; }
322
323     CSSBasicShape* getShapeValue() const { return m_primitiveUnitType != CSS_SHAPE ? 0 : m_value.shape; }
324
325     CSSCalcValue* cssCalcValue() const { return m_primitiveUnitType != CSS_CALC ? 0 : m_value.calc; }
326
327     CSSPropertyID getPropertyID() const { return m_primitiveUnitType == CSS_PROPERTY_ID ? m_value.propertyID : CSSPropertyInvalid; }
328     CSSValueID getValueID() const { return m_primitiveUnitType == CSS_VALUE_ID ? m_value.valueID : CSSValueInvalid; }
329
330     template<typename T> inline operator T() const; // Defined in CSSPrimitiveValueMappings.h
331
332     String customCSSText(CSSTextFormattingFlags = QuoteCSSStringIfNeeded) const;
333
334     bool isQuirkValue() { return m_isQuirkValue; }
335
336     PassRefPtrWillBeRawPtr<CSSPrimitiveValue> cloneForCSSOM() const;
337     void setCSSOMSafe() { m_isCSSOMSafe = true; }
338
339     bool equals(const CSSPrimitiveValue&) const;
340
341     void traceAfterDispatch(Visitor*);
342
343     static UnitTypes canonicalUnitTypeForCategory(UnitCategory);
344     static double conversionToCanonicalUnitsScaleFactor(unsigned short unitType);
345
346 private:
347     CSSPrimitiveValue(CSSValueID);
348     CSSPrimitiveValue(CSSPropertyID);
349     // FIXME: int vs. unsigned overloading is too subtle to distinguish the color and operator cases.
350     CSSPrimitiveValue(int parserOperator);
351     CSSPrimitiveValue(unsigned color); // RGB value
352     CSSPrimitiveValue(const Length& length)
353         : CSSValue(PrimitiveClass)
354     {
355         init(length);
356     }
357     CSSPrimitiveValue(const Length&, float zoom);
358     CSSPrimitiveValue(const LengthSize&);
359     CSSPrimitiveValue(const String&, UnitTypes);
360     CSSPrimitiveValue(double, UnitTypes);
361
362     template<typename T> CSSPrimitiveValue(T); // Defined in CSSPrimitiveValueMappings.h
363     template<typename T> CSSPrimitiveValue(T* val)
364         : CSSValue(PrimitiveClass)
365     {
366         init(PassRefPtr<T>(val));
367     }
368
369     template<typename T> CSSPrimitiveValue(PassRefPtr<T> val)
370         : CSSValue(PrimitiveClass)
371     {
372         init(val);
373     }
374
375     // Remove below overloaded constructors once all callers of CSSPrimitiveValue(...)
376     // have been converted to PassRefPtrWillBeRawPtr.
377     explicit CSSPrimitiveValue(CSSCalcValue*);
378     explicit CSSPrimitiveValue(PassRefPtrWillBeRawPtr<CSSCalcValue>);
379     explicit CSSPrimitiveValue(Pair*);
380     explicit CSSPrimitiveValue(PassRefPtrWillBeRawPtr<Pair>);
381     explicit CSSPrimitiveValue(Counter*);
382     explicit CSSPrimitiveValue(PassRefPtrWillBeRawPtr<Counter>);
383
384     static void create(int); // compile-time guard
385     static void create(unsigned); // compile-time guard
386     template<typename T> operator T*(); // compile-time guard
387
388     void init(const Length&);
389     void init(const LengthSize&);
390     void init(PassRefPtrWillBeRawPtr<Counter>);
391     void init(PassRefPtr<Rect>);
392     void init(PassRefPtrWillBeRawPtr<Pair>);
393     void init(PassRefPtr<Quad>);
394     void init(PassRefPtr<CSSBasicShape>);
395     void init(PassRefPtrWillBeRawPtr<CSSCalcValue>);
396     bool getDoubleValueInternal(UnitTypes targetUnitType, double* result) const;
397
398     double computeLengthDouble(const CSSToLengthConversionData&);
399
400     union {
401         CSSPropertyID propertyID;
402         CSSValueID valueID;
403         int parserOperator;
404         double num;
405         StringImpl* string;
406         Rect* rect;
407         Quad* quad;
408         unsigned rgbcolor;
409         CSSBasicShape* shape;
410         // FIXME: oilpan: Should be members, but no support for members in unions. Just trace the raw ptr for now.
411         CSSCalcValue* calc;
412         Counter* counter;
413         Pair* pair;
414     } m_value;
415 };
416
417 DEFINE_CSS_VALUE_TYPE_CASTS(CSSPrimitiveValue, isPrimitiveValue());
418
419 } // namespace WebCore
420
421 #endif // CSSPrimitiveValue_h