File: font_test_helpers.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 (170 lines) | stat: -rw-r--r-- 6,560 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
// Copyright 2016 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/testing/font_test_helpers.h"

#include "base/memory/scoped_refptr.h"
#include "build/build_config.h"
#include "third_party/blink/renderer/platform/fonts/font.h"
#include "third_party/blink/renderer/platform/fonts/font_custom_platform_data.h"
#include "third_party/blink/renderer/platform/fonts/font_selector.h"
#include "third_party/blink/renderer/platform/fonts/font_variant_emoji.h"
#include "third_party/blink/renderer/platform/testing/unit_test_helpers.h"
#include "third_party/blink/renderer/platform/wtf/shared_buffer.h"

namespace blink {
namespace test {

namespace {

class TestFontSelector : public FontSelector {
 public:
  static TestFontSelector* Create(const String& path) {
    std::optional<Vector<char>> data = test::ReadFromFile(path);
    CHECK(data);
    scoped_refptr<SharedBuffer> font_buffer =
        SharedBuffer::Create(std::move(*data));
    String ots_parse_message;
    return MakeGarbageCollected<TestFontSelector>(
        FontCustomPlatformData::Create(font_buffer.get(), ots_parse_message));
  }

  static TestFontSelector* Create(base::span<const uint8_t> data) {
    scoped_refptr<SharedBuffer> font_buffer = SharedBuffer::Create(data);
    String ots_parse_message;
    FontCustomPlatformData* font_custom_platform_data =
        FontCustomPlatformData::Create(font_buffer.get(), ots_parse_message);
    if (!font_custom_platform_data)
      return nullptr;
    return MakeGarbageCollected<TestFontSelector>(
        std::move(font_custom_platform_data));
  }

  TestFontSelector(FontCustomPlatformData* custom_platform_data)
      : custom_platform_data_(custom_platform_data) {
    DCHECK(custom_platform_data_);
  }
  ~TestFontSelector() override = default;

  void Trace(Visitor* visitor) const override {
    visitor->Trace(custom_platform_data_);
    FontSelector::Trace(visitor);
  }

  FontData* GetFontData(const FontDescription& font_description,
                        const FontFamily&) override {
    FontSelectionCapabilities normal_capabilities(
        {kNormalWidthValue, kNormalWidthValue},
        {kNormalSlopeValue, kNormalSlopeValue},
        {kNormalWeightValue, kNormalWeightValue});
    const FontPlatformData* platform_data =
        custom_platform_data_->GetFontPlatformData(
            font_description.EffectiveFontSize(),
            font_description.AdjustedSpecifiedSize(),
            font_description.IsSyntheticBold() &&
                font_description.SyntheticBoldAllowed(),
            font_description.IsSyntheticItalic() &&
                font_description.SyntheticItalicAllowed(),
            font_description.GetFontSelectionRequest(), normal_capabilities,
            font_description.FontOpticalSizing(),
            font_description.TextRendering(), {},
            font_description.Orientation());
    return MakeGarbageCollected<SimpleFontData>(
        platform_data, MakeGarbageCollected<CustomFontData>());
  }

  void WillUseFontData(const FontDescription&,
                       const FontFamily& family,
                       const String& text) override {}
  void WillUseRange(const FontDescription&,
                    const AtomicString& family_name,
                    const FontDataForRangeSet&) override {}

  unsigned Version() const override { return 0; }
  void FontCacheInvalidated() override {}
  void ReportSuccessfulFontFamilyMatch(
      const AtomicString& font_family_name) override {}
  void ReportFailedFontFamilyMatch(
      const AtomicString& font_family_name) override {}
  void ReportSuccessfulLocalFontMatch(const AtomicString& font_name) override {}
  void ReportFailedLocalFontMatch(const AtomicString& font_name) override {}
  void ReportNotDefGlyph() const override {}
  void ReportEmojiSegmentGlyphCoverage(unsigned, unsigned) override {}
  ExecutionContext* GetExecutionContext() const override { return nullptr; }
  FontFaceCache* GetFontFaceCache() override { return nullptr; }

  void RegisterForInvalidationCallbacks(FontSelectorClient*) override {}
  void UnregisterForInvalidationCallbacks(FontSelectorClient*) override {}

  bool IsPlatformFamilyMatchAvailable(
      const FontDescription&,
      const FontFamily& passed_family) override {
    return false;
  }

 private:
  Member<FontCustomPlatformData> custom_platform_data_;
};

}  // namespace

Font* CreateTestFont(const AtomicString& family_name,
                     base::span<const uint8_t> data,
                     float size,
                     const FontDescription::VariantLigatures* ligatures) {
  FontDescription font_description;
  font_description.SetFamily(
      FontFamily(family_name, FontFamily::Type::kFamilyName));
  font_description.SetSpecifiedSize(size);
  font_description.SetComputedSize(size);
  if (ligatures)
    font_description.SetVariantLigatures(*ligatures);

  return MakeGarbageCollected<Font>(font_description,
                                    TestFontSelector::Create(data));
}

Font* CreateTestFont(const AtomicString& family_name,
                     const String& font_path,
                     float size,
                     const FontDescription::VariantLigatures* ligatures,
                     const FontVariantEmoji variant_emoji,
                     void (*init_font_description)(FontDescription*)) {
  FontDescription font_description;
  font_description.SetFamily(
      FontFamily(family_name, FontFamily::Type::kFamilyName));
  font_description.SetSpecifiedSize(size);
  font_description.SetComputedSize(size);
  font_description.SetVariantEmoji(variant_emoji);
  if (ligatures)
    font_description.SetVariantLigatures(*ligatures);
  if (init_font_description)
    (*init_font_description)(&font_description);

  return MakeGarbageCollected<Font>(font_description,
                                    TestFontSelector::Create(font_path));
}

Font* CreateAhemFont(float size) {
  return CreateTestFont(AtomicString("Ahem"), PlatformTestDataPath("Ahem.woff"),
                        size);
}

#if BUILDFLAG(IS_WIN)
void TestFontPrewarmer::PrewarmFamily(const WebString& family_name) {
  family_names_.push_back(family_name);
}

ScopedTestFontPrewarmer::ScopedTestFontPrewarmer()
    : saved_(FontCache::GetFontPrewarmer()) {
  FontCache::SetFontPrewarmer(&current_);
}

ScopedTestFontPrewarmer::~ScopedTestFontPrewarmer() {
  FontCache::SetFontPrewarmer(saved_);
}
#endif

}  // namespace test
}  // namespace blink