File: font_palette.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (236 lines) | stat: -rw-r--r-- 7,968 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef THIRD_PARTY_BLINK_RENDERER_PLATFORM_FONTS_FONT_PALETTE_H_
#define THIRD_PARTY_BLINK_RENDERER_PLATFORM_FONTS_FONT_PALETTE_H_

#include <memory>
#include "base/check.h"
#include "base/memory/scoped_refptr.h"
#include "third_party/blink/renderer/platform/graphics/color.h"
#include "third_party/blink/renderer/platform/platform_export.h"
#include "third_party/blink/renderer/platform/runtime_enabled_features.h"
#include "third_party/blink/renderer/platform/wtf/text/atomic_string.h"
#include "third_party/blink/renderer/platform/wtf/thread_safe_ref_counted.h"

namespace blink {

/* FontPalette stores CSS font-palette information in a
 * FontDescription. It's used for representing the computed style
 * information which can contain either light, dark or custom palette
 * information according to the font-palette property. */
class PLATFORM_EXPORT FontPalette : public RefCounted<FontPalette> {
 public:
  enum KeywordPaletteName {
    kNormalPalette = 0,
    kLightPalette = 1,
    kDarkPalette = 2,
    kCustomPalette = 3,
    kInterpolablePalette = 4,
  };

  // Data layout should match SkFontArguments::PaletteOverride::ColorOverride.
  struct FontPaletteOverride {
    uint16_t index;
    Color color;

    bool operator==(const FontPaletteOverride& other) const {
      return index == other.index && color == other.color;
    }
    DISALLOW_NEW();
  };

  enum BasePaletteValueType {
    kNoBasePalette,
    kLightBasePalette,
    kDarkBasePalette,
    kIndexBasePalette,
  };

  struct BasePaletteValue {
    BasePaletteValueType type;
    int index;

    bool hasValue() { return type != kNoBasePalette; }
    bool operator==(const BasePaletteValue& other) const {
      return type == other.type && index == other.index;
    }
    DISALLOW_NEW();
  };

  struct NonNormalizedPercentages {
    double start;
    double end;
    bool operator==(const NonNormalizedPercentages& other) const {
      return start == other.start && end == other.end;
      ;
    }
  };

  static scoped_refptr<FontPalette> Create() {
    return base::AdoptRef(new FontPalette());
  }

  static scoped_refptr<FontPalette> Create(KeywordPaletteName palette_name) {
    // Use AtomicString constructor for custom palette instantiation.
    DCHECK(palette_name != kCustomPalette);
    return base::AdoptRef(new FontPalette(palette_name));
  }

  static scoped_refptr<FontPalette> Create(AtomicString palette_values_name) {
    return base::AdoptRef(new FontPalette(std::move(palette_values_name)));
  }

  // We introduce a palette-mix() function to represent interpolated
  // font-palette values during animation/transition process, e.g. font-palette
  // property’s value at time 0.5 between the palettes “--p1” and “--p2” will be
  // presented as palette-mix(--p1, –p2, 0.5).
  static scoped_refptr<FontPalette> Mix(
      scoped_refptr<const FontPalette> start,
      scoped_refptr<const FontPalette> end,
      double start_percentage,
      double end_percentage,
      double normalized_percentage,
      double alpha_multiplier,
      Color::ColorSpace color_interpolation_space,
      std::optional<Color::HueInterpolationMethod> hue_interpolation_method) {
    return base::AdoptRef(new FontPalette(
        start, end, NonNormalizedPercentages(start_percentage, end_percentage),
        normalized_percentage, alpha_multiplier, color_interpolation_space,
        hue_interpolation_method));
  }

  void SetBasePalette(BasePaletteValue base_palette) {
    base_palette_ = base_palette;
  }

  void SetColorOverrides(Vector<FontPaletteOverride>&& overrides) {
    palette_overrides_ = overrides;
  }

  bool IsNormalPalette() const { return palette_keyword_ == kNormalPalette; }
  bool IsCustomPalette() const { return palette_keyword_ == kCustomPalette; }
  bool IsInterpolablePalette() const {
    return palette_keyword_ == kInterpolablePalette;
  }
  KeywordPaletteName GetPaletteNameKind() const { return palette_keyword_; }

  /* Returns the identifier of the @font-palette-values rule that should be
   * retrieved to complete the palette selection or palette override information
   * for this FontPalette object. */
  const AtomicString& GetPaletteValuesName() const {
    DCHECK(palette_keyword_ == kCustomPalette);
    return palette_values_name_;
  }

  const Vector<FontPaletteOverride>* GetColorOverrides() const {
    return &palette_overrides_;
  }

  BasePaletteValue GetBasePalette() const { return base_palette_; }

  void SetMatchFamilyName(AtomicString family_name) {
    match_font_family_ = family_name;
  }

  AtomicString GetMatchFamilyName() { return match_font_family_; }

  scoped_refptr<const FontPalette> GetStart() const {
    DCHECK(IsInterpolablePalette());
    return start_;
  }

  scoped_refptr<const FontPalette> GetEnd() const {
    DCHECK(IsInterpolablePalette());
    return end_;
  }

  double GetStartPercentage() const {
    DCHECK(IsInterpolablePalette());
    return percentages_.start;
  }

  double GetEndPercentage() const {
    DCHECK(IsInterpolablePalette());
    return percentages_.end;
  }

  double GetNormalizedPercentage() const {
    DCHECK(IsInterpolablePalette());
    return normalized_percentage_;
  }

  static NonNormalizedPercentages ComputeEndpointPercentagesFromNormalized(
      double normalized_percentage) {
    double end_percentage = normalized_percentage * 100.0;
    double start_percentage = 100.0 - end_percentage;
    return NonNormalizedPercentages(start_percentage, end_percentage);
  }

  double GetAlphaMultiplier() const {
    DCHECK((IsInterpolablePalette()));
    return alpha_multiplier_;
  }

  Color::ColorSpace GetColorInterpolationSpace() const {
    DCHECK(IsInterpolablePalette());
    return color_interpolation_space_;
  }

  std::optional<Color::HueInterpolationMethod> GetHueInterpolationMethod()
      const {
    DCHECK(IsInterpolablePalette());
    return hue_interpolation_method_;
  }

  String ToString() const;

  bool operator==(const FontPalette& other) const;
  bool operator!=(const FontPalette& other) const { return !(*this == other); }

  unsigned GetHash() const;

 private:
  explicit FontPalette(KeywordPaletteName palette_name)
      : palette_keyword_(palette_name), base_palette_({kNoBasePalette, 0}) {}
  explicit FontPalette(AtomicString palette_values_name)
      : palette_keyword_(kCustomPalette),
        palette_values_name_(palette_values_name),
        base_palette_({kNoBasePalette, 0}) {}
  FontPalette(
      scoped_refptr<const FontPalette> start,
      scoped_refptr<const FontPalette> end,
      NonNormalizedPercentages percentages,
      double normalized_percentage,
      double alpha_multiplier,
      Color::ColorSpace color_interpoaltion_space,
      std::optional<Color::HueInterpolationMethod> hue_interpolation_method)
      : palette_keyword_(kInterpolablePalette),
        start_(start),
        end_(end),
        percentages_(percentages),
        normalized_percentage_(normalized_percentage),
        alpha_multiplier_(alpha_multiplier),
        color_interpolation_space_(color_interpoaltion_space),
        hue_interpolation_method_(hue_interpolation_method) {}
  FontPalette()
      : palette_keyword_(kNormalPalette), base_palette_({kNoBasePalette, 0}) {}

  KeywordPaletteName palette_keyword_;
  AtomicString palette_values_name_;
  BasePaletteValue base_palette_;
  AtomicString match_font_family_;
  Vector<FontPaletteOverride> palette_overrides_;
  scoped_refptr<const FontPalette> start_;
  scoped_refptr<const FontPalette> end_;
  NonNormalizedPercentages percentages_;
  double normalized_percentage_;
  double alpha_multiplier_;
  Color::ColorSpace color_interpolation_space_;
  std::optional<Color::HueInterpolationMethod> hue_interpolation_method_;
};

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_PLATFORM_FONTS_FONT_PALETTE_H_