Fix build warning in third party modules
[platform/framework/web/chromium-efl.git] / third_party / blink / renderer / platform / fonts / font_palette.h
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.
4
5 #ifndef THIRD_PARTY_BLINK_RENDERER_PLATFORM_FONTS_FONT_PALETTE_H_
6 #define THIRD_PARTY_BLINK_RENDERER_PLATFORM_FONTS_FONT_PALETTE_H_
7
8 #include <memory>
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"
16
17 namespace blink {
18
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> {
24  public:
25   enum KeywordPaletteName {
26     kNormalPalette = 0,
27     kLightPalette = 1,
28     kDarkPalette = 2,
29     kCustomPalette = 3,
30     kInterpolablePalette = 4,
31   };
32
33   // Data layout should match SkFontarguments::PaletteOverride::ColorOverride.
34   struct FontPaletteOverride {
35     int index;
36     Color color;
37
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;
43     }
44     DISALLOW_NEW();
45   };
46
47   enum BasePaletteValueType {
48     kNoBasePalette,
49     kLightBasePalette,
50     kDarkBasePalette,
51     kIndexBasePalette,
52   };
53
54   struct BasePaletteValue {
55     BasePaletteValueType type;
56     int index;
57
58     bool hasValue() { return type != kNoBasePalette; }
59     bool operator==(const BasePaletteValue& other) const {
60       return type == other.type && index == other.index;
61     }
62     DISALLOW_NEW();
63   };
64
65   struct NonNormalizedPercentages {
66     double start;
67     double end;
68
69 #if BUILDFLAG(IS_TIZEN)
70     NonNormalizedPercentages() : start(0), end(0) {}
71     NonNormalizedPercentages(double start, double end)
72         : start(start), end(end) {}
73 #endif
74
75     bool operator==(const NonNormalizedPercentages& other) const {
76       return start == other.start && end == other.end;
77       ;
78     }
79   };
80
81   static scoped_refptr<FontPalette> Create() {
82     return base::AdoptRef(new FontPalette());
83   }
84
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));
89   }
90
91   static scoped_refptr<FontPalette> Create(AtomicString palette_values_name) {
92     return base::AdoptRef(new FontPalette(std::move(palette_values_name)));
93   }
94
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));
112   }
113
114   void SetBasePalette(BasePaletteValue base_palette) {
115     base_palette_ = base_palette;
116   }
117
118   void SetColorOverrides(Vector<FontPaletteOverride>&& overrides) {
119     palette_overrides_ = overrides;
120   }
121
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;
126   }
127   KeywordPaletteName GetPaletteNameKind() const { return palette_keyword_; }
128
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_;
135   }
136
137   const Vector<FontPaletteOverride>* GetColorOverrides() const {
138     return &palette_overrides_;
139   }
140
141   BasePaletteValue GetBasePalette() const { return base_palette_; }
142
143   void SetMatchFamilyName(AtomicString family_name) {
144     match_font_family_ = family_name;
145   }
146
147   AtomicString GetMatchFamilyName() { return match_font_family_; }
148
149   scoped_refptr<FontPalette> GetStart() const {
150     DCHECK(RuntimeEnabledFeatures::FontPaletteAnimationEnabled());
151     DCHECK(IsInterpolablePalette());
152     return start_;
153   }
154
155   scoped_refptr<FontPalette> GetEnd() const {
156     DCHECK(RuntimeEnabledFeatures::FontPaletteAnimationEnabled());
157     DCHECK(IsInterpolablePalette());
158     return end_;
159   }
160
161   double GetStartPercentage() const {
162     DCHECK(RuntimeEnabledFeatures::FontPaletteAnimationEnabled());
163     DCHECK(IsInterpolablePalette());
164     return percentages_.start;
165   }
166
167   double GetEndPercentage() const {
168     DCHECK(RuntimeEnabledFeatures::FontPaletteAnimationEnabled());
169     DCHECK(IsInterpolablePalette());
170     return percentages_.end;
171   }
172
173   double GetNormalizedPercentage() const {
174     DCHECK(RuntimeEnabledFeatures::FontPaletteAnimationEnabled());
175     DCHECK(IsInterpolablePalette());
176     return normalized_percentage_;
177   }
178
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);
184   }
185
186   double GetAlphaMultiplier() const {
187     DCHECK(RuntimeEnabledFeatures::FontPaletteAnimationEnabled());
188     DCHECK((IsInterpolablePalette()));
189     return alpha_multiplier_;
190   }
191
192   Color::ColorSpace GetColorInterpolationSpace() const {
193     DCHECK(RuntimeEnabledFeatures::FontPaletteAnimationEnabled());
194     DCHECK(IsInterpolablePalette());
195     return color_interpolation_space_;
196   }
197
198   absl::optional<Color::HueInterpolationMethod> GetHueInterpolationMethod()
199       const {
200     DCHECK(RuntimeEnabledFeatures::FontPaletteAnimationEnabled());
201     DCHECK(IsInterpolablePalette());
202     return hue_interpolation_method_;
203   }
204
205   String ToString() const;
206
207   bool operator==(const FontPalette& other) const;
208   bool operator!=(const FontPalette& other) const { return !(*this == other); }
209
210   unsigned GetHash() const;
211
212  private:
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}) {}
219   FontPalette(
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),
228         start_(start),
229         end_(end),
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) {}
235   FontPalette()
236       : palette_keyword_(kNormalPalette), base_palette_({kNoBasePalette, 0}) {}
237
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_;
250 };
251
252 }  // namespace blink
253
254 #endif  // THIRD_PARTY_BLINK_RENDERER_PLATFORM_FONTS_FONT_PALETTE_H_