OLD | NEW |
| (Empty) |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef CSSCalcLength_h | |
6 #define CSSCalcLength_h | |
7 | |
8 #include <array> | |
9 #include <bitset> | |
10 #include "core/css/cssom/CSSLengthValue.h" | |
11 #include "platform/wtf/BitVector.h" | |
12 | |
13 namespace blink { | |
14 | |
15 class CSSCalcExpressionNode; | |
16 class CSSSimpleLength; | |
17 | |
18 class CORE_EXPORT CSSCalcLength final : public CSSLengthValue { | |
19 DEFINE_WRAPPERTYPEINFO(); | |
20 | |
21 public: | |
22 class UnitData { | |
23 public: | |
24 UnitData() : values_(), has_value_for_unit_() {} | |
25 UnitData(const UnitData& other) | |
26 : values_(other.values_), | |
27 has_value_for_unit_(other.has_value_for_unit_) {} | |
28 | |
29 static std::unique_ptr<UnitData> FromExpressionNode( | |
30 const CSSCalcExpressionNode*); | |
31 CSSCalcExpressionNode* ToCSSCalcExpressionNode() const; | |
32 | |
33 bool Has(CSSPrimitiveValue::UnitType) const; | |
34 void Set(CSSPrimitiveValue::UnitType, double); | |
35 double Get(CSSPrimitiveValue::UnitType) const; | |
36 | |
37 void Add(const UnitData& right); | |
38 void Subtract(const UnitData& right); | |
39 void Multiply(double); | |
40 void Divide(double); | |
41 | |
42 private: | |
43 bool HasAtIndex(int) const; | |
44 void SetAtIndex(int, double); | |
45 double GetAtIndex(int) const; | |
46 | |
47 std::array<double, CSSLengthValue::kNumSupportedUnits> values_; | |
48 std::bitset<CSSLengthValue::kNumSupportedUnits> has_value_for_unit_; | |
49 }; | |
50 | |
51 static CSSCalcLength* Create(const CSSCalcDictionary&, ExceptionState&); | |
52 static CSSCalcLength* Create(const CSSLengthValue* length, ExceptionState&) { | |
53 return Create(length); | |
54 } | |
55 static CSSCalcLength* Create(const CSSLengthValue*); | |
56 static CSSCalcLength* FromCSSValue(const CSSPrimitiveValue&); | |
57 | |
58 static CSSCalcLength* FromLength(const Length&); | |
59 | |
60 #define GETTER_MACRO(name, type) \ | |
61 double name(bool& isNull) { \ | |
62 isNull = !unit_data_.Has(type); \ | |
63 return unit_data_.Get(type); \ | |
64 } | |
65 | |
66 GETTER_MACRO(px, CSSPrimitiveValue::UnitType::kPixels) | |
67 GETTER_MACRO(percent, CSSPrimitiveValue::UnitType::kPercentage) | |
68 GETTER_MACRO(em, CSSPrimitiveValue::UnitType::kEms) | |
69 GETTER_MACRO(ex, CSSPrimitiveValue::UnitType::kExs) | |
70 GETTER_MACRO(ch, CSSPrimitiveValue::UnitType::kChs) | |
71 GETTER_MACRO(rem, CSSPrimitiveValue::UnitType::kRems) | |
72 GETTER_MACRO(vw, CSSPrimitiveValue::UnitType::kViewportWidth) | |
73 GETTER_MACRO(vh, CSSPrimitiveValue::UnitType::kViewportHeight) | |
74 GETTER_MACRO(vmin, CSSPrimitiveValue::UnitType::kViewportMin) | |
75 GETTER_MACRO(vmax, CSSPrimitiveValue::UnitType::kViewportMax) | |
76 GETTER_MACRO(cm, CSSPrimitiveValue::UnitType::kCentimeters) | |
77 GETTER_MACRO(mm, CSSPrimitiveValue::UnitType::kMillimeters) | |
78 GETTER_MACRO(in, CSSPrimitiveValue::UnitType::kInches) | |
79 GETTER_MACRO(pc, CSSPrimitiveValue::UnitType::kPicas) | |
80 GETTER_MACRO(pt, CSSPrimitiveValue::UnitType::kPoints) | |
81 | |
82 #undef GETTER_MACRO | |
83 | |
84 bool ContainsPercent() const override; | |
85 | |
86 CSSValue* ToCSSValue() const override; | |
87 | |
88 StyleValueType GetType() const override { return kCalcLengthType; } | |
89 | |
90 protected: | |
91 CSSLengthValue* AddInternal(const CSSLengthValue* other) override; | |
92 CSSLengthValue* SubtractInternal(const CSSLengthValue* other) override; | |
93 CSSLengthValue* MultiplyInternal(double) override; | |
94 CSSLengthValue* DivideInternal(double) override; | |
95 | |
96 private: | |
97 CSSCalcLength(); | |
98 CSSCalcLength(const CSSCalcLength& other); | |
99 CSSCalcLength(const CSSSimpleLength& other); | |
100 CSSCalcLength(const UnitData& unit_data) : unit_data_(unit_data) {} | |
101 | |
102 UnitData unit_data_; | |
103 }; | |
104 | |
105 #define DEFINE_CALC_LENGTH_TYPE_CASTS(argumentType) \ | |
106 DEFINE_TYPE_CASTS( \ | |
107 CSSCalcLength, argumentType, value, \ | |
108 value->GetType() == CSSLengthValue::StyleValueType::kCalcLengthType, \ | |
109 value.GetType() == CSSLengthValue::StyleValueType::kCalcLengthType) | |
110 | |
111 DEFINE_CALC_LENGTH_TYPE_CASTS(CSSStyleValue); | |
112 | |
113 } // namespace blink | |
114 | |
115 #endif | |
OLD | NEW |