Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(152)

Side by Side Diff: Source/core/animation/LengthStyleInterpolation.cpp

Issue 813233002: Animation: Fix loss of type information when interpolating value of 0 (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Fix second accumulateLength method Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "config.h" 5 #include "config.h"
6 #include "core/animation/LengthStyleInterpolation.h" 6 #include "core/animation/LengthStyleInterpolation.h"
7 7
8 #include "core/css/CSSCalculationValue.h" 8 #include "core/css/CSSCalculationValue.h"
9 #include "core/css/resolver/StyleBuilder.h" 9 #include "core/css/resolver/StyleBuilder.h"
10 10
11 namespace blink { 11 namespace blink {
12 12
13 bool LengthStyleInterpolation::canCreateFrom(const CSSValue& value) 13 bool LengthStyleInterpolation::canCreateFrom(const CSSValue& value)
14 { 14 {
15 if (value.isPrimitiveValue()) { 15 if (value.isPrimitiveValue()) {
16 const CSSPrimitiveValue& primitiveValue = blink::toCSSPrimitiveValue(val ue); 16 const CSSPrimitiveValue& primitiveValue = blink::toCSSPrimitiveValue(val ue);
17 if (primitiveValue.cssCalcValue()) 17 if (primitiveValue.cssCalcValue())
18 return true; 18 return true;
19 19
20 CSSPrimitiveValue::LengthUnitType type; 20 CSSPrimitiveValue::LengthUnitType type;
21 // Only returns true if the type is a primitive length unit. 21 // Only returns true if the type is a primitive length unit.
22 return CSSPrimitiveValue::unitTypeToLengthUnitType(primitiveValue.primit iveType(), type); 22 return CSSPrimitiveValue::unitTypeToLengthUnitType(primitiveValue.primit iveType(), type);
23 } 23 }
24 return value.isCalcValue(); 24 return value.isCalcValue();
25 } 25 }
26 26
27 PassOwnPtrWillBeRawPtr<InterpolableValue> LengthStyleInterpolation::lengthToInte rpolableValue(const CSSValue& value) 27 PassOwnPtrWillBeRawPtr<InterpolableValue> LengthStyleInterpolation::lengthToInte rpolableValue(const CSSValue& value)
28 { 28 {
29 OwnPtrWillBeRawPtr<InterpolableList> result = InterpolableList::create(CSSPr imitiveValue::LengthUnitTypeCount); 29 OwnPtrWillBeRawPtr<InterpolableList> listOfValuesAndTypes = InterpolableList ::create(2);
30 OwnPtrWillBeRawPtr<InterpolableList> listOfValues = InterpolableList::create (CSSPrimitiveValue::LengthUnitTypeCount);
31 OwnPtrWillBeRawPtr<InterpolableList> listOfTypes = InterpolableList::create( CSSPrimitiveValue::LengthUnitTypeCount);
32
30 const CSSPrimitiveValue& primitive = toCSSPrimitiveValue(value); 33 const CSSPrimitiveValue& primitive = toCSSPrimitiveValue(value);
31 34
32 CSSLengthArray array; 35 CSSLengthArray arrayOfValues;
36 CSSPrimitiveValue::CSSLengthTypeArray arrayOfTypes;
33 for (size_t i = 0; i < CSSPrimitiveValue::LengthUnitTypeCount; i++) 37 for (size_t i = 0; i < CSSPrimitiveValue::LengthUnitTypeCount; i++)
34 array.append(0); 38 arrayOfValues.append(0);
35 primitive.accumulateLengthArray(array);
36 39
37 for (size_t i = 0; i < CSSPrimitiveValue::LengthUnitTypeCount; i++) 40 arrayOfTypes.ensureSize(CSSPrimitiveValue::LengthUnitTypeCount);
38 result->set(i, InterpolableNumber::create(array.at(i))); 41 primitive.accumulateLengthArray(arrayOfValues, arrayOfTypes);
39 42
40 return result.release(); 43 for (size_t i = 0; i < CSSPrimitiveValue::LengthUnitTypeCount; i++) {
44 listOfValues->set(i, InterpolableNumber::create(arrayOfValues.at(i)));
45 listOfTypes->set(i, InterpolableNumber::create(arrayOfTypes.get(i)));
46 }
47
48 listOfValuesAndTypes->set(0, listOfValues.release());
49 listOfValuesAndTypes->set(1, listOfTypes.release());
50
51 return listOfValuesAndTypes.release();
41 } 52 }
42 53
43 namespace { 54 namespace {
44 55
45 static CSSPrimitiveValue::UnitType toUnitType(int lengthUnitType) 56 static CSSPrimitiveValue::UnitType toUnitType(int lengthUnitType)
46 { 57 {
47 return static_cast<CSSPrimitiveValue::UnitType>(CSSPrimitiveValue::lengthUni tTypeToUnitType(static_cast<CSSPrimitiveValue::LengthUnitType>(lengthUnitType))) ; 58 return static_cast<CSSPrimitiveValue::UnitType>(CSSPrimitiveValue::lengthUni tTypeToUnitType(static_cast<CSSPrimitiveValue::LengthUnitType>(lengthUnitType))) ;
48 } 59 }
49 60
50 static PassRefPtrWillBeRawPtr<CSSCalcExpressionNode> constructCalcExpression(Pas sRefPtrWillBeRawPtr<CSSCalcExpressionNode> previous, const InterpolableList* lis t, size_t position) 61 static PassRefPtrWillBeRawPtr<CSSCalcExpressionNode> constructCalcExpression(Pas sRefPtrWillBeRawPtr<CSSCalcExpressionNode> previous, const InterpolableList* lis t, size_t position)
51 { 62 {
63 const InterpolableList* listOfValues = toInterpolableList(list->get(0));
64 const InterpolableList* listOfTypes = toInterpolableList(list->get(1));
52 while (position != CSSPrimitiveValue::LengthUnitTypeCount) { 65 while (position != CSSPrimitiveValue::LengthUnitTypeCount) {
53 const InterpolableNumber *subValue = toInterpolableNumber(list->get(posi tion)); 66 const InterpolableNumber *subValueType = toInterpolableNumber(listOfType s->get(position));
54 if (subValue->value()) { 67 if (subValueType->value()) {
55 RefPtrWillBeRawPtr<CSSCalcExpressionNode> next; 68 RefPtrWillBeRawPtr<CSSCalcExpressionNode> next;
69 double value = toInterpolableNumber(listOfValues->get(position))->va lue();
56 if (previous) 70 if (previous)
57 next = CSSCalcValue::createExpressionNode(previous, CSSCalcValue ::createExpressionNode(CSSPrimitiveValue::create(subValue->value(), toUnitType(p osition))), CalcAdd); 71 next = CSSCalcValue::createExpressionNode(previous, CSSCalcValue ::createExpressionNode(CSSPrimitiveValue::create(value, toUnitType(position))), CalcAdd);
58 else 72 else
59 next = CSSCalcValue::createExpressionNode(CSSPrimitiveValue::cre ate(subValue->value(), toUnitType(position))); 73 next = CSSCalcValue::createExpressionNode(CSSPrimitiveValue::cre ate(value, toUnitType(position)));
60 return constructCalcExpression(next, list, position + 1); 74 return constructCalcExpression(next, list, position + 1);
61 } 75 }
62 position++; 76 position++;
63 } 77 }
64 return previous; 78 return previous;
65 } 79 }
66 80
67 } 81 }
68 82
69 PassRefPtrWillBeRawPtr<CSSValue> LengthStyleInterpolation::interpolableValueToLe ngth(const InterpolableValue* value, ValueRange range) 83 PassRefPtrWillBeRawPtr<CSSValue> LengthStyleInterpolation::interpolableValueToLe ngth(const InterpolableValue* value, ValueRange range)
70 { 84 {
71 const InterpolableList* listValue = toInterpolableList(value); 85 const InterpolableList* listOfValuesAndTypes = toInterpolableList(value);
72 unsigned unitCount = 0; 86 const InterpolableList* listOfValues = toInterpolableList(listOfValuesAndTyp es->get(0));
87 const InterpolableList* listOfTypes = toInterpolableList(listOfValuesAndType s->get(1));
88 unsigned unitTypeCount = 0;
73 for (size_t i = 0; i < CSSPrimitiveValue::LengthUnitTypeCount; i++) { 89 for (size_t i = 0; i < CSSPrimitiveValue::LengthUnitTypeCount; i++) {
74 const InterpolableNumber* subValue = toInterpolableNumber(listValue->get (i)); 90 const InterpolableNumber* subType = toInterpolableNumber(listOfTypes->ge t(i));
75 if (subValue->value()) { 91 if (subType->value()) {
76 unitCount++; 92 unitTypeCount++;
77 } 93 }
78 } 94 }
79 95
80 switch (unitCount) { 96 switch (unitTypeCount) {
81 case 0: 97 case 0:
82 return CSSPrimitiveValue::create(0, CSSPrimitiveValue::CSS_PX); 98 ASSERT_NOT_REACHED();
83 case 1: 99 case 1:
84 for (size_t i = 0; i < CSSPrimitiveValue::LengthUnitTypeCount; i++) { 100 for (size_t i = 0; i < CSSPrimitiveValue::LengthUnitTypeCount; i++) {
85 const InterpolableNumber* subValue = toInterpolableNumber(listValue- >get(i)); 101 const InterpolableNumber *subValueType = toInterpolableNumber(listOf Types->get(i));
86 double value = subValue->value(); 102 if (subValueType->value()) {
87 if (value) { 103 double value = toInterpolableNumber(listOfValues->get(i))->value ();
88 if (range == ValueRangeNonNegative && value < 0) 104 if (range == ValueRangeNonNegative && value < 0)
89 value = 0; 105 value = 0;
90 return CSSPrimitiveValue::create(value, toUnitType(i)); 106 return CSSPrimitiveValue::create(value, toUnitType(i));
91 } 107 }
92 } 108 }
93 ASSERT_NOT_REACHED(); 109 ASSERT_NOT_REACHED();
94 default: 110 default:
95 return CSSPrimitiveValue::create(CSSCalcValue::create(constructCalcExpre ssion(nullptr, listValue, 0), range)); 111 return CSSPrimitiveValue::create(CSSCalcValue::create(constructCalcExpre ssion(nullptr, listOfValuesAndTypes, 0), range));
96 } 112 }
97 } 113 }
98 114
99 void LengthStyleInterpolation::apply(StyleResolverState& state) const 115 void LengthStyleInterpolation::apply(StyleResolverState& state) const
100 { 116 {
101 StyleBuilder::applyProperty(m_id, state, interpolableValueToLength(m_cachedV alue.get(), m_range).get()); 117 StyleBuilder::applyProperty(m_id, state, interpolableValueToLength(m_cachedV alue.get(), m_range).get());
102 } 118 }
103 119
104 void LengthStyleInterpolation::trace(Visitor* visitor) 120 void LengthStyleInterpolation::trace(Visitor* visitor)
105 { 121 {
106 StyleInterpolation::trace(visitor); 122 StyleInterpolation::trace(visitor);
107 } 123 }
108 124
109 } 125 }
OLDNEW
« no previous file with comments | « Source/core/animation/LengthPoint3DStyleInterpolationTest.cpp ('k') | Source/core/animation/LengthStyleInterpolationTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698