File: font_palette.cc

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 (104 lines) | stat: -rw-r--r-- 4,001 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
// 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.

#include "third_party/blink/renderer/platform/fonts/font_palette.h"

#include "third_party/blink/renderer/platform/wtf/hash_functions.h"
#include "third_party/blink/renderer/platform/wtf/text/atomic_string.h"
#include "third_party/blink/renderer/platform/wtf/text/atomic_string_hash.h"
#include "third_party/blink/renderer/platform/wtf/text/string_builder.h"
#include "third_party/skia/include/core/SkFontArguments.h"

namespace blink {

unsigned FontPalette::GetHash() const {
  unsigned computed_hash = 0;
  WTF::AddIntToHash(computed_hash, palette_keyword_);

  if (palette_keyword_ == kInterpolablePalette) {
    WTF::AddFloatToHash(computed_hash, percentages_.start);
    WTF::AddFloatToHash(computed_hash, percentages_.end);
    WTF::AddFloatToHash(computed_hash, normalized_percentage_);
    WTF::AddFloatToHash(computed_hash, alpha_multiplier_);
    WTF::AddIntToHash(computed_hash,
                      static_cast<uint8_t>(color_interpolation_space_));
    if (hue_interpolation_method_.has_value()) {
      WTF::AddIntToHash(computed_hash,
                        static_cast<uint8_t>(*hue_interpolation_method_));
    }

    WTF::AddIntToHash(computed_hash, start_->GetHash());
    WTF::AddIntToHash(computed_hash, end_->GetHash());
  }

  if (palette_keyword_ != kCustomPalette)
    return computed_hash;

  WTF::AddIntToHash(computed_hash, WTF::GetHash(palette_values_name_));
  WTF::AddIntToHash(computed_hash, match_font_family_.empty()
                                       ? 0
                                       : WTF::GetHash(match_font_family_));
  WTF::AddIntToHash(computed_hash, base_palette_.type);
  WTF::AddIntToHash(computed_hash, base_palette_.index);

  for (auto& override_entry : palette_overrides_) {
    WTF::AddIntToHash(computed_hash, override_entry.index);
  }
  return computed_hash;
}

String FontPalette::ToString() const {
  switch (palette_keyword_) {
    case kNormalPalette:
      return "normal";
    case kLightPalette:
      return "light";
    case kDarkPalette:
      return "dark";
    case kCustomPalette:
      return palette_values_name_.GetString();
    case kInterpolablePalette:
      StringBuilder builder;
      builder.Append("palette-mix(in ");
      if (hue_interpolation_method_.has_value()) {
        builder.Append(Color::SerializeInterpolationSpace(
            color_interpolation_space_, *hue_interpolation_method_));
      } else {
        builder.Append(
            Color::SerializeInterpolationSpace(color_interpolation_space_));
      }
      builder.Append(", ");
      builder.Append(start_->ToString());
      builder.Append(", ");
      builder.Append(end_->ToString());
      DCHECK(normalized_percentage_);
      builder.Append(" ");
      double normalized_percentage = normalized_percentage_ * 100;
      builder.AppendNumber(normalized_percentage);
      builder.Append("%)");
      return builder.ToString();
  }
}

bool FontPalette::operator==(const FontPalette& other) const {
  if (IsInterpolablePalette() != other.IsInterpolablePalette()) {
    return false;
  }
  if (IsInterpolablePalette() && other.IsInterpolablePalette()) {
    return *start_.get() == *other.start_.get() &&
           *end_.get() == *other.end_.get() &&
           percentages_ == other.percentages_ &&
           normalized_percentage_ == other.normalized_percentage_ &&
           alpha_multiplier_ == other.alpha_multiplier_ &&
           color_interpolation_space_ == other.color_interpolation_space_ &&
           hue_interpolation_method_ == other.hue_interpolation_method_;
  }
  return palette_keyword_ == other.palette_keyword_ &&
         palette_values_name_ == other.palette_values_name_ &&
         match_font_family_ == other.match_font_family_ &&
         base_palette_ == other.base_palette_ &&
         palette_overrides_ == other.palette_overrides_;
}

}  // namespace blink