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

Side by Side Diff: third_party/WebKit/Source/core/animation/LengthSVGInterpolation.cpp

Issue 1421533006: Make SVGLength wrap a CSSPrimitiveValue. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Review comments and tests addressed Created 5 years, 1 month 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 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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/LengthSVGInterpolation.h" 6 #include "core/animation/LengthSVGInterpolation.h"
7 7
8 #include "core/css/CSSHelper.h" 8 #include "core/css/CSSHelper.h"
9 #include "core/svg/SVGAnimatedLength.h" 9 #include "core/svg/SVGAnimatedLength.h"
10 #include "core/svg/SVGAnimatedLengthList.h" 10 #include "core/svg/SVGAnimatedLengthList.h"
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
53 53
54 enum LengthInterpolatedUnit { 54 enum LengthInterpolatedUnit {
55 LengthInterpolatedNumber, 55 LengthInterpolatedNumber,
56 LengthInterpolatedPercentage, 56 LengthInterpolatedPercentage,
57 LengthInterpolatedEMS, 57 LengthInterpolatedEMS,
58 LengthInterpolatedEXS, 58 LengthInterpolatedEXS,
59 LengthInterpolatedREMS, 59 LengthInterpolatedREMS,
60 LengthInterpolatedCHS, 60 LengthInterpolatedCHS,
61 }; 61 };
62 62
63 static const SVGLengthType unitTypes[] = { LengthTypeNumber, LengthTypePercentag e, LengthTypeEMS, LengthTypeEXS, LengthTypeREMS, LengthTypeCHS }; 63 static const CSSPrimitiveValue::UnitType unitTypes[] = {
64 CSSPrimitiveValue::UnitType::Number,
65 CSSPrimitiveValue::UnitType::Percentage,
66 CSSPrimitiveValue::UnitType::Ems,
67 CSSPrimitiveValue::UnitType::Exs,
68 CSSPrimitiveValue::UnitType::Rems,
69 CSSPrimitiveValue::UnitType::Chs
70 };
64 71
65 const size_t numLengthInterpolatedUnits = WTF_ARRAY_LENGTH(unitTypes); 72 const size_t numLengthInterpolatedUnits = WTF_ARRAY_LENGTH(unitTypes);
66 73
67 LengthInterpolatedUnit convertToInterpolatedUnit(SVGLengthType lengthType, doubl e& value) 74 LengthInterpolatedUnit convertToInterpolatedUnit(CSSPrimitiveValue::UnitType uni tType, double& value)
68 { 75 {
69 switch (lengthType) { 76 switch (unitType) {
70 case LengthTypeUnknown: 77 case CSSPrimitiveValue::UnitType::Unknown:
71 default: 78 default:
72 ASSERT_NOT_REACHED(); 79 ASSERT_NOT_REACHED();
73 case LengthTypePX: 80 case CSSPrimitiveValue::UnitType::Pixels:
74 case LengthTypeNumber: 81 case CSSPrimitiveValue::UnitType::Number:
75 return LengthInterpolatedNumber; 82 return LengthInterpolatedNumber;
76 case LengthTypePercentage: 83 case CSSPrimitiveValue::UnitType::Percentage:
77 return LengthInterpolatedPercentage; 84 return LengthInterpolatedPercentage;
78 case LengthTypeEMS: 85 case CSSPrimitiveValue::UnitType::Ems:
79 return LengthInterpolatedEMS; 86 return LengthInterpolatedEMS;
80 case LengthTypeEXS: 87 case CSSPrimitiveValue::UnitType::Exs:
81 return LengthInterpolatedEXS; 88 return LengthInterpolatedEXS;
82 case LengthTypeCM: 89 case CSSPrimitiveValue::UnitType::Centimeters:
83 value *= cssPixelsPerCentimeter; 90 value *= cssPixelsPerCentimeter;
84 return LengthInterpolatedNumber; 91 return LengthInterpolatedNumber;
85 case LengthTypeMM: 92 case CSSPrimitiveValue::UnitType::Millimeters:
86 value *= cssPixelsPerMillimeter; 93 value *= cssPixelsPerMillimeter;
87 return LengthInterpolatedNumber; 94 return LengthInterpolatedNumber;
88 case LengthTypeIN: 95 case CSSPrimitiveValue::UnitType::Inches:
89 value *= cssPixelsPerInch; 96 value *= cssPixelsPerInch;
90 return LengthInterpolatedNumber; 97 return LengthInterpolatedNumber;
91 case LengthTypePT: 98 case CSSPrimitiveValue::UnitType::Points:
92 value *= cssPixelsPerPoint; 99 value *= cssPixelsPerPoint;
93 return LengthInterpolatedNumber; 100 return LengthInterpolatedNumber;
94 case LengthTypePC: 101 case CSSPrimitiveValue::UnitType::Picas:
95 value *= cssPixelsPerPica; 102 value *= cssPixelsPerPica;
96 return LengthInterpolatedNumber; 103 return LengthInterpolatedNumber;
97 case LengthTypeREMS: 104 case CSSPrimitiveValue::UnitType::UserUnits:
105 return LengthInterpolatedNumber;
106 case CSSPrimitiveValue::UnitType::Rems:
98 return LengthInterpolatedREMS; 107 return LengthInterpolatedREMS;
99 case LengthTypeCHS: 108 case CSSPrimitiveValue::UnitType::Chs:
100 return LengthInterpolatedCHS; 109 return LengthInterpolatedCHS;
101 } 110 }
102 } 111 }
103 112
104 } // namespace 113 } // namespace
105 114
106 PassOwnPtr<InterpolableValue> LengthSVGInterpolation::toInterpolableValue(SVGLen gth* length, const SVGAnimatedPropertyBase* attribute, NonInterpolableType* ptrM odeData) 115 PassOwnPtr<InterpolableValue> LengthSVGInterpolation::toInterpolableValue(SVGLen gth* length, const SVGAnimatedPropertyBase* attribute, NonInterpolableType* ptrM odeData)
107 { 116 {
108 if (ptrModeData) 117 if (ptrModeData)
109 populateModeData(attribute, ptrModeData); 118 populateModeData(attribute, ptrModeData);
110 119
111 double value = length->valueInSpecifiedUnits(); 120 double value = length->valueInSpecifiedUnits();
112 LengthInterpolatedUnit unitType = convertToInterpolatedUnit(length->unitType (), value); 121 LengthInterpolatedUnit unitType = convertToInterpolatedUnit(length->typeWith CalcResolved(), value);
113 122
114 double values[numLengthInterpolatedUnits] = { }; 123 double values[numLengthInterpolatedUnits] = { };
115 values[unitType] = value; 124 values[unitType] = value;
116 125
117 OwnPtr<InterpolableList> listOfValues = InterpolableList::create(numLengthIn terpolatedUnits); 126 OwnPtr<InterpolableList> listOfValues = InterpolableList::create(numLengthIn terpolatedUnits);
118 for (size_t i = 0; i < numLengthInterpolatedUnits; ++i) 127 for (size_t i = 0; i < numLengthInterpolatedUnits; ++i)
119 listOfValues->set(i, InterpolableNumber::create(values[i])); 128 listOfValues->set(i, InterpolableNumber::create(values[i]));
120 return listOfValues.release(); 129 return listOfValues.release();
121 } 130 }
122 131
123 PassRefPtrWillBeRawPtr<SVGLength> LengthSVGInterpolation::fromInterpolableValue( const InterpolableValue& interpolableValue, const NonInterpolableType& modeData, const SVGElement* element) 132 PassRefPtrWillBeRawPtr<SVGLength> LengthSVGInterpolation::fromInterpolableValue( const InterpolableValue& interpolableValue, const NonInterpolableType& modeData, const SVGElement* element)
124 { 133 {
125 const InterpolableList& listOfValues = toInterpolableList(interpolableValue) ; 134 const InterpolableList& listOfValues = toInterpolableList(interpolableValue) ;
126 ASSERT(element); 135 ASSERT(element);
127 136
128 double value = 0; 137 double value = 0;
129 SVGLengthType lengthType = LengthTypeNumber; 138 CSSPrimitiveValue::UnitType unitType = CSSPrimitiveValue::UnitType::UserUnit s;
130 unsigned unitTypeCount = 0; 139 unsigned unitTypeCount = 0;
131 // We optimise for the common case where only one unit type is involved. 140 // We optimise for the common case where only one unit type is involved.
132 for (size_t i = 0; i < numLengthInterpolatedUnits; i++) { 141 for (size_t i = 0; i < numLengthInterpolatedUnits; i++) {
133 double entry = toInterpolableNumber(listOfValues.get(i))->value(); 142 double entry = toInterpolableNumber(listOfValues.get(i))->value();
134 if (!entry) 143 if (!entry)
135 continue; 144 continue;
136 unitTypeCount++; 145 unitTypeCount++;
137 if (unitTypeCount > 1) 146 if (unitTypeCount > 1)
138 break; 147 break;
139 148
140 value = entry; 149 value = entry;
141 lengthType = unitTypes[i]; 150 unitType = unitTypes[i];
142 } 151 }
143 152
144 if (unitTypeCount > 1) { 153 if (unitTypeCount > 1) {
145 value = 0; 154 value = 0;
146 lengthType = LengthTypeNumber; 155 unitType = CSSPrimitiveValue::UnitType::UserUnits;
147 156
148 // SVGLength does not support calc expressions, so we convert to canonic al units. 157 // SVGLength does not support calc expressions, so we convert to canonic al units.
149 SVGLengthContext lengthContext(element); 158 SVGLengthContext lengthContext(element);
150 for (size_t i = 0; i < numLengthInterpolatedUnits; i++) { 159 for (size_t i = 0; i < numLengthInterpolatedUnits; i++) {
151 double entry = toInterpolableNumber(listOfValues.get(i))->value(); 160 double entry = toInterpolableNumber(listOfValues.get(i))->value();
152 if (entry) 161 if (entry)
153 value += lengthContext.convertValueToUserUnits(entry, modeData.u nitMode, unitTypes[i]); 162 value += lengthContext.convertValueToUserUnits(entry, modeData.u nitMode, unitTypes[i]);
154 } 163 }
155 } 164 }
156 165
157 if (modeData.negativeValuesMode == ForbidNegativeLengths && value < 0) 166 if (modeData.negativeValuesMode == ForbidNegativeLengths && value < 0)
158 value = 0; 167 value = 0;
159 168
160 RefPtrWillBeRawPtr<SVGLength> result = SVGLength::create(modeData.unitMode); // defaults to the length 0 169 RefPtrWillBeRawPtr<SVGLength> result = SVGLength::create(modeData.unitMode); // defaults to the length 0
161 result->setUnitType(lengthType); 170 result->newValueSpecifiedUnits(unitType, value);
162 result->setValueInSpecifiedUnits(value);
163 return result.release(); 171 return result.release();
164 } 172 }
165 173
166 PassRefPtrWillBeRawPtr<SVGPropertyBase> LengthSVGInterpolation::interpolatedValu e(SVGElement& targetElement) const 174 PassRefPtrWillBeRawPtr<SVGPropertyBase> LengthSVGInterpolation::interpolatedValu e(SVGElement& targetElement) const
167 { 175 {
168 return fromInterpolableValue(*m_cachedValue, m_modeData, &targetElement); 176 return fromInterpolableValue(*m_cachedValue, m_modeData, &targetElement);
169 } 177 }
170 178
171 } 179 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698