1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 /* font specific types shared by both thebes and layout */
8 #ifndef GFX_FONT_PROPERTY_TYPES_H
9 #define GFX_FONT_PROPERTY_TYPES_H
20 #include "mozilla/Assertions.h"
21 #include "mozilla/ServoStyleConsts.h"
25 * This file is separate from gfxFont.h so that layout can include it
26 * without bringing in gfxFont.h and everything it includes.
31 using FontSlantStyle
= StyleFontStyle
;
32 using FontWeight
= StyleFontWeight
;
33 using FontStretch
= StyleFontStretch
;
36 * Convenience type to hold a <min, max> pair representing a range of values.
38 * The min and max are both inclusive, so when min == max the range represents
39 * a single value (not an empty range).
41 template <class T
, class Derived
>
42 class FontPropertyRange
{
43 // This implementation assumes the underlying property type is a 16-bit value
44 // (see FromScalar and AsScalar below).
45 static_assert(sizeof(T
) == 2, "FontPropertyValue should be a 16-bit type!");
49 * Construct a range from given minimum and maximum values (inclusive).
51 FontPropertyRange(T aMin
, T aMax
) : mValues(aMin
, aMax
) {
52 MOZ_ASSERT(aMin
<= aMax
);
56 * Construct a range representing a single value (min==max).
58 explicit FontPropertyRange(T aValue
) : mValues(aValue
, aValue
) {}
60 explicit FontPropertyRange(const FontPropertyRange
& aOther
) = default;
61 FontPropertyRange
& operator=(const FontPropertyRange
& aOther
) = default;
63 T
Min() const { return mValues
.first
; }
64 T
Max() const { return mValues
.second
; }
67 * Clamp the given value to this range.
69 * (We can't use mozilla::Clamp here because it only accepts integral types.)
71 T
Clamp(T aValue
) const {
72 return aValue
<= Min() ? Min() : (aValue
>= Max() ? Max() : aValue
);
76 * Return whether the range consists of a single unique value.
78 bool IsSingle() const { return Min() == Max(); }
80 bool operator==(const FontPropertyRange
& aOther
) const {
81 return mValues
== aOther
.mValues
;
83 bool operator!=(const FontPropertyRange
& aOther
) const {
84 return mValues
!= aOther
.mValues
;
88 * Conversion of the property range to/from a single 32-bit scalar value,
89 * suitable for IPC serialization, hashing, caching.
91 * No assumptions should be made about the numeric value of the scalar.
93 * This depends on the underlying property type being a 16-bit value!
95 using ScalarType
= uint32_t;
97 ScalarType
AsScalar() const {
98 return (mValues
.first
.UnsignedRaw() << 16) | mValues
.second
.UnsignedRaw();
101 static Derived
FromScalar(ScalarType aScalar
) {
102 static_assert(std::is_base_of_v
<FontPropertyRange
, Derived
>);
103 return Derived(T::FromRaw(aScalar
>> 16), T::FromRaw(aScalar
& 0xffff));
107 std::pair
<T
, T
> mValues
;
110 class WeightRange
: public FontPropertyRange
<FontWeight
, WeightRange
> {
112 WeightRange(FontWeight aMin
, FontWeight aMax
)
113 : FontPropertyRange(aMin
, aMax
) {}
115 explicit WeightRange(FontWeight aWeight
) : FontPropertyRange(aWeight
) {}
117 WeightRange(const WeightRange
& aOther
) = default;
119 void ToString(nsACString
& aOutString
, const char* aDelim
= "..") const {
120 aOutString
.AppendFloat(Min().ToFloat());
122 aOutString
.Append(aDelim
);
123 aOutString
.AppendFloat(Max().ToFloat());
128 class StretchRange
: public FontPropertyRange
<FontStretch
, StretchRange
> {
130 StretchRange(FontStretch aMin
, FontStretch aMax
)
131 : FontPropertyRange(aMin
, aMax
) {}
133 explicit StretchRange(FontStretch aStretch
) : FontPropertyRange(aStretch
) {}
135 StretchRange(const StretchRange
& aOther
) = default;
137 void ToString(nsACString
& aOutString
, const char* aDelim
= "..") const {
138 aOutString
.AppendFloat(Min().ToFloat());
140 aOutString
.Append(aDelim
);
141 aOutString
.AppendFloat(Max().ToFloat());
146 class SlantStyleRange
147 : public FontPropertyRange
<FontSlantStyle
, SlantStyleRange
> {
149 SlantStyleRange(FontSlantStyle aMin
, FontSlantStyle aMax
)
150 : FontPropertyRange(aMin
, aMax
) {}
152 explicit SlantStyleRange(FontSlantStyle aStyle
) : FontPropertyRange(aStyle
) {}
154 SlantStyleRange(const SlantStyleRange
& aOther
) = default;
156 void ToString(nsACString
& aOutString
, const char* aDelim
= "..") const {
157 Min().ToString(aOutString
);
159 aOutString
.Append(aDelim
);
160 Max().ToString(aOutString
);
165 } // namespace mozilla
167 #endif // GFX_FONT_PROPERTY_TYPES_H