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 #include "core/animation/LengthInterpolationFunctions.h" | |
6 | |
7 #include "core/css/CSSPrimitiveValue.h" | |
8 #include "core/css/CSSToLengthConversionData.h" | |
9 #include "platform/CalculationValue.h" | |
10 | |
11 namespace blink { | |
12 | |
13 // This class is implemented as a singleton whose instance represents the presen ce of percentages being used in a Length value | |
14 // while nullptr represents the absence of any percentages. | |
15 class CSSLengthNonInterpolableValue : public NonInterpolableValue { | |
16 public: | |
17 ~CSSLengthNonInterpolableValue() final { NOTREACHED(); } | |
18 static PassRefPtr<CSSLengthNonInterpolableValue> create(bool hasPercentage) | |
19 { | |
20 DEFINE_STATIC_REF(CSSLengthNonInterpolableValue, singleton, adoptRef(new CSSLengthNonInterpolableValue())); | |
21 DCHECK(singleton); | |
22 return hasPercentage ? singleton : nullptr; | |
23 } | |
24 static PassRefPtr<CSSLengthNonInterpolableValue> merge(const NonInterpolable Value* a, const NonInterpolableValue* b) | |
25 { | |
26 return create(hasPercentage(a) || hasPercentage(b)); | |
27 } | |
28 static bool hasPercentage(const NonInterpolableValue* nonInterpolableValue) | |
29 { | |
30 DCHECK(!nonInterpolableValue || nonInterpolableValue->getType() == CSSLe ngthNonInterpolableValue::staticType); | |
31 return static_cast<bool>(nonInterpolableValue); | |
32 } | |
33 | |
34 DECLARE_NON_INTERPOLABLE_VALUE_TYPE(); | |
35 | |
36 private: | |
37 CSSLengthNonInterpolableValue() { } | |
38 }; | |
39 | |
40 DEFINE_NON_INTERPOLABLE_VALUE_TYPE(CSSLengthNonInterpolableValue); | |
41 DEFINE_NON_INTERPOLABLE_VALUE_TYPE_CASTS(CSSLengthNonInterpolableValue); | |
42 | |
43 std::unique_ptr<InterpolableValue> LengthInterpolationFunctions::createInterpola blePixels(double pixels) | |
44 { | |
45 std::unique_ptr<InterpolableList> interpolableList = createNeutralInterpolab leValue(); | |
46 interpolableList->set(CSSPrimitiveValue::UnitTypePixels, InterpolableNumber: :create(pixels)); | |
47 return std::move(interpolableList); | |
48 } | |
49 | |
50 InterpolationValue LengthInterpolationFunctions::createInterpolablePercent(doubl e percent) | |
51 { | |
52 std::unique_ptr<InterpolableList> interpolableList = createNeutralInterpolab leValue(); | |
53 interpolableList->set(CSSPrimitiveValue::UnitTypePercentage, InterpolableNum ber::create(percent)); | |
54 return InterpolationValue(std::move(interpolableList), CSSLengthNonInterpola bleValue::create(true)); | |
55 } | |
56 | |
57 std::unique_ptr<InterpolableList> LengthInterpolationFunctions::createNeutralInt erpolableValue() | |
58 { | |
59 const size_t length = CSSPrimitiveValue::LengthUnitTypeCount; | |
60 std::unique_ptr<InterpolableList> values = InterpolableList::create(length); | |
61 for (size_t i = 0; i < length; i++) | |
62 values->set(i, InterpolableNumber::create(0)); | |
63 return values; | |
64 } | |
65 | |
66 InterpolationValue LengthInterpolationFunctions::maybeConvertCSSValue(const CSSV alue& value) | |
67 { | |
68 if (!value.isPrimitiveValue()) | |
69 return nullptr; | |
70 | |
71 const CSSPrimitiveValue& primitiveValue = toCSSPrimitiveValue(value); | |
72 if (!primitiveValue.isLength() && !primitiveValue.isPercentage() && !primiti veValue.isCalculatedPercentageWithLength()) | |
73 return nullptr; | |
74 | |
75 CSSLengthArray lengthArray; | |
76 primitiveValue.accumulateLengthArray(lengthArray); | |
77 | |
78 std::unique_ptr<InterpolableList> values = InterpolableList::create(CSSPrimi tiveValue::LengthUnitTypeCount); | |
79 for (size_t i = 0; i < CSSPrimitiveValue::LengthUnitTypeCount; i++) | |
80 values->set(i, InterpolableNumber::create(lengthArray.values[i])); | |
81 | |
82 bool hasPercentage = lengthArray.typeFlags.get(CSSPrimitiveValue::UnitTypePe rcentage); | |
83 return InterpolationValue(std::move(values), CSSLengthNonInterpolableValue:: create(hasPercentage)); | |
84 } | |
85 | |
86 InterpolationValue LengthInterpolationFunctions::maybeConvertLength(const Length & length, float zoom) | |
87 { | |
88 if (!length.isSpecified()) | |
89 return nullptr; | |
90 | |
91 PixelsAndPercent pixelsAndPercent = length.getPixelsAndPercent(); | |
92 std::unique_ptr<InterpolableList> values = createNeutralInterpolableValue(); | |
93 values->set(CSSPrimitiveValue::UnitTypePixels, InterpolableNumber::create(pi xelsAndPercent.pixels / zoom)); | |
94 values->set(CSSPrimitiveValue::UnitTypePercentage, InterpolableNumber::creat e(pixelsAndPercent.percent)); | |
95 | |
96 return InterpolationValue(std::move(values), CSSLengthNonInterpolableValue:: create(length.hasPercent())); | |
97 } | |
98 | |
99 PairwiseInterpolationValue LengthInterpolationFunctions::mergeSingles(Interpolat ionValue&& start, InterpolationValue&& end) | |
100 { | |
101 return PairwiseInterpolationValue( | |
102 std::move(start.interpolableValue), | |
103 std::move(end.interpolableValue), | |
104 CSSLengthNonInterpolableValue::merge(start.nonInterpolableValue.get(), e nd.nonInterpolableValue.get())); | |
105 } | |
106 | |
107 bool LengthInterpolationFunctions::nonInterpolableValuesAreCompatible(const NonI nterpolableValue*, const NonInterpolableValue*) | |
108 { | |
109 return true; | |
suzyh_UTC10 (ex-contributor)
2016/08/29 07:12:31
Looks like some further refactoring is needed as t
alancutter (OOO until 2018)
2016/09/05 07:40:22
Added type checks as discussed in https://coderevi
| |
110 } | |
111 | |
112 void LengthInterpolationFunctions::composite( | |
113 std::unique_ptr<InterpolableValue>& underlyingInterpolableValue, | |
114 RefPtr<NonInterpolableValue>& underlyingNonInterpolableValue, | |
115 double underlyingFraction, | |
116 const InterpolableValue& interpolableValue, | |
117 const NonInterpolableValue* nonInterpolableValue) | |
118 { | |
119 underlyingInterpolableValue->scaleAndAdd(underlyingFraction, interpolableVal ue); | |
120 underlyingNonInterpolableValue = CSSLengthNonInterpolableValue::merge(underl yingNonInterpolableValue.get(), nonInterpolableValue); | |
121 } | |
122 | |
123 void LengthInterpolationFunctions::subtractFromOneHundredPercent(InterpolationVa lue& result) | |
124 { | |
125 InterpolableList& list = toInterpolableList(*result.interpolableValue); | |
126 for (size_t i = 0; i < CSSPrimitiveValue::LengthUnitTypeCount; i++) { | |
127 double value = -toInterpolableNumber(*list.get(i)).value(); | |
128 if (i == CSSPrimitiveValue::UnitTypePercentage) | |
129 value += 100; | |
130 toInterpolableNumber(*list.getMutable(i)).set(value); | |
131 } | |
132 result.nonInterpolableValue = CSSLengthNonInterpolableValue::create(true); | |
133 } | |
134 | |
135 static double clampToRange(double x, ValueRange range) | |
136 { | |
137 return (range == ValueRangeNonNegative && x < 0) ? 0 : x; | |
138 } | |
139 | |
140 Length LengthInterpolationFunctions::createLength(const InterpolableValue& inter polableValue, const NonInterpolableValue* nonInterpolableValue, const CSSToLengt hConversionData& conversionData, ValueRange range) | |
141 { | |
142 const InterpolableList& interpolableList = toInterpolableList(interpolableVa lue); | |
143 bool hasPercentage = CSSLengthNonInterpolableValue::hasPercentage(nonInterpo lableValue); | |
suzyh_UTC10 (ex-contributor)
2016/08/29 07:12:31
Why is hasPercentage a static function instead non
alancutter (OOO until 2018)
2016/09/05 07:40:22
It is false if nonInterpolableValue is nullptr. Ca
| |
144 double pixels = 0; | |
145 double percentage = 0; | |
146 for (size_t i = 0; i < CSSPrimitiveValue::LengthUnitTypeCount; i++) { | |
147 double value = toInterpolableNumber(*interpolableList.get(i)).value(); | |
148 if (i == CSSPrimitiveValue::UnitTypePercentage) { | |
149 percentage = value; | |
150 } else { | |
151 CSSPrimitiveValue::UnitType type = CSSPrimitiveValue::lengthUnitType ToUnitType(static_cast<CSSPrimitiveValue::LengthUnitType>(i)); | |
152 pixels += conversionData.zoomedComputedPixels(value, type); | |
153 } | |
154 } | |
155 | |
156 if (percentage != 0) | |
157 hasPercentage = true; | |
158 if (pixels != 0 && hasPercentage) | |
159 return Length(CalculationValue::create(PixelsAndPercent(pixels, percenta ge), range)); | |
160 if (hasPercentage) | |
161 return Length(clampToRange(percentage, range), Percent); | |
162 return Length(CSSPrimitiveValue::clampToCSSLengthRange(clampToRange(pixels, range)), Fixed); | |
163 } | |
164 | |
165 } // namespace blink | |
OLD | NEW |