blob: 3571cb053bbd9da22272a5cb85159cd04c27f59d [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"
28#include "core/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
35class CSSCalcValue;
36class Counter;
37class Pair;
38class Quad;
39class RGBColor;
40class Rect;
41class RenderStyle;
42class CSSBasicShape;
43
44struct Length;
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 next
48// integer value.
49template<typename T> inline T roundForImpreciseConversion(double value)
50{
51 value += (value < 0) ? -0.01 : +0.01;
52 return ((value > std::numeric_limits<T>::max()) || (value < std::numeric_limits<T>::min())) ? 0 : static_cast<T>(value);
53}
54
55template<> inline float roundForImpreciseConversion(double value)
56{
57 double ceiledValue = ceil(value);
58 double proximityToNextInt = ceiledValue - value;
59 if (proximityToNextInt <= 0.01 && value > 0)
60 return static_cast<float>(ceiledValue);
61 if (proximityToNextInt >= 0.99 && value < 0)
62 return static_cast<float>(floor(value));
63 return static_cast<float>(value);
64}
65
66class CSSPrimitiveValue : public CSSValue {
67public:
68 enum UnitTypes {
69 CSS_UNKNOWN = 0,
70 CSS_NUMBER = 1,
71 CSS_PERCENTAGE = 2,
72 CSS_EMS = 3,
73 CSS_EXS = 4,
74 CSS_PX = 5,
75 CSS_CM = 6,
76 CSS_MM = 7,
77 CSS_IN = 8,
78 CSS_PT = 9,
79 CSS_PC = 10,
80 CSS_DEG = 11,
81 CSS_RAD = 12,
82 CSS_GRAD = 13,
83 CSS_MS = 14,
84 CSS_S = 15,
85 CSS_HZ = 16,
86 CSS_KHZ = 17,
87 CSS_DIMENSION = 18,
88 CSS_STRING = 19,
89 CSS_URI = 20,
90 CSS_IDENT = 21,
91 CSS_ATTR = 22,
92 CSS_COUNTER = 23,
93 CSS_RECT = 24,
94 CSS_RGBCOLOR = 25,
95 // From CSS Values and Units. Viewport-percentage Lengths (vw/vh/vmin/vmax).
96 CSS_VW = 26,
97 CSS_VH = 27,
98 CSS_VMIN = 28,
99 CSS_VMAX = 29,
100 CSS_DPPX = 30,
101 CSS_DPI = 31,
102 CSS_DPCM = 32,
103 CSS_FR = 33,
104 CSS_PAIR = 100, // We envision this being exposed as a means of getting computed style values for pairs (border-spacing/radius, background-position, etc.)
105 CSS_UNICODE_RANGE = 102,
106
107 // These next types are just used internally to allow us to translate back and forth from CSSPrimitiveValues to CSSParserValues.
108 CSS_PARSER_OPERATOR = 103,
109 CSS_PARSER_INTEGER = 104,
110 CSS_PARSER_HEXCOLOR = 105,
111
112 // This is used internally for unknown identifiers
113 CSS_PARSER_IDENTIFIER = 106,
114
115 // These are from CSS3 Values and Units, but that isn't a finished standard yet
116 CSS_TURN = 107,
117 CSS_REMS = 108,
118 CSS_CHS = 109,
119
120 // This is used internally for counter names (as opposed to counter values)
121 CSS_COUNTER_NAME = 110,
122
Ben Murdoch591b9582013-07-10 11:41:44 +0100123 // This is used by the CSS Shapes draft
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100124 CSS_SHAPE = 111,
125
126 // Used by border images.
127 CSS_QUAD = 112,
128
129 CSS_CALC = 113,
130 CSS_CALC_PERCENTAGE_WITH_NUMBER = 114,
131 CSS_CALC_PERCENTAGE_WITH_LENGTH = 115,
132 CSS_VARIABLE_NAME = 116,
Torne (Richard Coles)5267f702013-06-11 10:57:24 +0100133
134 CSS_PROPERTY_ID = 117,
135 CSS_VALUE_ID = 118
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100136 };
137
138 // This enum follows the CSSParser::Units enum augmented with UNIT_FREQUENCY for frequencies.
139 enum UnitCategory {
140 UNumber,
141 UPercent,
142 ULength,
143 UAngle,
144 UTime,
145 UFrequency,
146 UViewportPercentageLength,
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100147 UResolution,
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100148 UOther
149 };
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100150 static UnitCategory unitCategory(CSSPrimitiveValue::UnitTypes);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100151
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 }
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100169 bool isLength() const
170 {
171 unsigned short type = primitiveType();
172 return (type >= CSS_EMS && type <= CSS_PC) || type == CSS_REMS || type == CSS_CHS;
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; }
Torne (Richard Coles)e5249552013-05-15 11:35:13 +0100189 bool isResolution() const
190 {
191 unsigned short type = primitiveType();
192 return type >= CSS_DPPX && type <= CSS_DPCM;
193 }
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100194 bool isVariableName() const { return primitiveType() == CSS_VARIABLE_NAME; }
195 bool isViewportPercentageLength() const { return m_primitiveUnitType >= CSS_VW && m_primitiveUnitType <= CSS_VMAX; }
196 bool isFlex() const { return primitiveType() == CSS_FR; }
Torne (Richard Coles)5267f702013-06-11 10:57:24 +0100197 bool isValueID() const { return m_primitiveUnitType == CSS_VALUE_ID; }
Ben Murdoch591b9582013-07-10 11:41:44 +0100198 bool colorIsDerivedFromElement() const;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100199
Torne (Richard Coles)5267f702013-06-11 10:57:24 +0100200 static PassRefPtr<CSSPrimitiveValue> createIdentifier(CSSValueID valueID) { return adoptRef(new CSSPrimitiveValue(valueID)); }
201 static PassRefPtr<CSSPrimitiveValue> createIdentifier(CSSPropertyID propertyID) { return adoptRef(new CSSPrimitiveValue(propertyID)); }
202 static PassRefPtr<CSSPrimitiveValue> createParserOperator(int parserOperator) { return adoptRef(new CSSPrimitiveValue(parserOperator)); }
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100203 static PassRefPtr<CSSPrimitiveValue> createColor(unsigned rgbValue) { return adoptRef(new CSSPrimitiveValue(rgbValue)); }
204 static PassRefPtr<CSSPrimitiveValue> create(double value, UnitTypes type) { return adoptRef(new CSSPrimitiveValue(value, type)); }
205 static PassRefPtr<CSSPrimitiveValue> create(const String& value, UnitTypes type) { return adoptRef(new CSSPrimitiveValue(value, type)); }
206
207 template<typename T> static PassRefPtr<CSSPrimitiveValue> create(T value)
208 {
209 return adoptRef(new CSSPrimitiveValue(value));
210 }
211
212 // This value is used to handle quirky margins in reflow roots (body, td, and th) like WinIE.
213 // The basic idea is that a stylesheet can use the value __qem (for quirky em) instead of em.
214 // When the quirky value is used, if you're in quirks mode, the margin will collapse away
215 // inside a table cell.
216 static PassRefPtr<CSSPrimitiveValue> createAllowingMarginQuirk(double value, UnitTypes type)
217 {
218 CSSPrimitiveValue* quirkValue = new CSSPrimitiveValue(value, type);
219 quirkValue->m_isQuirkValue = true;
220 return adoptRef(quirkValue);
221 }
222
223 ~CSSPrimitiveValue();
224
225 void cleanup();
226
227 unsigned short primitiveType() const;
228
229 double computeDegrees();
230
231 enum TimeUnit { Seconds, Milliseconds };
232 template <typename T, TimeUnit timeUnit> T computeTime()
233 {
234 if (timeUnit == Seconds && m_primitiveUnitType == CSS_S)
235 return getValue<T>();
236 if (timeUnit == Seconds && m_primitiveUnitType == CSS_MS)
237 return getValue<T>() / 1000;
238 if (timeUnit == Milliseconds && m_primitiveUnitType == CSS_MS)
239 return getValue<T>();
240 if (timeUnit == Milliseconds && m_primitiveUnitType == CSS_S)
241 return getValue<T>() * 1000;
242 ASSERT_NOT_REACHED();
243 return 0;
244 }
245
246 /*
247 * computes a length in pixels out of the given CSSValue. Need the RenderStyle to get
248 * the fontinfo in case val is defined in em or ex.
249 *
250 * The metrics have to be a bit different for screen and printer output.
251 * For screen output we assume 1 inch == 72 px, for printer we assume 300 dpi
252 *
253 * this is screen/printer dependent, so we probably need a config option for this,
254 * and some tool to calibrate.
255 */
256 template<typename T> T computeLength(RenderStyle* currStyle, RenderStyle* rootStyle, float multiplier = 1.0f, bool computingFontSize = false);
257
258 // Converts to a Length, mapping various unit types appropriately.
259 template<int> Length convertToLength(RenderStyle* currStyle, RenderStyle* rootStyle, double multiplier = 1.0, bool computingFontSize = false);
260
261 // use with care!!!
262 void setPrimitiveType(unsigned short type) { m_primitiveUnitType = type; }
263
264 double getDoubleValue(unsigned short unitType, ExceptionCode&) const;
265 double getDoubleValue(unsigned short unitType) const;
266 double getDoubleValue() const;
267
268 void setFloatValue(unsigned short unitType, double floatValue, ExceptionCode&);
269 float getFloatValue(unsigned short unitType, ExceptionCode& ec) const { return getValue<float>(unitType, ec); }
270 float getFloatValue(unsigned short unitType) const { return getValue<float>(unitType); }
271 float getFloatValue() const { return getValue<float>(); }
272
273 int getIntValue(unsigned short unitType, ExceptionCode& ec) const { return getValue<int>(unitType, ec); }
274 int getIntValue(unsigned short unitType) const { return getValue<int>(unitType); }
275 int getIntValue() const { return getValue<int>(); }
276
277 template<typename T> inline T getValue(unsigned short unitType, ExceptionCode& ec) const { return clampTo<T>(getDoubleValue(unitType, ec)); }
278 template<typename T> inline T getValue(unsigned short unitType) const { return clampTo<T>(getDoubleValue(unitType)); }
279 template<typename T> inline T getValue() const { return clampTo<T>(getDoubleValue()); }
280
281 void setStringValue(unsigned short stringType, const String& stringValue, ExceptionCode&);
282 String getStringValue(ExceptionCode&) const;
283 String getStringValue() const;
284
285 Counter* getCounterValue(ExceptionCode&) const;
286 Counter* getCounterValue() const { return m_primitiveUnitType != CSS_COUNTER ? 0 : m_value.counter; }
287
288 Rect* getRectValue(ExceptionCode&) const;
289 Rect* getRectValue() const { return m_primitiveUnitType != CSS_RECT ? 0 : m_value.rect; }
290
291 Quad* getQuadValue(ExceptionCode&) const;
292 Quad* getQuadValue() const { return m_primitiveUnitType != CSS_QUAD ? 0 : m_value.quad; }
293
294 PassRefPtr<RGBColor> getRGBColorValue(ExceptionCode&) const;
295 RGBA32 getRGBA32Value() const { return m_primitiveUnitType != CSS_RGBCOLOR ? 0 : m_value.rgbcolor; }
296
297 Pair* getPairValue(ExceptionCode&) const;
298 Pair* getPairValue() const { return m_primitiveUnitType != CSS_PAIR ? 0 : m_value.pair; }
299
300 CSSBasicShape* getShapeValue() const { return m_primitiveUnitType != CSS_SHAPE ? 0 : m_value.shape; }
301
302 CSSCalcValue* cssCalcValue() const { return m_primitiveUnitType != CSS_CALC ? 0 : m_value.calc; }
303
Torne (Richard Coles)5267f702013-06-11 10:57:24 +0100304 CSSPropertyID getPropertyID() const { return m_primitiveUnitType == CSS_PROPERTY_ID ? m_value.propertyID : CSSPropertyInvalid; }
305 CSSValueID getValueID() const { return m_primitiveUnitType == CSS_VALUE_ID ? m_value.valueID : CSSValueInvalid; }
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100306
307 template<typename T> inline operator T() const; // Defined in CSSPrimitiveValueMappings.h
308
Ben Murdoch591b9582013-07-10 11:41:44 +0100309 String customCssText(CssTextFormattingFlags = QuoteCSSStringIfNeeded) const;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100310 String customSerializeResolvingVariables(const HashMap<AtomicString, String>&) const;
311 bool hasVariableReference() const;
312
313 bool isQuirkValue() { return m_isQuirkValue; }
314
315 void addSubresourceStyleURLs(ListHashSet<KURL>&, const StyleSheetContents*) const;
316
317 Length viewportPercentageLength();
318
319 PassRefPtr<CSSPrimitiveValue> cloneForCSSOM() const;
320 void setCSSOMSafe() { m_isCSSOMSafe = true; }
321
322 bool equals(const CSSPrimitiveValue&) const;
323
324 void reportDescendantMemoryUsage(MemoryObjectInfo*) const;
325
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100326 static UnitTypes canonicalUnitTypeForCategory(UnitCategory);
327 static double conversionToCanonicalUnitsScaleFactor(unsigned short unitType);
328
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100329private:
Torne (Richard Coles)5267f702013-06-11 10:57:24 +0100330 CSSPrimitiveValue(CSSValueID);
331 CSSPrimitiveValue(CSSPropertyID);
332 // FIXME: int vs. unsigned overloading is too subtle to distinguish the color and operator cases.
333 CSSPrimitiveValue(int parserOperator);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100334 CSSPrimitiveValue(unsigned color); // RGB value
335 CSSPrimitiveValue(const Length&);
336 CSSPrimitiveValue(const String&, UnitTypes);
337 CSSPrimitiveValue(double, UnitTypes);
338
339 template<typename T> CSSPrimitiveValue(T); // Defined in CSSPrimitiveValueMappings.h
340 template<typename T> CSSPrimitiveValue(T* val)
341 : CSSValue(PrimitiveClass)
342 {
343 init(PassRefPtr<T>(val));
344 }
345
346 template<typename T> CSSPrimitiveValue(PassRefPtr<T> val)
347 : CSSValue(PrimitiveClass)
348 {
349 init(val);
350 }
351
352 static void create(int); // compile-time guard
353 static void create(unsigned); // compile-time guard
354 template<typename T> operator T*(); // compile-time guard
355
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100356 void init(PassRefPtr<Counter>);
357 void init(PassRefPtr<Rect>);
358 void init(PassRefPtr<Pair>);
359 void init(PassRefPtr<Quad>);
360 void init(PassRefPtr<CSSBasicShape>);
361 void init(PassRefPtr<CSSCalcValue>);
362 bool getDoubleValueInternal(UnitTypes targetUnitType, double* result) const;
363
364 double computeLengthDouble(RenderStyle* currentStyle, RenderStyle* rootStyle, float multiplier, bool computingFontSize);
365
366 union {
Torne (Richard Coles)5267f702013-06-11 10:57:24 +0100367 CSSPropertyID propertyID;
368 CSSValueID valueID;
369 int parserOperator;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100370 double num;
371 StringImpl* string;
372 Counter* counter;
373 Rect* rect;
374 Quad* quad;
375 unsigned rgbcolor;
376 Pair* pair;
377 CSSBasicShape* shape;
378 CSSCalcValue* calc;
379 } m_value;
380};
381
Torne (Richard Coles)e5249552013-05-15 11:35:13 +0100382inline CSSPrimitiveValue* toCSSPrimitiveValue(CSSValue* value)
383{
384 ASSERT_WITH_SECURITY_IMPLICATION(!value || value->isPrimitiveValue());
385 return static_cast<CSSPrimitiveValue*>(value);
386}
387
388inline const CSSPrimitiveValue* toCSSPrimitiveValue(const CSSValue* value)
389{
390 ASSERT_WITH_SECURITY_IMPLICATION(!value || value->isPrimitiveValue());
391 return static_cast<const CSSPrimitiveValue*>(value);
392}
393
394// Catch unneeded cast.
395void toCSSPrimitiveValue(const CSSPrimitiveValue*);
396
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100397} // namespace WebCore
398
399#endif // CSSPrimitiveValue_h