Bug 1700051: part 48) Slightly simplify `mozInlineSpellWordUtil::FindRealWordContaini...
[gecko.git] / layout / style / nsCSSValue.h
blob352eb00823f2ca9a1ce966025fcd08443ad022f7
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 /* representation of simple property values within CSS declarations */
9 #ifndef nsCSSValue_h___
10 #define nsCSSValue_h___
12 #include "mozilla/Attributes.h"
13 #include "mozilla/CORSMode.h"
14 #include "mozilla/EnumTypeTraits.h"
15 #include "mozilla/FontPropertyTypes.h"
16 #include "mozilla/MemoryReporting.h"
17 #include "mozilla/ServoBindingTypes.h"
18 #include "mozilla/URLExtraData.h"
19 #include "mozilla/UniquePtr.h"
21 #include "nsCoord.h"
22 #include "nsTArray.h"
24 #include <type_traits>
26 // XXX Avoid including this here by moving function bodies to the cpp file
27 #include "mozilla/FloatingPoint.h"
29 class imgRequestProxy;
30 class nsAtom;
31 class nsIContent;
33 class nsIPrincipal;
34 class nsIURI;
35 class nsPresContext;
36 template <class T>
37 class nsPtrHashKey;
38 struct RawServoCssUrlData;
40 namespace mozilla {
41 class CSSStyleSheet;
42 } // namespace mozilla
44 // Forward declaration copied here since ServoBindings.h #includes nsCSSValue.h.
45 extern "C" {
46 mozilla::URLExtraData* Servo_CssUrlData_GetExtraData(const RawServoCssUrlData*);
47 bool Servo_CssUrlData_IsLocalRef(const RawServoCssUrlData* url);
50 enum nsCSSUnit : uint32_t {
51 eCSSUnit_Null = 0, // (n/a) null unit, value is not specified
53 eCSSUnit_Integer = 70, // (int) simple value
54 eCSSUnit_Enumerated = 71, // (int) value has enumerated meaning
56 eCSSUnit_Percent = 100, // (float) (1.0 == 100%) value is percentage of
57 // something
58 eCSSUnit_Number = 101, // (float) value is numeric (usually multiplier,
59 // different behavior than percent)
61 // Font relative measure
62 eCSSUnit_EM = 800, // (float) == current font size
63 eCSSUnit_XHeight = 801, // (float) distance from top of lower case x to
64 // baseline
65 eCSSUnit_Char = 802, // (float) number of characters, used for width with
66 // monospace font
67 eCSSUnit_RootEM = 803, // (float) == root element font size
69 // Screen relative measure
70 eCSSUnit_Point = 900, // (float) 4/3 of a CSS pixel
71 eCSSUnit_Inch = 901, // (float) 96 CSS pixels
72 eCSSUnit_Millimeter = 902, // (float) 96/25.4 CSS pixels
73 eCSSUnit_Centimeter = 903, // (float) 96/2.54 CSS pixels
74 eCSSUnit_Pica = 904, // (float) 12 points == 16 CSS pixls
75 eCSSUnit_Quarter = 905, // (float) 96/101.6 CSS pixels
76 eCSSUnit_Pixel = 906, // (float) CSS pixel unit
78 // Angular units
79 eCSSUnit_Degree = 1000, // (float) 360 per circle
81 // Frequency units
82 eCSSUnit_Hertz = 2000, // (float) 1/seconds
83 eCSSUnit_Kilohertz = 2001, // (float) 1000 Hertz
85 // Time units
86 eCSSUnit_Seconds = 3000, // (float) Standard time
87 eCSSUnit_Milliseconds = 3001, // (float) 1/1000 second
89 // Flexible fraction (CSS Grid)
90 eCSSUnit_FlexFraction = 4000, // (float) Fraction of free space
93 struct nsCSSValuePair;
94 struct nsCSSValuePair_heap;
95 struct nsCSSValueList;
96 struct nsCSSValueList_heap;
97 struct nsCSSValueSharedList;
98 struct nsCSSValuePairList;
99 struct nsCSSValuePairList_heap;
101 class nsCSSValue {
102 public:
103 explicit nsCSSValue() : mUnit(eCSSUnit_Null) {}
105 nsCSSValue(int32_t aValue, nsCSSUnit aUnit);
106 nsCSSValue(float aValue, nsCSSUnit aUnit);
107 nsCSSValue(const nsCSSValue& aCopy);
108 nsCSSValue(nsCSSValue&& aOther) : mUnit(aOther.mUnit), mValue(aOther.mValue) {
109 aOther.mUnit = eCSSUnit_Null;
111 template <typename T, typename = std::enable_if_t<std::is_enum<T>::value>>
112 explicit nsCSSValue(T aValue) : mUnit(eCSSUnit_Enumerated) {
113 static_assert(mozilla::EnumTypeFitsWithin<T, int32_t>::value,
114 "aValue must be an enum that fits within mValue.mInt");
115 mValue.mInt = static_cast<int32_t>(aValue);
118 nsCSSValue& operator=(const nsCSSValue& aCopy);
119 nsCSSValue& operator=(nsCSSValue&& aCopy);
120 bool operator==(const nsCSSValue& aOther) const;
122 bool operator!=(const nsCSSValue& aOther) const { return !(*this == aOther); }
124 nsCSSUnit GetUnit() const { return mUnit; }
125 bool IsLengthUnit() const {
126 return eCSSUnit_EM <= mUnit && mUnit <= eCSSUnit_Pixel;
128 bool IsLengthPercentUnit() const {
129 return IsLengthUnit() || mUnit == eCSSUnit_Percent;
132 * What the spec calls relative length units is, for us, split
133 * between relative length units and pixel length units.
135 * A "relative" length unit is a multiple of some derived metric,
136 * such as a font em-size, which itself was controlled by an input CSS
137 * length. Relative length units should not be scaled by zooming, since
138 * the underlying CSS length would already have been scaled.
140 bool IsRelativeLengthUnit() const {
141 return eCSSUnit_EM <= mUnit && mUnit <= eCSSUnit_RootEM;
144 * A "pixel" length unit is a some multiple of CSS pixels.
146 static bool IsPixelLengthUnit(nsCSSUnit aUnit) {
147 return eCSSUnit_Point <= aUnit && aUnit <= eCSSUnit_Pixel;
149 bool IsPixelLengthUnit() const { return IsPixelLengthUnit(mUnit); }
150 static bool IsPercentLengthUnit(nsCSSUnit aUnit) {
151 return aUnit == eCSSUnit_Percent;
153 bool IsPercentLengthUnit() { return IsPercentLengthUnit(mUnit); }
154 static bool IsFloatUnit(nsCSSUnit aUnit) { return eCSSUnit_Number <= aUnit; }
155 bool IsAngularUnit() const { return eCSSUnit_Degree == mUnit; }
156 bool IsFrequencyUnit() const {
157 return eCSSUnit_Hertz <= mUnit && mUnit <= eCSSUnit_Kilohertz;
159 bool IsTimeUnit() const {
160 return eCSSUnit_Seconds <= mUnit && mUnit <= eCSSUnit_Milliseconds;
163 int32_t GetIntValue() const {
164 MOZ_ASSERT(mUnit == eCSSUnit_Integer || mUnit == eCSSUnit_Enumerated,
165 "not an int value");
166 return mValue.mInt;
169 float GetPercentValue() const {
170 MOZ_ASSERT(mUnit == eCSSUnit_Percent, "not a percent value");
171 return mValue.mFloat;
174 float GetFloatValue() const {
175 MOZ_ASSERT(eCSSUnit_Number <= mUnit, "not a float value");
176 MOZ_ASSERT(!mozilla::IsNaN(mValue.mFloat));
177 return mValue.mFloat;
180 float GetAngleValue() const {
181 MOZ_ASSERT(eCSSUnit_Degree == mUnit, "not an angle value");
182 return mValue.mFloat;
185 // Converts any angle to radians.
186 double GetAngleValueInRadians() const;
188 // Converts any angle to degrees.
189 double GetAngleValueInDegrees() const;
191 nscoord GetPixelLength() const;
193 void Reset() { mUnit = eCSSUnit_Null; }
194 ~nsCSSValue() { Reset(); }
196 public:
197 void SetIntValue(int32_t aValue, nsCSSUnit aUnit);
198 template <typename T, typename = std::enable_if_t<std::is_enum<T>::value>>
199 void SetEnumValue(T aValue) {
200 static_assert(mozilla::EnumTypeFitsWithin<T, int32_t>::value,
201 "aValue must be an enum that fits within mValue.mInt");
202 SetIntValue(static_cast<int32_t>(aValue), eCSSUnit_Enumerated);
204 void SetPercentValue(float aValue);
205 void SetFloatValue(float aValue, nsCSSUnit aUnit);
206 // converts the nscoord to pixels
207 void SetIntegerCoordValue(nscoord aCoord);
209 protected:
210 nsCSSUnit mUnit;
211 union {
212 int32_t mInt;
213 float mFloat;
214 } mValue;
217 #endif /* nsCSSValue_h___ */