1 // Copyright 2022 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef THIRD_PARTY_BLINK_RENDERER_PLATFORM_FONTS_FONT_PALETTE_H_
6 #define THIRD_PARTY_BLINK_RENDERER_PLATFORM_FONTS_FONT_PALETTE_H_
9 #include "base/check.h"
10 #include "base/memory/scoped_refptr.h"
11 #include "third_party/blink/renderer/platform/graphics/color.h"
12 #include "third_party/blink/renderer/platform/platform_export.h"
13 #include "third_party/blink/renderer/platform/runtime_enabled_features.h"
14 #include "third_party/blink/renderer/platform/wtf/text/atomic_string.h"
15 #include "third_party/blink/renderer/platform/wtf/thread_safe_ref_counted.h"
19 /* FontPalette stores CSS font-palette information in a
20 * FontDescription. It's used for representing the computed style
21 * information which can contain either light, dark or custom palette
22 * information according to the font-palette property. */
23 class PLATFORM_EXPORT FontPalette : public RefCounted<FontPalette> {
25 enum KeywordPaletteName {
30 kInterpolablePalette = 4,
33 // Data layout should match SkFontarguments::PaletteOverride::ColorOverride.
34 struct FontPaletteOverride {
38 FontPaletteOverride(){}
39 FontPaletteOverride(int index, Color color)
40 : index(index), color(color) {}
41 bool operator==(const FontPaletteOverride& other) const {
42 return index == other.index && color == other.color;
47 enum BasePaletteValueType {
54 struct BasePaletteValue {
55 BasePaletteValueType type;
58 bool hasValue() { return type != kNoBasePalette; }
59 bool operator==(const BasePaletteValue& other) const {
60 return type == other.type && index == other.index;
65 struct NonNormalizedPercentages {
69 #if BUILDFLAG(IS_TIZEN)
70 NonNormalizedPercentages() : start(0), end(0) {}
71 NonNormalizedPercentages(double start, double end)
72 : start(start), end(end) {}
75 bool operator==(const NonNormalizedPercentages& other) const {
76 return start == other.start && end == other.end;
81 static scoped_refptr<FontPalette> Create() {
82 return base::AdoptRef(new FontPalette());
85 static scoped_refptr<FontPalette> Create(KeywordPaletteName palette_name) {
86 // Use AtomicString constructor for custom palette instantiation.
87 DCHECK(palette_name != kCustomPalette);
88 return base::AdoptRef(new FontPalette(palette_name));
91 static scoped_refptr<FontPalette> Create(AtomicString palette_values_name) {
92 return base::AdoptRef(new FontPalette(std::move(palette_values_name)));
95 // We introduce a palette-mix() function to represent interpolated
96 // font-palette values during animation/transition process, e.g. font-palette
97 // property’s value at time 0.5 between the palettes “--p1” and “--p2” will be
98 // presented as palette-mix(--p1, –p2, 0.5).
99 static scoped_refptr<FontPalette> Mix(
100 scoped_refptr<FontPalette> start,
101 scoped_refptr<FontPalette> end,
102 double start_percentage,
103 double end_percentage,
104 double normalized_percentage,
105 double alpha_multiplier,
106 Color::ColorSpace color_interpolation_space,
107 absl::optional<Color::HueInterpolationMethod> hue_interpolation_method) {
108 return base::AdoptRef(new FontPalette(
109 start, end, NonNormalizedPercentages(start_percentage, end_percentage),
110 normalized_percentage, alpha_multiplier, color_interpolation_space,
111 hue_interpolation_method));
114 void SetBasePalette(BasePaletteValue base_palette) {
115 base_palette_ = base_palette;
118 void SetColorOverrides(Vector<FontPaletteOverride>&& overrides) {
119 palette_overrides_ = overrides;
122 bool IsNormalPalette() const { return palette_keyword_ == kNormalPalette; }
123 bool IsCustomPalette() const { return palette_keyword_ == kCustomPalette; }
124 bool IsInterpolablePalette() const {
125 return palette_keyword_ == kInterpolablePalette;
127 KeywordPaletteName GetPaletteNameKind() const { return palette_keyword_; }
129 /* Returns the identifier of the @font-palette-values rule that should be
130 * retrieved to complete the palette selection or palette override information
131 * for this FontPalette object. */
132 const AtomicString& GetPaletteValuesName() const {
133 DCHECK(palette_keyword_ == kCustomPalette);
134 return palette_values_name_;
137 const Vector<FontPaletteOverride>* GetColorOverrides() const {
138 return &palette_overrides_;
141 BasePaletteValue GetBasePalette() const { return base_palette_; }
143 void SetMatchFamilyName(AtomicString family_name) {
144 match_font_family_ = family_name;
147 AtomicString GetMatchFamilyName() { return match_font_family_; }
149 scoped_refptr<FontPalette> GetStart() const {
150 DCHECK(RuntimeEnabledFeatures::FontPaletteAnimationEnabled());
151 DCHECK(IsInterpolablePalette());
155 scoped_refptr<FontPalette> GetEnd() const {
156 DCHECK(RuntimeEnabledFeatures::FontPaletteAnimationEnabled());
157 DCHECK(IsInterpolablePalette());
161 double GetStartPercentage() const {
162 DCHECK(RuntimeEnabledFeatures::FontPaletteAnimationEnabled());
163 DCHECK(IsInterpolablePalette());
164 return percentages_.start;
167 double GetEndPercentage() const {
168 DCHECK(RuntimeEnabledFeatures::FontPaletteAnimationEnabled());
169 DCHECK(IsInterpolablePalette());
170 return percentages_.end;
173 double GetNormalizedPercentage() const {
174 DCHECK(RuntimeEnabledFeatures::FontPaletteAnimationEnabled());
175 DCHECK(IsInterpolablePalette());
176 return normalized_percentage_;
179 static NonNormalizedPercentages ComputeEndpointPercentagesFromNormalized(
180 double normalized_percentage) {
181 double end_percentage = normalized_percentage * 100.0;
182 double start_percentage = 100.0 - end_percentage;
183 return NonNormalizedPercentages(start_percentage, end_percentage);
186 double GetAlphaMultiplier() const {
187 DCHECK(RuntimeEnabledFeatures::FontPaletteAnimationEnabled());
188 DCHECK((IsInterpolablePalette()));
189 return alpha_multiplier_;
192 Color::ColorSpace GetColorInterpolationSpace() const {
193 DCHECK(RuntimeEnabledFeatures::FontPaletteAnimationEnabled());
194 DCHECK(IsInterpolablePalette());
195 return color_interpolation_space_;
198 absl::optional<Color::HueInterpolationMethod> GetHueInterpolationMethod()
200 DCHECK(RuntimeEnabledFeatures::FontPaletteAnimationEnabled());
201 DCHECK(IsInterpolablePalette());
202 return hue_interpolation_method_;
205 String ToString() const;
207 bool operator==(const FontPalette& other) const;
208 bool operator!=(const FontPalette& other) const { return !(*this == other); }
210 unsigned GetHash() const;
213 explicit FontPalette(KeywordPaletteName palette_name)
214 : palette_keyword_(palette_name), base_palette_({kNoBasePalette, 0}) {}
215 explicit FontPalette(AtomicString palette_values_name)
216 : palette_keyword_(kCustomPalette),
217 palette_values_name_(palette_values_name),
218 base_palette_({kNoBasePalette, 0}) {}
220 scoped_refptr<FontPalette> start,
221 scoped_refptr<FontPalette> end,
222 NonNormalizedPercentages percentages,
223 double normalized_percentage,
224 double alpha_multiplier,
225 Color::ColorSpace color_interpoaltion_space,
226 absl::optional<Color::HueInterpolationMethod> hue_interpolation_method)
227 : palette_keyword_(kInterpolablePalette),
230 percentages_(percentages),
231 normalized_percentage_(normalized_percentage),
232 alpha_multiplier_(alpha_multiplier),
233 color_interpolation_space_(color_interpoaltion_space),
234 hue_interpolation_method_(hue_interpolation_method) {}
236 : palette_keyword_(kNormalPalette), base_palette_({kNoBasePalette, 0}) {}
238 KeywordPaletteName palette_keyword_;
239 AtomicString palette_values_name_;
240 BasePaletteValue base_palette_;
241 AtomicString match_font_family_;
242 Vector<FontPaletteOverride> palette_overrides_;
243 scoped_refptr<FontPalette> start_;
244 scoped_refptr<FontPalette> end_;
245 NonNormalizedPercentages percentages_;
246 double normalized_percentage_;
247 double alpha_multiplier_;
248 Color::ColorSpace color_interpolation_space_;
249 absl::optional<Color::HueInterpolationMethod> hue_interpolation_method_;
254 #endif // THIRD_PARTY_BLINK_RENDERER_PLATFORM_FONTS_FONT_PALETTE_H_