OLD | NEW |
---|---|
1 /* | 1 /* |
2 * (C) 1999-2003 Lars Knoll (knoll@kde.org) | 2 * (C) 1999-2003 Lars Knoll (knoll@kde.org) |
3 * Copyright (C) 2004, 2005, 2006, 2008 Apple Inc. All rights reserved. | 3 * Copyright (C) 2004, 2005, 2006, 2008 Apple Inc. All rights reserved. |
4 * Copyright (C) 2007 Alexey Proskuryakov <ap@webkit.org> | 4 * Copyright (C) 2007 Alexey Proskuryakov <ap@webkit.org> |
5 * | 5 * |
6 * This library is free software; you can redistribute it and/or | 6 * This library is free software; you can redistribute it and/or |
7 * modify it under the terms of the GNU Library General Public | 7 * modify it under the terms of the GNU Library General Public |
8 * License as published by the Free Software Foundation; either | 8 * License as published by the Free Software Foundation; either |
9 * version 2 of the License, or (at your option) any later version. | 9 * version 2 of the License, or (at your option) any later version. |
10 * | 10 * |
(...skipping 11 matching lines...) Expand all Loading... | |
22 #ifndef CSSPrimitiveValue_h | 22 #ifndef CSSPrimitiveValue_h |
23 #define CSSPrimitiveValue_h | 23 #define CSSPrimitiveValue_h |
24 | 24 |
25 #include "core/CSSPropertyNames.h" | 25 #include "core/CSSPropertyNames.h" |
26 #include "core/CSSValueKeywords.h" | 26 #include "core/CSSValueKeywords.h" |
27 #include "core/CoreExport.h" | 27 #include "core/CoreExport.h" |
28 #include "core/css/CSSValue.h" | 28 #include "core/css/CSSValue.h" |
29 #include "wtf/BitVector.h" | 29 #include "wtf/BitVector.h" |
30 #include "wtf/Forward.h" | 30 #include "wtf/Forward.h" |
31 #include "wtf/MathExtras.h" | 31 #include "wtf/MathExtras.h" |
32 #include "wtf/TypeTraits.h" | |
33 #include "wtf/text/StringHash.h" | 32 #include "wtf/text/StringHash.h" |
34 #include "wtf/text/StringView.h" | 33 #include "wtf/text/StringView.h" |
35 | 34 |
36 namespace blink { | 35 namespace blink { |
37 | 36 |
38 class CSSCalcValue; | 37 class CSSCalcValue; |
39 class CSSToLengthConversionData; | 38 class CSSToLengthConversionData; |
40 class Length; | 39 class Length; |
41 | 40 |
42 // Dimension calculations are imprecise, often resulting in values of e.g. | 41 // Dimension calculations are imprecise, often resulting in values of e.g. |
43 // 44.99998. We need to go ahead and round if we're really close to the next | 42 // 44.99998. We need to go ahead and round if we're really close to the next |
44 // integer value. | 43 // integer value. |
45 template<typename T> inline T roundForImpreciseConversion(double value) | 44 template<typename T> inline T roundForImpreciseConversion(double value) |
46 { | 45 { |
47 value += (value < 0) ? -0.01 : +0.01; | 46 value += (value < 0) ? -0.01 : +0.01; |
48 return ((value > std::numeric_limits<T>::max()) || (value < std::numeric_lim its<T>::min())) ? 0 : static_cast<T>(value); | 47 return ((value > std::numeric_limits<T>::max()) || (value < std::numeric_lim its<T>::min())) ? 0 : static_cast<T>(value); |
49 } | 48 } |
50 | 49 |
51 template<> inline float roundForImpreciseConversion(double value) | 50 template<> inline float roundForImpreciseConversion(double value) |
52 { | 51 { |
53 double ceiledValue = ceil(value); | 52 double ceiledValue = ceil(value); |
54 double proximityToNextInt = ceiledValue - value; | 53 double proximityToNextInt = ceiledValue - value; |
55 if (proximityToNextInt <= 0.01 && value > 0) | 54 if (proximityToNextInt <= 0.01 && value > 0) |
56 return static_cast<float>(ceiledValue); | 55 return static_cast<float>(ceiledValue); |
57 if (proximityToNextInt >= 0.99 && value < 0) | 56 if (proximityToNextInt >= 0.99 && value < 0) |
58 return static_cast<float>(floor(value)); | 57 return static_cast<float>(floor(value)); |
59 return static_cast<float>(value); | 58 return static_cast<float>(value); |
60 } | 59 } |
61 | 60 |
62 // CSSPrimitiveValues are immutable. This class has manual ref-counting | 61 // CSSPrimitiveValue stores both numbers/lengths (e.g. 1, 10px, 4%) and calc() |
Timothy Loh
2016/09/30 05:44:35
4% isn't a length, also it stores other values lik
sashab
2016/09/30 06:45:48
Done
| |
63 // of unioned types and does not have the code necessary | 62 // values (e.g. calc(3px + 2em)). |
64 // to handle any kind of mutations. | 63 // TODO(sashab): Split calc functionality out of CSSPrimitiveValue. |
Timothy Loh
2016/09/30 05:44:35
I think calc makes sense to be in CSSPrimtiveValue
sashab
2016/09/30 06:45:48
Done
| |
65 class CORE_EXPORT CSSPrimitiveValue : public CSSValue { | 64 class CORE_EXPORT CSSPrimitiveValue : public CSSValue { |
66 public: | 65 public: |
67 // These units are iterated through, so be careful when adding or changing t he order. | 66 // These units are iterated through, so be careful when adding or changing t he order. |
68 enum class UnitType { | 67 enum class UnitType { |
69 Unknown, | 68 Unknown, |
70 Number, | 69 Number, |
71 Percentage, | 70 Percentage, |
72 // Length units | 71 // Length units |
73 Ems, | 72 Ems, |
74 Exs, | 73 Exs, |
(...skipping 25 matching lines...) Expand all Loading... | |
100 DotsPerInch, | 99 DotsPerInch, |
101 DotsPerCentimeter, | 100 DotsPerCentimeter, |
102 // Other units | 101 // Other units |
103 Fraction, | 102 Fraction, |
104 Integer, | 103 Integer, |
105 Calc, | 104 Calc, |
106 CalcPercentageWithNumber, | 105 CalcPercentageWithNumber, |
107 CalcPercentageWithLength, | 106 CalcPercentageWithLength, |
108 CalcLengthWithNumber, | 107 CalcLengthWithNumber, |
109 CalcPercentageWithLengthAndNumber, | 108 CalcPercentageWithLengthAndNumber, |
110 ValueID, | |
111 | 109 |
112 // This value is used to handle quirky margins in reflow roots (body, td , and th) like WinIE. | 110 // This value is used to handle quirky margins in reflow roots (body, td , and th) like WinIE. |
113 // The basic idea is that a stylesheet can use the value __qem (for quir ky em) instead of em. | 111 // The basic idea is that a stylesheet can use the value __qem (for quir ky em) instead of em. |
114 // When the quirky value is used, if you're in quirks mode, the margin w ill collapse away | 112 // When the quirky value is used, if you're in quirks mode, the margin w ill collapse away |
115 // inside a table cell. This quirk is specified in the HTML spec but our impl is different. | 113 // inside a table cell. This quirk is specified in the HTML spec but our impl is different. |
116 // TODO: Remove this. crbug.com/443952 | 114 // TODO: Remove this. crbug.com/443952 |
117 QuirkyEms, | 115 QuirkyEms, |
118 }; | 116 }; |
119 | 117 |
120 enum LengthUnitType { | 118 enum LengthUnitType { |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
195 bool isLength() const { return isLength(typeWithCalcResolved()); } | 193 bool isLength() const { return isLength(typeWithCalcResolved()); } |
196 bool isNumber() const { return typeWithCalcResolved() == UnitType::Number || typeWithCalcResolved() == UnitType::Integer; } | 194 bool isNumber() const { return typeWithCalcResolved() == UnitType::Number || typeWithCalcResolved() == UnitType::Integer; } |
197 bool isPercentage() const { return typeWithCalcResolved() == UnitType::Perce ntage; } | 195 bool isPercentage() const { return typeWithCalcResolved() == UnitType::Perce ntage; } |
198 bool isPx() const { return typeWithCalcResolved() == UnitType::Pixels; } | 196 bool isPx() const { return typeWithCalcResolved() == UnitType::Pixels; } |
199 bool isTime() const { return type() == UnitType::Seconds || type() == UnitTy pe::Milliseconds; } | 197 bool isTime() const { return type() == UnitType::Seconds || type() == UnitTy pe::Milliseconds; } |
200 bool isCalculated() const { return type() == UnitType::Calc; } | 198 bool isCalculated() const { return type() == UnitType::Calc; } |
201 bool isCalculatedPercentageWithNumber() const { return typeWithCalcResolved( ) == UnitType::CalcPercentageWithNumber; } | 199 bool isCalculatedPercentageWithNumber() const { return typeWithCalcResolved( ) == UnitType::CalcPercentageWithNumber; } |
202 bool isCalculatedPercentageWithLength() const { return typeWithCalcResolved( ) == UnitType::CalcPercentageWithLength; } | 200 bool isCalculatedPercentageWithLength() const { return typeWithCalcResolved( ) == UnitType::CalcPercentageWithLength; } |
203 static bool isResolution(UnitType type) { return type >= UnitType::DotsPerPi xel && type <= UnitType::DotsPerCentimeter; } | 201 static bool isResolution(UnitType type) { return type >= UnitType::DotsPerPi xel && type <= UnitType::DotsPerCentimeter; } |
204 bool isFlex() const { return typeWithCalcResolved() == UnitType::Fraction; } | 202 bool isFlex() const { return typeWithCalcResolved() == UnitType::Fraction; } |
205 bool isValueID() const { return type() == UnitType::ValueID; } | |
206 bool colorIsDerivedFromElement() const; | |
207 | 203 |
208 static CSSPrimitiveValue* createIdentifier(CSSValueID); | |
209 static CSSPrimitiveValue* create(double value, UnitType); | 204 static CSSPrimitiveValue* create(double value, UnitType); |
210 static CSSPrimitiveValue* create(const Length& value, float zoom) | 205 static CSSPrimitiveValue* create(const Length& value, float zoom) |
211 { | 206 { |
212 return new CSSPrimitiveValue(value, zoom); | 207 return new CSSPrimitiveValue(value, zoom); |
213 } | 208 } |
209 | |
210 // TODO(sashab): Remove this. | |
214 template<typename T> static CSSPrimitiveValue* create(T value) | 211 template<typename T> static CSSPrimitiveValue* create(T value) |
215 { | 212 { |
216 static_assert(!std::is_same<T, CSSValueID>::value, "Do not call create() with a CSSValueID; call createIdentifier() instead"); | |
217 return new CSSPrimitiveValue(value); | 213 return new CSSPrimitiveValue(value); |
218 } | 214 } |
219 | 215 |
220 ~CSSPrimitiveValue(); | 216 ~CSSPrimitiveValue(); |
221 | 217 |
222 UnitType typeWithCalcResolved() const; | 218 UnitType typeWithCalcResolved() const; |
223 | 219 |
224 double computeDegrees() const; | 220 double computeDegrees() const; |
225 double computeSeconds() const; | 221 double computeSeconds() const; |
226 | 222 |
227 // Computes a length in pixels, resolving relative lengths | 223 // Computes a length in pixels, resolving relative lengths |
228 template<typename T> T computeLength(const CSSToLengthConversionData&) const ; | 224 template<typename T> T computeLength(const CSSToLengthConversionData&) const ; |
229 | 225 |
230 // Converts to a Length (Fixed, Percent or Calculated) | 226 // Converts to a Length (Fixed, Percent or Calculated) |
231 Length convertToLength(const CSSToLengthConversionData&) const; | 227 Length convertToLength(const CSSToLengthConversionData&) const; |
232 | 228 |
233 double getDoubleValue() const; | 229 double getDoubleValue() const; |
234 float getFloatValue() const { return getValue<float>(); } | 230 float getFloatValue() const { return getValue<float>(); } |
235 int getIntValue() const { return getValue<int>(); } | 231 int getIntValue() const { return getValue<int>(); } |
236 template<typename T> inline T getValue() const { return clampTo<T>(getDouble Value()); } | 232 template<typename T> inline T getValue() const { return clampTo<T>(getDouble Value()); } |
237 | 233 |
238 CSSCalcValue* cssCalcValue() const { ASSERT(isCalculated()); return m_value. calc; } | 234 CSSCalcValue* cssCalcValue() const { ASSERT(isCalculated()); return m_value. calc; } |
239 | 235 |
240 CSSValueID getValueID() const { return type() == UnitType::ValueID ? m_value .valueID : CSSValueInvalid; } | |
241 | |
242 template<typename T> inline T convertTo() const; // Defined in CSSPrimitiveV alueMappings.h | 236 template<typename T> inline T convertTo() const; // Defined in CSSPrimitiveV alueMappings.h |
243 | 237 |
244 static const char* unitTypeToString(UnitType); | 238 static const char* unitTypeToString(UnitType); |
245 static UnitType stringToUnitType(StringView string) | 239 static UnitType stringToUnitType(StringView string) |
246 { | 240 { |
247 if (string.is8Bit()) | 241 if (string.is8Bit()) |
248 return stringToUnitType(string.characters8(), string.length()); | 242 return stringToUnitType(string.characters8(), string.length()); |
249 return stringToUnitType(string.characters16(), string.length()); | 243 return stringToUnitType(string.characters16(), string.length()); |
250 } | 244 } |
251 | 245 |
252 String customCSSText() const; | 246 String customCSSText() const; |
253 | 247 |
254 bool equals(const CSSPrimitiveValue&) const; | 248 bool equals(const CSSPrimitiveValue&) const; |
255 | 249 |
256 DECLARE_TRACE_AFTER_DISPATCH(); | 250 DECLARE_TRACE_AFTER_DISPATCH(); |
257 | 251 |
258 static UnitType canonicalUnitTypeForCategory(UnitCategory); | 252 static UnitType canonicalUnitTypeForCategory(UnitCategory); |
259 static double conversionToCanonicalUnitsScaleFactor(UnitType); | 253 static double conversionToCanonicalUnitsScaleFactor(UnitType); |
260 | 254 |
261 // Returns true and populates lengthUnitType, if unitType is a length unit. Otherwise, returns false. | 255 // Returns true and populates lengthUnitType, if unitType is a length unit. Otherwise, returns false. |
262 static bool unitTypeToLengthUnitType(UnitType, LengthUnitType&); | 256 static bool unitTypeToLengthUnitType(UnitType, LengthUnitType&); |
263 static UnitType lengthUnitTypeToUnitType(LengthUnitType); | 257 static UnitType lengthUnitTypeToUnitType(LengthUnitType); |
264 | 258 |
265 private: | 259 private: |
266 CSSPrimitiveValue(CSSValueID); | |
267 CSSPrimitiveValue(const Length&, float zoom); | 260 CSSPrimitiveValue(const Length&, float zoom); |
268 CSSPrimitiveValue(double, UnitType); | 261 CSSPrimitiveValue(double, UnitType); |
269 | 262 |
270 template<typename T> CSSPrimitiveValue(T); // Defined in CSSPrimitiveValueMa ppings.h | 263 template<typename T> CSSPrimitiveValue(T); // Defined in CSSPrimitiveValueMa ppings.h |
271 | 264 |
272 template<typename T> CSSPrimitiveValue(T* val) | 265 template<typename T> CSSPrimitiveValue(T* val) |
273 : CSSValue(PrimitiveClass) | 266 : CSSValue(PrimitiveClass) |
274 { | 267 { |
275 init(val); | 268 init(val); |
276 } | 269 } |
277 | 270 |
278 static void create(int); // compile-time guard | 271 static void create(int); // compile-time guard |
279 static void create(unsigned); // compile-time guard | 272 static void create(unsigned); // compile-time guard |
280 template<typename T> operator T*(); // compile-time guard | 273 template<typename T> operator T*(); // compile-time guard |
281 | 274 |
282 // Code generated by CSSPrimitiveValueUnitTrie.cpp.tmpl | 275 // Code generated by CSSPrimitiveValueUnitTrie.cpp.tmpl |
283 static UnitType stringToUnitType(const LChar*, unsigned length); | 276 static UnitType stringToUnitType(const LChar*, unsigned length); |
284 static UnitType stringToUnitType(const UChar*, unsigned length); | 277 static UnitType stringToUnitType(const UChar*, unsigned length); |
285 | 278 |
286 void init(UnitType); | 279 void init(UnitType); |
287 void init(const Length&); | 280 void init(const Length&); |
288 void init(CSSCalcValue*); | 281 void init(CSSCalcValue*); |
289 | 282 |
290 double computeLengthDouble(const CSSToLengthConversionData&) const; | 283 double computeLengthDouble(const CSSToLengthConversionData&) const; |
291 | 284 |
292 inline UnitType type() const { return static_cast<UnitType>(m_primitiveUnitT ype); } | 285 inline UnitType type() const { return static_cast<UnitType>(m_primitiveUnitT ype); } |
293 | 286 |
294 union { | 287 union { |
295 CSSValueID valueID; | |
296 double num; | 288 double num; |
297 // FIXME: oilpan: Should be a member, but no support for members in unio ns. Just trace the raw ptr for now. | 289 // FIXME: oilpan: Should be a member, but no support for members in unio ns. Just trace the raw ptr for now. |
298 CSSCalcValue* calc; | 290 CSSCalcValue* calc; |
299 } m_value; | 291 } m_value; |
300 }; | 292 }; |
301 | 293 |
302 using CSSLengthArray = CSSPrimitiveValue::CSSLengthArray; | 294 using CSSLengthArray = CSSPrimitiveValue::CSSLengthArray; |
303 | 295 |
304 DEFINE_CSS_VALUE_TYPE_CASTS(CSSPrimitiveValue, isPrimitiveValue()); | 296 DEFINE_CSS_VALUE_TYPE_CASTS(CSSPrimitiveValue, isPrimitiveValue()); |
305 | 297 |
306 } // namespace blink | 298 } // namespace blink |
307 | 299 |
308 #endif // CSSPrimitiveValue_h | 300 #endif // CSSPrimitiveValue_h |
OLD | NEW |