blob: dc3dc3071ad5f30a6dc0570f0200647c1a2b0f57 [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,
142#if ENABLE(RESOLUTION_MEDIA_QUERY)
143 UResolution,
144#endif
145 UOther
146 };
147
148 bool isAngle() const
149 {
150 return m_primitiveUnitType == CSS_DEG
151 || m_primitiveUnitType == CSS_RAD
152 || m_primitiveUnitType == CSS_GRAD
153 || m_primitiveUnitType == CSS_TURN;
154 }
155 bool isAttr() const { return m_primitiveUnitType == CSS_ATTR; }
156 bool isCounter() const { return m_primitiveUnitType == CSS_COUNTER; }
157 bool isFontIndependentLength() const { return m_primitiveUnitType >= CSS_PX && m_primitiveUnitType <= CSS_PC; }
158 bool isFontRelativeLength() const
159 {
160 return m_primitiveUnitType == CSS_EMS
161 || m_primitiveUnitType == CSS_EXS
162 || m_primitiveUnitType == CSS_REMS
163 || m_primitiveUnitType == CSS_CHS;
164 }
165 bool isIdent() const { return m_primitiveUnitType == CSS_IDENT; }
166 bool isLength() const
167 {
168 unsigned short type = primitiveType();
169 return (type >= CSS_EMS && type <= CSS_PC) || type == CSS_REMS || type == CSS_CHS;
170 }
171 bool isNumber() const { return primitiveType() == CSS_NUMBER; }
172 bool isPercentage() const { return primitiveType() == CSS_PERCENTAGE; }
173 bool isPx() const { return primitiveType() == CSS_PX; }
174 bool isRect() const { return m_primitiveUnitType == CSS_RECT; }
175 bool isRGBColor() const { return m_primitiveUnitType == CSS_RGBCOLOR; }
176 bool isShape() const { return m_primitiveUnitType == CSS_SHAPE; }
177 bool isString() const { return m_primitiveUnitType == CSS_STRING; }
178 bool isTime() const { return m_primitiveUnitType == CSS_S || m_primitiveUnitType == CSS_MS; }
179 bool isURI() const { return m_primitiveUnitType == CSS_URI; }
180 bool isCalculated() const { return m_primitiveUnitType == CSS_CALC; }
181 bool isCalculatedPercentageWithNumber() const { return primitiveType() == CSS_CALC_PERCENTAGE_WITH_NUMBER; }
182 bool isCalculatedPercentageWithLength() const { return primitiveType() == CSS_CALC_PERCENTAGE_WITH_LENGTH; }
183 bool isDotsPerInch() const { return primitiveType() == CSS_DPI; }
184 bool isDotsPerPixel() const { return primitiveType() == CSS_DPPX; }
185 bool isDotsPerCentimeter() const { return primitiveType() == CSS_DPCM; }
186 bool isVariableName() const { return primitiveType() == CSS_VARIABLE_NAME; }
187 bool isViewportPercentageLength() const { return m_primitiveUnitType >= CSS_VW && m_primitiveUnitType <= CSS_VMAX; }
188 bool isFlex() const { return primitiveType() == CSS_FR; }
189
190 static PassRefPtr<CSSPrimitiveValue> createIdentifier(int identifier) { return adoptRef(new CSSPrimitiveValue(identifier)); }
191 static PassRefPtr<CSSPrimitiveValue> createColor(unsigned rgbValue) { return adoptRef(new CSSPrimitiveValue(rgbValue)); }
192 static PassRefPtr<CSSPrimitiveValue> create(double value, UnitTypes type) { return adoptRef(new CSSPrimitiveValue(value, type)); }
193 static PassRefPtr<CSSPrimitiveValue> create(const String& value, UnitTypes type) { return adoptRef(new CSSPrimitiveValue(value, type)); }
194
195 template<typename T> static PassRefPtr<CSSPrimitiveValue> create(T value)
196 {
197 return adoptRef(new CSSPrimitiveValue(value));
198 }
199
200 // This value is used to handle quirky margins in reflow roots (body, td, and th) like WinIE.
201 // The basic idea is that a stylesheet can use the value __qem (for quirky em) instead of em.
202 // When the quirky value is used, if you're in quirks mode, the margin will collapse away
203 // inside a table cell.
204 static PassRefPtr<CSSPrimitiveValue> createAllowingMarginQuirk(double value, UnitTypes type)
205 {
206 CSSPrimitiveValue* quirkValue = new CSSPrimitiveValue(value, type);
207 quirkValue->m_isQuirkValue = true;
208 return adoptRef(quirkValue);
209 }
210
211 ~CSSPrimitiveValue();
212
213 void cleanup();
214
215 unsigned short primitiveType() const;
216
217 double computeDegrees();
218
219 enum TimeUnit { Seconds, Milliseconds };
220 template <typename T, TimeUnit timeUnit> T computeTime()
221 {
222 if (timeUnit == Seconds && m_primitiveUnitType == CSS_S)
223 return getValue<T>();
224 if (timeUnit == Seconds && m_primitiveUnitType == CSS_MS)
225 return getValue<T>() / 1000;
226 if (timeUnit == Milliseconds && m_primitiveUnitType == CSS_MS)
227 return getValue<T>();
228 if (timeUnit == Milliseconds && m_primitiveUnitType == CSS_S)
229 return getValue<T>() * 1000;
230 ASSERT_NOT_REACHED();
231 return 0;
232 }
233
234 /*
235 * computes a length in pixels out of the given CSSValue. Need the RenderStyle to get
236 * the fontinfo in case val is defined in em or ex.
237 *
238 * The metrics have to be a bit different for screen and printer output.
239 * For screen output we assume 1 inch == 72 px, for printer we assume 300 dpi
240 *
241 * this is screen/printer dependent, so we probably need a config option for this,
242 * and some tool to calibrate.
243 */
244 template<typename T> T computeLength(RenderStyle* currStyle, RenderStyle* rootStyle, float multiplier = 1.0f, bool computingFontSize = false);
245
246 // Converts to a Length, mapping various unit types appropriately.
247 template<int> Length convertToLength(RenderStyle* currStyle, RenderStyle* rootStyle, double multiplier = 1.0, bool computingFontSize = false);
248
249 // use with care!!!
250 void setPrimitiveType(unsigned short type) { m_primitiveUnitType = type; }
251
252 double getDoubleValue(unsigned short unitType, ExceptionCode&) const;
253 double getDoubleValue(unsigned short unitType) const;
254 double getDoubleValue() const;
255
256 void setFloatValue(unsigned short unitType, double floatValue, ExceptionCode&);
257 float getFloatValue(unsigned short unitType, ExceptionCode& ec) const { return getValue<float>(unitType, ec); }
258 float getFloatValue(unsigned short unitType) const { return getValue<float>(unitType); }
259 float getFloatValue() const { return getValue<float>(); }
260
261 int getIntValue(unsigned short unitType, ExceptionCode& ec) const { return getValue<int>(unitType, ec); }
262 int getIntValue(unsigned short unitType) const { return getValue<int>(unitType); }
263 int getIntValue() const { return getValue<int>(); }
264
265 template<typename T> inline T getValue(unsigned short unitType, ExceptionCode& ec) const { return clampTo<T>(getDoubleValue(unitType, ec)); }
266 template<typename T> inline T getValue(unsigned short unitType) const { return clampTo<T>(getDoubleValue(unitType)); }
267 template<typename T> inline T getValue() const { return clampTo<T>(getDoubleValue()); }
268
269 void setStringValue(unsigned short stringType, const String& stringValue, ExceptionCode&);
270 String getStringValue(ExceptionCode&) const;
271 String getStringValue() const;
272
273 Counter* getCounterValue(ExceptionCode&) const;
274 Counter* getCounterValue() const { return m_primitiveUnitType != CSS_COUNTER ? 0 : m_value.counter; }
275
276 Rect* getRectValue(ExceptionCode&) const;
277 Rect* getRectValue() const { return m_primitiveUnitType != CSS_RECT ? 0 : m_value.rect; }
278
279 Quad* getQuadValue(ExceptionCode&) const;
280 Quad* getQuadValue() const { return m_primitiveUnitType != CSS_QUAD ? 0 : m_value.quad; }
281
282 PassRefPtr<RGBColor> getRGBColorValue(ExceptionCode&) const;
283 RGBA32 getRGBA32Value() const { return m_primitiveUnitType != CSS_RGBCOLOR ? 0 : m_value.rgbcolor; }
284
285 Pair* getPairValue(ExceptionCode&) const;
286 Pair* getPairValue() const { return m_primitiveUnitType != CSS_PAIR ? 0 : m_value.pair; }
287
288 CSSBasicShape* getShapeValue() const { return m_primitiveUnitType != CSS_SHAPE ? 0 : m_value.shape; }
289
290 CSSCalcValue* cssCalcValue() const { return m_primitiveUnitType != CSS_CALC ? 0 : m_value.calc; }
291
292 int getIdent() const { return m_primitiveUnitType == CSS_IDENT ? m_value.ident : 0; }
293
294 template<typename T> inline operator T() const; // Defined in CSSPrimitiveValueMappings.h
295
296 String customCssText() const;
297 String customSerializeResolvingVariables(const HashMap<AtomicString, String>&) const;
298 bool hasVariableReference() const;
299
300 bool isQuirkValue() { return m_isQuirkValue; }
301
302 void addSubresourceStyleURLs(ListHashSet<KURL>&, const StyleSheetContents*) const;
303
304 Length viewportPercentageLength();
305
306 PassRefPtr<CSSPrimitiveValue> cloneForCSSOM() const;
307 void setCSSOMSafe() { m_isCSSOMSafe = true; }
308
309 bool equals(const CSSPrimitiveValue&) const;
310
311 void reportDescendantMemoryUsage(MemoryObjectInfo*) const;
312
313private:
314 // FIXME: int vs. unsigned overloading is too subtle to distinguish the color and identifier cases.
315 CSSPrimitiveValue(int ident);
316 CSSPrimitiveValue(unsigned color); // RGB value
317 CSSPrimitiveValue(const Length&);
318 CSSPrimitiveValue(const String&, UnitTypes);
319 CSSPrimitiveValue(double, UnitTypes);
320
321 template<typename T> CSSPrimitiveValue(T); // Defined in CSSPrimitiveValueMappings.h
322 template<typename T> CSSPrimitiveValue(T* val)
323 : CSSValue(PrimitiveClass)
324 {
325 init(PassRefPtr<T>(val));
326 }
327
328 template<typename T> CSSPrimitiveValue(PassRefPtr<T> val)
329 : CSSValue(PrimitiveClass)
330 {
331 init(val);
332 }
333
334 static void create(int); // compile-time guard
335 static void create(unsigned); // compile-time guard
336 template<typename T> operator T*(); // compile-time guard
337
338 static UnitTypes canonicalUnitTypeForCategory(UnitCategory category);
339
340 void init(PassRefPtr<Counter>);
341 void init(PassRefPtr<Rect>);
342 void init(PassRefPtr<Pair>);
343 void init(PassRefPtr<Quad>);
344 void init(PassRefPtr<CSSBasicShape>);
345 void init(PassRefPtr<CSSCalcValue>);
346 bool getDoubleValueInternal(UnitTypes targetUnitType, double* result) const;
347
348 double computeLengthDouble(RenderStyle* currentStyle, RenderStyle* rootStyle, float multiplier, bool computingFontSize);
349
350 union {
351 int ident;
352 double num;
353 StringImpl* string;
354 Counter* counter;
355 Rect* rect;
356 Quad* quad;
357 unsigned rgbcolor;
358 Pair* pair;
359 CSSBasicShape* shape;
360 CSSCalcValue* calc;
361 } m_value;
362};
363
364} // namespace WebCore
365
366#endif // CSSPrimitiveValue_h