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
|
/*
* Copyright (C) 2025 Samuel Weinig <sam@webkit.org>
*
* 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,
* 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.
*/
#pragma once
#include <WebCore/CSSPrimitiveKeywordList.h>
#include <WebCore/LayoutUnit.h>
#include <WebCore/StyleLengthWrapperData.h>
#include <WebCore/StylePrimitiveNumericTypes.h>
#include <wtf/StdLibExtras.h>
#include <wtf/text/TextStream.h>
namespace WebCore {
namespace Style {
template<typename, CSS::PrimitiveKeyword...> struct LengthWrapperBase;
template<typename, typename> struct MinimumEvaluation;
// Transitionary type acting as a `Style::PrimitiveNumericOrKeyword<...>` but implemented by wrapping a `LengthWrapperData`.
template<typename Numeric, CSS::PrimitiveKeyword... Ks> struct LengthWrapperBase {
using Base = LengthWrapperBase<Numeric, Ks...>;
using Keywords = CSS::PrimitiveKeywordList<Ks...>;
static constexpr bool hasKeywords = Keywords::count > 0;
static constexpr uint8_t indexForFirstKeyword = 0;
static constexpr uint8_t indexForLastKeyword = hasKeywords ? Keywords::count - 1: 0;
static constexpr uint8_t indexForFixed = hasKeywords ? indexForLastKeyword + 1 : 0;
static constexpr uint8_t indexForPercentage = indexForFixed + 1;
static constexpr uint8_t indexForCalc = indexForFixed + 2;
static constexpr uint8_t maxIndex = indexForCalc;
using Specified = Numeric;
using Fixed = typename Specified::Dimension;
using Percentage = typename Specified::Percentage;
using Calc = typename Specified::Calc;
static constexpr bool SupportsAuto = Keywords::isValidKeyword(CSS::Keyword::Auto { });
static constexpr bool SupportsNormal = Keywords::isValidKeyword(CSS::Keyword::Normal { });
static constexpr bool SupportsIntrinsic = Keywords::isValidKeyword(CSS::Keyword::Intrinsic { });
static constexpr bool SupportsMinIntrinsic = Keywords::isValidKeyword(CSS::Keyword::MinIntrinsic { });
static constexpr bool SupportsMinContent = Keywords::isValidKeyword(CSS::Keyword::MinContent { });
static constexpr bool SupportsMaxContent = Keywords::isValidKeyword(CSS::Keyword::MaxContent { });
static constexpr bool SupportsWebkitFillAvailable = Keywords::isValidKeyword(CSS::Keyword::WebkitFillAvailable { });
static constexpr bool SupportsFitContent = Keywords::isValidKeyword(CSS::Keyword::FitContent { });
static constexpr bool SupportsContent = Keywords::isValidKeyword(CSS::Keyword::Content { });
static constexpr bool SupportsNone = Keywords::isValidKeyword(CSS::Keyword::None { });
LengthWrapperBase(CSS::ValidKeywordForList<Keywords> auto keyword) : m_value(Keywords::offsetForKeyword(keyword)) { }
LengthWrapperBase(Fixed fixed) : m_value(indexForFixed, fixed.unresolvedValue()) { }
LengthWrapperBase(Fixed fixed, bool hasQuirk) : m_value(indexForFixed, fixed.unresolvedValue(), hasQuirk) { }
LengthWrapperBase(Percentage percent) : m_value(indexForPercentage, percent.value) { }
LengthWrapperBase(Calc&& calc) : m_value(indexForCalc, calc.protectedCalculation()) { }
LengthWrapperBase(Specified&& specified) : m_value(toData(specified)) { }
LengthWrapperBase(const Specified& specified) : m_value(toData(specified)) { }
LengthWrapperBase(CSS::ValueLiteral<CSS::LengthUnit::Px> literal) : LengthWrapperBase(Fixed { literal }) { }
LengthWrapperBase(CSS::ValueLiteral<CSS::PercentageUnit::Percentage> literal) : LengthWrapperBase(Percentage { literal }) { }
explicit LengthWrapperBase(WTF::HashTableEmptyValueType token) : m_value(token) { }
// IPC Support
explicit LengthWrapperBase(LengthWrapperData::IPCData&& data) : m_value { toData(WTFMove(data)) } { }
LengthWrapperData::IPCData ipcData() const { return m_value.ipcData(); }
ALWAYS_INLINE bool isFixed() const { return holdsAlternative<Fixed>(); }
ALWAYS_INLINE bool isPercent() const { return holdsAlternative<Percentage>(); }
ALWAYS_INLINE bool isCalculated() const { return holdsAlternative<Calc>();}
ALWAYS_INLINE bool isPercentOrCalculated() const { return isPercent() || isCalculated(); }
ALWAYS_INLINE bool isSpecified() const { return isFixed() || isPercent() || isCalculated(); }
// `isKnownZero` returns whether the value can be guaranteed to be `0`. Keywords and calc() return `false`.
ALWAYS_INLINE bool isKnownZero() const requires (Fixed::range.min <= 0 && Fixed::range.max >= 0) { return m_value.isKnownZero(evaluationKind()); }
// `isKnownPositive` returns whether the value can be guaranteed to be more than `0`. Keywords and calc() return `false`.
ALWAYS_INLINE bool isKnownPositive() const requires (Fixed::range.max > 0) { return m_value.isKnownPositive(evaluationKind()); }
// `isKnownNegative` returns whether the value can be guaranteed to be less than `0`. Keywords and calc() return `false`.
ALWAYS_INLINE bool isKnownNegative() const requires (Fixed::range.min < 0) { return m_value.isKnownNegative(evaluationKind()); }
// `isPossiblyZero` returns whether the value can possibly be `0`. Keywords and calc() return `true`.
ALWAYS_INLINE bool isPossiblyZero() const requires (Fixed::range.min <= 0 && Fixed::range.max >= 0) { return m_value.isPossiblyZero(evaluationKind()); }
// `isPossiblyPositive` returns whether the value can possibly be more than `0`. Keywords and calc() return `true.
ALWAYS_INLINE bool isPossiblyPositive() const requires (Fixed::range.max > 0) { return m_value.isPossiblyPositive(evaluationKind()); }
// `isPossiblyNegative` returns whether the value can possibly be less than `0`. Keywords and calc() return `true.
ALWAYS_INLINE bool isPossiblyNegative() const requires (Fixed::range.min < 0) { return m_value.isPossiblyNegative(evaluationKind()); }
std::optional<Fixed> tryFixed() const { return isFixed() ? std::make_optional(Fixed { m_value.value() }) : std::nullopt; }
std::optional<Percentage> tryPercentage() const { return isPercent() ? std::make_optional(Percentage { m_value.value() }) : std::nullopt; }
std::optional<Calc> tryCalc() const { return isCalculated() ? std::make_optional(Calc { m_value.calculationValue() }) : std::nullopt; }
std::optional<Specified> trySpecified() const
{
auto opaqueType = m_value.type();
if (opaqueType == indexForFixed) return Specified(Fixed { m_value.value() });
else if (opaqueType == indexForPercentage) return Specified(Percentage { m_value.value() });
else if (opaqueType == indexForCalc) return Specified(Calc { m_value.calculationValue() });
else return { };
}
template<typename T> bool holdsAlternative() const
{
if constexpr (CSS::ValidKeywordForList<T, Keywords>) return m_value.type() == Keywords::offsetForKeyword(T { });
else if constexpr (std::same_as<T, Fixed>) return m_value.type() == indexForFixed;
else if constexpr (std::same_as<T, Percentage>) return m_value.type() == indexForPercentage;
else if constexpr (std::same_as<T, Calc>) return m_value.type() == indexForCalc;
else if constexpr (std::same_as<T, Specified>) return m_value.type() == indexForFixed || m_value.type() == indexForPercentage || m_value.type() == indexForCalc;
}
template<typename... F> decltype(auto) switchOn(F&&... f) const
{
auto visitor = WTF::makeVisitor(std::forward<F>(f)...);
auto opaqueType = m_value.type();
if constexpr (hasKeywords) {
if (opaqueType <= indexForLastKeyword)
return Keywords::visitKeywordAtOffset(opaqueType, visitor);
}
if (opaqueType == indexForFixed) return visitor(Fixed { m_value.value() });
else if (opaqueType == indexForPercentage) return visitor(Percentage { m_value.value() });
else if (opaqueType == indexForCalc) return visitor(Calc { m_value.calculationValue() });
RELEASE_ASSERT_NOT_REACHED();
}
bool hasQuirk() const { return m_value.hasQuirk(); }
bool hasSameType(const LengthWrapperBase& other) const { return m_value.type() == other.m_value.type(); }
bool operator==(const LengthWrapperBase&) const = default;
private:
template<typename> friend struct ToPlatform;
template<typename, typename> friend struct Evaluation;
template<typename, typename> friend struct MinimumEvaluation;
template<typename> friend struct Blending;
template<typename> friend struct LengthWrapperBlendingSupport;
static LengthWrapperData toData(const Specified& specified)
{
return WTF::switchOn(specified,
[](const Fixed& fixed) {
return LengthWrapperData { indexForFixed, fixed.unresolvedValue() };
},
[](const Percentage& percentage) {
return LengthWrapperData { indexForPercentage, percentage.value };
},
[](const Calc& calc) {
return LengthWrapperData { indexForCalc, calc.protectedCalculation() };
}
);
}
static LengthWrapperData toData(LengthWrapperData::IPCData&& ipcData)
{
RELEASE_ASSERT(ipcData.opaqueType <= maxIndex);
RELEASE_ASSERT(ipcData.opaqueType != indexForCalc);
if (ipcData.opaqueType == indexForFixed) {
RELEASE_ASSERT(CSS::isWithinRange<Fixed::range>(ipcData.value));
}
if (ipcData.opaqueType == indexForPercentage) {
RELEASE_ASSERT(CSS::isWithinRange<Percentage::range>(ipcData.value));
}
return LengthWrapperData { WTFMove(ipcData) };
}
LengthWrapperDataEvaluationKind evaluationKind() const
{
auto opaqueType = m_value.type();
if constexpr (hasKeywords) {
if (opaqueType <= indexForLastKeyword)
return LengthWrapperDataEvaluationKind::Flag;
}
if (opaqueType == indexForFixed) return LengthWrapperDataEvaluationKind::Fixed;
else if (opaqueType == indexForPercentage) return LengthWrapperDataEvaluationKind::Percentage;
else if (opaqueType == indexForCalc) return LengthWrapperDataEvaluationKind::Calculation;
RELEASE_ASSERT_NOT_REACHED();
}
LengthWrapperData m_value;
};
// MARK: - Concepts
template<typename T> concept LengthWrapperBaseDerived = WTF::IsBaseOfTemplate<LengthWrapperBase, T>::value && VariantLike<T>;
// MARK: - Evaluation
template<LengthWrapperBaseDerived T, typename Result> struct Evaluation<T, Result> {
auto operator()(const T& value, NOESCAPE const Invocable<Result()> auto& lazyMaximumValueFunctor, ZoomNeeded token) -> Result
requires (T::Fixed::range.zoomOptions == CSS::RangeZoomOptions::Default)
{
return value.m_value.template valueForLengthWrapperDataWithLazyMaximum<Result, Result>(value.evaluationKind(), lazyMaximumValueFunctor, token);
}
auto operator()(const T& value, Result maximumValue, ZoomNeeded token) -> Result
requires (T::Fixed::range.zoomOptions == CSS::RangeZoomOptions::Default)
{
return value.m_value.template valueForLengthWrapperDataWithLazyMaximum<Result, Result>(value.evaluationKind(), [&] ALWAYS_INLINE_LAMBDA { return maximumValue; }, token);
}
auto operator()(const T& value, NOESCAPE const Invocable<Result()> auto& lazyMaximumValueFunctor, ZoomFactor zoom) -> Result
requires (T::Fixed::range.zoomOptions == CSS::RangeZoomOptions::Unzoomed)
{
return value.m_value.template valueForLengthWrapperDataWithLazyMaximum<Result, Result>(value.evaluationKind(), lazyMaximumValueFunctor, zoom);
}
auto operator()(const T& value, Result maximumValue, ZoomFactor zoom) -> Result
requires (T::Fixed::range.zoomOptions == CSS::RangeZoomOptions::Unzoomed)
{
return value.m_value.template valueForLengthWrapperDataWithLazyMaximum<Result, Result>(value.evaluationKind(), [&] ALWAYS_INLINE_LAMBDA { return maximumValue; }, zoom);
}
};
template<typename Result> struct EvaluationMinimumInvoker {
template<typename StyleType, typename Reference, typename Zoom> decltype(auto) operator()(const StyleType& value, NOESCAPE Reference&& reference, Zoom&& zoom) const
{
return MinimumEvaluation<StyleType, Result> { }(value, std::forward<Reference>(reference), std::forward<Zoom>(zoom));
}
};
template<typename Result> inline constexpr EvaluationMinimumInvoker<Result> evaluateMinimum{};
template<LengthWrapperBaseDerived T, typename Result> struct MinimumEvaluation<T, Result> {
auto operator()(const T& value, NOESCAPE const Invocable<Result()> auto& lazyMaximumValueFunctor, ZoomNeeded token) -> Result
requires (T::Fixed::range.zoomOptions == CSS::RangeZoomOptions::Default)
{
return value.m_value.template minimumValueForLengthWrapperDataWithLazyMaximum<LayoutUnit, LayoutUnit>(value.evaluationKind(), lazyMaximumValueFunctor, token);
}
auto operator()(const T& value, Result maximumValue, ZoomNeeded token) -> Result
requires (T::Fixed::range.zoomOptions == CSS::RangeZoomOptions::Default)
{
return value.m_value.template minimumValueForLengthWrapperDataWithLazyMaximum<LayoutUnit, LayoutUnit>(value.evaluationKind(), [&] ALWAYS_INLINE_LAMBDA { return LayoutUnit(maximumValue); }, token);
}
auto operator()(const T& value, NOESCAPE const Invocable<Result()> auto& lazyMaximumValueFunctor, ZoomFactor zoom) -> Result
requires (T::Fixed::range.zoomOptions == CSS::RangeZoomOptions::Unzoomed)
{
return value.m_value.template minimumValueForLengthWrapperDataWithLazyMaximum<LayoutUnit, LayoutUnit>(value.evaluationKind(), lazyMaximumValueFunctor, zoom);
}
auto operator()(const T& value, Result maximumValue, ZoomFactor zoom) -> Result
requires (T::Fixed::range.zoomOptions == CSS::RangeZoomOptions::Unzoomed)
{
return value.m_value.template minimumValueForLengthWrapperDataWithLazyMaximum<LayoutUnit, LayoutUnit>(value.evaluationKind(), [&] ALWAYS_INLINE_LAMBDA { return LayoutUnit(maximumValue); }, zoom);
}
};
// MARK: - Logging
template<LengthWrapperBaseDerived T> WTF::TextStream& operator<<(WTF::TextStream& ts, const T& value)
{
WTF::switchOn(value, [&](const auto& alternative) { ts << alternative; });
return ts;
}
} // namespace Style
} // namespace WebCore
|