File: read_anything_util.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (141 lines) | stat: -rw-r--r-- 5,929 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
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/common/read_anything/read_anything_util.h"

#include <algorithm>
#include <string>
#include <string_view>
#include <vector>

#include "base/containers/fixed_flat_map.h"
#include "base/containers/fixed_flat_set.h"
#include "base/metrics/histogram_functions.h"
#include "base/numerics/safe_conversions.h"

namespace {

// All values are in em.
constexpr double kMinScale = 0.5;
constexpr double kMaxScale = 4.5;
constexpr double kScaleStepSize = 0.25;

int AsSteps(double font_scale) {
  return base::ClampRound((font_scale - kMinScale) / kScaleStepSize);
}

}  // namespace

std::vector<std::string> GetSupportedFonts(std::string_view language_code) {
  // If you modify the set of fonts here, also change `LogFontName()` below.
  static constexpr auto kPoppinsLangs =
      base::MakeFixedFlatSet<std::string_view>(
          {"af", "ca", "cs", "da", "de", "en", "es", "et", "fi", "fil",
           "fr", "hi", "hr", "hu", "id", "it", "lt", "lv", "mr", "ms",
           "nl", "pl", "pt", "ro", "sk", "sl", "sv", "sw", "tr"});
  static constexpr auto kComicNeueLangs =
      base::MakeFixedFlatSet<std::string_view>(
          {"af", "ca",  "cs", "da", "de", "en", "es", "et",
           "fi", "fil", "fr", "hr", "hu", "id", "it", "ms",
           "nl", "pl",  "pt", "sk", "sl", "sv", "sw"});
  static constexpr auto kLexendDecaLangs =
      base::MakeFixedFlatSet<std::string_view>(
          {"af", "ca", "cs", "da", "de", "en", "es", "et", "fi", "fil",
           "fr", "hr", "hu", "id", "it", "lt", "lv", "ms", "nl", "pl",
           "pt", "ro", "sk", "sl", "sv", "sw", "tr", "vi"});
  static constexpr auto kEbGaramondLangs =
      base::MakeFixedFlatSet<std::string_view>(
          {"af", "bg", "ca", "cs", "da", "de", "en", "es", "et", "fi", "fil",
           "fr", "hr", "hu", "id", "it", "lt", "lv", "ms", "nl", "pl", "pt",
           "ro", "ru", "sk", "sl", "sr", "sv", "sw", "tr", "uk", "vi"});
  static constexpr auto kStixTwoTextLangs =
      base::MakeFixedFlatSet<std::string_view>(
          {"af", "bg", "ca", "cs", "da", "de", "en", "es", "et", "fi", "fil",
           "fr", "hr", "hu", "id", "it", "lt", "lv", "ms", "nl", "pl", "pt",
           "ro", "ru", "sk", "sl", "sr", "sv", "sw", "tr", "uk", "vi"});
  static constexpr auto kAndikaLangs = base::MakeFixedFlatSet<std::string_view>(
      {"af", "bg",  "ca", "cs", "da", "de", "en", "es", "et",
       "fi", "fil", "fr", "hr", "hu", "id", "it", "kr", "lt",
       "lu", "lv",  "ms", "nd", "nl", "nr", "pl", "pt", "ro",
       "ru", "sk",  "sl", "sr", "sv", "sw", "tr", "uk", "vi"});
  static constexpr auto kAtkinsonHyperlegibleLangs =
      base::MakeFixedFlatSet<std::string_view>(
          {"af", "ca", "cs", "da", "de", "en", "es", "et", "eu", "fi", "fil",
           "fr", "gl", "hr", "hu", "id", "is", "it", "kk", "lt", "ms", "nl",
           "no", "pl", "pt", "ro", "sl", "sq", "sr", "sv", "sw", "zu"});

  std::vector<std::string> font_choices = {"Sans-serif", "Serif"};
  if (language_code.empty() || kPoppinsLangs.contains(language_code)) {
    // Make this default by putting it first.
    font_choices.insert(font_choices.begin(), "Poppins");
  }
  if (language_code.empty() || kComicNeueLangs.contains(language_code)) {
    font_choices.push_back("Comic Neue");
  }
  if (language_code.empty() || kLexendDecaLangs.contains(language_code)) {
    font_choices.push_back("Lexend Deca");
  }
  if (language_code.empty() || kEbGaramondLangs.contains(language_code)) {
    font_choices.push_back("EB Garamond");
  }
  if (language_code.empty() || kStixTwoTextLangs.contains(language_code)) {
    font_choices.push_back("STIX Two Text");
  }
  if (language_code.empty() || kAndikaLangs.contains(language_code)) {
    font_choices.push_back("Andika");
  }
  if (language_code.empty() ||
      kAtkinsonHyperlegibleLangs.contains(language_code)) {
    font_choices.push_back("Atkinson Hyperlegible");
  }

  return font_choices;
}

void LogFontName(std::string_view font_name) {
  // These values are persisted to logs. Entries should not be renumbered and
  // numeric values should never be reused.
  // If you modify the set of fonts here, also change `GetSupportedFonts()`
  // above.
  // LINT.IfChange(ReadAnythingFont)
  enum class ReadAnythingFont {
    kPoppins = 0,
    kSansSerif = 1,
    kSerif = 2,
    kComicNeue = 3,
    kLexendDeca = 4,
    kEbGaramond = 5,
    kStixTwoText = 6,
    kAndika = 7,
    kAtkinsonHyperlegible = 8,
    kMaxValue = kAtkinsonHyperlegible,
  };
  // LINT.ThenChange(//tools/metrics/histograms/metadata/accessibility/enums.xml:ReadAnythingFontName)
  static constexpr auto kFontMap =
      base::MakeFixedFlatMap<std::string_view, ReadAnythingFont>(
          {{"Poppins", ReadAnythingFont::kPoppins},
           {"Sans-serif", ReadAnythingFont::kSansSerif},
           {"Serif", ReadAnythingFont::kSerif},
           {"Comic Neue", ReadAnythingFont::kComicNeue},
           {"Lexend Deca", ReadAnythingFont::kLexendDeca},
           {"EB Garamond", ReadAnythingFont::kEbGaramond},
           {"STIX Two Text", ReadAnythingFont::kStixTwoText},
           {"Andika", ReadAnythingFont::kAndika},
           {"Atkinson Hyperlegible", ReadAnythingFont::kAtkinsonHyperlegible}});
  if (const auto it = kFontMap.find(font_name); it != kFontMap.end()) {
    base::UmaHistogramEnumeration("Accessibility.ReadAnything.FontName",
                                  it->second);
  }
}

double AdjustFontScale(double font_scale, int increment) {
  return std::clamp(
      kMinScale + (AsSteps(font_scale) + increment) * kScaleStepSize, kMinScale,
      kMaxScale);
}

void LogFontScale(double font_scale) {
  base::UmaHistogramExactLinear("Accessibility.ReadAnything.FontScale",
                                AsSteps(font_scale), AsSteps(kMaxScale) + 1);
}