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 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895
|
// Copyright 2019 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/platform/geometry/calculation_expression_node.h"
#include <algorithm>
#include <cfloat>
#include <numeric>
#include "base/memory/scoped_refptr.h"
#include "base/notreached.h"
#include "third_party/blink/renderer/platform/geometry/length.h"
#include "third_party/blink/renderer/platform/geometry/length_functions.h"
#include "third_party/blink/renderer/platform/geometry/math_functions.h"
#include "third_party/blink/renderer/platform/wtf/math_extras.h"
namespace blink {
// ------ CalculationExpressionNumberNode ------
float CalculationExpressionNumberNode::Evaluate(float max_value,
const EvaluationInput&) const {
return value_;
}
bool CalculationExpressionNumberNode::Equals(
const CalculationExpressionNode& other) const {
auto* other_number = DynamicTo<CalculationExpressionNumberNode>(other);
if (!other_number) {
return false;
}
return value_ == other_number->Value();
}
scoped_refptr<const CalculationExpressionNode>
CalculationExpressionNumberNode::Zoom(double) const {
return base::MakeRefCounted<CalculationExpressionNumberNode>(value_);
}
#if DCHECK_IS_ON()
CalculationExpressionNode::ResultType
CalculationExpressionNumberNode::ResolvedResultType() const {
return result_type_;
}
#endif
// ------ CalculationExpressionSizingKeywordNode ------
CalculationExpressionSizingKeywordNode::CalculationExpressionSizingKeywordNode(
Keyword keyword)
: keyword_(keyword) {
if (keyword != Keyword::kSize && keyword != Keyword::kAny) {
if (keyword == Keyword::kAuto) {
has_auto_ = true;
} else if (keyword == Keyword::kWebkitFillAvailable ||
keyword == Keyword::kStretch) {
has_stretch_ = true;
} else {
has_content_or_intrinsic_ = true;
}
}
#if DCHECK_IS_ON()
result_type_ = ResultType::kPixelsAndPercent;
#endif
}
float CalculationExpressionSizingKeywordNode::Evaluate(
float max_value,
const EvaluationInput& input) const {
Length::Type intrinsic_type = Length::kFixed;
switch (keyword_) {
case Keyword::kSize:
CHECK(input.size_keyword_basis);
return *input.size_keyword_basis;
case Keyword::kAny:
return 0.0f;
case Keyword::kAuto:
intrinsic_type = Length::Type::kAuto;
break;
case Keyword::kContent:
intrinsic_type =
input.calc_size_keyword_behavior == CalcSizeKeywordBehavior::kAsAuto
? Length::Type::kAuto
: Length::Type::kContent;
break;
case Keyword::kMinContent:
case Keyword::kWebkitMinContent:
CHECK_EQ(input.calc_size_keyword_behavior,
CalcSizeKeywordBehavior::kAsSpecified);
intrinsic_type = Length::Type::kMinContent;
break;
case Keyword::kMaxContent:
case Keyword::kWebkitMaxContent:
CHECK_EQ(input.calc_size_keyword_behavior,
CalcSizeKeywordBehavior::kAsSpecified);
intrinsic_type = Length::Type::kMaxContent;
break;
case Keyword::kFitContent:
case Keyword::kWebkitFitContent:
intrinsic_type =
input.calc_size_keyword_behavior == CalcSizeKeywordBehavior::kAsAuto
? Length::Type::kAuto
: Length::Type::kFitContent;
break;
case Keyword::kStretch:
case Keyword::kWebkitFillAvailable:
intrinsic_type =
input.calc_size_keyword_behavior == CalcSizeKeywordBehavior::kAsAuto
? Length::Type::kAuto
: Length::Type::kFillAvailable;
break;
}
if (!input.intrinsic_evaluator) {
// TODO(https://crbug.com/313072): I'd like to be able to CHECK() this
// instead. However, we hit this code in three cases:
// * the code in ContentMinimumInlineSize, which passes max_value of 0
// * the (questionable) code in EvaluateValueIfNaNorInfinity(), which
// passes max_value of 1 or -1
// * the DCHECK()s in
// CSSLengthInterpolationType::ApplyStandardPropertyValue pass a max
// value of 100
// So we have to return something. Return 0 for now, though this may
// not be ideal.
CHECK(max_value == 1.0f || max_value == -1.0f || max_value == 0.0f ||
max_value == 100.0f);
return 0.0f;
}
CHECK(input.intrinsic_evaluator);
return (*input.intrinsic_evaluator)(Length(intrinsic_type));
}
// ------ CalculationExpressionColorChannelKeywordNode ------
CalculationExpressionColorChannelKeywordNode::
CalculationExpressionColorChannelKeywordNode(ColorChannelKeyword channel)
: channel_(channel) {
has_color_channel_keyword_ = true;
}
float CalculationExpressionColorChannelKeywordNode::Evaluate(
float max_value,
const EvaluationInput& evaluation_input) const {
// If the calling code hasn't set up the input environment, then always
// return zero.
if (evaluation_input.color_channel_keyword_values.empty()) {
return 0;
}
return evaluation_input.color_channel_keyword_values.at(channel_);
}
// ------ CalculationExpressionPixelsAndPercentNode ------
float CalculationExpressionPixelsAndPercentNode::Evaluate(
float max_value,
const EvaluationInput&) const {
return value_.pixels + value_.percent / 100 * max_value;
}
bool CalculationExpressionPixelsAndPercentNode::Equals(
const CalculationExpressionNode& other) const {
auto* other_pixels_and_percent =
DynamicTo<CalculationExpressionPixelsAndPercentNode>(other);
if (!other_pixels_and_percent) {
return false;
}
return value_.pixels == other_pixels_and_percent->value_.pixels &&
value_.percent == other_pixels_and_percent->value_.percent;
}
scoped_refptr<const CalculationExpressionNode>
CalculationExpressionPixelsAndPercentNode::Zoom(double factor) const {
PixelsAndPercent result(value_.pixels * factor, value_.percent,
value_.has_explicit_pixels,
value_.has_explicit_percent);
return base::MakeRefCounted<CalculationExpressionPixelsAndPercentNode>(
result);
}
#if DCHECK_IS_ON()
CalculationExpressionNode::ResultType
CalculationExpressionPixelsAndPercentNode::ResolvedResultType() const {
return result_type_;
}
#endif
// ------ CalculationExpressionOperationNode ------
namespace {
bool ShouldConvertRad2DegForOperator(CalculationOperator op) {
return op == CalculationOperator::kSin || op == CalculationOperator::kCos ||
op == CalculationOperator::kTan;
}
} // namespace
// static
scoped_refptr<const CalculationExpressionNode>
CalculationExpressionOperationNode::CreateSimplified(Children&& children,
CalculationOperator op) {
switch (op) {
case CalculationOperator::kAdd:
case CalculationOperator::kSubtract: {
DCHECK_EQ(children.size(), 2u);
if (!children[0]->IsPixelsAndPercent() ||
!children[1]->IsPixelsAndPercent()) {
return base::MakeRefCounted<CalculationExpressionOperationNode>(
Children({std::move(children[0]), std::move(children[1])}), op);
}
const auto& left_pixels_and_percent =
To<CalculationExpressionPixelsAndPercentNode>(*children[0]);
PixelsAndPercent right_pixels_and_percent =
To<CalculationExpressionPixelsAndPercentNode>(*children[1])
.GetPixelsAndPercent();
PixelsAndPercent value = left_pixels_and_percent.GetPixelsAndPercent();
if (op == CalculationOperator::kAdd) {
value += right_pixels_and_percent;
} else {
value -= right_pixels_and_percent;
}
return base::MakeRefCounted<CalculationExpressionPixelsAndPercentNode>(
value);
}
case CalculationOperator::kMultiply: {
DCHECK_EQ(children.size(), 2u);
if (children.front()->IsOperation() || children.back()->IsOperation()) {
return base::MakeRefCounted<CalculationExpressionOperationNode>(
Children({std::move(children[0]), std::move(children[1])}), op);
}
auto& maybe_pixels_and_percent_node =
children[0]->IsNumber() ? children[1] : children[0];
if (!maybe_pixels_and_percent_node->IsPixelsAndPercent()) {
return base::MakeRefCounted<CalculationExpressionOperationNode>(
Children({std::move(children[0]), std::move(children[1])}), op);
}
auto& number_node = children[0]->IsNumber() ? children[0] : children[1];
const auto& number = To<CalculationExpressionNumberNode>(*number_node);
PixelsAndPercent pixels_and_percent =
To<CalculationExpressionPixelsAndPercentNode>(
*maybe_pixels_and_percent_node)
.GetPixelsAndPercent();
pixels_and_percent *= number.Value();
return base::MakeRefCounted<CalculationExpressionPixelsAndPercentNode>(
pixels_and_percent);
}
case CalculationOperator::kInvert: {
DCHECK_EQ(children.size(), 1u);
auto* number = DynamicTo<CalculationExpressionNumberNode>(*children[0]);
if (number) {
return base::MakeRefCounted<CalculationExpressionNumberNode>(
1.0 / number->Value());
}
return base::MakeRefCounted<CalculationExpressionOperationNode>(
Children({std::move(children[0])}), op);
}
case CalculationOperator::kMin:
case CalculationOperator::kMax: {
DCHECK(children.size());
float simplified_px;
bool can_simplify = true;
for (wtf_size_t i = 0; i < children.size(); ++i) {
const auto* pixels_and_percent =
DynamicTo<CalculationExpressionPixelsAndPercentNode>(*children[i]);
if (!pixels_and_percent || pixels_and_percent->Percent()) {
can_simplify = false;
break;
}
if (!i) {
simplified_px = pixels_and_percent->Pixels();
} else {
if (op == CalculationOperator::kMin) {
simplified_px =
std::min(simplified_px, pixels_and_percent->Pixels());
} else {
simplified_px =
std::max(simplified_px, pixels_and_percent->Pixels());
}
}
}
if (can_simplify) {
return base::MakeRefCounted<CalculationExpressionPixelsAndPercentNode>(
PixelsAndPercent(simplified_px));
}
return base::MakeRefCounted<CalculationExpressionOperationNode>(
std::move(children), op);
}
case CalculationOperator::kClamp: {
DCHECK_EQ(children.size(), 3u);
Vector<float> operand_pixels;
operand_pixels.reserve(children.size());
bool can_simplify = true;
for (auto& child : children) {
const auto* pixels_and_percent =
DynamicTo<CalculationExpressionPixelsAndPercentNode>(*child);
if (!pixels_and_percent || pixels_and_percent->Percent()) {
can_simplify = false;
break;
}
operand_pixels.push_back(pixels_and_percent->Pixels());
}
if (can_simplify) {
float min_px = operand_pixels[0];
float val_px = operand_pixels[1];
float max_px = operand_pixels[2];
// clamp(MIN, VAL, MAX) is identical to max(MIN, min(VAL, MAX))
// according to the spec,
// https://drafts.csswg.org/css-values-4/#funcdef-clamp.
float clamped_px = std::max(min_px, std::min(val_px, max_px));
return base::MakeRefCounted<CalculationExpressionPixelsAndPercentNode>(
PixelsAndPercent(clamped_px));
}
return base::MakeRefCounted<CalculationExpressionOperationNode>(
std::move(children), op);
}
case CalculationOperator::kRoundNearest:
case CalculationOperator::kRoundUp:
case CalculationOperator::kRoundDown:
case CalculationOperator::kRoundToZero:
case CalculationOperator::kMod:
case CalculationOperator::kRem: {
DCHECK_EQ(children.size(), 2u);
const auto* a =
DynamicTo<CalculationExpressionPixelsAndPercentNode>(*children[0]);
const auto* b =
DynamicTo<CalculationExpressionPixelsAndPercentNode>(*children[1]);
bool can_simplify = a && !a->Percent() && b && !b->Percent();
if (can_simplify) {
float value =
EvaluateSteppedValueFunction(op, a->Pixels(), b->Pixels());
return base::MakeRefCounted<CalculationExpressionPixelsAndPercentNode>(
PixelsAndPercent(value));
} else {
return base::MakeRefCounted<CalculationExpressionOperationNode>(
std::move(children), op);
}
}
case CalculationOperator::kLog: {
DCHECK_GE(children.size(), 1u);
DCHECK_LE(children.size(), 2u);
Vector<float> operand_pixels;
operand_pixels.reserve(children.size());
bool can_simplify = true;
for (auto& child : children) {
const auto* pixels_and_percent =
DynamicTo<CalculationExpressionPixelsAndPercentNode>(*child);
if (!pixels_and_percent || pixels_and_percent->Percent()) {
can_simplify = false;
break;
}
operand_pixels.push_back(pixels_and_percent->Pixels());
}
if (can_simplify) {
float value = operand_pixels.size() == 1u
? std::log(operand_pixels.front())
: std::log2(operand_pixels.front()) /
std::log2(operand_pixels.back());
return base::MakeRefCounted<CalculationExpressionPixelsAndPercentNode>(
PixelsAndPercent(value));
}
return base::MakeRefCounted<CalculationExpressionOperationNode>(
std::move(children), op);
}
case CalculationOperator::kHypot: {
DCHECK_GE(children.size(), 1u);
Vector<float> operand_pixels;
operand_pixels.reserve(children.size());
bool can_simplify = true;
for (auto& child : children) {
const auto* pixels_and_percent =
DynamicTo<CalculationExpressionPixelsAndPercentNode>(*child);
if (!pixels_and_percent || pixels_and_percent->Percent()) {
can_simplify = false;
break;
}
operand_pixels.push_back(pixels_and_percent->Pixels());
}
if (can_simplify) {
float value = 0;
for (float operand : operand_pixels) {
value = std::hypot(value, operand);
}
return base::MakeRefCounted<CalculationExpressionPixelsAndPercentNode>(
PixelsAndPercent(value));
}
return base::MakeRefCounted<CalculationExpressionOperationNode>(
std::move(children), op);
}
case CalculationOperator::kSin:
case CalculationOperator::kCos:
case CalculationOperator::kTan:
case CalculationOperator::kAsin:
case CalculationOperator::kAcos:
case CalculationOperator::kAtan:
case CalculationOperator::kAbs:
case CalculationOperator::kExp:
case CalculationOperator::kSqrt:
case CalculationOperator::kSign: {
DCHECK_EQ(children.size(), 1u);
const auto* pixels_and_percent =
DynamicTo<CalculationExpressionPixelsAndPercentNode>(
*children.front());
if (!pixels_and_percent || pixels_and_percent->Percent()) {
return base::MakeRefCounted<CalculationExpressionOperationNode>(
std::move(children), op);
}
float value = pixels_and_percent->Pixels();
if (op == CalculationOperator::kAbs) {
return base::MakeRefCounted<CalculationExpressionPixelsAndPercentNode>(
PixelsAndPercent(std::abs(value)));
} else if (op == CalculationOperator::kSign) {
if (value == 0 || std::isnan(value)) {
return base::MakeRefCounted<CalculationExpressionNumberNode>(value);
}
return base::MakeRefCounted<CalculationExpressionNumberNode>(
value > 0 ? 1 : -1);
} else if (op == CalculationOperator::kExp) {
return base::MakeRefCounted<CalculationExpressionNumberNode>(
std::exp(value));
} else if (op == CalculationOperator::kSqrt) {
return base::MakeRefCounted<CalculationExpressionNumberNode>(
std::sqrt(value));
} else {
if (ShouldConvertRad2DegForOperator(op) &&
children.front()->IsNumber()) {
value = Rad2deg(value);
}
value = EvaluateTrigonometricFunction(op, value);
return base::MakeRefCounted<CalculationExpressionNumberNode>(value);
}
}
case CalculationOperator::kProgress:
case CalculationOperator::kMediaProgress:
case CalculationOperator::kContainerProgress: {
DCHECK_EQ(children.size(), 3u);
Vector<float, 3> operand_pixels;
bool can_simplify = true;
for (scoped_refptr<const CalculationExpressionNode>& child : children) {
const auto* pixels_and_percent =
DynamicTo<CalculationExpressionPixelsAndPercentNode>(*child);
if (!pixels_and_percent || pixels_and_percent->Percent()) {
can_simplify = false;
break;
}
operand_pixels.push_back(pixels_and_percent->Pixels());
}
if (can_simplify) {
float progress_px = operand_pixels[0];
float from_px = operand_pixels[1];
float to_px = operand_pixels[2];
float progress_value = (progress_px - from_px) / (to_px - from_px);
float progress = std::clamp(progress_value, 0.f, 1.f);
return base::MakeRefCounted<CalculationExpressionPixelsAndPercentNode>(
PixelsAndPercent(progress));
}
return base::MakeRefCounted<CalculationExpressionOperationNode>(
std::move(children), op);
}
case CalculationOperator::kCalcSize: {
DCHECK_EQ(children.size(), 2u);
// TODO(https://crbug.com/313072): It may be worth implementing
// simplification for calc-size(), but it's not likely to be possible to
// simplify calc-size() in any of its real use cases.
return base::MakeRefCounted<CalculationExpressionOperationNode>(
std::move(children), op);
}
case CalculationOperator::kPow: {
DCHECK_EQ(children.size(), 2u);
if (children.front()->IsNumber() && children.back()->IsNumber()) {
float value = std::pow(
To<CalculationExpressionNumberNode>(*children.front()).Value(),
To<CalculationExpressionNumberNode>(*children.back()).Value());
return base::MakeRefCounted<CalculationExpressionNumberNode>(value);
}
return base::MakeRefCounted<CalculationExpressionOperationNode>(
std::move(children), op);
}
case CalculationOperator::kInvalid:
NOTREACHED();
case CalculationOperator::kAtan2: {
DCHECK_EQ(children.size(), 2u);
const auto* a =
DynamicTo<CalculationExpressionPixelsAndPercentNode>(*children[0]);
const auto* b =
DynamicTo<CalculationExpressionPixelsAndPercentNode>(*children[1]);
bool can_simplify = a && !a->Percent() && b && !b->Percent();
if (can_simplify) {
float value =
EvaluateTrigonometricFunction(op, a->Pixels(), {b->Pixels()});
return base::MakeRefCounted<CalculationExpressionPixelsAndPercentNode>(
PixelsAndPercent(value));
} else {
return base::MakeRefCounted<CalculationExpressionOperationNode>(
std::move(children), op);
}
}
}
}
CalculationExpressionOperationNode::CalculationExpressionOperationNode(
Children&& children,
CalculationOperator op)
: children_(std::move(children)), operator_(op) {
#if DCHECK_IS_ON()
result_type_ = ResolvedResultType();
DCHECK_NE(result_type_, ResultType::kInvalid);
#endif
if (op == CalculationOperator::kCalcSize) {
// "A calc-size() is treated, in all respects, as if it were its
// calc-size basis." This is particularly relevant for ignoring the
// presence of percentages in the calculation.
CHECK_EQ(children_.size(), 2u);
const auto& basis = children_[0];
has_content_or_intrinsic_ = basis->HasContentOrIntrinsicSize();
has_auto_ = basis->HasAuto();
has_percent_ = basis->HasPercent();
has_stretch_ = basis->HasStretch();
#if DCHECK_IS_ON()
{
const auto& calculation = children_[1];
DCHECK(!calculation->HasAuto());
DCHECK(!calculation->HasContentOrIntrinsicSize());
DCHECK(!calculation->HasStretch());
}
#endif
} else {
for (const auto& child : children_) {
DCHECK(!child->HasAuto());
DCHECK(!child->HasContentOrIntrinsicSize());
DCHECK(!child->HasStretch());
if (child->HasPercent()) {
has_percent_ = true;
}
if (child->HasColorChannelKeyword()) {
has_color_channel_keyword_ = true;
}
}
}
}
float CalculationExpressionOperationNode::Evaluate(
float max_value,
const EvaluationInput& input) const {
switch (operator_) {
case CalculationOperator::kAdd: {
DCHECK_EQ(children_.size(), 2u);
float left = children_[0]->Evaluate(max_value, input);
float right = children_[1]->Evaluate(max_value, input);
return left + right;
}
case CalculationOperator::kSubtract: {
DCHECK_EQ(children_.size(), 2u);
float left = children_[0]->Evaluate(max_value, input);
float right = children_[1]->Evaluate(max_value, input);
return left - right;
}
case CalculationOperator::kMultiply: {
DCHECK_EQ(children_.size(), 2u);
float left = children_[0]->Evaluate(max_value, input);
float right = children_[1]->Evaluate(max_value, input);
return left * right;
}
case CalculationOperator::kInvert: {
DCHECK_EQ(children_.size(), 1u);
float denominator = children_[0]->Evaluate(max_value, input);
return 1.0 / denominator;
}
case CalculationOperator::kMin: {
DCHECK(!children_.empty());
float minimum = children_[0]->Evaluate(max_value, input);
for (auto& child : children_) {
minimum = std::min(minimum, child->Evaluate(max_value, input));
}
return minimum;
}
case CalculationOperator::kMax: {
DCHECK(!children_.empty());
float maximum = children_[0]->Evaluate(max_value, input);
for (auto& child : children_) {
maximum = std::max(maximum, child->Evaluate(max_value, input));
}
return maximum;
}
case CalculationOperator::kClamp: {
DCHECK(!children_.empty());
float min = children_[0]->Evaluate(max_value, input);
float val = children_[1]->Evaluate(max_value, input);
float max = children_[2]->Evaluate(max_value, input);
// clamp(MIN, VAL, MAX) is identical to max(MIN, min(VAL, MAX))
return std::max(min, std::min(val, max));
}
case CalculationOperator::kRoundNearest:
case CalculationOperator::kRoundUp:
case CalculationOperator::kRoundDown:
case CalculationOperator::kRoundToZero:
case CalculationOperator::kMod:
case CalculationOperator::kRem: {
DCHECK_EQ(children_.size(), 2u);
float a = children_[0]->Evaluate(max_value, input);
float b = children_[1]->Evaluate(max_value, input);
return EvaluateSteppedValueFunction(operator_, a, b);
}
case CalculationOperator::kLog: {
DCHECK_GE(children_.size(), 1u);
DCHECK_LE(children_.size(), 2u);
return children_.size() == 1u
? std::log(children_.front()->Evaluate(max_value, input))
: std::log2(children_.front()->Evaluate(max_value, input)) /
std::log2(children_.back()->Evaluate(max_value, input));
}
case CalculationOperator::kHypot: {
DCHECK_GE(children_.size(), 1u);
float value = 0;
for (scoped_refptr<const CalculationExpressionNode> operand : children_) {
float a = operand->Evaluate(max_value, input);
value = std::hypot(value, a);
}
return value;
}
case CalculationOperator::kAbs:
case CalculationOperator::kExp:
case CalculationOperator::kSqrt:
case CalculationOperator::kSign: {
DCHECK_EQ(children_.size(), 1u);
const float value = children_.front()->Evaluate(max_value, input);
if (operator_ == CalculationOperator::kAbs) {
return std::abs(value);
} else if (operator_ == CalculationOperator::kExp) {
return std::exp(value);
} else if (operator_ == CalculationOperator::kSign) {
if (value == 0 || std::isnan(value)) {
return value;
}
return value > 0 ? 1 : -1;
} else {
return std::sqrt(value);
}
}
case CalculationOperator::kCalcSize: {
DCHECK_EQ(children_.size(), 2u);
EvaluationInput calculation_input(input);
calculation_input.size_keyword_basis =
children_[0]->Evaluate(max_value, input);
if (max_value == kIndefiniteSize.ToFloat()) {
// "When evaluating the calc-size calculation, if percentages are not
// definite in the given context, the resolve to 0px. Otherwise, they
// resolve as normal."
// -- https://drafts.csswg.org/css-values-5/#resolving-calc-size
max_value = 0.0f;
}
return children_[1]->Evaluate(max_value, calculation_input);
}
case CalculationOperator::kProgress:
case CalculationOperator::kMediaProgress:
case CalculationOperator::kContainerProgress: {
DCHECK(!children_.empty());
float progress = children_[0]->Evaluate(max_value, input);
float from = children_[1]->Evaluate(max_value, input);
float to = children_[2]->Evaluate(max_value, input);
float progress_value = (progress - from) / (to - from);
return std::clamp(progress_value, 0.f, 1.f);
}
case CalculationOperator::kPow: {
DCHECK_EQ(children_.size(), 2u);
float a = children_[0]->Evaluate(max_value, input);
float b = children_[1]->Evaluate(max_value, input);
return std::pow(a, b);
}
case CalculationOperator::kSin:
case CalculationOperator::kCos:
case CalculationOperator::kTan:
case CalculationOperator::kAsin:
case CalculationOperator::kAcos:
case CalculationOperator::kAtan:
case CalculationOperator::kAtan2: {
float a = children_[0]->Evaluate(max_value, input);
if (ShouldConvertRad2DegForOperator(operator_) &&
children_.front()->IsNumber()) {
a = Rad2deg(a);
}
std::optional<float> b =
operator_ == CalculationOperator::kAtan2
? std::optional<float>(children_[1]->Evaluate(max_value, input))
: std::nullopt;
return EvaluateTrigonometricFunction(operator_, a, b);
}
case CalculationOperator::kInvalid:
break;
// TODO(crbug.com/1284199): Support other math functions.
}
NOTREACHED();
}
bool CalculationExpressionOperationNode::Equals(
const CalculationExpressionNode& other) const {
auto* other_operation = DynamicTo<CalculationExpressionOperationNode>(other);
if (!other_operation) {
return false;
}
if (operator_ != other_operation->GetOperator()) {
return false;
}
using ValueType = Children::value_type;
return std::ranges::equal(
children_, other_operation->GetChildren(),
[](const ValueType& a, const ValueType& b) { return *a == *b; });
}
scoped_refptr<const CalculationExpressionNode>
CalculationExpressionOperationNode::Zoom(double factor) const {
switch (operator_) {
case CalculationOperator::kAdd:
case CalculationOperator::kSubtract:
DCHECK_EQ(children_.size(), 2u);
return CreateSimplified(
Children({children_[0]->Zoom(factor), children_[1]->Zoom(factor)}),
operator_);
case CalculationOperator::kMultiply: {
DCHECK_EQ(children_.size(), 2u);
auto& number = children_[0]->IsNumber() ? children_[0] : children_[1];
auto& pixels_and_percent =
children_[0]->IsNumber() ? children_[1] : children_[0];
return CreateSimplified(
Children({pixels_and_percent->Zoom(factor), number}), operator_);
}
case CalculationOperator::kInvert: {
DCHECK_EQ(children_.size(), 1u);
return CreateSimplified(Children({children_[0]->Zoom(factor)}),
operator_);
}
case CalculationOperator::kCalcSize: {
DCHECK_EQ(children_.size(), 2u);
return CreateSimplified(
Children({children_[0], children_[1]->Zoom(factor)}), operator_);
}
case CalculationOperator::kMin:
case CalculationOperator::kMax:
case CalculationOperator::kClamp:
case CalculationOperator::kRoundNearest:
case CalculationOperator::kRoundUp:
case CalculationOperator::kRoundDown:
case CalculationOperator::kRoundToZero:
case CalculationOperator::kMod:
case CalculationOperator::kRem:
case CalculationOperator::kHypot:
case CalculationOperator::kAbs:
case CalculationOperator::kLog:
case CalculationOperator::kExp:
case CalculationOperator::kSqrt:
case CalculationOperator::kSign:
case CalculationOperator::kProgress:
case CalculationOperator::kMediaProgress:
case CalculationOperator::kContainerProgress:
case CalculationOperator::kSin:
case CalculationOperator::kCos:
case CalculationOperator::kTan:
case CalculationOperator::kAsin:
case CalculationOperator::kAcos:
case CalculationOperator::kAtan:
case CalculationOperator::kAtan2:
case CalculationOperator::kPow: {
DCHECK(children_.size());
Vector<scoped_refptr<const CalculationExpressionNode>> cloned_operands;
cloned_operands.reserve(children_.size());
for (const auto& child : children_)
cloned_operands.push_back(child->Zoom(factor));
return CreateSimplified(std::move(cloned_operands), operator_);
}
case CalculationOperator::kInvalid:
NOTREACHED();
}
}
bool CalculationExpressionOperationNode::HasMinContent() const {
if (operator_ != CalculationOperator::kCalcSize) {
return false;
}
CHECK_EQ(children_.size(), 2u);
const auto& basis = children_[0];
return basis->HasMinContent();
}
bool CalculationExpressionOperationNode::HasMaxContent() const {
if (operator_ != CalculationOperator::kCalcSize) {
return false;
}
CHECK_EQ(children_.size(), 2u);
const auto& basis = children_[0];
return basis->HasMaxContent();
}
bool CalculationExpressionOperationNode::HasFitContent() const {
if (operator_ != CalculationOperator::kCalcSize) {
return false;
}
CHECK_EQ(children_.size(), 2u);
const auto& basis = children_[0];
return basis->HasFitContent();
}
#if DCHECK_IS_ON()
CalculationExpressionNode::ResultType
CalculationExpressionOperationNode::ResolvedResultType() const {
switch (operator_) {
case CalculationOperator::kAdd:
case CalculationOperator::kSubtract: {
DCHECK_EQ(children_.size(), 2u);
auto left_type = children_[0]->ResolvedResultType();
auto right_type = children_[1]->ResolvedResultType();
if (left_type == ResultType::kInvalid ||
right_type == ResultType::kInvalid || left_type != right_type)
return ResultType::kInvalid;
return left_type;
}
case CalculationOperator::kMultiply: {
DCHECK_EQ(children_.size(), 2u);
auto left_type = children_[0]->ResolvedResultType();
auto right_type = children_[1]->ResolvedResultType();
if (left_type == ResultType::kInvalid ||
right_type == ResultType::kInvalid ||
(left_type == ResultType::kPixelsAndPercent &&
right_type == ResultType::kPixelsAndPercent))
return ResultType::kInvalid;
if ((left_type == ResultType::kPixelsAndPercent &&
right_type == ResultType::kNumber) ||
(left_type == ResultType::kNumber &&
right_type == ResultType::kPixelsAndPercent))
return ResultType::kPixelsAndPercent;
return ResultType::kNumber;
}
case CalculationOperator::kInvert: {
DCHECK_EQ(children_.size(), 1u);
auto denominator_type = children_[0]->ResolvedResultType();
if (denominator_type == ResultType::kNumber) {
return ResultType::kNumber;
}
return ResultType::kInvalid;
}
case CalculationOperator::kCalcSize: {
DCHECK_EQ(children_.size(), 2u);
auto basis_type = children_[0]->ResolvedResultType();
auto calculation_type = children_[1]->ResolvedResultType();
if (basis_type != ResultType::kPixelsAndPercent ||
calculation_type != ResultType::kPixelsAndPercent) {
return ResultType::kInvalid;
}
return ResultType::kPixelsAndPercent;
}
case CalculationOperator::kMin:
case CalculationOperator::kMax:
case CalculationOperator::kClamp:
case CalculationOperator::kRoundNearest:
case CalculationOperator::kRoundUp:
case CalculationOperator::kRoundDown:
case CalculationOperator::kRoundToZero:
case CalculationOperator::kMod:
case CalculationOperator::kRem:
case CalculationOperator::kSqrt:
case CalculationOperator::kHypot:
case CalculationOperator::kAbs: {
DCHECK(children_.size());
auto first_child_type = children_.front()->ResolvedResultType();
for (const auto& child : children_) {
if (first_child_type != child->ResolvedResultType())
return ResultType::kInvalid;
}
return first_child_type;
}
case CalculationOperator::kSign:
case CalculationOperator::kContainerProgress:
case CalculationOperator::kProgress:
case CalculationOperator::kMediaProgress:
case CalculationOperator::kLog:
case CalculationOperator::kExp:
case CalculationOperator::kSin:
case CalculationOperator::kCos:
case CalculationOperator::kTan:
case CalculationOperator::kAsin:
case CalculationOperator::kAcos:
case CalculationOperator::kAtan:
case CalculationOperator::kAtan2:
case CalculationOperator::kPow:
return ResultType::kNumber;
case CalculationOperator::kInvalid:
NOTREACHED();
}
}
#endif
} // namespace blink
|