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 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115
|
/*
* Copyright (C) 2008-2021 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "CSSGradientValue.h"
#include "CSSCalcSymbolTable.h"
#include "CSSCalcValue.h"
#include "CSSPrimitiveValueMappings.h"
#include "CSSValueKeywords.h"
#include "CalculationValue.h"
#include "ColorInterpolation.h"
#include "StyleBuilderConverter.h"
#include "StyleBuilderState.h"
#include "StyleGradientImage.h"
#include <wtf/text/StringBuilder.h>
namespace WebCore {
// FIXME: Can `styleImageIsUncacheable` be replaced via extending the `ComputedStyleDependencies` system?
static inline bool styleImageIsUncacheable(const CSSPrimitiveValue& value)
{
return value.isFontRelativeLength() || value.isContainerPercentageLength();
}
static inline bool styleImageIsUncacheable(const CSSValue& value)
{
if (const CSSPrimitiveValue* primitive = dynamicDowncast<CSSPrimitiveValue>(value))
return styleImageIsUncacheable(*primitive);
return false;
}
static bool styleImageIsUncacheable(const CSSGradientColorStopList& stops)
{
for (auto& stop : stops) {
if (stop.position && styleImageIsUncacheable(*stop.position))
return true;
if (stop.color && Style::BuilderState::isColorFromPrimitiveValueDerivedFromElement(*stop.color))
return true;
}
return false;
}
static bool styleImageIsUncacheable(const CSSGradientPosition& position)
{
auto styleImageIsUncacheableForCoordinate = [](CSSValue& coordinate) -> bool {
if (coordinate.isPair())
return styleImageIsUncacheable(coordinate.second());
return styleImageIsUncacheable(coordinate);
};
return styleImageIsUncacheableForCoordinate(position.x) || styleImageIsUncacheableForCoordinate(position.y);
}
static bool styleImageIsUncacheable(const std::optional<CSSGradientPosition>& position)
{
return position && styleImageIsUncacheable(*position);
}
static inline std::optional<StyleColor> computeStopColor(const RefPtr<CSSPrimitiveValue>& color, Style::BuilderState& state)
{
if (!color)
return std::nullopt;
return state.colorFromPrimitiveValue(*color);
}
enum class StopPositionResolution { Standard, Deprecated };
template<StopPositionResolution resolution> static inline std::optional<Length> computeLengthStopPosition(const RefPtr<CSSPrimitiveValue>& position, Style::BuilderState& state)
{
if (!position)
return std::nullopt;
if constexpr (resolution == StopPositionResolution::Deprecated) {
if (position->isPercentage())
return Length(position->floatValue(CSSUnitType::CSS_PERCENTAGE), LengthType::Percent);
return Length(position->floatValue(CSSUnitType::CSS_NUMBER), LengthType::Fixed);
} else
return Style::BuilderConverter::convertLength(state, *position);
}
static inline std::variant<std::monostate, AngleRaw, PercentRaw> computeAngularStopPosition(const RefPtr<CSSPrimitiveValue>& position)
{
if (!position)
return std::monostate { };
if (position->isPercentage())
return { PercentRaw { position->doubleValue(CSSUnitType::CSS_PERCENTAGE) } };
if (position->isAngle())
return { AngleRaw { position->primitiveType(), position->doubleValue() } };
ASSERT_NOT_REACHED();
return std::monostate { };
}
template<StopPositionResolution resolution> static decltype(auto) computeLengthStops(const CSSGradientColorStopList& stops, Style::BuilderState& state)
{
return stops.map([&](auto& stop) -> StyleGradientImageLengthStop {
return { computeStopColor(stop.color, state), computeLengthStopPosition<resolution>(stop.position, state) };
});
}
static decltype(auto) computeAngularStops(const CSSGradientColorStopList& stops, Style::BuilderState& state)
{
return stops.map([&](auto& stop) -> StyleGradientImageAngularStop {
return { computeStopColor(stop.color, state), computeAngularStopPosition(stop.position) };
});
}
// MARK: StyleGradientDeprecatedPoint resolution.
static StyleGradientDeprecatedPoint::Coordinate resolvePointCoordinate(const Ref<CSSPrimitiveValue>& coordinate, Style::BuilderState&)
{
if (coordinate->isPercentage())
return { PercentRaw { coordinate->doubleValue(CSSUnitType::CSS_PERCENTAGE) } };
return { NumberRaw { coordinate->doubleValue(CSSUnitType::CSS_NUMBER) } };
}
static StyleGradientDeprecatedPoint resolvePoint(const CSSGradientDeprecatedPoint& point, Style::BuilderState& state)
{
return {
resolvePointCoordinate(point.x, state),
resolvePointCoordinate(point.y, state)
};
}
// MARK: StyleGradientPosition resolution
static StyleGradientPosition::Coordinate resolvePositionCoordinateX(const Ref<CSSValue>& coordinate, Style::BuilderState& state)
{
if (coordinate->isPair()) {
if (coordinate->first().valueID() == CSSValueRight)
return { convertTo100PercentMinusLength(Style::BuilderConverter::convertLength(state, coordinate->second())) };
return { Style::BuilderConverter::convertLength(state, coordinate->second()) };
}
switch (coordinate->valueID()) {
case CSSValueLeft:
return { Length(0, LengthType::Percent) };
case CSSValueCenter:
return { Length(50, LengthType::Percent) };
case CSSValueRight:
return { Length(100, LengthType::Percent) };
default:
break;
}
return { Style::BuilderConverter::convertLength(state, coordinate) };
}
static StyleGradientPosition::Coordinate resolvePositionCoordinateY(const Ref<CSSValue>& coordinate, Style::BuilderState& state)
{
if (coordinate->isPair()) {
if (coordinate->first().valueID() == CSSValueBottom)
return { convertTo100PercentMinusLength(Style::BuilderConverter::convertLength(state, coordinate->second())) };
return { Style::BuilderConverter::convertLength(state, coordinate->second()) };
}
switch (coordinate->valueID()) {
case CSSValueTop:
return { Length(0, LengthType::Percent) };
case CSSValueCenter:
return { Length(50, LengthType::Percent) };
case CSSValueBottom:
return { Length(100, LengthType::Percent) };
default:
break;
}
return { Style::BuilderConverter::convertLength(state, coordinate) };
}
static inline StyleGradientPosition resolvePosition(const CSSGradientPosition& position, Style::BuilderState& state)
{
return {
resolvePositionCoordinateX(position.x, state),
resolvePositionCoordinateY(position.y, state)
};
}
static inline std::optional<StyleGradientPosition> resolvePosition(const std::optional<CSSGradientPosition>& position, Style::BuilderState& state)
{
if (!position)
return std::nullopt;
return resolvePosition(*position, state);
}
// MARK: Serialization
static void serializationForCSS(StringBuilder& builder, const CSSGradientPosition& position)
{
builder.append(position.x->cssText(), ' ', position.y->cssText());
}
static void serializationForCSS(StringBuilder& builder, const CSSGradientDeprecatedPoint& position)
{
builder.append(position.x->cssText(), ' ', position.y->cssText());
}
// MARK: -
RefPtr<StyleImage> CSSLinearGradientValue::createStyleImage(Style::BuilderState& state) const
{
if (m_cachedStyleImage)
return m_cachedStyleImage;
auto gradientLine = WTF::switchOn(m_data.gradientLine,
[](const auto& value) -> StyleGradientImage::LinearData::GradientLine {
return evaluateCalc(value, { });
}
);
auto styleImage = StyleGradientImage::create(
StyleGradientImage::LinearData {
WTFMove(gradientLine),
m_repeating,
computeLengthStops<StopPositionResolution::Standard>(m_stops, state)
},
m_colorInterpolationMethod
);
if (!styleImageIsUncacheable())
m_cachedStyleImage = styleImage.ptr();
return styleImage;
}
RefPtr<StyleImage> CSSPrefixedLinearGradientValue::createStyleImage(Style::BuilderState& state) const
{
if (m_cachedStyleImage)
return m_cachedStyleImage;
auto gradientLine = WTF::switchOn(m_data.gradientLine,
[](const auto& value) -> StyleGradientImage::PrefixedLinearData::GradientLine {
return evaluateCalc(value, { });
}
);
auto styleImage = StyleGradientImage::create(
StyleGradientImage::PrefixedLinearData {
WTFMove(gradientLine),
m_repeating,
computeLengthStops<StopPositionResolution::Standard>(m_stops, state)
},
m_colorInterpolationMethod
);
if (!styleImageIsUncacheable())
m_cachedStyleImage = styleImage.ptr();
return styleImage;
}
RefPtr<StyleImage> CSSDeprecatedLinearGradientValue::createStyleImage(Style::BuilderState& state) const
{
if (m_cachedStyleImage)
return m_cachedStyleImage;
auto styleImage = StyleGradientImage::create(
StyleGradientImage::DeprecatedLinearData {
resolvePoint(m_data.first, state),
resolvePoint(m_data.second, state),
computeLengthStops<StopPositionResolution::Deprecated>(m_stops, state)
},
m_colorInterpolationMethod
);
if (!styleImageIsUncacheable())
m_cachedStyleImage = styleImage.ptr();
return styleImage;
}
RefPtr<StyleImage> CSSRadialGradientValue::createStyleImage(Style::BuilderState& state) const
{
if (m_cachedStyleImage)
return m_cachedStyleImage;
auto gradientBox = WTF::switchOn(m_data.gradientBox,
[&](std::monostate) -> StyleGradientImage::RadialData::GradientBox {
return std::monostate { };
},
[&](const CSSRadialGradientValue::Shape& shape) -> StyleGradientImage::RadialData::GradientBox {
return StyleGradientImage::RadialData::Shape {
shape.shape,
resolvePosition(shape.position, state)
};
},
[&](const CSSRadialGradientValue::Extent& extent) -> StyleGradientImage::RadialData::GradientBox {
return StyleGradientImage::RadialData::Extent {
extent.extent,
resolvePosition(extent.position, state)
};
},
[&](const CSSRadialGradientValue::Length& length) -> StyleGradientImage::RadialData::GradientBox {
return StyleGradientImage::RadialData::Length {
Style::BuilderConverter::convertLength(state, length.length),
resolvePosition(length.position, state)
};
},
[&](const CSSRadialGradientValue::Size& size) -> StyleGradientImage::RadialData::GradientBox {
return StyleGradientImage::RadialData::Size {
LengthSize {
Style::BuilderConverter::convertLength(state, size.size.first),
Style::BuilderConverter::convertLength(state, size.size.second)
},
resolvePosition(size.position, state)
};
},
[&](const CSSRadialGradientValue::CircleOfLength& circleOfLength) -> StyleGradientImage::RadialData::GradientBox {
return StyleGradientImage::RadialData::CircleOfLength {
Style::BuilderConverter::convertLength(state, circleOfLength.length),
resolvePosition(circleOfLength.position, state)
};
},
[&](const CSSRadialGradientValue::CircleOfExtent& circleOfExtent) -> StyleGradientImage::RadialData::GradientBox {
return StyleGradientImage::RadialData::CircleOfExtent {
circleOfExtent.extent,
resolvePosition(circleOfExtent.position, state)
};
},
[&](const CSSRadialGradientValue::EllipseOfSize& ellipseOfSize) -> StyleGradientImage::RadialData::GradientBox {
return StyleGradientImage::RadialData::EllipseOfSize {
LengthSize {
Style::BuilderConverter::convertLength(state, ellipseOfSize.size.first),
Style::BuilderConverter::convertLength(state, ellipseOfSize.size.second)
},
resolvePosition(ellipseOfSize.position, state)
};
},
[&](const CSSRadialGradientValue::EllipseOfExtent& ellipseOfExtent) -> StyleGradientImage::RadialData::GradientBox {
return StyleGradientImage::RadialData::EllipseOfExtent {
ellipseOfExtent.extent,
resolvePosition(ellipseOfExtent.position, state)
};
},
[&](const CSSGradientPosition& position) -> StyleGradientImage::RadialData::GradientBox {
return resolvePosition(position, state);
}
);
auto styleImage = StyleGradientImage::create(
StyleGradientImage::RadialData {
WTFMove(gradientBox),
m_repeating,
computeLengthStops<StopPositionResolution::Standard>(m_stops, state)
},
m_colorInterpolationMethod
);
if (!styleImageIsUncacheable())
m_cachedStyleImage = styleImage.ptr();
return styleImage;
}
RefPtr<StyleImage> CSSPrefixedRadialGradientValue::createStyleImage(Style::BuilderState& state) const
{
if (m_cachedStyleImage)
return m_cachedStyleImage;
auto gradientBox = WTF::switchOn(m_data.gradientBox,
[&](std::monostate) -> StyleGradientImage::PrefixedRadialData::GradientBox {
return std::monostate { };
},
[&](const CSSPrefixedRadialGradientValue::ShapeKeyword& shape) -> StyleGradientImage::PrefixedRadialData::GradientBox {
return shape;
},
[&](const CSSPrefixedRadialGradientValue::ExtentKeyword& extent) -> StyleGradientImage::PrefixedRadialData::GradientBox {
return extent;
},
[&](const CSSPrefixedRadialGradientValue::ShapeAndExtent& shapeAndExtent) -> StyleGradientImage::PrefixedRadialData::GradientBox {
return shapeAndExtent;
},
[&](const CSSPrefixedRadialGradientValue::MeasuredSize& measuredSize) -> StyleGradientImage::PrefixedRadialData::GradientBox {
return StyleGradientImage::PrefixedRadialData::MeasuredSize {
LengthSize {
Style::BuilderConverter::convertLength(state, measuredSize.size.first),
Style::BuilderConverter::convertLength(state, measuredSize.size.second)
}
};
}
);
auto styleImage = StyleGradientImage::create(
StyleGradientImage::PrefixedRadialData {
WTFMove(gradientBox),
resolvePosition(m_data.position, state),
m_repeating,
computeLengthStops<StopPositionResolution::Standard>(m_stops, state)
},
m_colorInterpolationMethod
);
if (!styleImageIsUncacheable())
m_cachedStyleImage = styleImage.ptr();
return styleImage;
}
RefPtr<StyleImage> CSSDeprecatedRadialGradientValue::createStyleImage(Style::BuilderState& state) const
{
if (m_cachedStyleImage)
return m_cachedStyleImage;
auto resolveRadius = [](const std::variant<NumberRaw, UnevaluatedCalc<NumberRaw>>& radius, Style::BuilderState& state) -> float {
return evaluateCalc(radius, { }).value * state.cssToLengthConversionData().zoom();
};
auto styleImage = StyleGradientImage::create(
StyleGradientImage::DeprecatedRadialData {
resolvePoint(m_data.first, state),
resolvePoint(m_data.second, state),
resolveRadius(m_data.firstRadius, state),
resolveRadius(m_data.secondRadius, state),
computeLengthStops<StopPositionResolution::Deprecated>(m_stops, state)
},
m_colorInterpolationMethod
);
if (!styleImageIsUncacheable())
m_cachedStyleImage = styleImage.ptr();
return styleImage;
}
RefPtr<StyleImage> CSSConicGradientValue::createStyleImage(Style::BuilderState& state) const
{
if (m_cachedStyleImage)
return m_cachedStyleImage;
auto resolveAngle = [&](const CSSConicGradientValue::Angle& angle) -> std::optional<AngleRaw> {
return WTF::switchOn(angle,
[](std::monostate) -> std::optional<AngleRaw> {
return std::nullopt;
},
[](auto& value) -> std::optional<AngleRaw> {
return evaluateCalc(value, { });
}
);
};
auto styleImage = StyleGradientImage::create(
StyleGradientImage::ConicData {
resolveAngle(m_data.angle),
resolvePosition(m_data.position, state),
m_repeating,
computeAngularStops(m_stops, state)
},
m_colorInterpolationMethod
);
if (!styleImageIsUncacheable())
m_cachedStyleImage = styleImage.ptr();
return styleImage;
}
static bool appendColorInterpolationMethod(StringBuilder& builder, CSSGradientColorInterpolationMethod colorInterpolationMethod, bool needsLeadingSpace)
{
return WTF::switchOn(colorInterpolationMethod.method.colorSpace,
[&](const ColorInterpolationMethod::OKLab&) {
if (colorInterpolationMethod.defaultMethod != CSSGradientColorInterpolationMethod::Default::OKLab) {
builder.append(needsLeadingSpace ? " "_s : ""_s, "in oklab"_s);
return true;
}
return false;
},
[&](const ColorInterpolationMethod::SRGB&) {
if (colorInterpolationMethod.defaultMethod != CSSGradientColorInterpolationMethod::Default::SRGB) {
builder.append(needsLeadingSpace ? " "_s : ""_s, "in srgb"_s);
return true;
}
return false;
},
[&]<typename MethodColorSpace> (const MethodColorSpace& methodColorSpace) {
builder.append(needsLeadingSpace ? " "_s : ""_s, "in "_s, serializationForCSS(methodColorSpace.interpolationColorSpace));
if constexpr (hasHueInterpolationMethod<MethodColorSpace>)
serializationForCSS(builder, methodColorSpace.hueInterpolationMethod);
return true;
}
);
}
static void appendGradientStops(StringBuilder& builder, const Vector<CSSGradientColorStop, 2>& stops)
{
for (auto& stop : stops) {
double position = stop.position->doubleValue(CSSUnitType::CSS_NUMBER);
if (!position)
builder.append(", from("_s, stop.color->cssText(), ')');
else if (position == 1)
builder.append(", to("_s, stop.color->cssText(), ')');
else
builder.append(", color-stop("_s, position, ", "_s, stop.color->cssText(), ')');
}
}
template<typename T, typename U> static void appendSpaceSeparatedOptionalCSSPtrText(StringBuilder& builder, const T& a, const U& b)
{
if (a && b)
builder.append(a->cssText(), ' ', b->cssText());
else if (a)
builder.append(a->cssText());
else if (b)
builder.append(b->cssText());
}
static void writeColorStop(StringBuilder& builder, const CSSGradientColorStop& stop)
{
appendSpaceSeparatedOptionalCSSPtrText(builder, stop.color, stop.position);
}
// MARK: - Linear.
static ASCIILiteral cssText(CSSLinearGradientValue::Horizontal horizontal)
{
switch (horizontal) {
case CSSLinearGradientValue::Horizontal::Left:
return "left"_s;
case CSSLinearGradientValue::Horizontal::Right:
return "right"_s;
}
RELEASE_ASSERT_NOT_REACHED();
}
static ASCIILiteral cssText(CSSLinearGradientValue::Vertical vertical)
{
switch (vertical) {
case CSSLinearGradientValue::Vertical::Top:
return "top"_s;
case CSSLinearGradientValue::Vertical::Bottom:
return "bottom"_s;
}
RELEASE_ASSERT_NOT_REACHED();
}
String CSSLinearGradientValue::customCSSText() const
{
StringBuilder result;
result.append(m_repeating == CSSGradientRepeat::Repeating ? "repeating-linear-gradient("_s : "linear-gradient("_s);
bool wroteSomething = false;
WTF::switchOn(m_data.gradientLine,
[&](std::monostate) { },
[&](const AngleRaw& angle) {
if (CSSPrimitiveValue::computeDegrees(angle.type, angle.value) == 180)
return;
serializationForCSS(result, angle);
wroteSomething = true;
},
[&](const UnevaluatedCalc<AngleRaw>& angleCalc) {
serializationForCSS(result, angleCalc);
wroteSomething = true;
},
[&](Horizontal horizontal) {
result.append("to "_s, WebCore::cssText(horizontal));
wroteSomething = true;
},
[&](Vertical vertical) {
if (vertical == Vertical::Bottom)
return;
result.append("to "_s, WebCore::cssText(vertical));
wroteSomething = true;
},
[&](const std::pair<Horizontal, Vertical>& pair) {
result.append("to "_s, WebCore::cssText(pair.first), ' ', WebCore::cssText(pair.second));
wroteSomething = true;
}
);
if (appendColorInterpolationMethod(result, m_colorInterpolationMethod, wroteSomething))
wroteSomething = true;
if (wroteSomething)
result.append(", "_s);
result.append(interleave(m_stops, writeColorStop, ", "_s), ')');
return result.toString();
}
bool CSSLinearGradientValue::equals(const CSSLinearGradientValue& other) const
{
return m_stops == other.m_stops
&& m_repeating == other.m_repeating
&& m_colorInterpolationMethod == other.m_colorInterpolationMethod
&& m_data == other.m_data;
}
bool CSSLinearGradientValue::styleImageIsUncacheable() const
{
return WebCore::styleImageIsUncacheable(m_stops);
}
// MARK: - Prefixed Linear.
static ASCIILiteral cssText(CSSPrefixedLinearGradientValue::Horizontal horizontal)
{
switch (horizontal) {
case CSSPrefixedLinearGradientValue::Horizontal::Left:
return "left"_s;
case CSSPrefixedLinearGradientValue::Horizontal::Right:
return "right"_s;
}
RELEASE_ASSERT_NOT_REACHED();
}
static ASCIILiteral cssText(CSSPrefixedLinearGradientValue::Vertical vertical)
{
switch (vertical) {
case CSSPrefixedLinearGradientValue::Vertical::Top:
return "top"_s;
case CSSPrefixedLinearGradientValue::Vertical::Bottom:
return "bottom"_s;
}
RELEASE_ASSERT_NOT_REACHED();
}
String CSSPrefixedLinearGradientValue::customCSSText() const
{
StringBuilder result;
result.append(m_repeating == CSSGradientRepeat::Repeating ? "-webkit-repeating-linear-gradient("_s : "-webkit-linear-gradient("_s);
WTF::switchOn(m_data.gradientLine,
[&](std::monostate) {
result.append("top"_s);
},
[&](const AngleRaw& angle) {
serializationForCSS(result, angle);
},
[&](const UnevaluatedCalc<AngleRaw>& angleCalc) {
serializationForCSS(result, angleCalc);
},
[&](Horizontal horizontal) {
result.append(WebCore::cssText(horizontal));
},
[&](Vertical vertical) {
result.append(WebCore::cssText(vertical));
},
[&](const std::pair<Horizontal, Vertical>& pair) {
result.append(WebCore::cssText(pair.first), ' ', WebCore::cssText(pair.second));
}
);
for (auto& stop : m_stops) {
result.append(", "_s);
writeColorStop(result, stop);
}
result.append(')');
return result.toString();
}
bool CSSPrefixedLinearGradientValue::equals(const CSSPrefixedLinearGradientValue& other) const
{
return m_stops == other.m_stops
&& m_repeating == other.m_repeating
&& m_colorInterpolationMethod == other.m_colorInterpolationMethod
&& m_data == other.m_data;
}
bool CSSPrefixedLinearGradientValue::styleImageIsUncacheable() const
{
return WebCore::styleImageIsUncacheable(m_stops);
}
// MARK: - Deprecated Linear.
String CSSDeprecatedLinearGradientValue::customCSSText() const
{
StringBuilder result;
result.append("-webkit-gradient(linear, "_s);
serializationForCSS(result, m_data.first);
result.append(", "_s);
serializationForCSS(result, m_data.second);
appendGradientStops(result, m_stops);
result.append(')');
return result.toString();
}
bool CSSDeprecatedLinearGradientValue::equals(const CSSDeprecatedLinearGradientValue& other) const
{
return m_stops == other.m_stops
&& m_colorInterpolationMethod == other.m_colorInterpolationMethod
&& m_data == other.m_data;
}
bool CSSDeprecatedLinearGradientValue::styleImageIsUncacheable() const
{
return WebCore::styleImageIsUncacheable(m_stops);
}
// MARK: - Radial.
static ASCIILiteral cssText(CSSRadialGradientValue::ExtentKeyword extent)
{
switch (extent) {
case CSSRadialGradientValue::ExtentKeyword::ClosestCorner:
return "closest-corner"_s;
case CSSRadialGradientValue::ExtentKeyword::ClosestSide:
return "closest-side"_s;
case CSSRadialGradientValue::ExtentKeyword::FarthestCorner:
return "farthest-corner"_s;
case CSSRadialGradientValue::ExtentKeyword::FarthestSide:
return "farthest-side"_s;
}
RELEASE_ASSERT_NOT_REACHED();
}
static bool isCenterPosition(const CSSValue& value)
{
if (isValueID(value, CSSValueCenter))
return true;
auto* number = dynamicDowncast<CSSPrimitiveValue>(value);
return number && number->doubleValue(CSSUnitType::CSS_PERCENTAGE) == 50;
}
static bool isCenterPosition(const CSSGradientPosition& position)
{
return isCenterPosition(position.x) && isCenterPosition(position.y);
}
String CSSRadialGradientValue::customCSSText() const
{
StringBuilder result;
result.append(m_repeating == CSSGradientRepeat::Repeating ? "repeating-radial-gradient("_s : "radial-gradient("_s);
bool wroteSomething = false;
auto appendPosition = [&](const CSSGradientPosition& position) {
if (!isCenterPosition(position)) {
if (wroteSomething)
result.append(' ');
result.append("at "_s);
serializationForCSS(result, position);
wroteSomething = true;
}
};
auto appendOptionalPosition = [&](const std::optional<CSSGradientPosition>& position) {
if (!position)
return;
appendPosition(*position);
};
WTF::switchOn(m_data.gradientBox,
[&](std::monostate) { },
[&](const Shape& data) {
if (data.shape != ShapeKeyword::Ellipse) {
result.append("circle"_s);
wroteSomething = true;
}
appendOptionalPosition(data.position);
},
[&](const Extent& data) {
if (data.extent != ExtentKeyword::FarthestCorner) {
result.append(WebCore::cssText(data.extent));
wroteSomething = true;
}
appendOptionalPosition(data.position);
},
[&](const Length& data) {
result.append(data.length->cssText());
wroteSomething = true;
appendOptionalPosition(data.position);
},
[&](const CircleOfLength& data) {
result.append(data.length->cssText());
wroteSomething = true;
appendOptionalPosition(data.position);
},
[&](const CircleOfExtent& data) {
if (data.extent != ExtentKeyword::FarthestCorner)
result.append("circle "_s, WebCore::cssText(data.extent));
else
result.append("circle"_s);
wroteSomething = true;
appendOptionalPosition(data.position);
},
[&](const Size& data) {
result.append(data.size.first->cssText(), ' ', data.size.second->cssText());
wroteSomething = true;
appendOptionalPosition(data.position);
},
[&](const EllipseOfSize& data) {
result.append(data.size.first->cssText(), ' ', data.size.second->cssText());
wroteSomething = true;
appendOptionalPosition(data.position);
},
[&](const EllipseOfExtent& data) {
if (data.extent != ExtentKeyword::FarthestCorner) {
result.append(WebCore::cssText(data.extent));
wroteSomething = true;
}
appendOptionalPosition(data.position);
},
[&](const CSSGradientPosition& data) {
appendPosition(data);
}
);
if (appendColorInterpolationMethod(result, m_colorInterpolationMethod, wroteSomething))
wroteSomething = true;
if (wroteSomething)
result.append(", "_s);
result.append(interleave(m_stops, writeColorStop, ", "_s), ')');
return result.toString();
}
bool CSSRadialGradientValue::Length::operator==(const CSSRadialGradientValue::Length& other) const
{
return compareCSSValue(length, other.length)
&& position == other.position;
}
bool CSSRadialGradientValue::CircleOfLength::operator==(const CSSRadialGradientValue::CircleOfLength& other) const
{
return compareCSSValue(length, other.length)
&& position == other.position;
}
bool CSSRadialGradientValue::Size::operator==(const CSSRadialGradientValue::Size& other) const
{
return compareCSSValue(size.first, other.size.first)
&& compareCSSValue(size.second, other.size.second)
&& position == other.position;
}
bool CSSRadialGradientValue::EllipseOfSize::operator==(const CSSRadialGradientValue::EllipseOfSize& other) const
{
return compareCSSValue(size.first, other.size.first)
&& compareCSSValue(size.second, other.size.second)
&& position == other.position;
}
bool CSSRadialGradientValue::equals(const CSSRadialGradientValue& other) const
{
return m_stops == other.m_stops
&& m_repeating == other.m_repeating
&& m_colorInterpolationMethod == other.m_colorInterpolationMethod
&& m_data == other.m_data;
}
bool CSSRadialGradientValue::styleImageIsUncacheable() const
{
if (WebCore::styleImageIsUncacheable(m_stops))
return true;
return WTF::switchOn(m_data.gradientBox,
[&](std::monostate) {
return false;
},
[&](const Shape& data) {
return WebCore::styleImageIsUncacheable(data.position);
},
[&](const Extent& data) {
return WebCore::styleImageIsUncacheable(data.position);
},
[&](const Length& data) {
return WebCore::styleImageIsUncacheable(data.position) || WebCore::styleImageIsUncacheable(data.length);
},
[&](const CircleOfLength& data) {
return WebCore::styleImageIsUncacheable(data.position) || WebCore::styleImageIsUncacheable(data.length);
},
[&](const CircleOfExtent& data) {
return WebCore::styleImageIsUncacheable(data.position);
},
[&](const Size& data) {
return WebCore::styleImageIsUncacheable(data.position) || WebCore::styleImageIsUncacheable(data.size.first) || WebCore::styleImageIsUncacheable(data.size.second);
},
[&](const EllipseOfSize& data) {
return WebCore::styleImageIsUncacheable(data.position) || WebCore::styleImageIsUncacheable(data.size.first) || WebCore::styleImageIsUncacheable(data.size.second);
},
[&](const EllipseOfExtent& data) {
return WebCore::styleImageIsUncacheable(data.position);
},
[&](const CSSGradientPosition& data) {
return WebCore::styleImageIsUncacheable(data);
}
);
}
// MARK: Prefixed Radial.
static ASCIILiteral cssText(CSSPrefixedRadialGradientValue::ShapeKeyword shape)
{
switch (shape) {
case CSSPrefixedRadialGradientValue::ShapeKeyword::Circle:
return "circle"_s;
case CSSPrefixedRadialGradientValue::ShapeKeyword::Ellipse:
return "ellipse"_s;
}
RELEASE_ASSERT_NOT_REACHED();
}
static ASCIILiteral cssText(CSSPrefixedRadialGradientValue::ExtentKeyword extent)
{
switch (extent) {
case CSSPrefixedRadialGradientValue::ExtentKeyword::ClosestCorner:
return "closest-corner"_s;
case CSSPrefixedRadialGradientValue::ExtentKeyword::ClosestSide:
return "closest-side"_s;
case CSSPrefixedRadialGradientValue::ExtentKeyword::FarthestCorner:
return "farthest-corner"_s;
case CSSPrefixedRadialGradientValue::ExtentKeyword::FarthestSide:
return "farthest-side"_s;
case CSSPrefixedRadialGradientValue::ExtentKeyword::Contain:
return "contain"_s;
case CSSPrefixedRadialGradientValue::ExtentKeyword::Cover:
return "cover"_s;
}
RELEASE_ASSERT_NOT_REACHED();
}
String CSSPrefixedRadialGradientValue::customCSSText() const
{
StringBuilder result;
result.append(m_repeating == CSSGradientRepeat::Repeating ? "-webkit-repeating-radial-gradient("_s : "-webkit-radial-gradient("_s);
if (m_data.position)
serializationForCSS(result, *m_data.position);
else
result.append("center"_s);
WTF::switchOn(m_data.gradientBox,
[&](std::monostate) { },
[&](const ShapeKeyword& shape) {
result.append(", "_s, WebCore::cssText(shape), " cover"_s);
},
[&](const ExtentKeyword& extent) {
result.append(", ellipse "_s, WebCore::cssText(extent));
},
[&](const ShapeAndExtent& shapeAndExtent) {
result.append(", "_s, WebCore::cssText(shapeAndExtent.shape), ' ', WebCore::cssText(shapeAndExtent.extent));
},
[&](const MeasuredSize& measuredSize) {
result.append(", "_s, measuredSize.size.first->cssText(), ' ', measuredSize.size.second->cssText());
}
);
for (auto& stop : m_stops) {
result.append(", "_s);
writeColorStop(result, stop);
}
result.append(')');
return result.toString();
}
bool CSSPrefixedRadialGradientValue::MeasuredSize::operator==(const CSSPrefixedRadialGradientValue::MeasuredSize& other) const
{
return compareCSSValue(size.first, other.size.first)
&& compareCSSValue(size.second, other.size.second);
}
bool CSSPrefixedRadialGradientValue::equals(const CSSPrefixedRadialGradientValue& other) const
{
return m_stops == other.m_stops
&& m_repeating == other.m_repeating
&& m_colorInterpolationMethod == other.m_colorInterpolationMethod
&& m_data == other.m_data;
}
bool CSSPrefixedRadialGradientValue::styleImageIsUncacheable() const
{
if (WebCore::styleImageIsUncacheable(m_stops))
return true;
if (WebCore::styleImageIsUncacheable(m_data.position))
return true;
return WTF::switchOn(m_data.gradientBox,
[&](std::monostate) {
return false;
},
[&](const ShapeKeyword&) {
return false;
},
[&](const ExtentKeyword&) {
return false;
},
[&](const ShapeAndExtent&) {
return false;
},
[&](const MeasuredSize& measuredSize) {
return WebCore::styleImageIsUncacheable(measuredSize.size.first) || WebCore::styleImageIsUncacheable(measuredSize.size.second);
}
);
}
// MARK: - Deprecated Radial.
String CSSDeprecatedRadialGradientValue::customCSSText() const
{
StringBuilder result;
result.append("-webkit-gradient(radial, "_s);
serializationForCSS(result, m_data.first);
result.append(", "_s);
serializationForCSS(result, m_data.firstRadius);
result.append(", "_s);
serializationForCSS(result, m_data.second);
result.append(", "_s);
serializationForCSS(result, m_data.secondRadius);
appendGradientStops(result, m_stops);
result.append(')');
return result.toString();
}
bool CSSDeprecatedRadialGradientValue::equals(const CSSDeprecatedRadialGradientValue& other) const
{
return m_stops == other.m_stops
&& m_colorInterpolationMethod == other.m_colorInterpolationMethod
&& m_data == other.m_data;
}
bool CSSDeprecatedRadialGradientValue::styleImageIsUncacheable() const
{
return WebCore::styleImageIsUncacheable(m_stops);
}
// MARK: - Conic
String CSSConicGradientValue::customCSSText() const
{
StringBuilder result;
result.append(m_repeating == CSSGradientRepeat::Repeating ? "repeating-conic-gradient("_s : "conic-gradient("_s);
bool wroteSomething = false;
WTF::switchOn(m_data.angle,
[&](std::monostate) { },
[&](const AngleRaw& angle) {
if (CSSPrimitiveValue::computeDegrees(angle.type, angle.value)) {
result.append("from "_s);
serializationForCSS(result, angle);
wroteSomething = true;
}
},
[&](const UnevaluatedCalc<AngleRaw>& angleCalc) {
result.append("from "_s);
serializationForCSS(result, angleCalc);
wroteSomething = true;
}
);
if (m_data.position && !isCenterPosition(*m_data.position)) {
if (wroteSomething)
result.append(' ');
result.append("at "_s);
serializationForCSS(result, *m_data.position);
wroteSomething = true;
}
if (appendColorInterpolationMethod(result, m_colorInterpolationMethod, wroteSomething))
wroteSomething = true;
if (wroteSomething)
result.append(", "_s);
result.append(interleave(m_stops, writeColorStop, ", "_s), ')');
return result.toString();
}
bool CSSConicGradientValue::equals(const CSSConicGradientValue& other) const
{
return m_stops == other.m_stops
&& m_repeating == other.m_repeating
&& m_colorInterpolationMethod == other.m_colorInterpolationMethod
&& m_data == other.m_data;
}
bool CSSConicGradientValue::styleImageIsUncacheable() const
{
return WebCore::styleImageIsUncacheable(m_stops) || WebCore::styleImageIsUncacheable(m_data.position);
}
} // namespace WebCore
|