blob: 139fb65eb29a8a212dba1033c3c8d07bf98c1ed9 [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)09380292014-02-21 12:17:33 +000041class LengthSize;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010042class Pair;
43class Quad;
44class RGBColor;
45class Rect;
46class RenderStyle;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010047
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010048// 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.
51template<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
57template<> 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
Ben Murdoch07a852d2014-03-31 11:51:52 +010068// CSSPrimitiveValues are immutable. This class has manual ref-counting
69// of unioned types and does not have the code necessary
70// to handle any kind of mutations. All DOM-exposed "setters" just throw
71// exceptions.
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010072class CSSPrimitiveValue : public CSSValue {
73public:
74 enum UnitTypes {
75 CSS_UNKNOWN = 0,
76 CSS_NUMBER = 1,
77 CSS_PERCENTAGE = 2,
78 CSS_EMS = 3,
79 CSS_EXS = 4,
80 CSS_PX = 5,
81 CSS_CM = 6,
82 CSS_MM = 7,
83 CSS_IN = 8,
84 CSS_PT = 9,
85 CSS_PC = 10,
86 CSS_DEG = 11,
87 CSS_RAD = 12,
88 CSS_GRAD = 13,
89 CSS_MS = 14,
90 CSS_S = 15,
91 CSS_HZ = 16,
92 CSS_KHZ = 17,
93 CSS_DIMENSION = 18,
94 CSS_STRING = 19,
95 CSS_URI = 20,
96 CSS_IDENT = 21,
97 CSS_ATTR = 22,
98 CSS_COUNTER = 23,
99 CSS_RECT = 24,
100 CSS_RGBCOLOR = 25,
101 // From CSS Values and Units. Viewport-percentage Lengths (vw/vh/vmin/vmax).
102 CSS_VW = 26,
103 CSS_VH = 27,
104 CSS_VMIN = 28,
105 CSS_VMAX = 29,
106 CSS_DPPX = 30,
107 CSS_DPI = 31,
108 CSS_DPCM = 32,
109 CSS_FR = 33,
110 CSS_PAIR = 100, // We envision this being exposed as a means of getting computed style values for pairs (border-spacing/radius, background-position, etc.)
111 CSS_UNICODE_RANGE = 102,
112
113 // These next types are just used internally to allow us to translate back and forth from CSSPrimitiveValues to CSSParserValues.
114 CSS_PARSER_OPERATOR = 103,
115 CSS_PARSER_INTEGER = 104,
116 CSS_PARSER_HEXCOLOR = 105,
117
118 // This is used internally for unknown identifiers
119 CSS_PARSER_IDENTIFIER = 106,
120
121 // These are from CSS3 Values and Units, but that isn't a finished standard yet
122 CSS_TURN = 107,
123 CSS_REMS = 108,
124 CSS_CHS = 109,
125
126 // This is used internally for counter names (as opposed to counter values)
127 CSS_COUNTER_NAME = 110,
128
Ben Murdoch591b9582013-07-10 11:41:44 +0100129 // This is used by the CSS Shapes draft
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100130 CSS_SHAPE = 111,
131
132 // Used by border images.
133 CSS_QUAD = 112,
134
135 CSS_CALC = 113,
136 CSS_CALC_PERCENTAGE_WITH_NUMBER = 114,
137 CSS_CALC_PERCENTAGE_WITH_LENGTH = 115,
Torne (Richard Coles)5267f702013-06-11 10:57:24 +0100138
139 CSS_PROPERTY_ID = 117,
140 CSS_VALUE_ID = 118
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100141 };
142
Ben Murdocha9984bf2014-04-10 11:22:39 +0100143 enum LengthUnitType {
144 UnitTypePixels = 0,
145 UnitTypePercentage,
146 UnitTypeFontSize,
147 UnitTypeFontXSize,
148 UnitTypeRootFontSize,
149 UnitTypeZeroCharacterWidth,
150 UnitTypeViewportWidth,
151 UnitTypeViewportHeight,
152 UnitTypeViewportMin,
153 UnitTypeViewportMax,
154
155 // This value must come after the last length unit type to enable iteration over the length unit types.
156 LengthUnitTypeCount,
157
158 // FIXME: This is used by AnimatableLength to represent calc objects and is not a type of length value.
159 // Remove this once we no longer need the distinction.
160 UnitTypeCalc,
161 };
162
163 typedef Vector<double, CSSPrimitiveValue::LengthUnitTypeCount> CSSLengthArray;
164 void accumulateLengthArray(CSSLengthArray&, double multiplier = 1) const;
165
Torne (Richard Coles)09380292014-02-21 12:17:33 +0000166 // This enum follows the BisonCSSParser::Units enum augmented with UNIT_FREQUENCY for frequencies.
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100167 enum UnitCategory {
168 UNumber,
169 UPercent,
170 ULength,
171 UAngle,
172 UTime,
173 UFrequency,
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100174 UResolution,
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100175 UOther
176 };
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100177 static UnitCategory unitCategory(CSSPrimitiveValue::UnitTypes);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100178
Ben Murdoch07a852d2014-03-31 11:51:52 +0100179 static UnitTypes fromName(const String& unit);
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +0000180
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100181 bool isAngle() const
182 {
183 return m_primitiveUnitType == CSS_DEG
184 || m_primitiveUnitType == CSS_RAD
185 || m_primitiveUnitType == CSS_GRAD
186 || m_primitiveUnitType == CSS_TURN;
187 }
188 bool isAttr() const { return m_primitiveUnitType == CSS_ATTR; }
189 bool isCounter() const { return m_primitiveUnitType == CSS_COUNTER; }
190 bool isFontIndependentLength() const { return m_primitiveUnitType >= CSS_PX && m_primitiveUnitType <= CSS_PC; }
191 bool isFontRelativeLength() const
192 {
193 return m_primitiveUnitType == CSS_EMS
194 || m_primitiveUnitType == CSS_EXS
195 || m_primitiveUnitType == CSS_REMS
196 || m_primitiveUnitType == CSS_CHS;
197 }
Ben Murdoch10f88d52014-04-24 10:50:33 +0100198 static bool isViewportPercentageLength(unsigned short type) { return type >= CSS_VW && type <= CSS_VMAX; }
199 static bool isLength(unsigned short type)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100200 {
Ben Murdoch10f88d52014-04-24 10:50:33 +0100201 return (type >= CSS_EMS && type <= CSS_PC) || type == CSS_REMS || type == CSS_CHS || isViewportPercentageLength(type);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100202 }
Ben Murdoch10f88d52014-04-24 10:50:33 +0100203 bool isLength() const { return isLength(primitiveType()); }
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100204 bool isNumber() const { return primitiveType() == CSS_NUMBER; }
205 bool isPercentage() const { return primitiveType() == CSS_PERCENTAGE; }
206 bool isPx() const { return primitiveType() == CSS_PX; }
207 bool isRect() const { return m_primitiveUnitType == CSS_RECT; }
208 bool isRGBColor() const { return m_primitiveUnitType == CSS_RGBCOLOR; }
209 bool isShape() const { return m_primitiveUnitType == CSS_SHAPE; }
210 bool isString() const { return m_primitiveUnitType == CSS_STRING; }
211 bool isTime() const { return m_primitiveUnitType == CSS_S || m_primitiveUnitType == CSS_MS; }
212 bool isURI() const { return m_primitiveUnitType == CSS_URI; }
213 bool isCalculated() const { return m_primitiveUnitType == CSS_CALC; }
214 bool isCalculatedPercentageWithNumber() const { return primitiveType() == CSS_CALC_PERCENTAGE_WITH_NUMBER; }
215 bool isCalculatedPercentageWithLength() const { return primitiveType() == CSS_CALC_PERCENTAGE_WITH_LENGTH; }
Bo Liuf91f5fa2014-05-01 10:37:55 -0700216 static bool isDotsPerInch(UnitTypes type) { return type == CSS_DPI; }
217 static bool isDotsPerPixel(UnitTypes type) { return type == CSS_DPPX; }
218 static bool isDotsPerCentimeter(UnitTypes type) { return type == CSS_DPCM; }
219 static bool isResolution(UnitTypes type) { return type >= CSS_DPPX && type <= CSS_DPCM; }
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100220 bool isFlex() const { return primitiveType() == CSS_FR; }
Torne (Richard Coles)5267f702013-06-11 10:57:24 +0100221 bool isValueID() const { return m_primitiveUnitType == CSS_VALUE_ID; }
Ben Murdoch591b9582013-07-10 11:41:44 +0100222 bool colorIsDerivedFromElement() const;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100223
Torne (Richard Coles)09380292014-02-21 12:17:33 +0000224 static PassRefPtrWillBeRawPtr<CSSPrimitiveValue> createIdentifier(CSSValueID valueID)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100225 {
Bo Liuf91f5fa2014-05-01 10:37:55 -0700226 return adoptRefWillBeNoop(new CSSPrimitiveValue(valueID));
Torne (Richard Coles)09380292014-02-21 12:17:33 +0000227 }
228 static PassRefPtrWillBeRawPtr<CSSPrimitiveValue> createIdentifier(CSSPropertyID propertyID)
229 {
Bo Liuf91f5fa2014-05-01 10:37:55 -0700230 return adoptRefWillBeNoop(new CSSPrimitiveValue(propertyID));
Torne (Richard Coles)09380292014-02-21 12:17:33 +0000231 }
232 static PassRefPtrWillBeRawPtr<CSSPrimitiveValue> createParserOperator(int parserOperator)
233 {
Bo Liuf91f5fa2014-05-01 10:37:55 -0700234 return adoptRefWillBeNoop(new CSSPrimitiveValue(parserOperator, CSS_PARSER_OPERATOR));
Torne (Richard Coles)09380292014-02-21 12:17:33 +0000235 }
236 static PassRefPtrWillBeRawPtr<CSSPrimitiveValue> createColor(unsigned rgbValue)
237 {
Bo Liuf91f5fa2014-05-01 10:37:55 -0700238 return adoptRefWillBeNoop(new CSSPrimitiveValue(rgbValue, CSS_RGBCOLOR));
Torne (Richard Coles)09380292014-02-21 12:17:33 +0000239 }
240 static PassRefPtrWillBeRawPtr<CSSPrimitiveValue> create(double value, UnitTypes type)
241 {
Bo Liuf91f5fa2014-05-01 10:37:55 -0700242 return adoptRefWillBeNoop(new CSSPrimitiveValue(value, type));
Torne (Richard Coles)09380292014-02-21 12:17:33 +0000243 }
244 static PassRefPtrWillBeRawPtr<CSSPrimitiveValue> create(const String& value, UnitTypes type)
245 {
Bo Liuf91f5fa2014-05-01 10:37:55 -0700246 return adoptRefWillBeNoop(new CSSPrimitiveValue(value, type));
Torne (Richard Coles)09380292014-02-21 12:17:33 +0000247 }
248 static PassRefPtrWillBeRawPtr<CSSPrimitiveValue> create(const Length& value, float zoom)
249 {
Bo Liuf91f5fa2014-05-01 10:37:55 -0700250 return adoptRefWillBeNoop(new CSSPrimitiveValue(value, zoom));
Torne (Richard Coles)09380292014-02-21 12:17:33 +0000251 }
Bo Liuf91f5fa2014-05-01 10:37:55 -0700252 static PassRefPtrWillBeRawPtr<CSSPrimitiveValue> create(const LengthSize& value, const RenderStyle& style)
Torne (Richard Coles)09380292014-02-21 12:17:33 +0000253 {
Bo Liuf91f5fa2014-05-01 10:37:55 -0700254 return adoptRefWillBeNoop(new CSSPrimitiveValue(value, style));
Torne (Richard Coles)09380292014-02-21 12:17:33 +0000255 }
256 template<typename T> static PassRefPtrWillBeRawPtr<CSSPrimitiveValue> create(T value)
257 {
Bo Liuf91f5fa2014-05-01 10:37:55 -0700258 return adoptRefWillBeNoop(new CSSPrimitiveValue(value));
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100259 }
260
261 // This value is used to handle quirky margins in reflow roots (body, td, and th) like WinIE.
262 // The basic idea is that a stylesheet can use the value __qem (for quirky em) instead of em.
263 // When the quirky value is used, if you're in quirks mode, the margin will collapse away
264 // inside a table cell.
Torne (Richard Coles)09380292014-02-21 12:17:33 +0000265 static PassRefPtrWillBeRawPtr<CSSPrimitiveValue> createAllowingMarginQuirk(double value, UnitTypes type)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100266 {
267 CSSPrimitiveValue* quirkValue = new CSSPrimitiveValue(value, type);
268 quirkValue->m_isQuirkValue = true;
Bo Liuf91f5fa2014-05-01 10:37:55 -0700269 return adoptRefWillBeNoop(quirkValue);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100270 }
271
272 ~CSSPrimitiveValue();
273
274 void cleanup();
275
276 unsigned short primitiveType() const;
277
278 double computeDegrees();
279
280 enum TimeUnit { Seconds, Milliseconds };
281 template <typename T, TimeUnit timeUnit> T computeTime()
282 {
283 if (timeUnit == Seconds && m_primitiveUnitType == CSS_S)
284 return getValue<T>();
285 if (timeUnit == Seconds && m_primitiveUnitType == CSS_MS)
286 return getValue<T>() / 1000;
287 if (timeUnit == Milliseconds && m_primitiveUnitType == CSS_MS)
288 return getValue<T>();
289 if (timeUnit == Milliseconds && m_primitiveUnitType == CSS_S)
290 return getValue<T>() * 1000;
291 ASSERT_NOT_REACHED();
292 return 0;
293 }
294
295 /*
Torne (Richard Coles)a854de02013-12-18 16:25:25 +0000296 * Computes a length in pixels out of the given CSSValue
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100297 *
298 * The metrics have to be a bit different for screen and printer output.
299 * For screen output we assume 1 inch == 72 px, for printer we assume 300 dpi
300 *
301 * this is screen/printer dependent, so we probably need a config option for this,
302 * and some tool to calibrate.
303 */
Torne (Richard Coles)a854de02013-12-18 16:25:25 +0000304 template<typename T> T computeLength(const CSSToLengthConversionData&);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100305
306 // Converts to a Length, mapping various unit types appropriately.
Torne (Richard Coles)a854de02013-12-18 16:25:25 +0000307 template<int> Length convertToLength(const CSSToLengthConversionData&);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100308
Ben Murdochdf957042013-08-06 11:01:27 +0100309 double getDoubleValue(unsigned short unitType, ExceptionState&) const;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100310 double getDoubleValue(unsigned short unitType) const;
311 double getDoubleValue() const;
312
Ben Murdochdf957042013-08-06 11:01:27 +0100313 void setFloatValue(unsigned short unitType, double floatValue, ExceptionState&);
Torne (Richard Coles)51b29062013-11-28 11:56:03 +0000314 float getFloatValue(unsigned short unitType, ExceptionState& exceptionState) const { return getValue<float>(unitType, exceptionState); }
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100315 float getFloatValue(unsigned short unitType) const { return getValue<float>(unitType); }
316 float getFloatValue() const { return getValue<float>(); }
317
Torne (Richard Coles)51b29062013-11-28 11:56:03 +0000318 int getIntValue(unsigned short unitType, ExceptionState& exceptionState) const { return getValue<int>(unitType, exceptionState); }
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100319 int getIntValue(unsigned short unitType) const { return getValue<int>(unitType); }
320 int getIntValue() const { return getValue<int>(); }
321
Torne (Richard Coles)51b29062013-11-28 11:56:03 +0000322 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 +0100323 template<typename T> inline T getValue(unsigned short unitType) const { return clampTo<T>(getDoubleValue(unitType)); }
324 template<typename T> inline T getValue() const { return clampTo<T>(getDoubleValue()); }
325
Ben Murdochdf957042013-08-06 11:01:27 +0100326 void setStringValue(unsigned short stringType, const String& stringValue, ExceptionState&);
327 String getStringValue(ExceptionState&) const;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100328 String getStringValue() const;
329
Ben Murdochdf957042013-08-06 11:01:27 +0100330 Counter* getCounterValue(ExceptionState&) const;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100331 Counter* getCounterValue() const { return m_primitiveUnitType != CSS_COUNTER ? 0 : m_value.counter; }
332
Ben Murdochdf957042013-08-06 11:01:27 +0100333 Rect* getRectValue(ExceptionState&) const;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100334 Rect* getRectValue() const { return m_primitiveUnitType != CSS_RECT ? 0 : m_value.rect; }
335
Ben Murdochdf957042013-08-06 11:01:27 +0100336 Quad* getQuadValue(ExceptionState&) const;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100337 Quad* getQuadValue() const { return m_primitiveUnitType != CSS_QUAD ? 0 : m_value.quad; }
338
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +0000339 PassRefPtrWillBeRawPtr<RGBColor> getRGBColorValue(ExceptionState&) const;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100340 RGBA32 getRGBA32Value() const { return m_primitiveUnitType != CSS_RGBCOLOR ? 0 : m_value.rgbcolor; }
341
Ben Murdochdf957042013-08-06 11:01:27 +0100342 Pair* getPairValue(ExceptionState&) const;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100343 Pair* getPairValue() const { return m_primitiveUnitType != CSS_PAIR ? 0 : m_value.pair; }
344
345 CSSBasicShape* getShapeValue() const { return m_primitiveUnitType != CSS_SHAPE ? 0 : m_value.shape; }
Ben Murdoch02772c62013-07-26 10:21:05 +0100346
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100347 CSSCalcValue* cssCalcValue() const { return m_primitiveUnitType != CSS_CALC ? 0 : m_value.calc; }
348
Torne (Richard Coles)5267f702013-06-11 10:57:24 +0100349 CSSPropertyID getPropertyID() const { return m_primitiveUnitType == CSS_PROPERTY_ID ? m_value.propertyID : CSSPropertyInvalid; }
350 CSSValueID getValueID() const { return m_primitiveUnitType == CSS_VALUE_ID ? m_value.valueID : CSSValueInvalid; }
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100351
352 template<typename T> inline operator T() const; // Defined in CSSPrimitiveValueMappings.h
353
Bo Liuf91f5fa2014-05-01 10:37:55 -0700354 static const char* unitTypeToString(UnitTypes);
Torne (Richard Coles)bfe35902013-10-22 16:41:51 +0100355 String customCSSText(CSSTextFormattingFlags = QuoteCSSStringIfNeeded) const;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100356
357 bool isQuirkValue() { return m_isQuirkValue; }
358
Torne (Richard Coles)09380292014-02-21 12:17:33 +0000359 PassRefPtrWillBeRawPtr<CSSPrimitiveValue> cloneForCSSOM() const;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100360 void setCSSOMSafe() { m_isCSSOMSafe = true; }
361
362 bool equals(const CSSPrimitiveValue&) const;
363
Torne (Richard Coles)09380292014-02-21 12:17:33 +0000364 void traceAfterDispatch(Visitor*);
365
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100366 static UnitTypes canonicalUnitTypeForCategory(UnitCategory);
367 static double conversionToCanonicalUnitsScaleFactor(unsigned short unitType);
368
Ben Murdocha9984bf2014-04-10 11:22:39 +0100369 // Returns true and populates lengthUnitType, if unitType is a length unit. Otherwise, returns false.
370 static bool unitTypeToLengthUnitType(unsigned short unitType, LengthUnitType&);
371 static unsigned short lengthUnitTypeToUnitType(LengthUnitType);
372
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100373private:
Torne (Richard Coles)5267f702013-06-11 10:57:24 +0100374 CSSPrimitiveValue(CSSValueID);
375 CSSPrimitiveValue(CSSPropertyID);
Ben Murdoch07a852d2014-03-31 11:51:52 +0100376 // int vs. unsigned is too subtle to distinguish types, so require a UnitType.
377 CSSPrimitiveValue(int parserOperator, UnitTypes);
378 CSSPrimitiveValue(unsigned color, UnitTypes); // RGB value
Torne (Richard Coles)c0e19a62013-08-30 15:15:11 +0100379 CSSPrimitiveValue(const Length& length)
380 : CSSValue(PrimitiveClass)
381 {
382 init(length);
383 }
Torne (Richard Coles)1e202182013-10-18 15:46:42 +0100384 CSSPrimitiveValue(const Length&, float zoom);
Bo Liuf91f5fa2014-05-01 10:37:55 -0700385 CSSPrimitiveValue(const LengthSize&, const RenderStyle&);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100386 CSSPrimitiveValue(const String&, UnitTypes);
387 CSSPrimitiveValue(double, UnitTypes);
388
389 template<typename T> CSSPrimitiveValue(T); // Defined in CSSPrimitiveValueMappings.h
390 template<typename T> CSSPrimitiveValue(T* val)
391 : CSSValue(PrimitiveClass)
392 {
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +0000393 init(PassRefPtrWillBeRawPtr<T>(val));
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100394 }
395
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +0000396 template<typename T> CSSPrimitiveValue(PassRefPtrWillBeRawPtr<T> val)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100397 : CSSValue(PrimitiveClass)
398 {
399 init(val);
400 }
401
402 static void create(int); // compile-time guard
403 static void create(unsigned); // compile-time guard
404 template<typename T> operator T*(); // compile-time guard
405
Torne (Richard Coles)c0e19a62013-08-30 15:15:11 +0100406 void init(const Length&);
Bo Liuf91f5fa2014-05-01 10:37:55 -0700407 void init(const LengthSize&, const RenderStyle&);
Torne (Richard Coles)09380292014-02-21 12:17:33 +0000408 void init(PassRefPtrWillBeRawPtr<Counter>);
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +0000409 void init(PassRefPtrWillBeRawPtr<Rect>);
Torne (Richard Coles)09380292014-02-21 12:17:33 +0000410 void init(PassRefPtrWillBeRawPtr<Pair>);
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +0000411 void init(PassRefPtrWillBeRawPtr<Quad>);
412 void init(PassRefPtrWillBeRawPtr<CSSBasicShape>);
Torne (Richard Coles)09380292014-02-21 12:17:33 +0000413 void init(PassRefPtrWillBeRawPtr<CSSCalcValue>);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100414 bool getDoubleValueInternal(UnitTypes targetUnitType, double* result) const;
415
Torne (Richard Coles)a854de02013-12-18 16:25:25 +0000416 double computeLengthDouble(const CSSToLengthConversionData&);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100417
418 union {
Torne (Richard Coles)5267f702013-06-11 10:57:24 +0100419 CSSPropertyID propertyID;
420 CSSValueID valueID;
421 int parserOperator;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100422 double num;
423 StringImpl* string;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100424 unsigned rgbcolor;
Torne (Richard Coles)09380292014-02-21 12:17:33 +0000425 // FIXME: oilpan: Should be members, but no support for members in unions. Just trace the raw ptr for now.
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +0000426 CSSBasicShape* shape;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100427 CSSCalcValue* calc;
Torne (Richard Coles)09380292014-02-21 12:17:33 +0000428 Counter* counter;
429 Pair* pair;
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +0000430 Rect* rect;
431 Quad* quad;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100432 } m_value;
433};
434
Ben Murdocha9984bf2014-04-10 11:22:39 +0100435typedef CSSPrimitiveValue::CSSLengthArray CSSLengthArray;
436
Torne (Richard Coles)bfe35902013-10-22 16:41:51 +0100437DEFINE_CSS_VALUE_TYPE_CASTS(CSSPrimitiveValue, isPrimitiveValue());
Torne (Richard Coles)e5249552013-05-15 11:35:13 +0100438
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100439} // namespace WebCore
440
441#endif // CSSPrimitiveValue_h