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