[CSS Container Queries] Basic support for container units
[webkit.git] / Source / WebCore / css / CSSPrimitiveValue.h
blobc91302a4abd1bef10aa43bb045e6070387cdabe2
1 /*
2 * (C) 1999-2003 Lars Knoll (knoll@kde.org)
3 * Copyright (C) 2004, 2005, 2006, 2008 Apple Inc. All rights reserved.
4 * Copyright (C) 2007 Alexey Proskuryakov <ap@webkit.org>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
22 #pragma once
24 #include "CSSPropertyNames.h"
25 #include "CSSUnits.h"
26 #include "CSSValue.h"
27 #include "CSSValueKeywords.h"
28 #include "Color.h"
29 #include "ExceptionOr.h"
30 #include "LayoutUnit.h"
31 #include <utility>
32 #include <wtf/Forward.h>
33 #include <wtf/MathExtras.h>
35 namespace WebCore {
37 class CSSBasicShape;
38 class CSSCalcValue;
39 class CSSToLengthConversionData;
40 class Counter;
41 class DeprecatedCSSOMPrimitiveValue;
42 class FontCascadeDescription;
43 class FontMetrics;
44 class Pair;
45 class Quad;
46 class RGBColor;
47 class Rect;
48 class RenderStyle;
49 class RenderView;
51 struct CSSFontFamily;
52 struct Length;
53 struct LengthSize;
55 // Max/min values for CSS, needs to slightly smaller/larger than the true max/min values to allow for rounding without overflowing.
56 // Subtract two (rather than one) to allow for values to be converted to float and back without exceeding the LayoutUnit::max.
57 const int maxValueForCssLength = intMaxForLayoutUnit - 2;
58 const int minValueForCssLength = intMinForLayoutUnit + 2;
60 // Dimension calculations are imprecise, often resulting in values of e.g.
61 // 44.99998. We need to round if we're really close to the next integer value.
62 template<typename T> inline T roundForImpreciseConversion(double value)
64 value += (value < 0) ? -0.01 : +0.01;
65 return ((value > std::numeric_limits<T>::max()) || (value < std::numeric_limits<T>::min())) ? 0 : static_cast<T>(value);
68 template<> inline float roundForImpreciseConversion(double value)
70 double ceiledValue = ceil(value);
71 double proximityToNextInt = ceiledValue - value;
72 if (proximityToNextInt <= 0.01 && value > 0)
73 return static_cast<float>(ceiledValue);
74 if (proximityToNextInt >= 0.99 && value < 0)
75 return static_cast<float>(floor(value));
76 return static_cast<float>(value);
79 class CSSPrimitiveValue final : public CSSValue {
80 public:
81 static constexpr bool isLength(CSSUnitType);
82 static double computeDegrees(CSSUnitType, double angle);
84 // FIXME: Some of these use primitiveUnitType() and some use primitiveType(). Many that use primitiveUnitType() are likely broken with calc().
85 bool isAngle() const { return unitCategory(primitiveType()) == CSSUnitCategory::Angle; }
86 bool isAttr() const { return primitiveUnitType() == CSSUnitType::CSS_ATTR; }
87 bool isCounter() const { return primitiveUnitType() == CSSUnitType::CSS_COUNTER; }
88 bool isFontIndependentLength() const { return isFontIndependentLength(primitiveUnitType()); }
89 bool isFontRelativeLength() const { return isFontRelativeLength(primitiveUnitType()); }
90 bool isQuirkyEms() const { return primitiveType() == CSSUnitType::CSS_QUIRKY_EMS; }
91 bool isLength() const { return isLength(static_cast<CSSUnitType>(primitiveType())); }
92 bool isNumber() const { return primitiveType() == CSSUnitType::CSS_NUMBER; }
93 bool isInteger() const { return primitiveType() == CSSUnitType::CSS_INTEGER; }
94 bool isNumberOrInteger() const { return isNumber() || isInteger(); }
95 bool isPercentage() const { return primitiveType() == CSSUnitType::CSS_PERCENTAGE; }
96 bool isPx() const { return primitiveType() == CSSUnitType::CSS_PX; }
97 bool isRect() const { return primitiveUnitType() == CSSUnitType::CSS_RECT; }
98 bool isPair() const { return primitiveUnitType() == CSSUnitType::CSS_PAIR; }
99 bool isPropertyID() const { return primitiveUnitType() == CSSUnitType::CSS_PROPERTY_ID; }
100 bool isRGBColor() const { return primitiveUnitType() == CSSUnitType::CSS_RGBCOLOR; }
101 bool isShape() const { return primitiveUnitType() == CSSUnitType::CSS_SHAPE; }
102 bool isString() const { return primitiveUnitType() == CSSUnitType::CSS_STRING; }
103 bool isFontFamily() const { return primitiveUnitType() == CSSUnitType::CSS_FONT_FAMILY; }
104 bool isTime() const { return unitCategory(primitiveUnitType()) == CSSUnitCategory::Time; }
105 bool isFrequency() const { return unitCategory(primitiveType()) == CSSUnitCategory::Frequency; }
106 bool isURI() const { return primitiveUnitType() == CSSUnitType::CSS_URI; }
107 bool isCalculated() const { return primitiveUnitType() == CSSUnitType::CSS_CALC; }
108 bool isCalculatedPercentageWithNumber() const { return primitiveType() == CSSUnitType::CSS_CALC_PERCENTAGE_WITH_NUMBER; }
109 bool isCalculatedPercentageWithLength() const { return primitiveType() == CSSUnitType::CSS_CALC_PERCENTAGE_WITH_LENGTH; }
110 bool isDotsPerInch() const { return primitiveType() == CSSUnitType::CSS_DPI; }
111 bool isDotsPerPixel() const { return primitiveType() == CSSUnitType::CSS_DPPX; }
112 bool isDotsPerCentimeter() const { return primitiveType() == CSSUnitType::CSS_DPCM; }
113 bool isX() const { return primitiveType() == CSSUnitType::CSS_X; }
114 bool isResolution() const { return unitCategory(primitiveType()) == CSSUnitCategory::Resolution; }
115 bool isViewportPercentageLength() const { return isViewportPercentageLength(primitiveUnitType()); }
116 bool isValueID() const { return primitiveUnitType() == CSSUnitType::CSS_VALUE_ID; }
117 bool isFlex() const { return primitiveType() == CSSUnitType::CSS_FR; }
118 bool isCustomIdent() const { return primitiveUnitType() == CSSUnitType::CustomIdent; }
120 bool isInitialValue() const { return valueID() == CSSValueInitial; }
121 bool isImplicitInitialValue() const { return isInitialValue() && m_isImplicit; }
122 bool isInheritValue() const { return valueID() == CSSValueInherit; }
123 bool isUnsetValue() const { return valueID() == CSSValueUnset; }
124 bool isRevertValue() const { return valueID() == CSSValueRevert; }
125 bool isRevertLayerValue() const { return valueID() == CSSValueRevertLayer; }
126 bool isCSSWideKeyword() const;
128 static Ref<CSSPrimitiveValue> createIdentifier(CSSValueID valueID) { return adoptRef(*new CSSPrimitiveValue(valueID)); }
129 static Ref<CSSPrimitiveValue> createIdentifier(CSSPropertyID propertyID) { return adoptRef(*new CSSPrimitiveValue(propertyID)); }
131 static Ref<CSSPrimitiveValue> create(double value, CSSUnitType type) { return adoptRef(*new CSSPrimitiveValue(value, type)); }
132 static Ref<CSSPrimitiveValue> create(const String& value, CSSUnitType type) { return adoptRef(*new CSSPrimitiveValue(value, type)); }
133 static Ref<CSSPrimitiveValue> create(const Length& value, const RenderStyle& style) { return adoptRef(*new CSSPrimitiveValue(value, style)); }
134 static Ref<CSSPrimitiveValue> create(const LengthSize& value, const RenderStyle& style) { return adoptRef(*new CSSPrimitiveValue(value, style)); }
136 template<typename T> static Ref<CSSPrimitiveValue> create(T&&);
137 template<typename T> static Ref<CSSPrimitiveValue> create(T&&, CSSPropertyID);
139 ~CSSPrimitiveValue();
141 void cleanup();
143 CSSUnitType primitiveType() const;
144 ExceptionOr<float> getFloatValue(CSSUnitType) const;
146 double computeDegrees() const;
148 enum TimeUnit { Seconds, Milliseconds };
149 template<typename T, TimeUnit timeUnit> T computeTime() const;
151 template<typename T> T computeLength(const CSSToLengthConversionData&) const;
152 template<int> Length convertToLength(const CSSToLengthConversionData&) const;
154 bool convertingToLengthRequiresNonNullStyle(int lengthConversion) const;
156 double doubleValue(CSSUnitType) const;
158 // It's usually wrong to call this; it can trigger type conversion in calc without sufficient context to resolve relative length units.
159 double doubleValue() const;
161 double doubleValueDividingBy100IfPercentage() const;
163 // These return nullopt for calc, for which range checking is not done at parse time: <https://www.w3.org/TR/css3-values/#calc-range>.
164 std::optional<bool> isZero() const;
165 std::optional<bool> isPositive() const;
166 std::optional<bool> isNegative() const;
167 bool isCenterPosition() const;
169 template<typename T> inline T value(CSSUnitType type) const { return clampTo<T>(doubleValue(type)); }
170 template<typename T> inline T value() const { return clampTo<T>(doubleValue()); }
172 float floatValue(CSSUnitType type) const { return value<float>(type); }
173 float floatValue() const { return value<float>(); }
175 int intValue(CSSUnitType type) const { return value<int>(type); }
176 int intValue() const { return value<int>(); }
178 WEBCORE_EXPORT String stringValue() const;
180 const Color& color() const { ASSERT(primitiveUnitType() == CSSUnitType::CSS_RGBCOLOR); return *m_value.color; }
181 Counter* counterValue() const { return primitiveUnitType() != CSSUnitType::CSS_COUNTER ? nullptr : m_value.counter; }
182 CSSCalcValue* cssCalcValue() const { return primitiveUnitType() != CSSUnitType::CSS_CALC ? nullptr : m_value.calc; }
183 const CSSFontFamily& fontFamily() const { ASSERT(primitiveUnitType() == CSSUnitType::CSS_FONT_FAMILY); return *m_value.fontFamily; }
184 Pair* pairValue() const { return primitiveUnitType() != CSSUnitType::CSS_PAIR ? nullptr : m_value.pair; }
185 CSSPropertyID propertyID() const { return primitiveUnitType() == CSSUnitType::CSS_PROPERTY_ID ? m_value.propertyID : CSSPropertyInvalid; }
186 Quad* quadValue() const { return primitiveUnitType() != CSSUnitType::CSS_QUAD ? nullptr : m_value.quad; }
187 Rect* rectValue() const { return primitiveUnitType() != CSSUnitType::CSS_RECT ? nullptr : m_value.rect; }
188 CSSBasicShape* shapeValue() const { return primitiveUnitType() != CSSUnitType::CSS_SHAPE ? nullptr : m_value.shape; }
189 CSSValueID valueID() const { return primitiveUnitType() == CSSUnitType::CSS_VALUE_ID ? m_value.valueID : CSSValueInvalid; }
191 template<typename T> inline operator T() const; // Defined in CSSPrimitiveValueMappings.h
193 String customCSSText() const;
195 bool equals(const CSSPrimitiveValue&) const;
197 static std::optional<double> conversionToCanonicalUnitsScaleFactor(CSSUnitType);
198 static String unitTypeString(CSSUnitType);
200 static double computeUnzoomedNonCalcLengthDouble(CSSUnitType, double value, CSSPropertyID, const FontMetrics* = nullptr, const FontCascadeDescription* = nullptr, const FontCascadeDescription* rootFontDescription = nullptr, const RenderView* = nullptr);
201 static double computeNonCalcLengthDouble(const CSSToLengthConversionData&, CSSUnitType, double value);
202 // True if computeNonCalcLengthDouble would produce identical results when resolved against both these styles.
203 static bool equalForLengthResolution(const RenderStyle&, const RenderStyle&);
205 Ref<DeprecatedCSSOMPrimitiveValue> createDeprecatedCSSOMPrimitiveWrapper(CSSStyleDeclaration&) const;
207 void collectDirectComputationalDependencies(HashSet<CSSPropertyID>&) const;
208 void collectDirectRootComputationalDependencies(HashSet<CSSPropertyID>&) const;
210 private:
211 friend class CSSValuePool;
212 friend class StaticCSSValuePool;
213 friend LazyNeverDestroyed<CSSPrimitiveValue>;
215 CSSPrimitiveValue(CSSValueID);
216 CSSPrimitiveValue(CSSPropertyID);
217 CSSPrimitiveValue(const Color&);
218 CSSPrimitiveValue(const Length&);
219 CSSPrimitiveValue(const Length&, const RenderStyle&);
220 CSSPrimitiveValue(const LengthSize&, const RenderStyle&);
221 CSSPrimitiveValue(const String&, CSSUnitType);
222 CSSPrimitiveValue(double, CSSUnitType);
224 CSSPrimitiveValue(StaticCSSValueTag, CSSValueID);
225 CSSPrimitiveValue(StaticCSSValueTag, const Color&);
226 CSSPrimitiveValue(StaticCSSValueTag, double, CSSUnitType);
227 enum ImplicitInitialValueTag { ImplicitInitialValue };
228 CSSPrimitiveValue(StaticCSSValueTag, ImplicitInitialValueTag);
230 template<typename T> CSSPrimitiveValue(T); // Defined in CSSPrimitiveValueMappings.h
231 template<typename T> CSSPrimitiveValue(T, CSSPropertyID); // Defined in CSSPrimitiveValueMappings.h
232 template<typename T> CSSPrimitiveValue(RefPtr<T>&&);
233 template<typename T> CSSPrimitiveValue(Ref<T>&&);
235 static void create(int); // compile-time guard
236 static void create(unsigned); // compile-time guard
237 template<typename T> operator T*(); // compile-time guard
239 void init(const Length&);
240 void init(const LengthSize&, const RenderStyle&);
241 void init(Ref<CSSBasicShape>&&);
242 void init(RefPtr<CSSCalcValue>&&);
243 void init(Ref<Counter>&&);
244 void init(Ref<Pair>&&);
245 void init(Ref<Quad>&&);
246 void init(Ref<Rect>&&);
248 CSSUnitType primitiveUnitType() const { return static_cast<CSSUnitType>(m_primitiveUnitType); }
249 void setPrimitiveUnitType(CSSUnitType type) { m_primitiveUnitType = static_cast<unsigned>(type); }
251 std::optional<double> doubleValueInternal(CSSUnitType targetUnitType) const;
253 double computeLengthDouble(const CSSToLengthConversionData&) const;
255 ALWAYS_INLINE String formatNumberForCustomCSSText() const;
256 NEVER_INLINE String formatNumberValue(StringView) const;
257 NEVER_INLINE String formatIntegerValue(StringView) const;
258 static constexpr bool isFontIndependentLength(CSSUnitType);
259 static constexpr bool isFontRelativeLength(CSSUnitType);
260 static constexpr bool isResolution(CSSUnitType);
261 static constexpr bool isViewportPercentageLength(CSSUnitType);
263 union {
264 CSSPropertyID propertyID;
265 CSSValueID valueID;
266 double num;
267 StringImpl* string;
268 Counter* counter;
269 Rect* rect;
270 Quad* quad;
271 const Color* color;
272 Pair* pair;
273 CSSBasicShape* shape;
274 CSSCalcValue* calc;
275 const CSSFontFamily* fontFamily;
276 } m_value;
279 constexpr bool CSSPrimitiveValue::isFontIndependentLength(CSSUnitType type)
281 return type == CSSUnitType::CSS_PX
282 || type == CSSUnitType::CSS_CM
283 || type == CSSUnitType::CSS_MM
284 || type == CSSUnitType::CSS_IN
285 || type == CSSUnitType::CSS_PT
286 || type == CSSUnitType::CSS_PC;
289 constexpr bool CSSPrimitiveValue::isFontRelativeLength(CSSUnitType type)
291 return type == CSSUnitType::CSS_EMS
292 || type == CSSUnitType::CSS_EXS
293 || type == CSSUnitType::CSS_LHS
294 || type == CSSUnitType::CSS_RLHS
295 || type == CSSUnitType::CSS_REMS
296 || type == CSSUnitType::CSS_CHS
297 || type == CSSUnitType::CSS_IC
298 || type == CSSUnitType::CSS_QUIRKY_EMS;
301 constexpr bool CSSPrimitiveValue::isLength(CSSUnitType type)
303 return type == CSSUnitType::CSS_EMS
304 || type == CSSUnitType::CSS_EXS
305 || type == CSSUnitType::CSS_PX
306 || type == CSSUnitType::CSS_CM
307 || type == CSSUnitType::CSS_MM
308 || type == CSSUnitType::CSS_IN
309 || type == CSSUnitType::CSS_PT
310 || type == CSSUnitType::CSS_PC
311 || type == CSSUnitType::CSS_REMS
312 || type == CSSUnitType::CSS_CHS
313 || type == CSSUnitType::CSS_IC
314 || type == CSSUnitType::CSS_Q
315 || type == CSSUnitType::CSS_LHS
316 || type == CSSUnitType::CSS_RLHS
317 || type == CSSUnitType::CSS_CQW
318 || type == CSSUnitType::CSS_CQH
319 || type == CSSUnitType::CSS_CQI
320 || type == CSSUnitType::CSS_CQB
321 || type == CSSUnitType::CSS_CQMIN
322 || type == CSSUnitType::CSS_CQMAX
323 || isViewportPercentageLength(type)
324 || type == CSSUnitType::CSS_QUIRKY_EMS;
327 constexpr bool CSSPrimitiveValue::isResolution(CSSUnitType type)
329 return type == CSSUnitType::CSS_DPPX
330 || type == CSSUnitType::CSS_DPI
331 || type == CSSUnitType::CSS_DPCM;
334 constexpr bool CSSPrimitiveValue::isViewportPercentageLength(CSSUnitType type)
336 return type >= CSSUnitType::FirstViewportCSSUnitType && type <= CSSUnitType::LastViewporCSSUnitType;
339 template<typename T> inline Ref<CSSPrimitiveValue> CSSPrimitiveValue::create(T&& value)
341 return adoptRef(*new CSSPrimitiveValue(std::forward<T>(value)));
344 template<typename T> inline Ref<CSSPrimitiveValue> CSSPrimitiveValue::create(T&& value, CSSPropertyID propertyID)
346 return adoptRef(*new CSSPrimitiveValue(std::forward<T>(value), propertyID));
349 template<typename T, CSSPrimitiveValue::TimeUnit timeUnit> inline T CSSPrimitiveValue::computeTime() const
351 if (timeUnit == Seconds && primitiveType() == CSSUnitType::CSS_S)
352 return value<T>();
353 if (timeUnit == Seconds && primitiveType() == CSSUnitType::CSS_MS)
354 return value<T>() / 1000;
355 if (timeUnit == Milliseconds && primitiveType() == CSSUnitType::CSS_MS)
356 return value<T>();
357 if (timeUnit == Milliseconds && primitiveType() == CSSUnitType::CSS_S)
358 return value<T>() * 1000;
359 ASSERT_NOT_REACHED();
360 return 0;
363 template<typename T> inline CSSPrimitiveValue::CSSPrimitiveValue(RefPtr<T>&& value)
364 : CSSValue(PrimitiveClass)
366 init(WTFMove(value));
369 template<typename T> inline CSSPrimitiveValue::CSSPrimitiveValue(Ref<T>&& value)
370 : CSSValue(PrimitiveClass)
372 init(WTFMove(value));
375 inline double CSSPrimitiveValue::computeDegrees(CSSUnitType type, double angle)
377 switch (type) {
378 case CSSUnitType::CSS_DEG:
379 return angle;
380 case CSSUnitType::CSS_RAD:
381 return rad2deg(angle);
382 case CSSUnitType::CSS_GRAD:
383 return grad2deg(angle);
384 case CSSUnitType::CSS_TURN:
385 return turn2deg(angle);
386 default:
387 ASSERT_NOT_REACHED();
388 return 0;
392 } // namespace WebCore
394 SPECIALIZE_TYPE_TRAITS_CSS_VALUE(CSSPrimitiveValue, isPrimitiveValue())