Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 #include "core/css/resolver/StyleResolverState.h" | |
| 11 #include "platform/CalculationValue.h" | |
| 10 | 12 |
| 11 namespace blink { | 13 namespace blink { |
| 12 | 14 |
| 13 bool LengthStyleInterpolation::canCreateFrom(const CSSValue& value) | 15 bool LengthStyleInterpolation::canCreateFrom(const CSSValue& value) |
| 14 { | 16 { |
| 15 if (value.isPrimitiveValue()) { | 17 if (value.isPrimitiveValue()) { |
| 16 const CSSPrimitiveValue& primitiveValue = blink::toCSSPrimitiveValue(val ue); | 18 const CSSPrimitiveValue& primitiveValue = blink::toCSSPrimitiveValue(val ue); |
| 17 if (primitiveValue.cssCalcValue()) | 19 if (primitiveValue.cssCalcValue()) |
| 18 return true; | 20 return true; |
| 19 | 21 |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 45 listOfValues->set(i, InterpolableNumber::create(arrayOfValues.at(i))); | 47 listOfValues->set(i, InterpolableNumber::create(arrayOfValues.at(i))); |
| 46 listOfTypes->set(i, InterpolableNumber::create(arrayOfTypes.get(i))); | 48 listOfTypes->set(i, InterpolableNumber::create(arrayOfTypes.get(i))); |
| 47 } | 49 } |
| 48 | 50 |
| 49 listOfValuesAndTypes->set(0, listOfValues.release()); | 51 listOfValuesAndTypes->set(0, listOfValues.release()); |
| 50 listOfValuesAndTypes->set(1, listOfTypes.release()); | 52 listOfValuesAndTypes->set(1, listOfTypes.release()); |
| 51 | 53 |
| 52 return listOfValuesAndTypes.release(); | 54 return listOfValuesAndTypes.release(); |
| 53 } | 55 } |
| 54 | 56 |
| 57 bool LengthStyleInterpolation::isPixelsOrPercentOnly(const InterpolableValue& va lue) | |
| 58 { | |
| 59 const InterpolableList& types = *toInterpolableList(toInterpolableList(value ).get(1)); | |
| 60 bool result = false; | |
| 61 for (size_t i = 0; i < CSSPrimitiveValue::LengthUnitTypeCount; i++) { | |
| 62 bool typeIsPresent = toInterpolableNumber(types.get(i))->value(); | |
| 63 if (i == CSSPrimitiveValue::UnitTypePixels) | |
| 64 result |= typeIsPresent; | |
| 65 else if (i == CSSPrimitiveValue::UnitTypePercentage) | |
| 66 result |= typeIsPresent; | |
| 67 else if (typeIsPresent) | |
| 68 return false; | |
| 69 } | |
| 70 return result; | |
| 71 } | |
| 72 | |
| 73 LengthStyleInterpolation::LengthSetter LengthStyleInterpolation::lengthSetterFor Property(CSSPropertyID property) | |
| 74 { | |
| 75 switch (property) { | |
| 76 case CSSPropertyBottom: | |
| 77 return &LayoutStyle::setBottom; | |
| 78 case CSSPropertyFlexBasis: | |
| 79 return &LayoutStyle::setFlexBasis; | |
| 80 case CSSPropertyHeight: | |
| 81 return &LayoutStyle::setHeight; | |
| 82 case CSSPropertyLeft: | |
| 83 return &LayoutStyle::setLeft; | |
| 84 case CSSPropertyLineHeight: | |
| 85 return &LayoutStyle::setLineHeight; | |
| 86 case CSSPropertyMarginBottom: | |
| 87 return &LayoutStyle::setMarginBottom; | |
| 88 case CSSPropertyMarginLeft: | |
| 89 return &LayoutStyle::setMarginLeft; | |
| 90 case CSSPropertyMarginRight: | |
| 91 return &LayoutStyle::setMarginRight; | |
| 92 case CSSPropertyMarginTop: | |
| 93 return &LayoutStyle::setMarginTop; | |
| 94 case CSSPropertyMaxHeight: | |
| 95 return &LayoutStyle::setMaxHeight; | |
| 96 case CSSPropertyMaxWidth: | |
| 97 return &LayoutStyle::setMaxWidth; | |
| 98 case CSSPropertyMinHeight: | |
| 99 return &LayoutStyle::setMinHeight; | |
| 100 case CSSPropertyMinWidth: | |
| 101 return &LayoutStyle::setMinWidth; | |
| 102 case CSSPropertyMotionPosition: | |
| 103 return &LayoutStyle::setMotionPosition; | |
|
Eric Willigers
2015/03/06 02:22:08
Will need rebasing. MotionPosition has been rename
alancutter (OOO until 2018)
2015/03/06 07:18:58
Done.
| |
| 104 case CSSPropertyPaddingBottom: | |
| 105 return &LayoutStyle::setPaddingBottom; | |
| 106 case CSSPropertyPaddingLeft: | |
| 107 return &LayoutStyle::setPaddingLeft; | |
| 108 case CSSPropertyPaddingRight: | |
| 109 return &LayoutStyle::setPaddingRight; | |
| 110 case CSSPropertyPaddingTop: | |
| 111 return &LayoutStyle::setPaddingTop; | |
| 112 case CSSPropertyRight: | |
| 113 return &LayoutStyle::setRight; | |
| 114 case CSSPropertyShapeMargin: | |
| 115 return &LayoutStyle::setShapeMargin; | |
| 116 case CSSPropertyTop: | |
| 117 return &LayoutStyle::setTop; | |
| 118 case CSSPropertyWidth: | |
| 119 return &LayoutStyle::setWidth; | |
| 120 default: | |
| 121 return nullptr; | |
|
dstockwell
2015/03/06 02:32:07
When does this happen?
alancutter (OOO until 2018)
2015/03/06 07:18:58
There are several properties handled by LengthStyl
dstockwell
2015/03/09 02:39:25
Could we add them to the list and then assert not
alancutter (OOO until 2018)
2015/03/09 03:08:59
Done.
| |
| 122 } | |
| 123 } | |
| 124 | |
| 55 namespace { | 125 namespace { |
| 56 | 126 |
| 57 static CSSPrimitiveValue::UnitType toUnitType(int lengthUnitType) | 127 static CSSPrimitiveValue::UnitType toUnitType(int lengthUnitType) |
| 58 { | 128 { |
| 59 return static_cast<CSSPrimitiveValue::UnitType>(CSSPrimitiveValue::lengthUni tTypeToUnitType(static_cast<CSSPrimitiveValue::LengthUnitType>(lengthUnitType))) ; | 129 return static_cast<CSSPrimitiveValue::UnitType>(CSSPrimitiveValue::lengthUni tTypeToUnitType(static_cast<CSSPrimitiveValue::LengthUnitType>(lengthUnitType))) ; |
| 60 } | 130 } |
| 61 | 131 |
| 62 static PassRefPtrWillBeRawPtr<CSSCalcExpressionNode> constructCalcExpression(Pas sRefPtrWillBeRawPtr<CSSCalcExpressionNode> previous, const InterpolableList* lis t, size_t position) | 132 static PassRefPtrWillBeRawPtr<CSSCalcExpressionNode> constructCalcExpression(Pas sRefPtrWillBeRawPtr<CSSCalcExpressionNode> previous, const InterpolableList* lis t, size_t position) |
| 63 { | 133 { |
| 64 const InterpolableList* listOfValues = toInterpolableList(list->get(0)); | 134 const InterpolableList* listOfValues = toInterpolableList(list->get(0)); |
| 65 const InterpolableList* listOfTypes = toInterpolableList(list->get(1)); | 135 const InterpolableList* listOfTypes = toInterpolableList(list->get(1)); |
| 66 while (position != CSSPrimitiveValue::LengthUnitTypeCount) { | 136 while (position != CSSPrimitiveValue::LengthUnitTypeCount) { |
| 67 const InterpolableNumber *subValueType = toInterpolableNumber(listOfType s->get(position)); | 137 const InterpolableNumber *subValueType = toInterpolableNumber(listOfType s->get(position)); |
| 68 if (subValueType->value()) { | 138 if (subValueType->value()) { |
| 69 RefPtrWillBeRawPtr<CSSCalcExpressionNode> next; | 139 RefPtrWillBeRawPtr<CSSCalcExpressionNode> next; |
| 70 double value = toInterpolableNumber(listOfValues->get(position))->va lue(); | 140 double value = toInterpolableNumber(listOfValues->get(position))->va lue(); |
| 71 if (previous) | 141 if (previous) |
| 72 next = CSSCalcValue::createExpressionNode(previous, CSSCalcValue ::createExpressionNode(CSSPrimitiveValue::create(value, toUnitType(position))), CalcAdd); | 142 next = CSSCalcValue::createExpressionNode(previous, CSSCalcValue ::createExpressionNode(CSSPrimitiveValue::create(value, toUnitType(position))), CalcAdd); |
| 73 else | 143 else |
| 74 next = CSSCalcValue::createExpressionNode(CSSPrimitiveValue::cre ate(value, toUnitType(position))); | 144 next = CSSCalcValue::createExpressionNode(CSSPrimitiveValue::cre ate(value, toUnitType(position))); |
| 75 return constructCalcExpression(next, list, position + 1); | 145 return constructCalcExpression(next, list, position + 1); |
| 76 } | 146 } |
| 77 position++; | 147 position++; |
| 78 } | 148 } |
| 79 return previous; | 149 return previous; |
| 80 } | 150 } |
| 81 | 151 |
| 152 static double clampToRange(double x, ValueRange range) | |
| 153 { | |
| 154 return (range == ValueRangeNonNegative && x < 0) ? 0 : x; | |
| 155 } | |
| 156 | |
| 157 static Length lengthFromInterpolableValue(const InterpolableValue& value, Interp olationRange interpolationRange, float zoom) | |
| 158 { | |
| 159 const InterpolableList& values = *toInterpolableList(toInterpolableList(valu e).get(0)); | |
| 160 const InterpolableList& types = *toInterpolableList(toInterpolableList(value ).get(1)); | |
| 161 bool hasPixels = toInterpolableNumber(types.get(CSSPrimitiveValue::UnitTypeP ixels))->value(); | |
| 162 bool hasPercent = toInterpolableNumber(types.get(CSSPrimitiveValue::UnitType Percentage))->value(); | |
| 163 | |
| 164 ValueRange range = (interpolationRange == RangeNonNegative) ? ValueRangeNonN egative : ValueRangeAll; | |
| 165 PixelsAndPercent pixelsAndPercent(0, 0); | |
| 166 if (hasPixels) | |
| 167 pixelsAndPercent.pixels = toInterpolableNumber(values.get(CSSPrimitiveVa lue::UnitTypePixels))->value() * zoom; | |
| 168 if (hasPercent) | |
| 169 pixelsAndPercent.percent = toInterpolableNumber(values.get(CSSPrimitiveV alue::UnitTypePercentage))->value(); | |
| 170 | |
| 171 if (hasPixels && hasPercent) | |
| 172 return Length(CalculationValue::create(pixelsAndPercent, range)); | |
| 173 if (hasPixels) | |
| 174 return Length(clampToRange(pixelsAndPercent.pixels, range), Fixed); | |
| 175 if (hasPercent) | |
| 176 return Length(clampToRange(pixelsAndPercent.percent, range), Percent); | |
| 177 ASSERT_NOT_REACHED(); | |
| 178 return Length(0, Fixed); | |
| 179 } | |
| 180 | |
| 82 } | 181 } |
| 83 | 182 |
| 84 PassRefPtrWillBeRawPtr<CSSPrimitiveValue> LengthStyleInterpolation::fromInterpol ableValue(const InterpolableValue& value, InterpolationRange range) | 183 PassRefPtrWillBeRawPtr<CSSPrimitiveValue> LengthStyleInterpolation::fromInterpol ableValue(const InterpolableValue& value, InterpolationRange range) |
| 85 { | 184 { |
| 86 const InterpolableList* listOfValuesAndTypes = toInterpolableList(&value); | 185 const InterpolableList* listOfValuesAndTypes = toInterpolableList(&value); |
| 87 const InterpolableList* listOfValues = toInterpolableList(listOfValuesAndTyp es->get(0)); | 186 const InterpolableList* listOfValues = toInterpolableList(listOfValuesAndTyp es->get(0)); |
| 88 const InterpolableList* listOfTypes = toInterpolableList(listOfValuesAndType s->get(1)); | 187 const InterpolableList* listOfTypes = toInterpolableList(listOfValuesAndType s->get(1)); |
| 89 unsigned unitTypeCount = 0; | 188 unsigned unitTypeCount = 0; |
| 90 for (size_t i = 0; i < CSSPrimitiveValue::LengthUnitTypeCount; i++) { | 189 for (size_t i = 0; i < CSSPrimitiveValue::LengthUnitTypeCount; i++) { |
| 91 const InterpolableNumber* subType = toInterpolableNumber(listOfTypes->ge t(i)); | 190 const InterpolableNumber* subType = toInterpolableNumber(listOfTypes->ge t(i)); |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 110 } | 209 } |
| 111 ASSERT_NOT_REACHED(); | 210 ASSERT_NOT_REACHED(); |
| 112 default: | 211 default: |
| 113 ValueRange valueRange = (range == RangeNonNegative) ? ValueRangeNonNegat ive : ValueRangeAll; | 212 ValueRange valueRange = (range == RangeNonNegative) ? ValueRangeNonNegat ive : ValueRangeAll; |
| 114 return CSSPrimitiveValue::create(CSSCalcValue::create(constructCalcExpre ssion(nullptr, listOfValuesAndTypes, 0), valueRange)); | 213 return CSSPrimitiveValue::create(CSSCalcValue::create(constructCalcExpre ssion(nullptr, listOfValuesAndTypes, 0), valueRange)); |
| 115 } | 214 } |
| 116 } | 215 } |
| 117 | 216 |
| 118 void LengthStyleInterpolation::apply(StyleResolverState& state) const | 217 void LengthStyleInterpolation::apply(StyleResolverState& state) const |
| 119 { | 218 { |
| 120 StyleBuilder::applyProperty(m_id, state, fromInterpolableValue(*m_cachedValu e.get(), m_range).get()); | 219 if (m_lengthSetter) |
| 220 (state.style()->*m_lengthSetter)(lengthFromInterpolableValue(*m_cachedVa lue, m_range, state.style()->effectiveZoom())); | |
| 221 else | |
| 222 StyleBuilder::applyProperty(m_id, state, fromInterpolableValue(*m_cached Value, m_range).get()); | |
|
dstockwell
2015/03/06 02:34:57
Is there a way that we could assert that both path
alancutter (OOO until 2018)
2015/03/06 07:18:58
Done, though you might want to check if this ASSER
| |
| 223 | |
|
Eric Willigers
2015/03/06 02:22:08
Unneeded blank line
alancutter (OOO until 2018)
2015/03/06 07:18:58
Done.
| |
| 121 } | 224 } |
| 122 | 225 |
| 123 DEFINE_TRACE(LengthStyleInterpolation) | 226 DEFINE_TRACE(LengthStyleInterpolation) |
| 124 { | 227 { |
| 125 StyleInterpolation::trace(visitor); | 228 StyleInterpolation::trace(visitor); |
| 126 } | 229 } |
| 127 | 230 |
| 128 } | 231 } |
| OLD | NEW |