blob: 65b1cfbf66fc01fc790977fe852e49e9d5570b7e [file] [log] [blame]
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001/*
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
Torne (Richard Coles)5267f702013-06-11 10:57:24 +010025#include "CSSPropertyNames.h"
26#include "CSSValueKeywords.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010027#include "core/css/CSSValue.h"
Torne (Richard Coles)f79f16f2013-10-31 11:16:44 +000028#include "platform/graphics/Color.h"
Torne (Richard Coles)5267f702013-06-11 10:57:24 +010029#include "wtf/Forward.h"
30#include "wtf/MathExtras.h"
31#include "wtf/PassRefPtr.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010032
33namespace WebCore {
34
Ben Murdochdf957042013-08-06 11:01:27 +010035class CSSBasicShape;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010036class CSSCalcValue;
Torne (Richard Coles)a854de02013-12-18 16:25:25 +000037class CSSToLengthConversionData;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010038class Counter;
Ben Murdochdf957042013-08-06 11:01:27 +010039class ExceptionState;
Torne (Richard Coles)bfe35902013-10-22 16:41:51 +010040class Length;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010041class Pair;
42class Quad;
43class RGBColor;
44class Rect;
45class RenderStyle;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010046
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010047// Dimension calculations are imprecise, often resulting in values of e.g.
48// 44.99998. We need to go ahead and round if we're really close to the next
49// integer value.
50template<typename T> inline T roundForImpreciseConversion(double value)
51{
52 value += (value < 0) ? -0.01 : +0.01;
53 return ((value > std::numeric_limits<T>::max()) || (value < std::numeric_limits<T>::min())) ? 0 : static_cast<T>(value);
54}
55
56template<> inline float roundForImpreciseConversion(double value)
57{
58 double ceiledValue = ceil(value);
59 double proximityToNextInt = ceiledValue - value;
60 if (proximityToNextInt <= 0.01 && value > 0)
61 return static_cast<float>(ceiledValue);
62 if (proximityToNextInt >= 0.99 && value < 0)
63 return static_cast<float>(floor(value));
64 return static_cast<float>(value);
65}
66
67class CSSPrimitiveValue : public CSSValue {
68public:
69 enum UnitTypes {
70 CSS_UNKNOWN = 0,
71 CSS_NUMBER = 1,
72 CSS_PERCENTAGE = 2,
73 CSS_EMS = 3,
74 CSS_EXS = 4,
75 CSS_PX = 5,
76 CSS_CM = 6,
77 CSS_MM = 7,
78 CSS_IN = 8,
79 CSS_PT = 9,
80 CSS_PC = 10,
81 CSS_DEG = 11,
82 CSS_RAD = 12,
83 CSS_GRAD = 13,
84 CSS_MS = 14,
85 CSS_S = 15,
86 CSS_HZ = 16,
87 CSS_KHZ = 17,
88 CSS_DIMENSION = 18,
89 CSS_STRING = 19,
90 CSS_URI = 20,
91 CSS_IDENT = 21,
92 CSS_ATTR = 22,
93 CSS_COUNTER = 23,
94 CSS_RECT = 24,
95 CSS_RGBCOLOR = 25,
96 // From CSS Values and Units. Viewport-percentage Lengths (vw/vh/vmin/vmax).
97 CSS_VW = 26,
98 CSS_VH = 27,
99 CSS_VMIN = 28,
100 CSS_VMAX = 29,
101 CSS_DPPX = 30,
102 CSS_DPI = 31,
103 CSS_DPCM = 32,
104 CSS_FR = 33,
105 CSS_PAIR = 100, // We envision this being exposed as a means of getting computed style values for pairs (border-spacing/radius, background-position, etc.)
106 CSS_UNICODE_RANGE = 102,
107
108 // These next types are just used internally to allow us to translate back and forth from CSSPrimitiveValues to CSSParserValues.
109 CSS_PARSER_OPERATOR = 103,
110 CSS_PARSER_INTEGER = 104,
111 CSS_PARSER_HEXCOLOR = 105,
112
113 // This is used internally for unknown identifiers
114 CSS_PARSER_IDENTIFIER = 106,
115
116 // These are from CSS3 Values and Units, but that isn't a finished standard yet
117 CSS_TURN = 107,
118 CSS_REMS = 108,
119 CSS_CHS = 109,
120
121 // This is used internally for counter names (as opposed to counter values)
122 CSS_COUNTER_NAME = 110,
123
Ben Murdoch591b9582013-07-10 11:41:44 +0100124 // This is used by the CSS Shapes draft
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100125 CSS_SHAPE = 111,
126
127 // Used by border images.
128 CSS_QUAD = 112,
129
130 CSS_CALC = 113,
131 CSS_CALC_PERCENTAGE_WITH_NUMBER = 114,
132 CSS_CALC_PERCENTAGE_WITH_LENGTH = 115,
133 CSS_VARIABLE_NAME = 116,
Torne (Richard Coles)5267f702013-06-11 10:57:24 +0100134
135 CSS_PROPERTY_ID = 117,
136 CSS_VALUE_ID = 118
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100137 };
138
139 // This enum follows the CSSParser::Units enum augmented with UNIT_FREQUENCY for frequencies.
140 enum UnitCategory {
141 UNumber,
142 UPercent,
143 ULength,
144 UAngle,
145 UTime,
146 UFrequency,
147 UViewportPercentageLength,
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100148 UResolution,
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100149 UOther
150 };
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100151 static UnitCategory unitCategory(CSSPrimitiveValue::UnitTypes);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100152
153 bool isAngle() const
154 {
155 return m_primitiveUnitType == CSS_DEG
156 || m_primitiveUnitType == CSS_RAD
157 || m_primitiveUnitType == CSS_GRAD
158 || m_primitiveUnitType == CSS_TURN;
159 }
160 bool isAttr() const { return m_primitiveUnitType == CSS_ATTR; }
161 bool isCounter() const { return m_primitiveUnitType == CSS_COUNTER; }
162 bool isFontIndependentLength() const { return m_primitiveUnitType >= CSS_PX && m_primitiveUnitType <= CSS_PC; }
163 bool isFontRelativeLength() const
164 {
165 return m_primitiveUnitType == CSS_EMS
166 || m_primitiveUnitType == CSS_EXS
167 || m_primitiveUnitType == CSS_REMS
168 || m_primitiveUnitType == CSS_CHS;
169 }
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100170 bool isLength() const
171 {
172 unsigned short type = primitiveType();
173 return (type >= CSS_EMS && type <= CSS_PC) || type == CSS_REMS || type == CSS_CHS;
174 }
175 bool isNumber() const { return primitiveType() == CSS_NUMBER; }
176 bool isPercentage() const { return primitiveType() == CSS_PERCENTAGE; }
177 bool isPx() const { return primitiveType() == CSS_PX; }
178 bool isRect() const { return m_primitiveUnitType == CSS_RECT; }
179 bool isRGBColor() const { return m_primitiveUnitType == CSS_RGBCOLOR; }
180 bool isShape() const { return m_primitiveUnitType == CSS_SHAPE; }
181 bool isString() const { return m_primitiveUnitType == CSS_STRING; }
182 bool isTime() const { return m_primitiveUnitType == CSS_S || m_primitiveUnitType == CSS_MS; }
183 bool isURI() const { return m_primitiveUnitType == CSS_URI; }
184 bool isCalculated() const { return m_primitiveUnitType == CSS_CALC; }
185 bool isCalculatedPercentageWithNumber() const { return primitiveType() == CSS_CALC_PERCENTAGE_WITH_NUMBER; }
186 bool isCalculatedPercentageWithLength() const { return primitiveType() == CSS_CALC_PERCENTAGE_WITH_LENGTH; }
187 bool isDotsPerInch() const { return primitiveType() == CSS_DPI; }
188 bool isDotsPerPixel() const { return primitiveType() == CSS_DPPX; }
189 bool isDotsPerCentimeter() const { return primitiveType() == CSS_DPCM; }
Torne (Richard Coles)e5249552013-05-15 11:35:13 +0100190 bool isResolution() const
191 {
192 unsigned short type = primitiveType();
193 return type >= CSS_DPPX && type <= CSS_DPCM;
194 }
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100195 bool isVariableName() const { return primitiveType() == CSS_VARIABLE_NAME; }
196 bool isViewportPercentageLength() const { return m_primitiveUnitType >= CSS_VW && m_primitiveUnitType <= CSS_VMAX; }
197 bool isFlex() const { return primitiveType() == CSS_FR; }
Torne (Richard Coles)5267f702013-06-11 10:57:24 +0100198 bool isValueID() const { return m_primitiveUnitType == CSS_VALUE_ID; }
Ben Murdoch591b9582013-07-10 11:41:44 +0100199 bool colorIsDerivedFromElement() const;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100200
Torne (Richard Coles)5267f702013-06-11 10:57:24 +0100201 static PassRefPtr<CSSPrimitiveValue> createIdentifier(CSSValueID valueID) { return adoptRef(new CSSPrimitiveValue(valueID)); }
202 static PassRefPtr<CSSPrimitiveValue> createIdentifier(CSSPropertyID propertyID) { return adoptRef(new CSSPrimitiveValue(propertyID)); }
203 static PassRefPtr<CSSPrimitiveValue> createParserOperator(int parserOperator) { return adoptRef(new CSSPrimitiveValue(parserOperator)); }
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100204 static PassRefPtr<CSSPrimitiveValue> createColor(unsigned rgbValue) { return adoptRef(new CSSPrimitiveValue(rgbValue)); }
205 static PassRefPtr<CSSPrimitiveValue> create(double value, UnitTypes type) { return adoptRef(new CSSPrimitiveValue(value, type)); }
206 static PassRefPtr<CSSPrimitiveValue> create(const String& value, UnitTypes type) { return adoptRef(new CSSPrimitiveValue(value, type)); }
Torne (Richard Coles)1e202182013-10-18 15:46:42 +0100207 static PassRefPtr<CSSPrimitiveValue> create(const Length& value, float zoom) { return adoptRef(new CSSPrimitiveValue(value, zoom)); }
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100208
209 template<typename T> static PassRefPtr<CSSPrimitiveValue> create(T value)
210 {
211 return adoptRef(new CSSPrimitiveValue(value));
212 }
213
214 // This value is used to handle quirky margins in reflow roots (body, td, and th) like WinIE.
215 // The basic idea is that a stylesheet can use the value __qem (for quirky em) instead of em.
216 // When the quirky value is used, if you're in quirks mode, the margin will collapse away
217 // inside a table cell.
218 static PassRefPtr<CSSPrimitiveValue> createAllowingMarginQuirk(double value, UnitTypes type)
219 {
220 CSSPrimitiveValue* quirkValue = new CSSPrimitiveValue(value, type);
221 quirkValue->m_isQuirkValue = true;
222 return adoptRef(quirkValue);
223 }
224
225 ~CSSPrimitiveValue();
226
227 void cleanup();
228
229 unsigned short primitiveType() const;
230
231 double computeDegrees();
232
233 enum TimeUnit { Seconds, Milliseconds };
234 template <typename T, TimeUnit timeUnit> T computeTime()
235 {
236 if (timeUnit == Seconds && m_primitiveUnitType == CSS_S)
237 return getValue<T>();
238 if (timeUnit == Seconds && m_primitiveUnitType == CSS_MS)
239 return getValue<T>() / 1000;
240 if (timeUnit == Milliseconds && m_primitiveUnitType == CSS_MS)
241 return getValue<T>();
242 if (timeUnit == Milliseconds && m_primitiveUnitType == CSS_S)
243 return getValue<T>() * 1000;
244 ASSERT_NOT_REACHED();
245 return 0;
246 }
247
248 /*
Torne (Richard Coles)a854de02013-12-18 16:25:25 +0000249 * Computes a length in pixels out of the given CSSValue
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100250 *
251 * The metrics have to be a bit different for screen and printer output.
252 * For screen output we assume 1 inch == 72 px, for printer we assume 300 dpi
253 *
254 * this is screen/printer dependent, so we probably need a config option for this,
255 * and some tool to calibrate.
256 */
Torne (Richard Coles)a854de02013-12-18 16:25:25 +0000257 template<typename T> T computeLength(const CSSToLengthConversionData&);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100258
259 // Converts to a Length, mapping various unit types appropriately.
Torne (Richard Coles)a854de02013-12-18 16:25:25 +0000260 template<int> Length convertToLength(const CSSToLengthConversionData&);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100261
262 // use with care!!!
263 void setPrimitiveType(unsigned short type) { m_primitiveUnitType = type; }
264
Ben Murdochdf957042013-08-06 11:01:27 +0100265 double getDoubleValue(unsigned short unitType, ExceptionState&) const;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100266 double getDoubleValue(unsigned short unitType) const;
267 double getDoubleValue() const;
268
Ben Murdochdf957042013-08-06 11:01:27 +0100269 void setFloatValue(unsigned short unitType, double floatValue, ExceptionState&);
Torne (Richard Coles)51b29062013-11-28 11:56:03 +0000270 float getFloatValue(unsigned short unitType, ExceptionState& exceptionState) const { return getValue<float>(unitType, exceptionState); }
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100271 float getFloatValue(unsigned short unitType) const { return getValue<float>(unitType); }
272 float getFloatValue() const { return getValue<float>(); }
273
Torne (Richard Coles)51b29062013-11-28 11:56:03 +0000274 int getIntValue(unsigned short unitType, ExceptionState& exceptionState) const { return getValue<int>(unitType, exceptionState); }
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100275 int getIntValue(unsigned short unitType) const { return getValue<int>(unitType); }
276 int getIntValue() const { return getValue<int>(); }
277
Torne (Richard Coles)51b29062013-11-28 11:56:03 +0000278 template<typename T> inline T getValue(unsigned short unitType, ExceptionState& exceptionState) const { return clampTo<T>(getDoubleValue(unitType, exceptionState)); }
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100279 template<typename T> inline T getValue(unsigned short unitType) const { return clampTo<T>(getDoubleValue(unitType)); }
280 template<typename T> inline T getValue() const { return clampTo<T>(getDoubleValue()); }
281
Ben Murdochdf957042013-08-06 11:01:27 +0100282 void setStringValue(unsigned short stringType, const String& stringValue, ExceptionState&);
283 String getStringValue(ExceptionState&) const;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100284 String getStringValue() const;
285
Ben Murdochdf957042013-08-06 11:01:27 +0100286 Counter* getCounterValue(ExceptionState&) const;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100287 Counter* getCounterValue() const { return m_primitiveUnitType != CSS_COUNTER ? 0 : m_value.counter; }
288
Ben Murdochdf957042013-08-06 11:01:27 +0100289 Rect* getRectValue(ExceptionState&) const;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100290 Rect* getRectValue() const { return m_primitiveUnitType != CSS_RECT ? 0 : m_value.rect; }
291
Ben Murdochdf957042013-08-06 11:01:27 +0100292 Quad* getQuadValue(ExceptionState&) const;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100293 Quad* getQuadValue() const { return m_primitiveUnitType != CSS_QUAD ? 0 : m_value.quad; }
294
Ben Murdochdf957042013-08-06 11:01:27 +0100295 PassRefPtr<RGBColor> getRGBColorValue(ExceptionState&) const;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100296 RGBA32 getRGBA32Value() const { return m_primitiveUnitType != CSS_RGBCOLOR ? 0 : m_value.rgbcolor; }
297
Ben Murdochdf957042013-08-06 11:01:27 +0100298 Pair* getPairValue(ExceptionState&) const;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100299 Pair* getPairValue() const { return m_primitiveUnitType != CSS_PAIR ? 0 : m_value.pair; }
300
301 CSSBasicShape* getShapeValue() const { return m_primitiveUnitType != CSS_SHAPE ? 0 : m_value.shape; }
Ben Murdoch02772c62013-07-26 10:21:05 +0100302
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100303 CSSCalcValue* cssCalcValue() const { return m_primitiveUnitType != CSS_CALC ? 0 : m_value.calc; }
304
Torne (Richard Coles)5267f702013-06-11 10:57:24 +0100305 CSSPropertyID getPropertyID() const { return m_primitiveUnitType == CSS_PROPERTY_ID ? m_value.propertyID : CSSPropertyInvalid; }
306 CSSValueID getValueID() const { return m_primitiveUnitType == CSS_VALUE_ID ? m_value.valueID : CSSValueInvalid; }
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100307
308 template<typename T> inline operator T() const; // Defined in CSSPrimitiveValueMappings.h
309
Torne (Richard Coles)bfe35902013-10-22 16:41:51 +0100310 String customCSSText(CSSTextFormattingFlags = QuoteCSSStringIfNeeded) const;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100311 String customSerializeResolvingVariables(const HashMap<AtomicString, String>&) const;
312 bool hasVariableReference() const;
313
314 bool isQuirkValue() { return m_isQuirkValue; }
315
316 void addSubresourceStyleURLs(ListHashSet<KURL>&, const StyleSheetContents*) const;
317
318 Length viewportPercentageLength();
Ben Murdoch02772c62013-07-26 10:21:05 +0100319
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100320 PassRefPtr<CSSPrimitiveValue> cloneForCSSOM() const;
321 void setCSSOMSafe() { m_isCSSOMSafe = true; }
322
323 bool equals(const CSSPrimitiveValue&) const;
324
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100325 static UnitTypes canonicalUnitTypeForCategory(UnitCategory);
326 static double conversionToCanonicalUnitsScaleFactor(unsigned short unitType);
327
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100328private:
Torne (Richard Coles)5267f702013-06-11 10:57:24 +0100329 CSSPrimitiveValue(CSSValueID);
330 CSSPrimitiveValue(CSSPropertyID);
331 // FIXME: int vs. unsigned overloading is too subtle to distinguish the color and operator cases.
332 CSSPrimitiveValue(int parserOperator);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100333 CSSPrimitiveValue(unsigned color); // RGB value
Torne (Richard Coles)c0e19a62013-08-30 15:15:11 +0100334 CSSPrimitiveValue(const Length& length)
335 : CSSValue(PrimitiveClass)
336 {
337 init(length);
338 }
Torne (Richard Coles)1e202182013-10-18 15:46:42 +0100339 CSSPrimitiveValue(const Length&, float zoom);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100340 CSSPrimitiveValue(const String&, UnitTypes);
341 CSSPrimitiveValue(double, UnitTypes);
342
343 template<typename T> CSSPrimitiveValue(T); // Defined in CSSPrimitiveValueMappings.h
344 template<typename T> CSSPrimitiveValue(T* val)
345 : CSSValue(PrimitiveClass)
346 {
347 init(PassRefPtr<T>(val));
348 }
349
350 template<typename T> CSSPrimitiveValue(PassRefPtr<T> val)
351 : CSSValue(PrimitiveClass)
352 {
353 init(val);
354 }
355
356 static void create(int); // compile-time guard
357 static void create(unsigned); // compile-time guard
358 template<typename T> operator T*(); // compile-time guard
359
Torne (Richard Coles)c0e19a62013-08-30 15:15:11 +0100360 void init(const Length&);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100361 void init(PassRefPtr<Counter>);
362 void init(PassRefPtr<Rect>);
363 void init(PassRefPtr<Pair>);
364 void init(PassRefPtr<Quad>);
365 void init(PassRefPtr<CSSBasicShape>);
366 void init(PassRefPtr<CSSCalcValue>);
367 bool getDoubleValueInternal(UnitTypes targetUnitType, double* result) const;
368
Torne (Richard Coles)a854de02013-12-18 16:25:25 +0000369 double computeLengthDouble(const CSSToLengthConversionData&);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100370
371 union {
Torne (Richard Coles)5267f702013-06-11 10:57:24 +0100372 CSSPropertyID propertyID;
373 CSSValueID valueID;
374 int parserOperator;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100375 double num;
376 StringImpl* string;
377 Counter* counter;
378 Rect* rect;
379 Quad* quad;
380 unsigned rgbcolor;
381 Pair* pair;
382 CSSBasicShape* shape;
383 CSSCalcValue* calc;
384 } m_value;
385};
386
Torne (Richard Coles)bfe35902013-10-22 16:41:51 +0100387DEFINE_CSS_VALUE_TYPE_CASTS(CSSPrimitiveValue, isPrimitiveValue());
Torne (Richard Coles)e5249552013-05-15 11:35:13 +0100388
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100389} // namespace WebCore
390
391#endif // CSSPrimitiveValue_h