1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
|
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "third_party/blink/renderer/core/animation/interpolable_value.h"
#include <memory>
#include "third_party/blink/renderer/core/animation/css_color_interpolation_type.h"
#include "third_party/blink/renderer/core/animation/interpolable_style_color.h"
#include "third_party/blink/renderer/core/css/css_math_expression_node.h"
#include "third_party/blink/renderer/core/css/css_math_function_value.h"
#include "third_party/blink/renderer/core/css/css_numeric_literal_value.h"
namespace blink {
namespace {
using UnitType = CSSPrimitiveValue::UnitType;
CSSMathExpressionNode* NumberNode(double number,
UnitType unit_type = UnitType::kNumber) {
return CSSMathExpressionNumericLiteral::Create(
CSSNumericLiteralValue::Create(number, unit_type));
}
} // namespace
InterpolableNumber::InterpolableNumber(double value, UnitType unit_type) {
SetDouble(value, unit_type);
}
InterpolableNumber::InterpolableNumber(
const CSSMathExpressionNode& expression) {
SetExpression(expression);
}
InterpolableNumber::InterpolableNumber(const CSSPrimitiveValue& value) {
if (const auto* numeric = DynamicTo<CSSNumericLiteralValue>(value)) {
SetDouble(numeric->DoubleValue(), numeric->GetType());
} else {
CHECK(value.IsMathFunctionValue());
const auto& function = To<CSSMathFunctionValue>(value);
SetExpression(*function.ExpressionNode());
}
}
double InterpolableNumber::Value(
const CSSLengthResolver& length_resolver) const {
if (IsDoubleValue()) {
return value_ *
CSSPrimitiveValue::ConversionToCanonicalUnitsScaleFactor(unit_type_);
}
std::optional<double> result =
expression_->ComputeValueInCanonicalUnit(length_resolver);
CHECK(result.has_value());
return result.value();
}
void InterpolableNumber::SetExpression(
const CSSMathExpressionNode& expression) {
type_ = Type::kExpression;
expression_ = &expression;
unit_type_ = expression.ResolvedUnitType();
}
void InterpolableNumber::SetDouble(double value, UnitType unit_type) {
type_ = Type::kDouble;
value_ = value;
unit_type_ = unit_type;
}
const CSSMathExpressionNode& InterpolableNumber::AsExpression() const {
if (IsExpression()) {
return *expression_;
}
return *NumberNode(value_, unit_type_);
}
bool InterpolableNumber::Equals(const InterpolableValue& other) const {
const auto& other_number = To<InterpolableNumber>(other);
if (IsDoubleValue() && other_number.IsDoubleValue() &&
unit_type_ == other_number.unit_type_) {
return value_ == To<InterpolableNumber>(other).value_;
}
return AsExpression() == other_number.AsExpression();
}
bool InterpolableList::Equals(const InterpolableValue& other) const {
const auto& other_list = To<InterpolableList>(other);
if (length() != other_list.length())
return false;
for (wtf_size_t i = 0; i < length(); i++) {
if (!values_[i]->Equals(*other_list.values_[i]))
return false;
}
return true;
}
void InterpolableNumber::AssertCanInterpolateWith(
const InterpolableValue& other) const {
DCHECK(other.IsNumber());
}
double InterpolableNumber::Interpolate(double from,
double to,
double progress) {
if (progress == 0 || from == to) {
return from;
} else if (progress == 1) {
return to;
} else {
return from * (1 - progress) + to * progress;
}
}
void InterpolableNumber::Interpolate(const InterpolableValue& to,
const double progress,
InterpolableValue& result) const {
const auto& to_number = To<InterpolableNumber>(to);
auto& result_number = To<InterpolableNumber>(result);
if (IsDoubleValue() && to_number.IsDoubleValue() &&
unit_type_ == to_number.unit_type_) {
result_number.SetDouble(Interpolate(value_, to_number.Value(), progress),
unit_type_);
return;
}
const CSSMathExpressionNode* blended_from =
CSSMathExpressionOperation::CreateArithmeticOperationAndSimplifyCalcSize(
&AsExpression(), NumberNode(1 - progress),
CSSMathOperator::kMultiply);
const CSSMathExpressionNode* blended_to =
CSSMathExpressionOperation::CreateArithmeticOperationAndSimplifyCalcSize(
&to_number.AsExpression(), NumberNode(progress),
CSSMathOperator::kMultiply);
const CSSMathExpressionNode* result_expression =
CSSMathExpressionOperation::CreateArithmeticOperationAndSimplifyCalcSize(
blended_from, blended_to, CSSMathOperator::kAdd);
result_number.SetExpression(*result_expression);
}
void InterpolableList::AssertCanInterpolateWith(
const InterpolableValue& other) const {
DCHECK(other.IsList());
DCHECK_EQ(To<InterpolableList>(other).length(), length());
}
void InterpolableList::Interpolate(const InterpolableValue& to,
const double progress,
InterpolableValue& result) const {
const auto& to_list = To<InterpolableList>(to);
auto& result_list = To<InterpolableList>(result);
for (wtf_size_t i = 0; i < length(); i++) {
DCHECK(values_[i]);
DCHECK(to_list.values_[i]);
if (values_[i]->IsStyleColor() || to_list.values_[i]->IsStyleColor() ||
result_list.values_[i]->IsStyleColor()) {
CSSColorInterpolationType::EnsureInterpolableStyleColor(result_list, i);
InterpolableStyleColor::Interpolate(*values_[i], *(to_list.values_[i]),
progress, *(result_list.values_[i]));
continue;
}
values_[i]->Interpolate(*(to_list.values_[i]), progress,
*(result_list.values_[i]));
}
}
InterpolableList* InterpolableList::RawCloneAndZero() const {
auto* result = MakeGarbageCollected<InterpolableList>(length());
for (wtf_size_t i = 0; i < length(); i++) {
result->Set(i, values_[i]->CloneAndZero());
}
return result;
}
void InterpolableNumber::Scale(double scale) {
if (IsDoubleValue()) {
value_ *= scale;
return;
}
SetExpression(
*CSSMathExpressionOperation::CreateArithmeticOperationAndSimplifyCalcSize(
&AsExpression(), NumberNode(scale), CSSMathOperator::kMultiply));
}
void InterpolableNumber::Scale(const InterpolableNumber& other) {
if (IsDoubleValue() && other.IsDoubleValue() &&
(unit_type_ == CSSPrimitiveValue::UnitType::kNumber ||
other.unit_type_ == CSSPrimitiveValue::UnitType::kNumber)) {
SetDouble(
value_ * other.value_,
(unit_type_ == CSSPrimitiveValue::UnitType::kNumber ? other.unit_type_
: unit_type_));
return;
}
SetExpression(
*CSSMathExpressionOperation::CreateArithmeticOperationAndSimplifyCalcSize(
&AsExpression(), &other.AsExpression(), CSSMathOperator::kMultiply));
}
void InterpolableList::Scale(double scale) {
for (wtf_size_t i = 0; i < length(); i++)
values_[i]->Scale(scale);
}
void InterpolableNumber::Add(const InterpolableValue& other) {
const auto& other_number = To<InterpolableNumber>(other);
if (IsDoubleValue() && other_number.IsDoubleValue() &&
unit_type_ == other_number.unit_type_) {
value_ += other_number.value_;
return;
}
const CSSMathExpressionNode* result =
CSSMathExpressionOperation::CreateArithmeticOperationAndSimplifyCalcSize(
&AsExpression(), &other_number.AsExpression(), CSSMathOperator::kAdd);
SetExpression(*result);
}
void InterpolableList::Add(const InterpolableValue& other) {
const auto& other_list = To<InterpolableList>(other);
DCHECK_EQ(other_list.length(), length());
for (wtf_size_t i = 0; i < length(); i++)
values_[i]->Add(*other_list.values_[i]);
}
void InterpolableList::ScaleAndAdd(double scale,
const InterpolableValue& other) {
const auto& other_list = To<InterpolableList>(other);
DCHECK_EQ(other_list.length(), length());
for (wtf_size_t i = 0; i < length(); i++)
values_[i]->ScaleAndAdd(scale, *other_list.values_[i]);
}
} // namespace blink
|